001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.engine;
018:
019: import java.text.DateFormat;
020: import java.util.Date;
021: import java.util.Map;
022:
023: import javax.servlet.ServletConfig;
024:
025: import org.apache.commons.configuration.Configuration;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.jetspeed.JetspeedPortalContext;
029: import org.apache.jetspeed.PortalContext;
030: import org.apache.jetspeed.PortalReservedParameters;
031: import org.apache.jetspeed.administration.PortalConfiguration;
032: import org.apache.jetspeed.administration.PortalConfigurationImpl;
033: import org.apache.jetspeed.components.ComponentManager;
034: import org.apache.jetspeed.exception.JetspeedException;
035: import org.apache.jetspeed.pipeline.Pipeline;
036: import org.apache.jetspeed.request.RequestContext;
037: import org.apache.jetspeed.request.RequestContextComponent;
038: import org.apache.jetspeed.statistics.PortalStatistics;
039: import org.apache.ojb.broker.util.ClassHelper;
040: import org.apache.pluto.PortletContainer;
041: import org.apache.pluto.PortletContainerException;
042: import org.apache.pluto.factory.Factory;
043: import org.apache.pluto.services.ContainerService;
044: import org.apache.pluto.services.factory.FactoryManagerService;
045: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
046:
047: /**
048: * <p>
049: * AbstractEngine
050: * </p>
051: * <p>
052: *
053: * </p>
054: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
055: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
056: * @version $Id: AbstractEngine.java 188433 2005-03-23 22:50:44Z ate $
057: *
058: */
059: public class JetspeedEngine implements Engine {
060: private final PortalContext context;
061: private final ServletConfig config;
062: private final ComponentManager componentManager;
063: private Map pipelineMapper;
064: private PortalStatistics statistics;
065:
066: protected static final Log log = LogFactory
067: .getLog(JetspeedEngine.class);
068: protected String defaultPipelineName;
069:
070: public JetspeedEngine(Configuration configuration,
071: String applicationRoot, ServletConfig config,
072: ComponentManager componentManager) {
073: this (new PortalConfigurationImpl(configuration),
074: applicationRoot, config, componentManager);
075: }
076:
077: public JetspeedEngine(PortalConfiguration configuration,
078: String applicationRoot, ServletConfig config,
079: ComponentManager componentManager) {
080: this .componentManager = componentManager;
081: this .context = new JetspeedPortalContext(this , configuration,
082: applicationRoot);
083: this .config = config;
084: context.setApplicationRoot(applicationRoot);
085: context.setConfiguration(configuration);
086:
087: defaultPipelineName = configuration.getString(PIPELINE_DEFAULT,
088: "jetspeed-pipeline");
089: configuration.setString(
090: JetspeedEngineConstants.APPLICATION_ROOT_KEY,
091: applicationRoot);
092:
093: // Make these availble as beans to Spring
094: componentManager.addComponent("Engine", this );
095: componentManager.addComponent("PortalContext", context);
096: componentManager.addComponent("PortalConfiguration",
097: configuration);
098: }
099:
100: /**
101: * Initializes the engine with a commons configuration, starting all early
102: * initable services.
103: *
104: * @param configuration
105: * a commons <code>Configuration</code> set
106: * @param applicationRoot
107: * a <code>String</code> path to the application root for
108: * resources
109: * @param
110: * @throws JetspeedException
111: * when the engine fails to initilialize
112: */
113: public void start() throws JetspeedException {
114: DateFormat format = DateFormat.getInstance();
115: Date startTime = new Date();
116: try {
117: log.info("Starting Jetspeed Engine ("
118: + getClass().getName() + ") at "
119: + format.format(startTime));
120:
121: // patch up OJB
122: ClassLoader ploader2 = this .getClass().getClassLoader();
123: //ClassLoader ploader2 = Thread.currentThread().getContextClassLoader();
124: ClassHelper.setClassLoader(ploader2);
125:
126: //Start the ComponentManager
127: componentManager.start();
128: pipelineMapper = (Map) componentManager
129: .getComponent("pipeline-map");
130: try {
131: statistics = (PortalStatistics) componentManager
132: .getComponent("PortalStatistics");
133: } catch (Exception e) {
134: // silenty ignore, its not configured
135: // TODO: statistics as an AOP advice
136: }
137: // TODO: complete this work for JSP (https://issues.apache.org/jira/browse/JS2-711)
138: // I think config.getServletName is incorrect, need to fix this and change this name to jetspeed-layouts:: when looking up in registry
139: // but not when dispatching, still trying to figure that out
140: //PortletApplicationManagement pam = (PortletApplicationManagement)componentManager.getComponent("PAM");
141: //pam.startInternalApplication(config.getServletName());
142:
143: } catch (Throwable e) {
144: e.printStackTrace();
145: log.error(e.toString());
146: throw new JetspeedException(
147: "Jetspeed Initialization exception!", e);
148: } finally {
149: Date endTime = new Date();
150: long elapsedTime = (endTime.getTime() - startTime.getTime()) / 1000;
151: log.info("Finished starting Jetspeed Engine ("
152: + getClass().getName() + ") at "
153: + format.format(endTime) + ". Elapsed time: "
154: + elapsedTime + " seconds.");
155: }
156: }
157:
158: /**
159: * Get the servlet configuration if this engine is running under a servlet
160: * container.
161: *
162: * @return config The servlet configuration
163: */
164: public ServletConfig getServletConfig() {
165: return this .config;
166: }
167:
168: public void shutdown() throws JetspeedException {
169:
170: try {
171: PortletContainer container = (PortletContainer) componentManager
172: .getComponent(PortletContainer.class);
173: if (container != null) {
174: container.shutdown();
175: }
176:
177: componentManager.stop();
178: } catch (PortletContainerException e) {
179: throw new JetspeedException(e);
180: }
181: System.gc();
182: }
183:
184: public void service(RequestContext context)
185: throws JetspeedException {
186: long start = System.currentTimeMillis();
187: String targetPipeline = context
188: .getRequestParameter(PortalReservedParameters.PIPELINE);
189: if (null == targetPipeline) {
190: targetPipeline = (String) context
191: .getAttribute(PortalReservedParameters.PIPELINE);
192: if (null == targetPipeline) {
193: String pipelineKey = context.getRequest()
194: .getServletPath();
195: if (null != pipelineKey) {
196: if (pipelineKey.equals("/portal"))
197: targetPipeline = this .defaultPipelineName;
198: else
199: targetPipeline = (String) pipelineMapper
200: .get(pipelineKey);
201: // System.out.println("pipeline = " + targetPipeline);
202: } else {
203: targetPipeline = this .defaultPipelineName;
204: }
205: }
206: }
207: Pipeline pipeline = null;
208: if (targetPipeline != null) {
209: Pipeline specificPipeline = getPipeline(targetPipeline);
210: if (specificPipeline != null) {
211: pipeline = specificPipeline;
212: }
213: } else
214: pipeline = getPipeline();
215:
216: context.setPipeline(pipeline);
217: pipeline.invoke(context);
218:
219: long end = System.currentTimeMillis();
220: if (statistics != null)
221: statistics.logPageAccess(context, PortalStatistics.HTTP_OK,
222: end - start);
223: }
224:
225: /**
226: * Returns the context associated with this engine.
227: *
228: * @return an <code>EngineContext</code> associated with this engine
229: */
230: public PortalContext getContext() {
231: return this .context;
232: }
233:
234: /**
235: * Given a application relative path, returns the real path relative to the
236: * application root
237: *
238: */
239: public String getRealPath(String path) {
240: String result = "";
241: String base = context.getApplicationRoot();
242: if (base.endsWith(java.io.File.separator)) {
243: if (path.startsWith("/")) {
244: result = base.concat(path.substring(1));
245: return result;
246: }
247: } else {
248: if (!path.startsWith("/")) {
249: result = base.concat("/").concat(path);
250: return result;
251: }
252: }
253: return base.concat(path);
254: }
255:
256: public Pipeline getPipeline(String pipelineName) {
257: return (Pipeline) componentManager.getComponent(pipelineName);
258: }
259:
260: public Pipeline getPipeline() {
261: return getPipeline(defaultPipelineName);
262: }
263:
264: /**
265: * @see org.apache.jetspeed.engine.Engine#getCurrentRequestContext()
266: */
267: public RequestContext getCurrentRequestContext() {
268: RequestContextComponent contextComponent = (RequestContextComponent) getComponentManager()
269: .getComponent(RequestContextComponent.class);
270: return contextComponent.getRequestContext();
271: }
272:
273: public ComponentManager getComponentManager() {
274: return this .componentManager;
275: }
276:
277: /**
278: * <p>
279: * getFactory
280: * </p>
281: *
282: * @see org.apache.pluto.services.factory.FactoryManagerService#getFactory(java.lang.Class)
283: * @param theClass
284: * @return
285: */
286: public Factory getFactory(Class theClass) {
287: return (Factory) getComponentManager().getComponent(theClass);
288: }
289:
290: /**
291: * <p>
292: * getContainerService
293: * </p>
294: *
295: * @see org.apache.pluto.services.PortletContainerEnvironment#getContainerService(java.lang.Class)
296: * @param service
297: * @return
298: */
299: public ContainerService getContainerService(Class service) {
300: if (service.equals(FactoryManagerService.class)) {
301: return this ;
302: }
303:
304: try {
305: return (ContainerService) getComponentManager()
306: .getComponent(service);
307: } catch (NoSuchBeanDefinitionException e) {
308: log.warn("No ContainerService defined for "
309: + service.getName());
310: return null;
311: }
312: }
313:
314: }
|