001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.cli;
020
021import java.io.File;
022import java.net.InetAddress;
023import java.net.UnknownHostException;
024import java.rmi.ConnectException;
025import java.rmi.NotBoundException;
026import java.rmi.RemoteException;
027import java.rmi.registry.LocateRegistry;
028import java.rmi.registry.Registry;
029import java.rmi.server.RemoteServer;
030import java.rmi.server.UnicastRemoteObject;
031import java.util.Map;
032
033import javax.servlet.ServletException;
034
035
036//import lucee.cli.servlet.ServletConfigImpl;
037//import lucee.cli.servlet.ServletContextImpl;
038
039public class CLIFactory extends Thread {
040        
041        private static final int PORT=8893;
042        private File root;
043        private String servletName;
044        private Map<String, String> config;
045        private long idleTime;
046        
047        public CLIFactory(File root, String servletName, Map<String, String> config) {
048                this.root=root;
049                this.servletName=servletName;
050                this.config=config;
051                
052                this.idleTime=60000;
053                String strIdle = config.get("idle");
054                if(strIdle!=null) {
055                        try{
056                                idleTime=Long.parseLong(strIdle);
057                        }
058                        catch(Throwable t){
059                                if(t instanceof ThreadDeath) throw (ThreadDeath)t;
060                        }
061                }
062        }
063        
064        @Override
065        public void run() {
066                
067                String name=root.getAbsolutePath();
068                InetAddress current=null;
069                try {
070                        current = InetAddress.getLocalHost();
071                } catch (UnknownHostException e1) {
072                        e1.printStackTrace();
073                        return;
074                }
075                try {
076                        try {
077                                // first try to call existing service
078                                invoke(current,name);
079                                
080                        } 
081                        catch (ConnectException e) {
082                                startInvoker(name);
083                                invoke(current,name);
084                        }
085                }
086                catch (Throwable t) {
087                        if(t instanceof ThreadDeath) throw (ThreadDeath)t;
088                        t.printStackTrace();
089                }
090        }
091        
092        private void invoke(InetAddress current, String name) throws RemoteException, NotBoundException {
093                Registry registry = LocateRegistry.getRegistry(current.getHostAddress(),PORT);
094                CLIInvoker invoker = (CLIInvoker) registry.lookup( name );
095                invoker.invoke(config);
096        }
097
098        private void startInvoker(String name) throws ServletException, RemoteException {
099                Registry myReg = getRegistry(PORT);
100                CLIInvokerImpl invoker=new CLIInvokerImpl(root, servletName);
101                CLIInvoker stub = (CLIInvoker) UnicastRemoteObject.exportObject( invoker, 0 );
102                myReg.rebind( name, stub );
103                if(idleTime>0){
104                        Closer closer = new Closer(myReg,invoker,name,idleTime);
105                        closer.setDaemon(false);
106                        closer.start();
107                }
108        }
109
110        public static Registry getRegistry(int port) {
111                Registry registry=null;
112                try {
113                
114                         registry= LocateRegistry.createRegistry( port );
115                } 
116                catch (RemoteException e) {}
117                
118                try {
119                
120                         if(registry==null)registry= LocateRegistry.getRegistry(port);
121                }
122                catch (RemoteException e) {
123                }
124                
125                
126                RemoteServer.setLog(System.out );
127                
128                return registry;
129        }
130}