001    /*
002     * Copyright 1999-2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.latka.jelly;
018    
019    import org.apache.commons.jelly.JellyContext;
020    import org.apache.commons.jelly.Tag;
021    import org.apache.commons.jelly.TagSupport;
022    
023    import org.apache.commons.latka.event.LatkaEventInfo;
024    import org.apache.commons.latka.event.LatkaEventListener;
025    import org.apache.commons.latka.http.Request;
026    
027    /**
028     *
029     * @author  Morgan Delagrange
030     */
031    public class JellyUtils {
032            
033        protected static final String EVENT_INFO_VAR =
034            "latkaEventInfo";
035        protected static final String EVENT_LISTENER_VAR =
036            "latkaEventListener";
037    
038        protected static JellyUtils _utils = new JellyUtils();
039    
040        /** Creates a new instance of SuiteTag */
041        public JellyUtils() {
042        }
043    
044        public static JellyUtils getInstance() {
045            return _utils;
046        }
047        
048        /**
049         * Return the LatkaEventInfo for the context,
050         * or null if none has been set
051         * 
052         * @param context Context for the current Jelly script
053         * @return LatkaEventInfo for the context,
054         *         or null if none has been set
055         */
056        public LatkaEventInfo getLatkaEventInfo(JellyContext context) {
057            return (LatkaEventInfo) context.getVariable(EVENT_INFO_VAR);
058        }
059    
060        /**
061         * Provide a LatkaEventInfo object.
062         * 
063         * @param context 
064         * @param listener LatkaEventInfo object
065         */
066        public void setLatkaEventInfo(JellyContext context, LatkaEventInfo info) {
067            context.setVariable(EVENT_INFO_VAR,info);
068        }
069    
070    
071        /**
072         * Remove a LatkaEventInfo object.
073         * 
074         * @param context 
075         */
076        public void removeLatkaEventInfo(JellyContext context) {
077            context.removeVariable(EVENT_INFO_VAR);
078        }
079    
080        /**
081         * Return the LatkaEventListener for the context,
082         * or null if none has been set
083         * 
084         * @param context Context for the current Jelly script
085         * @return LatkaEventListener for the context,
086         *         or null if none has been set
087         */
088        public LatkaEventListener getLatkaEventListener(JellyContext context) {
089            return (LatkaEventListener) context.getVariable(EVENT_LISTENER_VAR);
090        }
091    
092        /**
093         * Provide a LatkaEventListener object.
094         * 
095         * @param context 
096         * @param listener LatkaEventListener object
097         */
098        public void setLatkaEventListener(JellyContext context, LatkaEventListener listener) {
099            context.setVariable(EVENT_LISTENER_VAR,listener);
100        }
101    
102        /**
103         * Given a tag, find a parent RequestTag and return
104         * its Request object.
105         * 
106         * @param tag    Child tag of RequestTag
107         * @return Request
108         */
109        public Request findParentRequest(Tag tag) {
110            RequestTag requestTag = 
111                (RequestTag) TagSupport.findAncestorWithClass(tag.getParent(), RequestTag.class);
112            return requestTag.getRequest();
113        }
114    }