001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.rGUI.server;
020
021import java.rmi.NoSuchObjectException;
022import java.rmi.NoSuchObjectException;
023import pt.ua.dicoogle.server.users.UserSessions;
024import java.util.Timer;
025import java.util.TimerTask;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import java.rmi.RemoteException;
032import java.rmi.server.UnicastRemoteObject;
033import javax.rmi.ssl.SslRMIServerSocketFactory;
034import pt.ua.dicoogle.Main;
035import pt.ua.dicoogle.core.ServerSettings;
036import pt.ua.dicoogle.plugins.PluginController;
037import pt.ua.dicoogle.rGUI.MultihomeSslRMIClientSocketFactory;
038
039import pt.ua.dicoogle.rGUI.interfaces.IUser;
040import pt.ua.dicoogle.rGUI.interfaces.controllers.IDicomSend;
041import pt.ua.dicoogle.rGUI.interfaces.controllers.IPluginControllerUser;
042import pt.ua.dicoogle.rGUI.interfaces.controllers.ISearch;
043import pt.ua.dicoogle.rGUI.server.controllers.DicomSend;
044import pt.ua.dicoogle.rGUI.server.controllers.PluginController4user;
045import pt.ua.dicoogle.rGUI.server.controllers.Search;
046import pt.ua.dicoogle.server.users.User;
047import pt.ua.dicoogle.server.users.UsersXML;
048
049/**
050 *
051 * @author Samuel Campos <samuelcampos@ua.pt>
052 */
053@Deprecated
054public class UserFeatures implements IUser {
055    private UserSessions userSessions;
056    private User user;
057    private int port;
058
059    private Timer timer;
060    private static int timeoutTime = 10000; //10 seconds
061    private TimerTask task;
062    private boolean timeoutCanceled = false;
063
064    /*
065     * These are the controlers that have been exported to RMI
066     */
067    private ISearch search = null, searchStub = null;
068    private IPluginControllerUser pcontroller = null;
069    private static IDicomSend dicomSendStub = null;
070
071
072    public UserFeatures(User user) {
073        userSessions = UserSessions.getInstance();
074
075        this.user = user;
076
077        timer = new Timer();
078        task = new TimeOut();
079
080        timer.schedule(task, timeoutTime, timeoutTime);
081
082        port = ServerSettings.getInstance().getRemoteGUIPort();
083    }
084
085    @Override
086    public void logout() throws RemoteException {
087        task.cancel();
088
089        if(search != null){
090            UnicastRemoteObject.unexportObject(search, true);
091            search = null;
092        }
093        
094        
095        // unexport this object^M
096        try {
097            UnicastRemoteObject.unexportObject(this, true);
098        } catch (NoSuchObjectException ex) {}; 
099        userSessions.userLogout(this);
100    }
101
102    
103    @Override
104    public boolean changePassword(String oldPassHash, String newPassHash) throws RemoteException {
105        boolean result = user.changePassword(oldPassHash, newPassHash);
106
107        // save the xml with new user informations
108        if(result){
109            UsersXML xml = new UsersXML();
110            xml.printXML();
111        }
112        return result;
113    }
114
115    @Override
116    public String getUsername() throws RemoteException {
117        if(user != null)
118            return user.getUsername();
119        
120        return "";
121    }
122
123    @Override
124    public ISearch getSearch() throws RemoteException {
125        if(search == null)
126        {
127            search = new Search();
128
129            searchStub = (ISearch) UnicastRemoteObject.exportObject(search, port, new MultihomeSslRMIClientSocketFactory(), new SslRMIServerSocketFactory());
130        }
131
132        return searchStub;
133    }
134
135
136    @Override
137    public IDicomSend getDicomSend() throws RemoteException {
138        if(dicomSendStub == null)
139            dicomSendStub = (IDicomSend) UnicastRemoteObject.exportObject(DicomSend.getInstance(), port, new MultihomeSslRMIClientSocketFactory(), new SslRMIServerSocketFactory());
140
141        return dicomSendStub;
142    }
143
144
145    /**
146     * Cancel the task and reschedule the task
147     * @throws RemoteException
148     */
149    @Override
150    public void KeepAlive() throws RemoteException {
151        if(!timeoutCanceled){
152            task.cancel();
153            timer.purge();
154
155            task = new TimeOut();
156            timer.schedule(task, timeoutTime, timeoutTime);
157        }
158    }
159
160    /**
161     * If the number is equal to the number randomly generated
162     * in Main class, the timetout stops.
163     *
164     * @param number
165     * @return if the number is equal or not
166     * @throws RemoteException
167     */
168    @Override
169    public boolean shtudownTimeout(int number) throws RemoteException {
170        if(number == Main.randomInteger){
171            timeoutCanceled = true;
172            task.cancel();
173            timer.purge();
174
175            return true;
176        }
177        return false;
178    }
179
180    @Override
181    public IPluginControllerUser getPluginController() throws RemoteException
182    {
183        if(pcontroller == null)
184        {
185            pcontroller = (IPluginControllerUser) UnicastRemoteObject.exportObject(PluginController4user.getInstance(), port, new MultihomeSslRMIClientSocketFactory(), new SslRMIServerSocketFactory());
186        }
187        return pcontroller;
188    }
189
190    
191    /**
192     * This class extends TimerTask
193     * and when the run method is called the user logout
194     */
195    private class TimeOut extends TimerTask{
196
197        @Override
198        public void run() {
199            try {
200                logout();
201            } catch (RemoteException ex) {
202                LoggerFactory.getLogger(UserFeatures.class).error(ex.getMessage(), ex);
203            }
204        }
205    }
206}