You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by pi...@apache.org on 2004/11/01 14:34:06 UTC
svn commit: rev 56231 - cocoon/whiteboard/kernel/src/org/apache/cocoon/kernel/startup
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 {@link ServletLoader} is a {@link ServletContextListener} managing the
+ * initialization and destruction of a {@link Kernel} instance.</p>
+ *
+ * <p>{@link Servlet} instances can have access to the {@link Kernel} instance by
+ * looking for the <code>"org.apache.cocoon.kernel.Kernel"</code>
+ * {@link #ATTRIBUTE attribute} in their {@link 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:pier@apache.org">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 {@link ServletContext} attribute name of the {@link Kernel} instance
+ * associated with this web application.</p>
+ */
+ public static final String ATTRIBUTE = "org.apache.cocoon.kernel.Kernel";
+
+ /** <p>The current {@link Deployer} instance.</p> */
+ Deployer deployer = null;
+
+ /**
+ * <p>Create a new {@link 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 {@link ServletWrapper} provides a simple and <b>non-secure</b> wrapping
+ * of a {@link Servlet} deployed as a {@link Kernel} block.</p>
+ *
+ * @author <a href="mailto:pier@apache.org">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 {@link Servlet} instance.</p> */
+ private Servlet instance = null;
+
+ /**
+ * <p>Create a new {@link ServletWrapper} instance.</p>
+ */
+ public ServletWrapper() {
+ super();
+ }
+
+ /**
+ * <p>Initialize the servlet wrapped by this {@link 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 {@link 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 {@link 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 {@link ServletConfig} instance of the servlet wrapped by this
+ * {@link ServletWrapper}.</p>
+ */
+ public ServletConfig getServletConfig() {
+ if (this.instance == null) throw new IllegalStateException("Uninitialized");
+ return(this.instance.getServletConfig());
+ }
+
+ /**
+ * <p>Return the information detail {@link String} of the servlet wrapped by
+ * this {@link ServletWrapper}.</p>
+ */
+ public String getServletInfo() {
+ if (this.instance == null) throw new IllegalStateException("Uninitialized");
+ return(this.instance.getServletInfo());
+ }
+}