KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > Cocoon


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon;
17
18 import org.apache.avalon.excalibur.component.ComponentProxyGenerator;
19 import org.apache.avalon.excalibur.component.DefaultRoleManager;
20 import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
21 import org.apache.avalon.excalibur.logger.LoggerManager;
22 import org.apache.avalon.framework.activity.Disposable;
23 import org.apache.avalon.framework.activity.Initializable;
24 import org.apache.avalon.framework.component.Component;
25 import org.apache.avalon.framework.component.ComponentException;
26 import org.apache.avalon.framework.component.ComponentManager;
27 import org.apache.avalon.framework.component.Composable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.configuration.DefaultConfiguration;
31 import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
32 import org.apache.avalon.framework.container.ContainerUtil;
33 import org.apache.avalon.framework.context.Context;
34 import org.apache.avalon.framework.context.ContextException;
35 import org.apache.avalon.framework.context.Contextualizable;
36 import org.apache.avalon.framework.context.DefaultContext;
37 import org.apache.avalon.framework.logger.AbstractLogEnabled;
38 import org.apache.avalon.framework.logger.Logger;
39 import org.apache.avalon.framework.thread.ThreadSafe;
40
41 import org.apache.cocoon.components.CocoonComponentManager;
42 import org.apache.cocoon.components.ComponentContext;
43 import org.apache.cocoon.components.pipeline.ProcessingPipeline;
44 import org.apache.cocoon.components.source.SourceUtil;
45 import org.apache.cocoon.components.source.impl.DelayedRefreshSourceWrapper;
46 import org.apache.cocoon.environment.Environment;
47 import org.apache.cocoon.environment.ObjectModelHelper;
48 import org.apache.cocoon.environment.Request;
49 import org.apache.cocoon.environment.Session;
50 import org.apache.cocoon.util.ClassUtils;
51 import org.apache.cocoon.util.Deprecation;
52 import org.apache.cocoon.util.location.Location;
53 import org.apache.cocoon.util.location.LocationImpl;
54 import org.apache.cocoon.util.location.LocationUtils;
55
56 import org.apache.commons.lang.SystemUtils;
57 import org.apache.excalibur.instrument.InstrumentManageable;
58 import org.apache.excalibur.instrument.InstrumentManager;
59 import org.apache.excalibur.source.Source;
60 import org.apache.excalibur.source.SourceResolver;
61 import org.apache.excalibur.source.impl.URLSource;
62 import org.apache.excalibur.xml.impl.XercesParser;
63 import org.apache.excalibur.xml.sax.SAXParser;
64 import org.xml.sax.InputSource JavaDoc;
65
66 import java.io.BufferedInputStream JavaDoc;
67 import java.io.File JavaDoc;
68 import java.io.IOException JavaDoc;
69 import java.net.URL JavaDoc;
70 import java.util.Collections JavaDoc;
71 import java.util.Enumeration JavaDoc;
72 import java.util.Map JavaDoc;
73
74 /**
75  * The Cocoon Object is the main Kernel for the entire Cocoon system.
76  *
77  * @author <a HREF="mailto:pier@apache.org">Pierpaolo Fumagalli</a> (Apache Software Foundation)
78  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
79  * @author <a HREF="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
80  * @version CVS $Id: Cocoon.java 280807 2005-09-14 09:38:48Z sylvain $
81  */

