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