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