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