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.registry.LocateRegistry;
022import java.rmi.registry.Registry;
023import java.rmi.server.ExportException;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import java.rmi.server.UnicastRemoteObject;
030
031import javax.rmi.ssl.SslRMIServerSocketFactory;
032import javax.swing.JOptionPane;
033import pt.ua.dicoogle.Main;
034import pt.ua.dicoogle.core.ServerSettings;
035import pt.ua.dicoogle.rGUI.MultihomeSslRMIClientSocketFactory;
036
037import pt.ua.dicoogle.rGUI.interfaces.ILogin;
038import pt.ua.dicoogle.utils.KeysManager;
039
040/**
041 * This class creates the GUI Server over RMI
042 *
043 * @author Samuel Campos <samuelcampos@ua.pt>
044 */
045@Deprecated
046public class GUIServer {
047
048    private int port;
049    private Registry reg;
050
051    /**
052     * Start the RMI GUI Server.
053     * Creates an RMI Registry on localhost
054     *
055     * @param port - where the RMI Registry will be bound
056     */
057    public GUIServer(){
058        
059        setProperties();
060
061        while(true){
062            this.port = ServerSettings.getInstance().getRemoteGUIPort();
063
064
065            try {
066                //creates a Registrey for associating names with objects
067                reg = LocateRegistry.createRegistry(this.port, new MultihomeSslRMIClientSocketFactory(), new SslRMIServerSocketFactory());
068
069                //reg = LocateRegistry.createRegistry(this.port);
070                // creates the remote object
071                ILogin stub = (ILogin) UnicastRemoteObject.exportObject(Login.getInstance(), this.port, new MultihomeSslRMIClientSocketFactory(), new SslRMIServerSocketFactory());
072                //ILogin stub = (ILogin) UnicastRemoteObject.exportObject(Login.getInstance(), 0);
073
074                //Associates the Remote Object to its name in the Registry
075                reg.rebind("login", stub);
076
077                //DebugManager.getInstance().debug("GUI Server is running on port " + this.port +".");
078
079                break;
080            } catch(ExportException ex){
081                /*
082                 JOptionPane.showMessageDialog(null, "Another server process is already running on port " + this.port,
083                        "Startup Error", JOptionPane.ERROR_MESSAGE);
084                 */
085
086                String sPort = JOptionPane.showInputDialog(null, "Another server process is already running on port " + this.port
087                        +"!\nDo you want to try another port?", "Startup Error", JOptionPane.ERROR_MESSAGE);
088
089                try{
090                    int pt = Integer.valueOf(sPort);
091                    ServerSettings.getInstance().setRemoteGUIPort(pt);
092                }
093                catch(Exception e){
094                    JOptionPane.showMessageDialog(null, "Port Invalid!",
095                        "Startup Error", JOptionPane.ERROR_MESSAGE);
096                    System.exit(-4);
097                }
098                
099
100            } catch (Exception ex) {
101                //DebugManager.getInstance().debug("Error associating the remote object to the NameRegistry!");
102
103                LoggerFactory.getLogger(GUIServer.class).error(ex.getMessage(), ex);
104
105                break;
106            }
107        }
108    }
109
110    /**
111     * Set the properties that are accurate to the RMI Server
112     */
113    private void setProperties(){
114        String keyStorePath = KeysManager.getServerKeyPath();
115
116        /**
117         * Put the external IP address into the java.rmi.server.hostname property
118         */
119        String externIP = ServerSettings.getInstance().getRGUIExternalIP();
120        if(externIP != null && !externIP.equals(""))
121        {
122            String hostname = System.getProperty("java.rmi.server.hostname");
123            System.setProperty("java.rmi.server.hostname", hostname + "!" + externIP);
124        }
125
126        //define the System properties to use a SSLKeystore
127        System.setProperty("javax.net.ssl.keyStore", keyStorePath);
128        System.setProperty("javax.net.ssl.keyStorePassword", "dicooglepacs");
129
130        if (Main.isFixedClient()) {
131            String TrustStorePath = KeysManager.getClientKeyPath();
132
133            //define the System property to use a SSL TrustStore
134            System.setProperty("javax.net.ssl.trustStore", TrustStorePath);
135        }
136    }
137}