Author: pier
Date: Mon Nov 1 05:34:06 2004
New Revision: 56231
Added:
cocoon/whiteboard/kernel/src/org/apache/cocoon/kernel/startup/ServletLoader.java
cocoon/whiteboard/kernel/src/org/apache/cocoon/kernel/startup/ServletWrapper.java
Log:
Servlet-based initialization and wrapping
Added:
cocoon/whiteboard/kernel/src/org/apache/cocoon/kernel/startup/ServletLoader.java
==============================================================================
--- (empty file)
+++
cocoon/whiteboard/kernel/src/org/apache/cocoon/kernel/startup/ServletLoader.java
Mon Nov 1 05:34:06 2004
@@ -0,0 +1,128 @@
+/*
===============================================================================
*
+ * Copyright (C) 1999-2004, The Apache Software Foundation. All rights
reserved. *
+ *
*
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not
use *
+ * this file except in compliance with the License. You may obtain a copy of
the *
+ * License at <http://www.apache.org/licenses/LICENSE-2.0>.
*
+ *
*
+ * Unless required by applicable law or agreed to in writing, software
distributed *
+ * under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR *
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for
the *
+ * specific language governing permissions and limitations under the License.
*
+ *
===============================================================================
*/
+package org.apache.cocoon.kernel.startup;
+
+import java.lang.reflect.Proxy;
+import java.net.URL;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import org.apache.cocoon.kernel.Kernel;
+import org.apache.cocoon.kernel.configuration.Configuration;
+import org.apache.cocoon.kernel.configuration.ConfigurationBuilder;
+import org.apache.cocoon.kernel.runtime.Deployer;
+import org.apache.cocoon.kernel.runtime.Wiring;
+
+/**
+ * <p>The [EMAIL PROTECTED] ServletLoader} is a [EMAIL PROTECTED]
ServletContextListener} managing the
+ * initialization and destruction of a [EMAIL PROTECTED] Kernel} instance.</p>
+ *
+ * <p>[EMAIL PROTECTED] Servlet} instances can have access to the [EMAIL
PROTECTED] Kernel} instance by
+ * looking for the <code>"org.apache.cocoon.kernel.Kernel"</code>
+ * [EMAIL PROTECTED] #ATTRIBUTE attribute} in their [EMAIL PROTECTED]
ServletContext}.</p>
+ *
+ * <p>The initialization of the kernel is dependant on three context
initialization
+ * parameters:</p>
+ *
+ * <ul>
+ * <li>
+ * <code>kernel-configuration</code>: specifies a compound descriptor
location
+ * document and instances deployer document.
+ * </li>
+ * </ul>
+ *
+ * <p>or:</p>
+ *
+ * <ul>
+ * <li><code>kernel-descriptors</code>: a descriptor location document.</li>
+ * <li><code>kernel-instances</code>: an instances deployer document.</li>
+ * </ul>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
+ * @author Copyright © 2000-2004 <a href="http://www.apache.org/">The
Apache
+ * Software Foundation</a>. All rights reserved.
+ */
+public class ServletLoader implements ServletContextListener {
+
+ /**
+ * <p>The [EMAIL PROTECTED] ServletContext} attribute name of the [EMAIL
PROTECTED] Kernel} instance
+ * associated with this web application.</p>
+ */
+ public static final String ATTRIBUTE = "org.apache.cocoon.kernel.Kernel";
+
+ /** <p>The current [EMAIL PROTECTED] Deployer} instance.</p> */
+ Deployer deployer = null;
+
+ /**
+ * <p>Create a new [EMAIL PROTECTED] ServletLoader} instance.</p>
+ */
+ public ServletLoader() {
+ this.deployer = new Deployer();
+ }
+
+ /**
+ * <p>Receive notification of initialization.</p>
+ */
+ public void contextInitialized(ServletContextEvent event) {
+ ServletContext context = event.getServletContext();
+
+ /* Retrieve the "descriptors" configuration */
+ String configuration =
context.getInitParameter("kernel-configuration");
+ String descriptors = context.getInitParameter("kernel-descriptors");
+ String instances = context.getInitParameter("kernel-instances");
+
+ /* Parse the (possibly two) configurations and initialize the deployer
*/
+ try {
+ if (configuration != null) {
+ URL configuration_url = context.getResource(configuration);
+ Configuration conf =
ConfigurationBuilder.parse(configuration_url);
+ this.deployer.initialize(conf);
+ } else {
+ URL descriptors_url = context.getResource(descriptors);
+ URL instances_url = context.getResource(instances);
+ Configuration desc =
ConfigurationBuilder.parse(descriptors_url);
+ Configuration inst = ConfigurationBuilder.parse(instances_url);
+ this.deployer.initialize(desc, inst);
+ }
+ } catch (Throwable t) {
+ context.log("Unable to intialize kernel", t);
+ throw new RuntimeException("Unable to initialize kernel", t);
+ }
+
+ /* Make the kernel available to the servlet loader */
+ ClassLoader loader = this.getClass().getClassLoader();
+ Class interfaces[] = new Class[] { Kernel.class };
+ Wiring wiring = new Wiring(this.deployer);
+ Kernel kernel = (Kernel) Proxy.newProxyInstance(loader, interfaces,
wiring);
+ context.setAttribute(ServletLoader.ATTRIBUTE, kernel);
+ }
+
+ /**
+ * <p>Receive notification of destruction.</p>
+ */
+ public void contextDestroyed(ServletContextEvent event) {
+ ServletContext context = event.getServletContext();
+ context.removeAttribute("KERNEL");
+ try {
+ this.deployer.destroy();
+ } catch (Throwable t) {
+ context.log("Unable to intialize destroy", t);
+ throw new RuntimeException("Unable to destroy kernel", t);
+ } finally {
+ this.deployer = null;
+ }
+ }
+}
Added:
cocoon/whiteboard/kernel/src/org/apache/cocoon/kernel/startup/ServletWrapper.java
==============================================================================
--- (empty file)
+++
cocoon/whiteboard/kernel/src/org/apache/cocoon/kernel/startup/ServletWrapper.java
Mon Nov 1 05:34:06 2004
@@ -0,0 +1,95 @@
+/*
===============================================================================
*
+ * Copyright (C) 1999-2004, The Apache Software Foundation. All rights
reserved. *
+ *
*
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not
use *
+ * this file except in compliance with the License. You may obtain a copy of
the *
+ * License at <http://www.apache.org/licenses/LICENSE-2.0>.
*
+ *
*
+ * Unless required by applicable law or agreed to in writing, software
distributed *
+ * under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR *
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for
the *
+ * specific language governing permissions and limitations under the License.
*
+ *
===============================================================================
*/
+package org.apache.cocoon.kernel.startup;
+
+import java.io.IOException;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.cocoon.kernel.Kernel;
+
+/**
+ * <p>The [EMAIL PROTECTED] ServletWrapper} provides a simple and
<b>non-secure</b> wrapping
+ * of a [EMAIL PROTECTED] Servlet} deployed as a [EMAIL PROTECTED] Kernel}
block.</p>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
+ * @author Copyright © 2000-2004 <a href="http://www.apache.org/">The
Apache
+ * Software Foundation</a>. All rights reserved.
+ */
+public class ServletWrapper implements Servlet {
+
+ /** <p>The wrapped [EMAIL PROTECTED] Servlet} instance.</p> */
+ private Servlet instance = null;
+
+ /**
+ * <p>Create a new [EMAIL PROTECTED] ServletWrapper} instance.</p>
+ */
+ public ServletWrapper() {
+ super();
+ }
+
+ /**
+ * <p>Initialize the servlet wrapped by this [EMAIL PROTECTED]
ServletWrapper}.</p>
+ */
+ public void init(ServletConfig config)
+ throws ServletException {
+ ServletContext context = config.getServletContext();
+ Kernel kernel = (Kernel) context.getAttribute(ServletLoader.ATTRIBUTE);
+ if (kernel == null) throw new ServletException("Unable to retrieve
Kernel");
+
+ String name = context.getInitParameter("block-instance");
+ if (name == null) throw new ServletException("No block instance
configured");
+
+ this.instance = (Servlet) kernel.lookup(name);
+ }
+
+ /**
+ * <p>Destroy the servlet wrapped by this [EMAIL PROTECTED]
ServletWrapper}.</p>
+ */
+ public void destroy() {
+ if (this.instance == null) throw new
IllegalStateException("Uninitialized");
+ this.instance.destroy();
+ }
+
+ /**
+ * <p>Forward a request to servlet wrapped by this [EMAIL PROTECTED]
ServletWrapper}.</p>
+ */
+ public void service(ServletRequest request, ServletResponse response)
+ throws ServletException, IOException {
+ if (this.instance == null) throw new
IllegalStateException("Uninitialized");
+ this.instance.service(request, response);
+ }
+
+ /**
+ * <p>Return the [EMAIL PROTECTED] ServletConfig} instance of the servlet
wrapped by this
+ * [EMAIL PROTECTED] ServletWrapper}.</p>
+ */
+ public ServletConfig getServletConfig() {
+ if (this.instance == null) throw new
IllegalStateException("Uninitialized");
+ return(this.instance.getServletConfig());
+ }
+
+ /**
+ * <p>Return the information detail [EMAIL PROTECTED] String} of the
servlet wrapped by
+ * this [EMAIL PROTECTED] ServletWrapper}.</p>
+ */
+ public String getServletInfo() {
+ if (this.instance == null) throw new
IllegalStateException("Uninitialized");
+ return(this.instance.getServletInfo());
+ }
+}