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/04/01 16:58:58 UTC

cvs commit: cocoon-2.2/src/kernel/org/apache/cocoon/kernel/startup Servlet.java ServletLogger.java

pier        2004/04/01 06:58:58

  Modified:    src/kernel/org/apache/cocoon/kernel KernelDeployer.java
               src/kernel/org/apache/cocoon/kernel/identification
                        DescriptorBuilder.java
  Added:       src/kernel/org/apache/cocoon/kernel/startup Servlet.java
                        ServletLogger.java
  Log:
  Add a couple of (very simple) classes for startup in Servlet environments
  and make sure that descriptors are resolved relative to the configuration
  file in which they're declared.
  
  Revision  Changes    Path
  1.6       +17 -2     cocoon-2.2/src/kernel/org/apache/cocoon/kernel/KernelDeployer.java
  
  Index: KernelDeployer.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/kernel/org/apache/cocoon/kernel/KernelDeployer.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- KernelDeployer.java	31 Mar 2004 13:14:38 -0000	1.5
  +++ KernelDeployer.java	1 Apr 2004 14:58:58 -0000	1.6
  @@ -15,6 +15,9 @@
    */
   package org.apache.cocoon.kernel;
   
  +import java.net.URL;
  +import java.net.MalformedURLException;
  +
   import java.util.HashMap;
   import java.util.HashSet;
   import java.util.Iterator;
  @@ -113,7 +116,19 @@
               String location = current.getStringAttribute("descriptor");
               
               /* Parse the block descriptor and get the configuration */
  -            Descriptor descriptor = DescriptorBuilder.newInstance(location);
  +            URL url = null;
  +            try {
  +                url = new URL(configuration.locationURL(), location);
  +            } catch (MalformedURLException exception) {
  +                throw new ConfigurationException("Unable to relativize descript"
  +                                                 + "or location \"" + location
  +                                                 + "\" against configuration \""
  +                                                 + configuration.location()
  +                                                 + "\"");
  +            }
  +
  +            /* Create a new descriptor instance */
  +            Descriptor descriptor = DescriptorBuilder.newInstance(url);
               if (this.loader.contains(descriptor)) {
                   throw new ConfigurationException("Descriptor \"" + descriptor
                                            + "\" configured twice", current);
  
  
  
  1.4       +25 -2     cocoon-2.2/src/kernel/org/apache/cocoon/kernel/identification/DescriptorBuilder.java
  
  Index: DescriptorBuilder.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/kernel/org/apache/cocoon/kernel/identification/DescriptorBuilder.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DescriptorBuilder.java	31 Mar 2004 13:01:39 -0000	1.3
  +++ DescriptorBuilder.java	1 Apr 2004 14:58:58 -0000	1.4
  @@ -16,6 +16,7 @@
   package org.apache.cocoon.kernel.identification;
   
   import java.io.IOException;
  +import java.net.URL;
   import org.apache.cocoon.kernel.configuration.Configuration;
   import org.apache.cocoon.kernel.configuration.ConfigurationBuilder;
   import org.apache.cocoon.kernel.configuration.ConfigurationException;
  @@ -39,11 +40,33 @@
           super();
       }
   
  +    
  +    /**
  +     * Create a new {@link Descriptor} instance given a {@link URL} locating
  +     * a descriptor to parse.</p>
  +     *
  +     * @param location the location of the descriptor.
  +     * @throws ConfigurationException if the specified {@link Configuration}
  +     *                                did not represent a valid descriptor.
  +     * @throws IdentificationException if the specified {@link Configuration}
  +     *                                 specified an invalid block identifier.
  +     * @throws NullPointerException if the {@link Descriptor} was <b>null</b>.
  +     */
  +    public static Descriptor newInstance(URL location)
  +    throws ConfigurationException, IdentificationException {
  +        try {
  +            return newInstance(ConfigurationBuilder.parse(location));
  +        } catch (IOException exception) {
  +            throw new ConfigurationException("Unable to parse descriptor \""
  +                                             + location + "\"", exception);
  +        }
  +    }
  +    
       /**
        * Create a new {@link Descriptor} instance given a {@link String} locating
        * a descriptor to parse.</p>
        *
  -     * @param location the location of the descriptor file.
  +     * @param location the location of the descriptor.
        * @throws ConfigurationException if the specified {@link Configuration}
        *                                did not represent a valid descriptor.
        * @throws IdentificationException if the specified {@link Configuration}
  
  
  
  1.1                  cocoon-2.2/src/kernel/org/apache/cocoon/kernel/startup/Servlet.java
  
  Index: Servlet.java
  ===================================================================
  /*
   * Copyright 1999-2004 The Apache Software Foundation.
   * 
   * 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 javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  
  import org.apache.cocoon.kernel.KernelDeployer;
  import org.apache.cocoon.kernel.Installer;
  import org.apache.cocoon.kernel.configuration.Configuration;
  import org.apache.cocoon.kernel.configuration.ConfigurationBuilder;
  
  /**
   *
   * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>
   * @version 1.0 (CVS $Revision: 1.1 $)
   */
  public class Servlet extends HttpServlet {
  
      private Logger logger = null;
  
      public void init()
      throws ServletException {
  
          /* Create a logger */
          ServletContext ctxt = this.getServletContext();
          String level = this.getInitParameter("log-level");
          String temp = this.getInitParameter("log-trace");
          boolean trace = ("true".equalsIgnoreCase(temp) ? true : false);
          if ("fatal".equalsIgnoreCase(level)) {
              this.logger = new ServletLogger(ServletLogger.FATAL, trace, ctxt);
          } else if ("error".equalsIgnoreCase(level)) {
              this.logger = new ServletLogger(ServletLogger.ERROR, trace, ctxt);
          } else if ("warn".equalsIgnoreCase(level)) {
              this.logger = new ServletLogger(ServletLogger.WARN,  trace, ctxt);
          } else if ("info".equalsIgnoreCase(level)) {
              this.logger = new ServletLogger(ServletLogger.INFO,  trace, ctxt);
          } else if ("debug".equalsIgnoreCase(level)) {
              this.logger = new ServletLogger(ServletLogger.DEBUG, trace, ctxt);
          } else {
              this.logger = new ServletLogger(ServletLogger.INFO,  trace, ctxt);
          }
  
          /* Find our configurations */
          String deplconf  = this.getInitParameter("deployer-config");
          String instconf = this.getInitParameter("installer-config");
          if (deplconf == null) {
              String message = "Parameter \"deployer-config\" not specified";
              logger.fatal(message);
              throw new ServletException(message);
          } else if (instconf == null) {
              String message = "Parameter \"installer-config\" not specified";
              logger.fatal(message);
              throw new ServletException(message);
          }
          
          /* Let's start up */
          this.logger.info("Kernel startup");
          try {
              Configuration conf = null;
  
              /* Now let's create our core deployer */
              KernelDeployer deployer = new KernelDeployer();
              deployer.logger(logger);
              conf = ConfigurationBuilder.parse(ctxt.getResource(deplconf));
              deployer.configure(conf);
              
              /* Instantiate an installer and process deployment */
              Installer installer = new Installer(deployer);
              conf = ConfigurationBuilder.parse(ctxt.getResource(instconf));
              installer.process(conf);
              
          } catch (Throwable throwable) {
              String message = "An error occurred initializing the kernel";
              logger.fatal(message, throwable);
              //throw new ServletException(message, throwable);
          }
      }
  
      public void destroy() {
          this.logger.info("Kernel shutdown");
      }
  }
  
  
  
  1.1                  cocoon-2.2/src/kernel/org/apache/cocoon/kernel/startup/ServletLogger.java
  
  Index: ServletLogger.java
  ===================================================================
  /*
   * Copyright 1999-2004 The Apache Software Foundation.
   * 
   * 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 javax.servlet.ServletContext;
  
  /**
   * <p>The {@link ServletLogger} is a simple {@link Logger} implementation
   * logging individual lines to a {@link ServletContext}.</p>
   *
   * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>
   * @version 1.0 (CVS $Revision: 1.1 $)
   */
  public class ServletLogger extends AbstractLogger {
  
      /** <p>Our {@link ServletContext} instance.</p> */
      private ServletContext context = null;
      
      /**
       * <p>Create a new {@link ServletLogger} associated with a specific
       * {@link ServletContext}.</p>
       *
       * @param context the {@link ServletContext} to log to.
       */
      public ServletLogger(ServletContext context) {
          this(DEBUG, true, context);
      }
      
      /**
       * <p>Create a new {@link ServletLogger} associated with a specific
       * {@link ServletContext}.</p>
       *
       * @param level the level of output.
       * @param trace if <b>true</b> exception stack traces will be produced.
       * @param context the {@link ServletContext} to log to.
       */
      public ServletLogger(int level, boolean trace, ServletContext context) {
          super(null, level, false, trace);
  
          if (context == null) throw new NullPointerException("Null context");
          this.context = context;
      }
      
      /* ====================================================================== */
  
      /**
       * <p>Write a line to the output.</p>
       *
       * @param line the line to write.
       */
      public void output(String line) {
          this.context.log(line);
      }
  }