001 package railo.runtime.gateway; 002 003 import java.util.Map; 004 005 import org.opencfml.eventgateway.Gateway; 006 import org.opencfml.eventgateway.GatewayEngine; 007 import org.opencfml.eventgateway.GatewayException; 008 009 import railo.commons.lang.StringUtil; 010 import railo.runtime.exp.PageException; 011 import railo.runtime.op.Caster; 012 import railo.runtime.type.Struct; 013 import railo.runtime.type.StructImpl; 014 015 public class CFCGateway implements Gateway { 016 017 //private static final Object OBJ = new Object(); 018 //private Component _cfc; 019 private String id; 020 private int state=Gateway.STOPPED; 021 private String cfcPath; 022 //private Config config; 023 //private String requestURI; 024 //private Resource cfcDirectory; 025 private GatewayEngineImpl engine; 026 027 public CFCGateway(String cfcPath) { 028 this.cfcPath=cfcPath; 029 } 030 031 /** 032 * @see org.opencfml.eventgateway.Gateway#init(java.lang.String, java.lang.String, railo.runtime.type.Struct) 033 */ 034 public void init(GatewayEngine engine,String id, String cfcPath, Map config) throws GatewayException { 035 this.engine=(GatewayEngineImpl) engine; 036 this.id=id; 037 038 //requestURI=engine.toRequestURI(cfcPath); 039 Struct args=new StructImpl(StructImpl.TYPE_LINKED); 040 args.setEL("id", id); 041 args.setEL("config", Caster.toStruct(config,null,false)); 042 if(!StringUtil.isEmpty(cfcPath)){ 043 try { 044 args.setEL("listener", this.engine.getComponent(cfcPath,id)); 045 } catch (PageException e) { 046 engine.log(this,GatewayEngine.LOGLEVEL_ERROR, e.getMessage()); 047 } 048 } 049 050 try { 051 callOneWay("init",args); 052 } catch (PageException pe) { 053 054 engine.log(this,GatewayEngine.LOGLEVEL_ERROR, pe.getMessage()); 055 //throw new PageGatewayException(pe); 056 } 057 058 } 059 060 /** 061 * @see org.opencfml.eventgateway.Gateway#doRestart() 062 */ 063 public void doRestart() throws GatewayException { 064 065 engine.log(this,GatewayEngine.LOGLEVEL_INFO,"restart"); 066 Struct args=new StructImpl(); 067 try{ 068 boolean has=callOneWay("restart",args); 069 if(!has){ 070 if(callOneWay("stop",args)){ 071 //engine.clear(cfcPath,id); 072 callOneWay("start",args); 073 } 074 } 075 } 076 catch(PageException pe){ 077 throw new PageGatewayException(pe); 078 } 079 080 } 081 082 /** 083 * @see org.opencfml.eventgateway.Gateway#doStart() 084 */ 085 public void doStart() throws GatewayException { 086 engine.log(this,GatewayEngine.LOGLEVEL_INFO,"start"); 087 Struct args=new StructImpl(); 088 state=STARTING; 089 try{ 090 callOneWay("start",args); 091 engine.log(this,GatewayEngine.LOGLEVEL_INFO,"running"); 092 state=RUNNING; 093 } 094 catch(PageException pe){ 095 state=FAILED; 096 throw new PageGatewayException(pe); 097 } 098 } 099 100 /** 101 * @see org.opencfml.eventgateway.Gateway#doStop() 102 */ 103 public void doStop() throws GatewayException { 104 105 engine.log(this,GatewayEngine.LOGLEVEL_INFO,"stop"); 106 Struct args=new StructImpl(); 107 state=STOPPING; 108 try{ 109 callOneWay("stop",args); 110 //engine.clear(cfcPath,id); 111 state=STOPPED; 112 } 113 catch(PageException pe){ 114 state=FAILED; 115 //engine.clear(cfcPath,id); 116 throw new PageGatewayException(pe); 117 } 118 } 119 120 /** 121 * @see org.opencfml.eventgateway.Gateway#getHelper() 122 */ 123 public Object getHelper() { 124 Struct args=new StructImpl(StructImpl.TYPE_LINKED); 125 return callEL("getHelper",args,null); 126 } 127 128 /** 129 * @see org.opencfml.eventgateway.Gateway#getId() 130 */ 131 public String getId() { 132 return id; 133 } 134 135 /** 136 * @see org.opencfml.eventgateway.Gateway#getState() 137 */ 138 public int getState() { 139 Struct args=new StructImpl(); 140 Integer state=Integer.valueOf(this.state); 141 try { 142 return GatewayEngineImpl.toIntState(Caster.toString(call("getState",args,state)),this.state); 143 } 144 catch (PageException pe) { 145 engine.log(this, GatewayEngine.LOGLEVEL_ERROR, pe.getMessage()); 146 } 147 return this.state; 148 } 149 150 151 152 /** 153 * @see org.opencfml.eventgateway.Gateway#sendMessage(railo.runtime.type.Struct) 154 */ 155 public String sendMessage(Map data) throws GatewayException { 156 Struct args=new StructImpl(StructImpl.TYPE_LINKED); 157 args.setEL("data", Caster.toStruct(data, null, false)); 158 try { 159 return Caster.toString(call("sendMessage",args,"")); 160 } catch (PageException pe) { 161 throw new PageGatewayException(pe); 162 } 163 } 164 165 private Object callEL(String methodName,Struct arguments, Object defaultValue) { 166 return engine.callEL(cfcPath,id, methodName, arguments, true, defaultValue); 167 } 168 169 private boolean callOneWay(String methodName,Struct arguments) throws PageException { 170 return engine.callOneWay(cfcPath,id, methodName, arguments, true); 171 } 172 173 private Object call(String methodName,Struct arguments, Object defaultValue) throws PageException { 174 return engine.call(cfcPath,id, methodName, arguments, true, defaultValue); 175 } 176 }