001    package railo.runtime.tag;
002    
003    import railo.commons.io.res.util.ResourceUtil;
004    import railo.commons.lang.StringUtil;
005    import railo.runtime.Mapping;
006    import railo.runtime.exp.ApplicationException;
007    import railo.runtime.exp.PageException;
008    import railo.runtime.ext.tag.TagImpl;
009    import railo.runtime.listener.AppListenerUtil;
010    import railo.runtime.listener.ApplicationContextPro;
011    import railo.runtime.listener.ClassicApplicationContext;
012    import railo.runtime.op.Caster;
013    import railo.runtime.orm.ORMUtil;
014    import railo.runtime.type.Scope;
015    import railo.runtime.type.Struct;
016    import railo.runtime.type.dt.TimeSpan;
017    
018    /**
019    * Defines scoping for a CFML application, enables or disables storing client variables, 
020    *                       and specifies a client variable storage mechanism. 
021    *                       By default, client variables are disabled. Also, enables session variables and sets timeouts 
022    *                       for session and application variables. Session and application variables are stored in memory.
023    *
024    *
025    *
026    **/
027    public final class Application extends TagImpl {
028    
029            
030    
031            private static final int ACTION_CREATE = 0;
032            private static final int ACTION_UPDATE = 1;
033        
034            private Boolean setClientCookies;
035            private Boolean setDomainCookies;
036            private Boolean setSessionManagement;
037            private String clientstorage;
038            private String sessionstorage;
039            private Boolean setClientManagement;
040            private TimeSpan sessionTimeout;
041            private TimeSpan clientTimeout;
042            private TimeSpan applicationTimeout;
043            private Mapping[] mappings;
044            private Mapping[] customTagMappings;
045            private Mapping[] componentMappings;
046            private String secureJsonPrefix;
047            private Boolean secureJson;
048            private String scriptrotect;
049            private String datasource;
050            private String defaultdatasource;
051            private int loginstorage=Scope.SCOPE_UNDEFINED;
052            
053            //ApplicationContextImpl appContext;
054        private String name="";
055            private int action=ACTION_CREATE;
056            private int localMode=-1;
057            private short sessionType=-1;
058            private boolean sessionCluster;
059            private boolean clientCluster;
060    
061            private boolean ormenabled;
062            private Struct ormsettings;
063            private Struct s3;
064            
065         
066        /**
067         * @see javax.servlet.jsp.tagext.Tag#release()
068         */
069        public void release() {
070            super.release();
071            setClientCookies=null;
072            setDomainCookies=null;
073            setSessionManagement=null;
074            clientstorage=null;
075            sessionstorage=null;
076            setClientManagement=null;
077            sessionTimeout=null;
078            clientTimeout=null;
079            applicationTimeout=null;
080            mappings=null;
081            customTagMappings=null;
082            componentMappings=null;
083            secureJson=null;
084            secureJsonPrefix=null;
085            loginstorage=Scope.SCOPE_UNDEFINED;
086            scriptrotect=null;
087            datasource=null;
088            defaultdatasource=null;
089            this.name="";
090            action=ACTION_CREATE;
091            localMode=-1;
092            sessionType=-1;
093            sessionCluster=false;
094            clientCluster=false;
095            
096            ormenabled=false;
097            ormsettings=null;
098            s3=null;
099            //appContext=null;
100        }
101        
102        /** set the value setclientcookies
103            *  Yes or No. Yes enables client cookies. Default is Yes. If you set this attribute to 
104            *               "No", CFML does not automatically send the CFID and CFTOKEN cookies to the client browser;
105            *               you must manually code CFID and CFTOKEN on the URL for every page that uses Session or Client variables.
106            * @param setClientCookies value to set
107            **/
108            public void setSetclientcookies(boolean setClientCookies)       {
109                    this.setClientCookies=setClientCookies?Boolean.TRUE:Boolean.FALSE;
110                //getAppContext().setSetClientCookies(setClientCookies);
111            }
112    
113        /** set the value setdomaincookies
114            *  Yes or No. Sets the CFID and CFTOKEN cookies for a domain, not just a single host. 
115            *               Applications that are running on clusters must set this value to Yes. The default is No.
116            * @param setDomainCookies value to set
117            **/
118            public void setSetdomaincookies(boolean setDomainCookies)       {
119                    this.setDomainCookies=setDomainCookies?Boolean.TRUE:Boolean.FALSE;
120                //getAppContext().setSetDomainCookies(setDomainCookies);
121            }
122    
123            /** set the value sessionmanagement
124            *  Yes or No. Yes enables session variables. Default is No.
125            * @param setSessionManagement value to set
126            **/
127            public void setSessionmanagement(boolean setSessionManagement)  {
128                    this.setSessionManagement=setSessionManagement?Boolean.TRUE:Boolean.FALSE;
129                //getAppContext().setSetSessionManagement(setSessionManagement);
130            }
131    
132        
133            /**
134             * @param datasource the datasource to set
135             * @throws PageException 
136             */
137            public void setDatasource(Object datasource) throws PageException {
138                    this.datasource = Caster.toString(datasource);
139            }
140            public void setDefaultdatasource(String defaultdatasource) {
141                    this.defaultdatasource = defaultdatasource;
142            }
143            
144            public void setLocalmode(String strLocalMode) throws ApplicationException {
145                    this.localMode = AppListenerUtil.toLocalMode(strLocalMode);
146                    
147            }
148    
149            /** set the value clientstorage
150            *  Specifies how Railo stores client variables
151            * @param clientstorage value to set
152            **/
153            public void setClientstorage(String clientstorage)      {
154                    this.clientstorage=clientstorage;
155            }
156    
157            public void setSessionstorage(String sessionstorage)    {
158                    this.sessionstorage=sessionstorage;
159            }
160    
161            /** set the value clientmanagement
162            *  Yes or No. Enables client variables. Default is No.
163            * @param setClientManagement value to set
164            **/
165            public void setClientmanagement(boolean setClientManagement)    {
166                    this.setClientManagement=setClientManagement?Boolean.TRUE:Boolean.FALSE;
167                //getAppContext().setSetClientManagement(setClientManagement);
168            }
169    
170            /** set the value sessiontimeout
171            *  Enter the CreateTimeSpan function and values in days, hours, minutes, and seconds, separated 
172            *               by commas, to specify the lifespan of session variables.
173            * @param sessionTimeout value to set
174            **/
175            public void setSessiontimeout(TimeSpan sessionTimeout)  {
176                    this.sessionTimeout=sessionTimeout;
177            }
178            public void setSessiontype(String sessionType) throws ApplicationException      {
179                    this.sessionType=AppListenerUtil.toSessionType(sessionType);
180            }
181            public void setClientcluster(boolean clientCluster) {
182                    this.clientCluster=clientCluster;
183            }
184            public void setSessioncluster(boolean sessionCluster) {
185                    this.sessionCluster=sessionCluster;
186            }
187            
188            public void setClienttimeout(TimeSpan clientTimeout)    {
189                    this.clientTimeout=clientTimeout;
190            }
191            
192    
193    
194            /**
195             * @param ormenabled the ormenabled to set
196             */
197            public void setOrmenabled(boolean ormenabled) {
198                    this.ormenabled = ormenabled;
199            }
200    
201            /**
202             * @param ormsettings the ormsettings to set
203             */
204            public void setOrmsettings(Struct ormsettings) {
205                    this.ormsettings = ormsettings;
206            }
207    
208            /**
209             * @param s3 the s3 to set
210             */
211            public void setS3(Struct s3) {
212                    this.s3 = s3;
213            }
214    
215            /** set the value applicationtimeout
216            *  Enter the CreateTimeSpan function and values in days, hours, minutes, and seconds, separated 
217            *               by commas, to specify the lifespan of application variables. 
218            * @param applicationTimeout value to set
219            **/
220            public void setApplicationtimeout(TimeSpan applicationTimeout)  {
221                    this.applicationTimeout=applicationTimeout;
222                //getAppContext().setApplicationTimeout(applicationTimeout);
223            }
224    
225            /** set the value name
226            *  The name of your application. This name can be up to 64 characters long.
227            *               Required for application and session variables, optional for client variables
228            * @param name value to set
229            **/
230            public void setName(String name)        {
231                this.name=name;
232            }
233            
234            public void setAction(String strAction) throws ApplicationException     {
235                    strAction=strAction.toLowerCase();
236            if(strAction.equals("create"))action=ACTION_CREATE;
237            else if(strAction.equals("update")) action=ACTION_UPDATE;
238            else throw new ApplicationException("invalid action definition ["+strAction+"] for tag application, valid values are [create,update]");
239        
240            }
241            
242            public void setMappings(Struct mappings) throws PageException   {
243                this.mappings=AppListenerUtil.toMappings(pageContext.getConfig(), mappings);
244                    //getAppContext().setMappings(AppListenerUtil.toMappings(pageContext, mappings));
245            }
246            
247            public void setCustomtagpaths(Object mappings) throws PageException     {
248                this.customTagMappings=AppListenerUtil.toCustomTagMappings(pageContext.getConfig(), mappings);
249            }
250            
251            public void setComponentpaths(Object mappings) throws PageException     {
252                this.componentMappings=AppListenerUtil.toCustomTagMappings(pageContext.getConfig(), mappings);
253                    //getAppContext().setCustomTagMappings(AppListenerUtil.toCustomTagMappings(pageContext, mappings));
254            }
255            
256    
257            public void setSecurejsonprefix(String secureJsonPrefix)        {
258                    this.secureJsonPrefix=secureJsonPrefix;
259                //getAppContext().setSecureJsonPrefix(secureJsonPrefix);
260            }
261            public void setSecurejson(boolean secureJson)   {
262                    this.secureJson=secureJson?Boolean.TRUE:Boolean.FALSE;
263                //getAppContext().setSecureJson(secureJson);
264            }
265            
266        /**
267         * @param loginstorage The loginstorage to set.
268         * @throws ApplicationException
269         */
270        public void setLoginstorage(String loginstorage) throws ApplicationException {
271            loginstorage=loginstorage.toLowerCase();
272            if(loginstorage.equals("session"))this.loginstorage=Scope.SCOPE_SESSION;
273            else if(loginstorage.equals("cookie"))this.loginstorage=Scope.SCOPE_COOKIE;
274            else throw new ApplicationException("invalid loginStorage definition ["+loginstorage+"] for tag application, valid values are [session,cookie]");
275        }
276            /**
277             * @param scriptrotect the scriptrotect to set
278             */                     
279            public void setScriptprotect(String strScriptrotect) {
280                    this.scriptrotect=strScriptrotect;
281                    //getAppContext().setScriptProtect(strScriptrotect);
282            }
283    
284    
285            /**
286             * @throws PageException 
287             * @see javax.servlet.jsp.tagext.Tag#doStartTag()
288            */
289            public int doStartTag() throws PageException    {
290            
291            ApplicationContextPro ac;
292            boolean initORM;
293            if(action==ACTION_CREATE){
294                    ac=new ClassicApplicationContext(pageContext.getConfig(),name,false,ResourceUtil.getResource(pageContext,pageContext.getCurrentPageSource()));
295                    initORM=set(ac);
296                    pageContext.setApplicationContext(ac);
297            }
298            else {
299                    ac=(ApplicationContextPro) pageContext.getApplicationContext();
300                    initORM=set(ac);
301            }
302            
303            if(initORM) ORMUtil.resetEngine(pageContext,false);
304            
305            return SKIP_BODY; 
306            }
307    
308            private boolean set(ApplicationContextPro ac) throws PageException {
309                    if(applicationTimeout!=null)                    ac.setApplicationTimeout(applicationTimeout);
310                    if(sessionTimeout!=null)                                ac.setSessionTimeout(sessionTimeout);
311                    if(clientTimeout!=null)                         ac.setClientTimeout(clientTimeout);
312                    if(clientstorage!=null) {
313                            ac.setClientstorage(clientstorage);
314                    }
315                    if(sessionstorage!=null)        {
316                            ac.setSessionstorage(sessionstorage);
317                    }
318                    if(customTagMappings!=null)                             ac.setCustomTagMappings(customTagMappings);
319                    if(componentMappings!=null)                             ac.setComponentMappings(componentMappings);
320                    if(mappings!=null)                                              ac.setMappings(mappings);
321                    if(loginstorage!=Scope.SCOPE_UNDEFINED) ac.setLoginStorage(loginstorage);
322                    if(!StringUtil.isEmpty(datasource))             {
323                            ac.setDefaultDataSource(datasource);
324                            ac.setORMDatasource(datasource);
325                    }
326                    if(!StringUtil.isEmpty(defaultdatasource))ac.setDefaultDataSource(defaultdatasource);
327                    if(scriptrotect!=null)                                  ac.setScriptProtect(AppListenerUtil.translateScriptProtect(scriptrotect));
328                    if(secureJson!=null)                                    ac.setSecureJson(secureJson.booleanValue());
329                    if(secureJsonPrefix!=null)                              ac.setSecureJsonPrefix(secureJsonPrefix);
330                    if(setClientCookies!=null)                              ac.setSetClientCookies(setClientCookies.booleanValue());
331                    if(setClientManagement!=null)                   ac.setSetClientManagement(setClientManagement.booleanValue());
332                    if(setDomainCookies!=null)                              ac.setSetDomainCookies(setDomainCookies.booleanValue());
333                    if(setSessionManagement!=null)                  ac.setSetSessionManagement(setSessionManagement.booleanValue());
334                    if(localMode!=-1)                                               ac.setLocalMode(localMode);
335                    if(sessionType!=-1)                                     ac.setSessionType(sessionType);
336                    ac.setClientCluster(clientCluster);
337                    ac.setSessionCluster(sessionCluster);
338                    if(s3!=null)                                                    ac.setS3(AppListenerUtil.toS3(s3));
339                    
340                    // ORM
341                    boolean initORM=false;
342                    ac.setORMEnabled(ormenabled);
343                    if(ormenabled) {
344                            initORM=true;
345                            AppListenerUtil.setORMConfiguration(pageContext, ac, ormsettings);
346                    }
347                    
348                    
349                    return initORM;
350            }
351    
352            /**
353            * @see javax.servlet.jsp.tagext.Tag#doEndTag()
354            */
355            public int doEndTag()   {
356                    return EVAL_PAGE;
357            }
358    
359    }