001    package railo.runtime.gateway;
002    
003    import java.io.IOException;
004    import java.util.Iterator;
005    import java.util.Map.Entry;
006    
007    import railo.runtime.gateway.GatewayPro;
008    import railo.runtime.gateway.GatewayEnginePro;
009    import railo.runtime.gateway.GatewayException;
010    import railo.runtime.gateway.proxy.GatewayProFactory;
011    
012    import railo.commons.lang.ClassException;
013    import railo.commons.lang.ClassUtil;
014    import railo.commons.lang.StringUtil;
015    import railo.runtime.config.Config;
016    import railo.runtime.exp.ApplicationException;
017    import railo.runtime.exp.PageException;
018    import railo.runtime.op.Caster;
019    import railo.runtime.op.Duplicator;
020    import railo.runtime.type.Collection.Key;
021    import railo.runtime.type.Struct;
022    
023    public class GatewayEntryImpl implements GatewayEntry {
024    
025            private String id;
026            private Struct custom;
027            private boolean readOnly;
028            private String listenerCfcPath;
029            private int startupMode;
030            private GatewayPro gateway;
031            private String cfcPath;
032            private String className;
033            private GatewayEnginePro engine;
034    
035            public GatewayEntryImpl(GatewayEnginePro engine,String id, String className, String cfcPath, String listenerCfcPath, String startupMode,Struct custom, boolean readOnly) {
036                    this.engine=engine;
037                    this.id=id;
038                    this.listenerCfcPath=listenerCfcPath;
039                    this.className=className;
040                    this.custom=custom;
041                    this.readOnly=readOnly;
042                    this.cfcPath=cfcPath;
043                    startupMode=startupMode.trim().toLowerCase();
044                    if("manual".equals(startupMode))this.startupMode=STARTUP_MODE_MANUAL;
045                    else if("disabled".equals(startupMode))this.startupMode=STARTUP_MODE_DISABLED;
046                    else this.startupMode=STARTUP_MODE_AUTOMATIC;
047            }
048            
049    
050            /**
051             * @return the gateway
052             * @throws ClassException 
053             * @throws PageException 
054             */
055            public void createGateway(Config config) throws ClassException, PageException {
056                    if(gateway==null){
057                            if(!StringUtil.isEmpty(className)){
058                                    Class clazz = ClassUtil.loadClass(config.getClassLoader(),className);
059                                    
060                                    gateway=GatewayProFactory.toGatewayPro(ClassUtil.loadInstance(clazz));
061                            }
062                            else if(!StringUtil.isEmpty(cfcPath)){
063                                    gateway=new CFCGateway(cfcPath);
064                            }
065                            else throw new ApplicationException("missing gateway source definitions");
066                            try{
067                                    //new GatewayThread(engine,gateway,GatewayThread.START).run();
068                                    gateway.init(engine,getId(), getListenerCfcPath(),getCustom());
069                                    if(getStartupMode()==GatewayEntry.STARTUP_MODE_AUTOMATIC){
070                                            new GatewayThread(engine,gateway,GatewayThread.START).start();
071                                            /*try{
072                                                    //gateway.doStart();
073                                            }
074                                            catch(GatewayException ge){
075                                                    engine.log(gateway,GatewayEngine.LOGLEVEL_ERROR, ge.getMessage());
076                                            }*/
077                                    }
078                            }
079                            catch(IOException ioe){
080                                    throw Caster.toPageException(ioe);
081                            }
082                    }
083            }
084            
085            @Override
086            public GatewayPro getGateway() {
087                    return gateway;
088            }
089            
090            @Override
091            public String getId() {
092                    return id;
093            }
094    
095            @Override
096            public Struct getCustom() {
097                    return (Struct) Duplicator.duplicate(custom,true);
098            }
099    
100            @Override
101            public boolean isReadOnly() {
102                    return readOnly;
103            }
104            
105    
106            /**
107             * @return the cfcPath
108             */
109            public String getListenerCfcPath() {
110                    return listenerCfcPath;
111            }
112            
113            @Override
114            public String getCfcPath() {
115                    return cfcPath;
116            }
117    
118    
119            /**
120             * @return the className
121             */
122            public String getClassName() {
123                    return className;
124            }
125    
126            /**
127             * @return the startupMode
128             */
129            public int getStartupMode() {
130                    return startupMode;
131            }
132    
133            public static String toStartup(int mode,String defautValue) {
134                    if(mode==STARTUP_MODE_MANUAL) return "manual";
135                    else if(mode==STARTUP_MODE_DISABLED) return "disabled";
136                    else if(mode==STARTUP_MODE_AUTOMATIC) return "automatic";
137                    return defautValue;
138            }
139    
140            public static int toStartup(String strMode, int defaultValue) {
141                    strMode=strMode.trim().toLowerCase();
142                    if("manual".equals(strMode)) return STARTUP_MODE_MANUAL;
143                    else if("disabled".equals(strMode)) return STARTUP_MODE_DISABLED;
144                    else if("automatic".equals(strMode)) return STARTUP_MODE_AUTOMATIC;
145                    return defaultValue;
146            }
147            
148            @Override
149            public boolean equals(Object obj){
150                    if(obj==this) return true;
151                    if(!(obj instanceof GatewayEntryImpl))return false;
152                    
153                    GatewayEntryImpl other=(GatewayEntryImpl) obj;
154                    if(!other.getId().equals(id)) return false;
155                    if(!equal(other.className,className)) return false;
156                    if(!equal(other.cfcPath,cfcPath)) return false;
157                    if(!equal(other.listenerCfcPath,listenerCfcPath)) return false;
158                    if(other.getStartupMode()!=startupMode) return false;
159                    
160                    Struct otherCustom = other.getCustom();
161                    if(otherCustom.size()!=custom.size()) return false;
162                    
163                    //Key[] keys = otherCustom.keys();
164                    Iterator<Entry<Key, Object>> it = otherCustom.entryIterator();
165                    Entry<Key, Object> e;
166                    Object ot,oc;
167                    while(it.hasNext()){
168                            e = it.next();
169                            ot=custom.get(e.getKey(),null);
170                            oc=e.getValue();
171                            if(ot==null) return false;
172                            if(!ot.equals(oc)) return false;
173                    }
174                    return true;
175            }
176    
177    
178            private static boolean equal(String left, String right) {
179                    if(left==null && right==null) return true;
180                    if(left!=null && right!=null) return left.equals(right);
181                    return false;
182            }
183    }