82 public class Cocoon
83         extends AbstractLogEnabled
84         implements ThreadSafe,
85                    Component,
86                    Initializable,
87                    Disposable,
88                    Modifiable,
89                    Processor,
90                    Contextualizable,
91                    Composable,
92                    InstrumentManageable {
93
94     // Register the location finder for Avalon configuration objects and exceptions
95
// and keep a strong reference to it.
96
private static final LocationUtils.LocationFinder confLocFinder = new LocationUtils.LocationFinder() {
97         public Location getLocation(Object JavaDoc obj, String JavaDoc description) {
98             if (obj instanceof Configuration) {
99                 Configuration config = (Configuration)obj;
100                 String JavaDoc locString = config.getLocation();
101                 Location result = LocationUtils.parse(locString);
102                 if (LocationUtils.isKnown(result)) {
103                     // Add description
104
StringBuffer JavaDoc desc = new StringBuffer JavaDoc().append('<');
105                     // Unfortunately Configuration.getPrefix() is not public
106
try {
107                         if (config.getNamespace().startsWith("http://apache.org/cocoon/sitemap/")) {
108                             desc.append("map:");
109                         }
110                     } catch (ConfigurationException e) {
111                         // no namespace: ignore
112
}
113                     desc.append(config.getName()).append('>');
114                     return new LocationImpl(desc.toString(), result);
115                 } else {
116                     return result;
117                 }
118             }
119             
120             if (obj instanceof Exception JavaDoc) {
121                 // Many exceptions in Cocoon have a message like "blah blah at file://foo/bar.xml:12:1"
122
String JavaDoc msg = ((Exception JavaDoc)obj).getMessage();
123                 if (msg == null) return null;
124                 
125                 int pos = msg.lastIndexOf(" at ");
126                 if (pos != -1) {
127                     return LocationUtils.parse(msg.substring(pos + 4));
128                 } else {
129                     // Will try other finders
130
return null;
131                 }
132             }
133             
134             // Try next finders.
135
return null;
136         }
137     };
138     
139     static {
140         LocationUtils.addFinder(confLocFinder);
141     }
142     
143     static Cocoon instance;
144
145     /** The root Cocoon logger */
146     private Logger rootLogger;
147
148     /** The application context */
149     private Context context;
150
151     /** The configuration file */
152     private Source configurationFile;
153
154     /** The configuration tree */
155     private Configuration configuration;
156
157     /** The logger manager */
158     private LoggerManager loggerManager;
159
160     /** The instrument manager */
161     private InstrumentManager instrumentManager;
162
163     /** The classpath (null if not available) */
164     private String JavaDoc classpath;
165
166     /** The working directory (null if not available) */
167     private File JavaDoc workDir;
168
169     /** The component manager. */
170     private ExcaliburComponentManager componentManager;
171
172     /** The parent component manager. */
173     private ComponentManager parentComponentManager;
174
175     /** Flag for disposed or not */
176     private boolean disposed;
177
178     /** Active request count */
179     private volatile int activeRequestCount;
180
181     /** The Processor if it is ThreadSafe */
182     private Processor threadSafeProcessor;
183
184     /** The source resolver */
185     protected SourceResolver sourceResolver;
186
187     /** An optional Avalon Component that is called before and after processing all requests. */
188     protected RequestListener requestListener;
189
190     /**
191      * Creates a new <code>Cocoon</code> instance.
192      *
193      * @exception ConfigurationException if an error occurs
194      */

195     public Cocoon() throws ConfigurationException {
196         // Set the system properties needed by Xalan2.
197
setSystemProperties();
198
199         // HACK: Provide a way to share an instance of Cocoon object between
200
// several servlets/portlets.
201
Cocoon.instance = this;
202     }
203
204     public void enableLogging(Logger logger) {
205         this.rootLogger = logger;
206         super.enableLogging(logger.getChildLogger("cocoon"));
207     }
208
209     /**
210      * Get the parent component manager. For purposes of
211      * avoiding extra method calls, the manager parameter may be null.
212      *
213      * @param manager the parent component manager. May be <code>null</code>
214      */

215     public void compose(ComponentManager manager)
216     throws ComponentException {
217         this.parentComponentManager = manager;
218     }
219
220     /**
221      * Describe <code>contextualize</code> method here.
222      *
223      * @param context a <code>Context</code> value
224      * @exception ContextException if an error occurs
225      */

226     public void contextualize(Context context) throws ContextException {
227         if (this.context == null) {
228             this.context = new ComponentContext(context);
229             ((DefaultContext) this.context).makeReadOnly();
230
231             this.classpath = (String JavaDoc)context.get(Constants.CONTEXT_CLASSPATH);
232             this.workDir = (File JavaDoc)context.get(Constants.CONTEXT_WORK_DIR);
233             try {
234                 // FIXME: add a configuration option for the refresh delay.
235
// for now, hard-coded to 1 second.
236
URLSource urlSource = new URLSource();
237                 urlSource.init((URL JavaDoc) context.get(Constants.CONTEXT_CONFIG_URL), null);
238                 this.configurationFile = new DelayedRefreshSourceWrapper(urlSource,
239                                                                          1000L);
240
241             } catch (IOException JavaDoc e) {
242                 throw new ContextException("Could not open configuration file.", e);
243             } catch (Exception JavaDoc e) {
244                 throw new ContextException("contextualize(..) Exception", e);
245             }
246         }
247     }
248
249     /**
250      * The <code>setLoggerManager</code> method will get a <code>LoggerManager</code>
251      * for further use.
252      *
253      * @param loggerManager a <code>LoggerManager</code> value
254      */

255     public void setLoggerManager(LoggerManager loggerManager) {
256         this.loggerManager = loggerManager;
257         Deprecation.setLogger(this.loggerManager.getLoggerForCategory("deprecation"));
258     }
259
260     /**
261      * Set the <code>InstrumentManager</code> for this Cocoon instance.
262      *
263      * @param manager an <code>InstrumentManager</code> instance
264      */

265     public void setInstrumentManager(final InstrumentManager manager) {
266         this.instrumentManager = manager;
267     }
268
269     /**
270      * The <code>initialize</code> method
271      *
272      * @exception Exception if an error occurs
273      */

274     public void initialize() throws Exception JavaDoc {
275         if (parentComponentManager != null) {
276             this.componentManager = new CocoonComponentManager(parentComponentManager,
277                                                                (ClassLoader JavaDoc) this.context.get(Constants.CONTEXT_CLASS_LOADER));
278         } else {
279             this.componentManager = new CocoonComponentManager((ClassLoader JavaDoc) this.context.get(Constants.CONTEXT_CLASS_LOADER));
280         }
281         ContainerUtil.enableLogging(this.componentManager, this.rootLogger.getChildLogger("manager"));
282         ContainerUtil.contextualize(this.componentManager, this.context);
283         this.componentManager.setInstrumentManager(this.instrumentManager);
284         getLogger().debug("New Cocoon object.");
285
286         // Log the System Properties.
287
dumpSystemProperties();
288
289         // Setup the default parser, for parsing configuration.
290
// If one need to use a different parser, set the given system property
291
// first check for deprecated property to be compatible:
292
String JavaDoc parser = getSystemProperty(Constants.DEPRECATED_PARSER_PROPERTY, Constants.DEFAULT_PARSER);
293         if (!Constants.DEFAULT_PARSER.equals(parser)) {
294             getLogger().warn("Deprecated property " +
295                              Constants.DEPRECATED_PARSER_PROPERTY + " is used. Please use " +
296                              Constants.PARSER_PROPERTY + " instead.");
297             if ("org.apache.cocoon.components.parser.XercesParser".equals(parser)) {
298                 parser = XercesParser.class.getName();
299             } else {
300                 getLogger().warn("Unknown value for deprecated property: " +
301                                  Constants.DEPRECATED_PARSER_PROPERTY + ", value: " + parser +
302                                  ". If you experience problems during startup, check the parser configuration section of the documentation.");
303             }
304         } else {
305             parser = getSystemProperty(Constants.PARSER_PROPERTY, Constants.DEFAULT_PARSER);
306         }
307         if (getLogger().isDebugEnabled()) {
308             getLogger().debug("Parser: " + parser);
309             getLogger().debug("Classpath: " + classpath);
310             getLogger().debug("Work directory: " + workDir.getCanonicalPath());
311         }
312
313         ExcaliburComponentManager startupManager = new ExcaliburComponentManager((ClassLoader JavaDoc) this.context.get(Constants.CONTEXT_CLASS_LOADER));
314         ContainerUtil.enableLogging(startupManager, this.rootLogger.getChildLogger("startup"));
315         ContainerUtil.contextualize(startupManager, this.context);
316         startupManager.setLoggerManager(this.loggerManager);
317
318         try {
319             startupManager.addComponent(SAXParser.ROLE,
320                                         ClassUtils.loadClass(parser),
321                                         new DefaultConfiguration("", "empty"));
322         } catch (Exception JavaDoc e) {
323             throw new ConfigurationException("Could not load parser " + parser, e);
324         }
325
326         ContainerUtil.initialize(startupManager);
327         configure(startupManager);
328         ContainerUtil.dispose(startupManager);
329         startupManager = null;
330
331         // add the logger manager to the component locator
332
final ComponentProxyGenerator proxyGenerator = new ComponentProxyGenerator();
333         final Component loggerManagerProxy = proxyGenerator.getProxy(LoggerManager.class.getName(),loggerManager);
334         componentManager.addComponentInstance(LoggerManager.ROLE,loggerManagerProxy);
335
336         ContainerUtil.initialize(this.componentManager);
337
338         // Get the Processor and keep it if it's ThreadSafe
339
Processor processor = (Processor)this.componentManager.lookup(Processor.ROLE);
340         if (processor instanceof ThreadSafe) {
341             if (getLogger().isDebugEnabled()) {
342                 getLogger().debug("Processor of class " + processor.getClass().getName() +
343                                   " is ThreadSafe");
344             }
345             this.threadSafeProcessor = processor;
346         } else {
347             if (getLogger().isDebugEnabled()) {
348                 getLogger().debug("Processor of class " + processor.getClass().getName() +
349                                   " is NOT ThreadSafe -- will be looked up at each request");
350             }
351             this.componentManager.release(processor);
352         }
353
354         this.sourceResolver = (SourceResolver)this.componentManager.lookup(SourceResolver.ROLE);
355
356         if (this.componentManager.hasComponent(RequestListener.ROLE)){
357             this.requestListener = (RequestListener) this.componentManager.lookup(RequestListener.ROLE);
358         }
359     }
360
361     /** Dump System Properties */
362     private void dumpSystemProperties() {
363         if (getLogger().isDebugEnabled()) {
364             try {
365                 Enumeration JavaDoc e = System.getProperties().propertyNames();
366                 getLogger().debug("===== System Properties Start =====");
367                 for (; e.hasMoreElements();) {
368                     String JavaDoc key = (String JavaDoc) e.nextElement();
369                     getLogger().debug(key + "=" + System.getProperty(key));
370                 }
371                 getLogger().debug("===== System Properties End =====");
372             } catch (SecurityException JavaDoc se) {
373                 // Ignore Exceptions.
374
}
375         }
376     }
377
378     /**
379      * Configure this <code>Cocoon</code> instance.
380      *
381      * @param startupManager an <code>ExcaliburComponentManager</code> value
382      * @exception ConfigurationException if an error occurs
383      * @exception ContextException if an error occurs
384      */

385     public void configure(ExcaliburComponentManager startupManager) throws ConfigurationException, ContextException {
386         SAXParser p = null;
387
388         Configuration roles = null;
389         try {
390             p = (SAXParser) startupManager.lookup(SAXParser.ROLE);
391             SAXConfigurationHandler b = new SAXConfigurationHandler();
392             URL JavaDoc url = ClassUtils.getResource("org/apache/cocoon/cocoon.roles");
393             InputSource JavaDoc is = new InputSource JavaDoc(url.openStream());
394             is.setSystemId(url.toString());
395             p.parse(is, b);
396             roles = b.getConfiguration();
397         } catch (Exception JavaDoc e) {
398             throw new ConfigurationException("Error trying to load configurations", e);
399         } finally {
400             if (p != null) startupManager.release((Component) p);
401         }
402
403         DefaultRoleManager drm = new DefaultRoleManager();
404         ContainerUtil.enableLogging(drm, this.rootLogger.getChildLogger("roles"));
405         ContainerUtil.configure(drm, roles);
406         roles = null;
407
408         try {
409             this.configurationFile.refresh();
410             p = (SAXParser)startupManager.lookup(SAXParser.ROLE);
411             SAXConfigurationHandler b = new SAXConfigurationHandler();
412             InputSource JavaDoc is = SourceUtil.getInputSource(this.configurationFile);
413             p.parse(is, b);
414             this.configuration = b.getConfiguration();
415         } catch (Exception JavaDoc e) {
416             throw new ConfigurationException("Error trying to load configurations",e);
417         } finally {
418             if (p != null) startupManager.release((Component)p);
419         }
420
421         Configuration conf = this.configuration;
422         if (getLogger().isDebugEnabled()) {
423             getLogger().debug("Root configuration: " + conf.getName());
424         }
425         if (!"cocoon".equals(conf.getName())) {
426             throw new ConfigurationException("Invalid configuration file\n" + conf.toString());
427         }
428         if (getLogger().isDebugEnabled()) {
429             getLogger().debug("Configuration version: " + conf.getAttribute("version"));
430         }
431         if (!Constants.CONF_VERSION.equals(conf.getAttribute("version"))) {
432             throw new ConfigurationException("Invalid configuration schema version. Must be '" + Constants.CONF_VERSION + "'.");
433         }
434
435         String JavaDoc userRoles = conf.getAttribute("user-roles", "");
436         if (!"".equals(userRoles)) {
437             try {
438                 p = (SAXParser)startupManager.lookup(SAXParser.ROLE);
439                 SAXConfigurationHandler b = new SAXConfigurationHandler();
440                 org.apache.cocoon.environment.Context context =
441                     (org.apache.cocoon.environment.Context) this.context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
442                 URL JavaDoc url = context.getResource(userRoles);
443                 if (url == null) {
444                     throw new ConfigurationException("User-roles configuration '"+userRoles+"' cannot be found.");
445                 }
446                 InputSource JavaDoc is = new InputSource JavaDoc(new BufferedInputStream JavaDoc(url.openStream()));
447                 is.setSystemId(url.toString());
448                 p.parse(is, b);
449                 roles = b.getConfiguration();
450             } catch (Exception JavaDoc e) {
451                 throw new ConfigurationException("Error trying to load user-roles configuration", e);
452             } finally {
453                 startupManager.release((Component)p);
454             }
455
456             DefaultRoleManager urm = new DefaultRoleManager(drm);
457             ContainerUtil.enableLogging(urm, this.rootLogger.getChildLogger("roles").getChildLogger("user"));
458             ContainerUtil.configure(urm, roles);
459             roles = null;
460             drm = urm;
461         }
462
463         this.componentManager.setRoleManager(drm);
464         this.componentManager.setLoggerManager(this.loggerManager);
465
466         getLogger().debug("Setting up components...");
467         ContainerUtil.configure(this.componentManager, conf);
468     }
469
470     /**
471      * Queries the class to estimate its ergodic period termination.
472      *
473      * @param date a <code>long</code> value
474      * @return a <code>boolean</code> value
475      */

476     public boolean modifiedSince(long date) {
477         return date < this.configurationFile.getLastModified();
478     }
479
480     /**
481      * Helper method to retrieve system property.
482      * Returns default value if SecurityException is caught.
483      */

484     public static String JavaDoc getSystemProperty(String JavaDoc property, String JavaDoc value) {
485         try {
486             return System.getProperty(property, value);
487         } catch (SecurityException JavaDoc e) {
488             System.err.println("Caught a SecurityException reading the system property '" + property + "';" +
489                                " Cocoon will default to '" + value + "' value.");
490             return value;
491         }
492     }
493
494     /**
495      * Sets required system properties.
496      */

497     protected void setSystemProperties() {
498         try {
499             // FIXME We shouldn't have to specify the SAXParser...
500
// This is needed by Xalan2, it is used by org.xml.sax.helpers.XMLReaderFactory
501
// to locate the SAX2 driver.
502
if (getSystemProperty("org.xml.sax.driver", null) == null) {
503                 System.setProperty("org.xml.sax.driver", "org.apache.xerces.parsers.SAXParser");
504             }
505         } catch (SecurityException JavaDoc e) {
506             // Ignore security exceptions
507
System.out.println("Caught a SecurityException writing the system property: " + e);
508         }
509
510         try {
511             // FIXME We shouldn't have to specify these. Needed to override jaxp implementation of weblogic.
512
if (getSystemProperty("javax.xml.parsers.DocumentBuilderFactory", "").startsWith("weblogic")) {
513                 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
514                 System.setProperty("javax.xml.parsers.SAXParserFactory","org.apache.xerces.jaxp.SAXParserFactoryImpl");
515             }
516         } catch (SecurityException JavaDoc e) {
517             // Ignore security exceptions
518
System.out.println("Caught a SecurityException writing the system property: " + e);
519         }
520     }
521
522     /**
523      * Dispose this instance
524      */

525     public void dispose() {
526         if (this.componentManager != null) {
527             if (this.requestListener != null) {
528                 this.componentManager.release(this.requestListener);
529             }
530             this.componentManager.release(this.threadSafeProcessor);
531             this.threadSafeProcessor = null;
532
533             this.componentManager.release(this.sourceResolver);
534             this.sourceResolver = null;
535
536             ContainerUtil.dispose(this.componentManager);
537             this.componentManager = null;
538         }
539
540         this.context = null;
541         if (Cocoon.instance == this) {
542             Cocoon.instance = null;
543         }
544         this.disposed = true;
545     }
546
547     /**
548      * Log debug information about the current environment.
549      *
550      * @param environment an <code>Environment</code> value
551      */

552     protected void debug(Environment environment, boolean internal) {
553         String JavaDoc lineSeparator = SystemUtils.LINE_SEPARATOR;
554         Map JavaDoc objectModel = environment.getObjectModel();
555         Request request = ObjectModelHelper.getRequest(objectModel);
556         Session session = request.getSession(false);
557         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
558         msg.append("DEBUGGING INFORMATION:").append(lineSeparator);
559         if (internal) {
560             msg.append("INTERNAL ");
561         }
562         msg.append("REQUEST: ").append(request.getRequestURI()).append(lineSeparator).append(lineSeparator);
563         msg.append("CONTEXT PATH: ").append(request.getContextPath()).append(lineSeparator);
564         msg.append("SERVLET PATH: ").append(request.getServletPath()).append(lineSeparator);
565         msg.append("PATH INFO: ").append(request.getPathInfo()).append(lineSeparator).append(lineSeparator);
566
567         msg.append("REMOTE HOST: ").append(request.getRemoteHost()).append(lineSeparator);
568         msg.append("REMOTE ADDRESS: ").append(request.getRemoteAddr()).append(lineSeparator);
569         msg.append("REMOTE USER: ").append(request.getRemoteUser()).append(lineSeparator);
570         msg.append("REQUEST SESSION ID: ").append(request.getRequestedSessionId()).append(lineSeparator);
571         msg.append("REQUEST PREFERRED LOCALE: ").append(request.getLocale().toString()).append(lineSeparator);
572         msg.append("SERVER HOST: ").append(request.getServerName()).append(lineSeparator);
573         msg.append("SERVER PORT: ").append(request.getServerPort()).append(lineSeparator).append(lineSeparator);
574
575         msg.append("METHOD: ").append(request.getMethod()).append(lineSeparator);
576         msg.append("CONTENT LENGTH: ").append(request.getContentLength()).append(lineSeparator);
577         msg.append("PROTOCOL: ").append(request.getProtocol()).append(lineSeparator);
578         msg.append("SCHEME: ").append(request.getScheme()).append(lineSeparator);
579         msg.append("AUTH TYPE: ").append(request.getAuthType()).append(lineSeparator).append(lineSeparator);
580         msg.append("CURRENT ACTIVE REQUESTS: ").append(activeRequestCount).append(lineSeparator);
581
582         // log all of the request parameters
583
Enumeration JavaDoc e = request.getParameterNames();
584
585         msg.append("REQUEST PARAMETERS:").append(lineSeparator).append(lineSeparator);
586
587         while (e.hasMoreElements()) {
588             String JavaDoc p = (String JavaDoc) e.nextElement();
589
590             msg.append("PARAM: '").append(p).append("' ")
591                .append("VALUES: '");
592             String JavaDoc[] params = request.getParameterValues(p);
593             for (int i = 0; i < params.length; i++) {
594                 msg.append("[" + params[i] + "]");
595                 if (i != (params.length - 1)) {
596                     msg.append(", ");
597                 }
598             }
599
600             msg.append("'").append(lineSeparator);
601         }
602
603         // log all of the header parameters
604
Enumeration JavaDoc e2 = request.getHeaderNames();
605
606         msg.append("HEADER PARAMETERS:").append(lineSeparator).append(lineSeparator);
607
608         while (e2.hasMoreElements()) {
609             String JavaDoc p = (String JavaDoc) e2.nextElement();
610
611             msg.append("PARAM: '").append(p).append("' ")
612                .append("VALUES: '");
613             Enumeration JavaDoc e3 = request.getHeaders(p);
614             while (e3.hasMoreElements()) {
615                 msg.append("[" + e3.nextElement() + "]");
616                 if (e3.hasMoreElements()) {
617                     msg.append(", ");
618                 }
619             }
620
621             msg.append("'").append(lineSeparator);
622         }
623
624         msg.append(lineSeparator).append("SESSION ATTRIBUTES:").append(lineSeparator).append(lineSeparator);
625
626         // log all of the session attributes
627
if (session != null) {
628             // Fix bug #12139: Session can be modified while still
629
// being enumerated here
630
synchronized (session) {
631                 e = session.getAttributeNames();
632                 while (e.hasMoreElements()) {
633                     String JavaDoc p = (String JavaDoc) e.nextElement();
634                     msg.append("PARAM: '").append(p).append("' ")
635                        .append("VALUE: '").append(session.getAttribute(p)).append("'")
636                        .append(lineSeparator);
637                 }
638             }
639         }
640
641         getLogger().debug(msg.toString());
642     }
643
644     /**
645      * Process the given <code>Environment</code> to produce the output.
646      *
647      * @param environment an <code>Environment</code> value
648      * @return a <code>boolean</code> value
649      * @exception Exception if an error occurs
650      */

651     public boolean process(Environment environment)
652     throws Exception JavaDoc {
653         if (this.disposed) {
654             throw new IllegalStateException JavaDoc("You cannot process a Disposed Cocoon engine.");
655         }
656
657         Object JavaDoc key = CocoonComponentManager.startProcessing(environment);
658         final int environmentDepth = CocoonComponentManager.markEnvironment();
659         CocoonComponentManager.enterEnvironment(environment,
660                                                 this.componentManager,
661                                                 this);
662         try {
663             boolean result;
664             if (getLogger().isDebugEnabled()) {
665                 ++activeRequestCount;
666                 debug(environment, false);
667             }
668
669
670             if (this.requestListener != null) {
671                 try {
672                     requestListener.onRequestStart(environment);
673                 } catch (Exception JavaDoc e) {
674                     getLogger().error("Error encountered monitoring request start: " + e.getMessage());
675                 }
676             }
677
678             if (this.threadSafeProcessor != null) {
679                 result = this.threadSafeProcessor.process(environment);
680                 if (this.requestListener != null) {
681                     try {
682                         requestListener.onRequestEnd(environment);
683                     } catch (Exception JavaDoc e) {
684                         getLogger().error("Error encountered monitoring request start: " + e.getMessage());
685                     }
686                 }
687             } else {
688                 Processor processor = (Processor)this.componentManager.lookup(Processor.ROLE);
689                 try {
690                     result = processor.process(environment);
691                     if (this.requestListener != null) {
692                         try {
693                             requestListener.onRequestEnd(environment);
694                         } catch (Exception JavaDoc e) {
695                             getLogger().error("Error encountered monitoring request start: " + e.getMessage());
696                         }
697                     }
698                 } finally {
699                     this.componentManager.release(processor);
700                 }
701             }
702             // commit response on success
703
environment.commitResponse();
704
705             return result;
706         } catch (Exception JavaDoc any) {
707             if (this.requestListener != null) {
708                 try {
709                     requestListener.onRequestException(environment, any);
710                 } catch (Exception JavaDoc e) {
711                     getLogger().error("Error encountered monitoring request start: " + e.getMessage());
712                 }
713             }
714             // reset response on error
715
environment.tryResetResponse();
716             throw any;
717         } finally {
718             CocoonComponentManager.leaveEnvironment();
719             CocoonComponentManager.endProcessing(environment, key);
720             if (getLogger().isDebugEnabled()) {
721                 --activeRequestCount;
722             }
723
724             // TODO (CZ): This is only for testing - remove it later on
725
CocoonComponentManager.checkEnvironment(environmentDepth, getLogger());
726         }
727     }
728
729     /**
730      * Process the given <code>Environment</code> to assemble
731      * a <code>ProcessingPipeline</code>.
732      * @since 2.1
733      */

734     public ProcessingPipeline buildPipeline(Environment environment)
735     throws Exception JavaDoc {
736         if (disposed) {
737             throw new IllegalStateException JavaDoc("You cannot process a Disposed Cocoon engine.");
738         }
739
740         try {
741             if (getLogger().isDebugEnabled()) {
742                 ++activeRequestCount;
743                 debug(environment, true);
744             }
745
746             if (this.threadSafeProcessor != null) {
747                 return this.threadSafeProcessor.buildPipeline(environment);
748             } else {
749                 Processor processor = (Processor)this.componentManager.lookup(Processor.ROLE);
750                 try {
751                     return processor.buildPipeline(environment);
752                 } finally {
753                     this.componentManager.release(processor);
754                 }
755             }
756
757         } finally {
758             if (getLogger().isDebugEnabled()) {
759                 --activeRequestCount;
760             }
761         }
762     }
763
764     /**
765      * Get the sitemap component configurations
766      * @since 2.1
767      */

768     public Map JavaDoc getComponentConfigurations() {
769         return Collections.EMPTY_MAP;
770     }
771
772     /**
773      * Return this (Cocoon is always at the root of the processing chain).
774      * @since 2.1.1
775      */

776     public Processor getRootProcessor() {
777         return this;
778     }
779
780     /**
781      * Accessor for active request count
782      */

783     public int getActiveRequestCount() {
784         return activeRequestCount;
785     }
786
787     public ExcaliburComponentManager getComponentManager() {
788         return this.componentManager;
789     }
790 }
791
Popular Tags