1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.engine;
18
19 import java.text.DateFormat;
20 import java.util.Date;
21 import java.util.Map;
22
23 import javax.servlet.ServletConfig;
24
25 import org.apache.commons.configuration.Configuration;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.jetspeed.JetspeedPortalContext;
29 import org.apache.jetspeed.PortalContext;
30 import org.apache.jetspeed.PortalReservedParameters;
31 import org.apache.jetspeed.administration.PortalConfiguration;
32 import org.apache.jetspeed.administration.PortalConfigurationImpl;
33 import org.apache.jetspeed.components.ComponentManager;
34 import org.apache.jetspeed.exception.JetspeedException;
35 import org.apache.jetspeed.pipeline.Pipeline;
36 import org.apache.jetspeed.request.RequestContext;
37 import org.apache.jetspeed.request.RequestContextComponent;
38 import org.apache.jetspeed.statistics.PortalStatistics;
39 import org.apache.ojb.broker.util.ClassHelper;
40 import org.apache.pluto.PortletContainer;
41 import org.apache.pluto.PortletContainerException;
42 import org.apache.pluto.factory.Factory;
43 import org.apache.pluto.services.ContainerService;
44 import org.apache.pluto.services.factory.FactoryManagerService;
45 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
46
47
48 /***
49 * <p>
50 * AbstractEngine
51 * </p>
52 * <p>
53 *
54 * </p>
55 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
56 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
57 * @version $Id: AbstractEngine.java 188433 2005-03-23 22:50:44Z ate $
58 *
59 */
60 public class JetspeedEngine implements Engine
61 {
62 private final PortalContext context;
63 private final ServletConfig config;
64 private final ComponentManager componentManager;
65 private Map pipelineMapper ;
66 private PortalStatistics statistics;
67
68 protected static final Log log = LogFactory.getLog(JetspeedEngine.class);
69 protected String defaultPipelineName;
70
71 public JetspeedEngine(Configuration configuration, String applicationRoot, ServletConfig config, ComponentManager componentManager )
72 {
73 this(new PortalConfigurationImpl(configuration), applicationRoot, config, componentManager);
74 }
75
76 public JetspeedEngine(PortalConfiguration configuration, String applicationRoot, ServletConfig config, ComponentManager componentManager )
77 {
78 this.componentManager = componentManager;
79 this.context = new JetspeedPortalContext(this, configuration, applicationRoot);
80 this.config = config;
81 context.setApplicationRoot(applicationRoot);
82 context.setConfiguration(configuration);
83
84 defaultPipelineName = configuration.getString(PIPELINE_DEFAULT, "jetspeed-pipeline");
85 configuration.setString(JetspeedEngineConstants.APPLICATION_ROOT_KEY, applicationRoot);
86
87
88 componentManager.addComponent("Engine", this);
89 componentManager.addComponent("PortalContext", context);
90 componentManager.addComponent("PortalConfiguration", configuration);
91 }
92
93
94
95 /***
96 * Initializes the engine with a commons configuration, starting all early
97 * initable services.
98 *
99 * @param configuration
100 * a commons <code>Configuration</code> set
101 * @param applicationRoot
102 * a <code>String</code> path to the application root for
103 * resources
104 * @param
105 * @throws JetspeedException
106 * when the engine fails to initilialize
107 */
108 public void start() throws JetspeedException
109 {
110 DateFormat format = DateFormat.getInstance();
111 Date startTime = new Date();
112 try
113 {
114 log.info("Starting Jetspeed Engine ("+getClass().getName()+") at "+format.format(startTime));
115
116
117 ClassLoader ploader2 = this.getClass().getClassLoader();
118
119 ClassHelper.setClassLoader(ploader2);
120
121
122 componentManager.start();
123 pipelineMapper = (Map)componentManager.getComponent("pipeline-map");
124 try
125 {
126 statistics = (PortalStatistics)componentManager.getComponent("PortalStatistics");
127 }
128 catch (Exception e)
129 {
130
131
132 }
133
134
135
136
137
138
139 }
140 catch (Throwable e)
141 {
142 e.printStackTrace();
143 log.error(e.toString());
144 throw new JetspeedException("Jetspeed Initialization exception!", e);
145 }
146 finally
147 {
148 Date endTime = new Date();
149 long elapsedTime = (endTime.getTime() - startTime.getTime()) / 1000;
150 log.info("Finished starting Jetspeed Engine ("+getClass().getName()+") at "+format.format(endTime)
151 +". Elapsed time: "+elapsedTime+" seconds.");
152 }
153 }
154
155 /***
156 * Get the servlet configuration if this engine is running under a servlet
157 * container.
158 *
159 * @return config The servlet configuration
160 */
161 public ServletConfig getServletConfig()
162 {
163 return this.config;
164 }
165
166
167
168 public void shutdown() throws JetspeedException
169 {
170
171 try
172 {
173 PortletContainer container = (PortletContainer) componentManager
174 .getComponent(PortletContainer.class);
175 if (container != null)
176 {
177 container.shutdown();
178 }
179
180 componentManager.stop();
181 }
182 catch (PortletContainerException e)
183 {
184 throw new JetspeedException(e);
185 }
186 System.gc();
187 }
188
189 public void service( RequestContext context ) throws JetspeedException
190 {
191 long start = System.currentTimeMillis();
192 String targetPipeline = context
193 .getRequestParameter(PortalReservedParameters.PIPELINE);
194 if (null == targetPipeline)
195 {
196 targetPipeline = (String)context.getAttribute(PortalReservedParameters.PIPELINE);
197 if (null == targetPipeline)
198 {
199 String pipelineKey = context.getRequest().getServletPath();
200 if (null != pipelineKey)
201 {
202 if (pipelineKey.equals("/portal"))
203 targetPipeline = this.defaultPipelineName;
204 else
205 targetPipeline = (String)pipelineMapper.get(pipelineKey);
206
207 }
208 else
209 {
210 targetPipeline = this.defaultPipelineName;
211 }
212 }
213 }
214 Pipeline pipeline = null;
215 if (targetPipeline != null)
216 {
217 Pipeline specificPipeline = getPipeline(targetPipeline);
218 if (specificPipeline != null)
219 {
220 pipeline = specificPipeline;
221 }
222 }
223 else
224 pipeline = getPipeline();
225
226 context.setPipeline(pipeline);
227 pipeline.invoke(context);
228
229 long end = System.currentTimeMillis();
230 if (statistics != null)
231 statistics.logPageAccess(context, PortalStatistics.HTTP_OK, end - start);
232 }
233
234 /***
235 * Returns the context associated with this engine.
236 *
237 * @return an <code>EngineContext</code> associated with this engine
238 */
239 public PortalContext getContext()
240 {
241 return this.context;
242 }
243
244 /***
245 * Given a application relative path, returns the real path relative to the
246 * application root
247 *
248 */
249 public String getRealPath( String path )
250 {
251 String result = "";
252 String base = context.getApplicationRoot();
253 if (base.endsWith(java.io.File.separator))
254 {
255 if (path.startsWith("/"))
256 {
257 result = base.concat(path.substring(1));
258 return result;
259 }
260 }
261 else
262 {
263 if (!path.startsWith("/"))
264 {
265 result = base.concat("/").concat(path);
266 return result;
267 }
268 }
269 return base.concat(path);
270 }
271
272 public Pipeline getPipeline( String pipelineName )
273 {
274 return (Pipeline) componentManager.getComponent(pipelineName);
275 }
276
277 public Pipeline getPipeline()
278 {
279 return getPipeline(defaultPipelineName);
280 }
281
282 /***
283 * @see org.apache.jetspeed.engine.Engine#getCurrentRequestContext()
284 */
285 public RequestContext getCurrentRequestContext()
286 {
287 RequestContextComponent contextComponent = (RequestContextComponent) getComponentManager()
288 .getComponent(RequestContextComponent.class);
289 return contextComponent.getRequestContext();
290 }
291
292 public ComponentManager getComponentManager()
293 {
294 return this.componentManager;
295 }
296 /***
297 * <p>
298 * getFactory
299 * </p>
300 *
301 * @see org.apache.pluto.services.factory.FactoryManagerService#getFactory(java.lang.Class)
302 * @param theClass
303 * @return
304 */
305 public Factory getFactory( Class theClass )
306 {
307 return (Factory) getComponentManager().getComponent(theClass);
308 }
309 /***
310 * <p>
311 * getContainerService
312 * </p>
313 *
314 * @see org.apache.pluto.services.PortletContainerEnvironment#getContainerService(java.lang.Class)
315 * @param service
316 * @return
317 */
318 public ContainerService getContainerService( Class service )
319 {
320 if(service.equals(FactoryManagerService.class))
321 {
322 return this;
323 }
324
325 try
326 {
327 return (ContainerService) getComponentManager().getComponent(service);
328 }
329 catch (NoSuchBeanDefinitionException e)
330 {
331 log.warn("No ContainerService defined for "+service.getName());
332 return null;
333 }
334 }
335
336 }