KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > events > JSPEventListener


1 package org.jahia.services.events;
2
3 import org.jahia.data.events.JahiaEvent;
4 import org.jahia.data.events.JahiaEventListener;
5 import org.jahia.params.ParamBean;
6 import org.jahia.registries.ServicesRegistry;
7
8 import java.io.File JavaDoc;
9 import org.jahia.content.events.ContentActivationEvent;
10
11 /**
12  * <p>Title: A Jahia Event Listener that dispatches to JSP files</p>
13  * <p>Description: This event listeners allows the use of JSP files as
14  * event listeners, allowing for customizations in a more script-like way
15  * than having to compile actual code. The JSPs here are not used as a
16  * template language, meaning that nothing will be done with the output of
17  * the JSP, but simply as a script extensions functionality.</p>
18  * <p>Copyright: Copyright (c) 2002</p>
19  * <p>Company: Jahia Ltd</p>
20  *
21  * @author Serge Huber
22  * @version 1.0
23  */

24
25 public class JSPEventListener extends JahiaEventListener {
26
27     private static org.apache.log4j.Logger logger =
28             org.apache.log4j.Logger.getLogger (JSPEventListener.class);
29
30     private boolean configLoaded = false;
31     private String JavaDoc defaultPathToJSP = null;
32     private String JavaDoc jspFileName = null;
33
34     private static final String JavaDoc DEFAULT_PATH_TO_JSP =
35             "/jsp/jahia/events/eventlistener.jsp";
36     private static final String JavaDoc JSP_FILE_NAME = "eventlistener.jsp";
37
38     private boolean checkConfig (ParamBean paramBean) {
39         if (configLoaded)
40             return configLoaded;
41
42         loadConfig (paramBean);
43
44         return configLoaded;
45     }
46
47     private void loadConfig (ParamBean paramBean) {
48
49         /**
50          * @todo for the moment this stuff is hardcoded, but we might want to
51          * make this configurable through an XML file.
52          */

53
54         defaultPathToJSP = DEFAULT_PATH_TO_JSP;
55         jspFileName = JSP_FILE_NAME;
56
57         configLoaded = true;
58     }
59
60     public JSPEventListener () {
61     }
62
63     private void dispatchToJSP (String JavaDoc eventName, JahiaEvent je) {
64         if (!checkConfig (je.getParams ())) {
65             return;
66         }
67
68         ParamBean paramBean = je.getParams ();
69         if (paramBean == null) {
70             logger.warn ("Cannot dispatch to JSP because we need request/response pair to do so for event " +
71                     eventName);
72             return;
73         }
74         try {
75             String JavaDoc jspFileName = resolveJSPFullFileName (paramBean);
76
77             logger.debug ("Dispatching to JSP " + jspFileName +
78                     " for processing of event " + eventName);
79             if (paramBean.getRequest() != null) {
80                 paramBean.getRequest ().setAttribute ("eventName", eventName);
81                 paramBean.getRequest ().setAttribute ("jahiaEvent", je);
82                 ServicesRegistry.getInstance ().getJahiaFetcherService ()
83                         .fetchServlet (paramBean, jspFileName);
84             }
85             /*
86             paramBean.getRequest().getRequestDispatcher(jspFileName).include(
87                 paramBean.getRequest(), paramBean.getResponse());
88             */

89         } catch (Throwable JavaDoc t) {
90             logger.error ("Error while dispatching to JSP : " + jspFileName, t);
91         }
92     }
93
94     private String JavaDoc resolveJSPFullFileName (ParamBean paramBean) {
95         String JavaDoc jspFullFileName;
96         if ((paramBean.getPage () != null) &&
97                 (paramBean.getPage ().getPageTemplate () != null) &&
98                 (paramBean.getPage ().getPageTemplate ().getSourcePath () != null)) {
99             jspFullFileName = paramBean.getPage ().getPageTemplate ().
100                     getSourcePath ();
101             logger.debug ("template source path :" + jspFullFileName);
102
103             jspFullFileName = jspFullFileName.substring (0,
104                     jspFullFileName.lastIndexOf ("/") + 1) + jspFileName;
105
106             logger.debug ("resolvedJSPFullFileName :" + jspFullFileName);
107
108             File JavaDoc jspFile = new File JavaDoc (paramBean.getContext ().getRealPath (jspFullFileName));
109             if (!jspFile.exists ()) {
110                 jspFullFileName = defaultPathToJSP;
111             }
112         } else {
113             jspFullFileName = defaultPathToJSP;
114         }
115         return jspFullFileName;
116     }
117
118     public void beforeServicesLoad (JahiaEvent je) {
119         dispatchToJSP ("beforeServicesLoad", je);
120     }
121
122     public void afterServicesLoad (JahiaEvent je) {
123         dispatchToJSP ("afterServicesLoad", je);
124     }
125
126     public void beforeFieldActivation (JahiaEvent je) {
127         dispatchToJSP ("beforeFieldActivation", je);
128     }
129
130     public void fieldAdded (JahiaEvent je) {
131         dispatchToJSP ("fieldAdded", je);
132     }
133
134     public void fieldUpdated (JahiaEvent je) {
135         dispatchToJSP ("fieldUpdated", je);
136     }
137
138     public void fieldDeleted (JahiaEvent je) {
139         dispatchToJSP ("fieldDeleted", je);
140     }
141
142     public void addContainerEngineAfterInit (JahiaEvent je) {
143         dispatchToJSP ("addContainerEngineAfterInit", je);
144     }
145
146     public void addContainerEngineBeforeSave (JahiaEvent je) {
147         dispatchToJSP ("addContainerEngineBeforeSave", je);
148     }
149
150     public void containerAdded (JahiaEvent je) {
151         dispatchToJSP ("containerAdded", je);
152     }
153
154     public void containerUpdated (JahiaEvent je) {
155         dispatchToJSP ("containerUpdated", je);
156     }
157
158     public void containerDeleted (JahiaEvent je) {
159         dispatchToJSP ("containerDeleted", je);
160     }
161
162     public void pageAdded (JahiaEvent je) {
163         dispatchToJSP ("pageAdded", je);
164     }
165
166     public void pageLoaded (JahiaEvent je) {
167         dispatchToJSP ("pageLoaded", je);
168     }
169
170     public void pagePropertiesSet (JahiaEvent je) {
171         dispatchToJSP ("pagePropertiesSet", je);
172     }
173
174     public void containerListPropertiesSet (JahiaEvent je) {
175         dispatchToJSP ("containerListPropertiesSet", je);
176     }
177
178     public void rightsSet (JahiaEvent je) {
179         dispatchToJSP ("rightsSet", je);
180     }
181
182     public void userLoggedIn (JahiaEvent je) {
183         dispatchToJSP ("userLoggedIn", je);
184     }
185
186     public void userLoggedOut (JahiaEvent je) {
187         dispatchToJSP ("userLoggedOut", je);
188     }
189
190     public void contentActivation (ContentActivationEvent theEvent) {
191         dispatchToJSP ("contentActivation", theEvent);
192     }
193 }
194
Popular Tags