mcconnell    2003/12/20 17:43:47

  Added:       merlin/kernel/servlet .cvsignore maven.xml
                        project.properties project.xml
               merlin/kernel/servlet/src/java/org/apache/avalon/merlin/servlet
                        MerlinServlet.java
               merlin/kernel/servlet/src/webapp/WEB-INF web.xml
               merlin/kernel/servlet/src/webapp/css merlin.css
               merlin/kernel/servlet/src/webapp/images Paper.gif
  Log:
  Addition of an embedded kernel in a servlet.
  
  Revision  Changes    Path
  1.1                  avalon/merlin/kernel/servlet/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  target
  maven.log
  velocity.log
  .classpath
  .project
  
  
  
  1.1                  avalon/merlin/kernel/servlet/maven.xml
  
  Index: maven.xml
  ===================================================================
  <project default="war:install" xmlns:maven="jelly:maven" xmlns:j="jelly:core" 
xmlns:util="jelly:util" xmlns:ant="jelly:ant">
  
    <!--
    Add the merlin.properties file.
    -->
    <postGoal name="java:compile">
      <ant:copy file="${maven.repo.local}/${pom.groupId}/properties/merlin.properties" 
         toDir="${maven.build.dir}/classes" verbose="yes"/>
    </postGoal>
  
  </project>
  
  
  
  1.1                  avalon/merlin/kernel/servlet/project.properties
  
  Index: project.properties
  ===================================================================
  #
  # war file name
  #
  
  maven.war.final.name = merlin.war
  
  
  
  
  
  1.1                  avalon/merlin/kernel/servlet/project.xml
  
  Index: project.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <project>
  
    <extend>${basedir}/../../project.xml</extend>
  
    <groupId>merlin</groupId>
    <id>merlin-servlet</id>
    <name>Merlin Servlet</name>
    <package>org.apache.avalon.merlin.servlet</package>
  
    <currentVersion>3.2</currentVersion>
    <inceptionYear>2002</inceptionYear>
    <shortDescription>Merlin Servlet</shortDescription>
  
    <description>
  Merlin servlet is a servlet with an embedded Merlin Kernel.
    </description>
  
    <dependencies>
      <dependency>
        <groupId>avalon-meta</groupId>
        <artifactId>avalon-meta-api</artifactId>
        <version>1.3</version>
        <type>jar</type>
        <properties>
          <war.bundle>true</war.bundle>
        </properties>
      </dependency>
      <dependency>
        <groupId>avalon-framework</groupId>
        <artifactId>avalon-framework-api</artifactId>
        <version>4.1.5</version>
        <type>jar</type>
        <properties>
          <war.bundle>true</war.bundle>
        </properties>
      </dependency>
      <dependency>
        <groupId>avalon-repository</groupId>
        <artifactId>avalon-repository-api</artifactId>
        <version>1.2</version>
        <type>jar</type>
        <properties>
          <war.bundle>true</war.bundle>
        </properties>
      </dependency>
      <dependency>
        <groupId>avalon-composition</groupId>
        <artifactId>avalon-composition-api</artifactId>
        <version>1.2.1</version>
        <type>jar</type>
        <properties>
          <war.bundle>true</war.bundle>
        </properties>
      </dependency>
      <dependency>
        <groupId>avalon-activation</groupId>
        <artifactId>avalon-activation-api</artifactId>
        <version>1.2.1</version>
        <type>jar</type>
        <properties>
          <war.bundle>true</war.bundle>
        </properties>
      </dependency>
      <dependency>
        <groupId>merlin</groupId>
        <artifactId>merlin-api</artifactId>
        <version>3.2</version>
        <type>jar</type>
        <properties>
          <war.bundle>true</war.bundle>
        </properties>
      </dependency>
      <dependency>
        <groupId>avalon-repository</groupId>
        <artifactId>avalon-repository-main</artifactId>
        <version>1.2</version>
        <type>jar</type>
        <properties>
          <war.bundle>true</war.bundle>
        </properties>
      </dependency>
      <dependency>
        <id>servletapi</id>
        <version>2.3</version>
      </dependency>
    </dependencies>
    
  </project>
  
  
  
  1.1                  
avalon/merlin/kernel/servlet/src/java/org/apache/avalon/merlin/servlet/MerlinServlet.java
  
  Index: MerlinServlet.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
      must not be used to endorse or promote products derived from this  software
      without  prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation. For more  information on the
   Apache Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.avalon.merlin.servlet;
  
  import java.io.File;
  import java.io.IOException;
  import java.util.Map;
  import java.util.Hashtable;
  import java.net.URL;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.avalon.activation.appliance.Appliance;
  import org.apache.avalon.activation.appliance.Block;
  import org.apache.avalon.composition.model.ContainmentModel;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.merlin.Kernel;
  import org.apache.avalon.repository.Artifact;
  import org.apache.avalon.repository.provider.Builder;
  import org.apache.avalon.repository.provider.InitialContext;
  import org.apache.avalon.repository.provider.Factory;
  import org.apache.avalon.repository.RepositoryException;
  import org.apache.avalon.repository.main.DefaultInitialContext;
  import org.apache.avalon.repository.main.DefaultBuilder;
  import org.apache.avalon.util.exception.ExceptionHelper;
  import org.apache.avalon.util.env.Env;
  
  
  /**
   * Servlet that handles the establishment of a Merlin Kernel
   * and registration of the kernel base URL under the servlet 
   * context using the key.
   * 
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
   */
  public class MerlinServlet extends HttpServlet
  {
      //----------------------------------------------------------
      // static
      //----------------------------------------------------------
  
      private static final String MERLIN_PROPERTIES = "merlin.properties";
  
      private static final String IMPLEMENTATION_KEY = "merlin.implementation";
  
      //----------------------------------------------------------
      // state
      //----------------------------------------------------------
  
      private Block m_block;
  
      private Kernel m_kernel;
  
      //----------------------------------------------------------
      // Servlet
      //----------------------------------------------------------
  
      /**
       * Initializes Servlet by the web server container.
       *
       * @exception ServletException if an error occurs
       */
      public void init()
          throws ServletException
      {
          ClassLoader classloader = MerlinServlet.class.getClassLoader();
  
          try
          {
              String path = getServletContext().getRealPath( "." );
              File base = new File( path );
              File system = getMerlinSystemRepository();
  
              InitialContext context = 
                     new DefaultInitialContext( 
                       base, classloader, null, system, null );
  
              Artifact artifact = 
                DefaultBuilder.createImplementationArtifact( 
                  classloader, 
                  null,
                  base, 
                  MERLIN_PROPERTIES, 
                  IMPLEMENTATION_KEY );
  
              Builder builder = 
                new DefaultBuilder( context, artifact );
              Factory factory = builder.getFactory();
              Map criteria = factory.createDefaultCriteria();
  
              criteria.put( "merlin.server", "true" );
              criteria.put( "merlin.info", "true" ); 
              criteria.put( "merlin.debug", "true" ); 
  
              //
              // this is where we customize content based on web.xml
              // (currently not implemented - lets see what we can do with 
              // with merlin.properties first of all)
              //
  
              m_kernel = (Kernel) factory.create( criteria );
              
              System.out.println("kernel established");
  
              getServletContext().setAttribute( 
                "merlin-root-block", m_kernel.getBlock() );
          }
          catch( Throwable e )
          {
              final String error = ExceptionHelper.packException( e, true );
              System.out.println( error );
              throw new ServletException( error, e );
          }
      }
  
      /**
       * Disposes of container manager and container instance.
       */
      public void destroy()
      {
          if( m_kernel != null )
          {
  
              System.out.println("tearing down");
              
              try
              {
                  m_kernel.shutdown();
              }
              catch( Throwable e )
              {
                  final String error =
                    "Runnable kernel shutdown failure.";
                  final String msg = ExceptionHelper.packException( error, e, true );
                  throw new RuntimeException( msg, null );
              }
              finally
              {
                  m_kernel = null;
              }
          }
      }
  
      /**
       * Respond to a GET request for the content produced by
       * this servlet.  This method should be overidden in a
       *
       * @param request The servlet request we are processing
       * @param response The servlet response we are producing
       *
       * @exception IOException if an input/output error occurs
       * @exception ServletException if a servlet error occurs
       */
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
        throws IOException, ServletException {
  
          response.setContentType("text/html");
          PrintWriter writer = response.getWriter();
  
          writer.println("<html>");
          writer.println("<head>");
          writer.println("<title>Merlin Test Servlet Page</title>");
          writer.println("</head>");
          writer.println("<body bgcolor=white>");
  
          writer.println("<table border=\"0\">");
          writer.println("<tr>");
          writer.println("<td>");
          writer.println("<h1>Merlin Servlet Info</h1>");
          writer.println("</td>");
          writer.println("</tr>");
          writer.println("</table>");
  
          writer.println("<hr/>");
      }
  
     /**
      * Return the merlin system repository root directory.
      *
      * @param line the command line construct
      * @return the merlin system root repository directory
      */
      private static File getMerlinSystemRepository()
      {
          return new File( getMerlinHome( ), "system" );
      }
  
     /**
      * Return the merlin home directory.
      * @return the merlin install directory
      */
      private static File getMerlinHome()
      {
          return new File( getMerlinHomePath() );
      }
  
     /**
      * Return the merlin home directory path.
      * @return the merlin install directory path
      */
      private static String getMerlinHomePath()
      {
          try
          {
              String merlin = 
                System.getProperty( 
                  "merlin.home", 
                  Env.getEnvVariable( "MERLIN_HOME" ) );
              if( null != merlin ) return merlin;
              return System.getProperty( "user.home" ) 
                + File.separator + ".merlin";
          }
          catch( Throwable e )
          {
              final String error = 
                "Internal error while attempting to access MERLIN_HOME environment.";
              final String message = 
                ExceptionHelper.packException( error, e, true );
              throw new RuntimeException( message );
          }
      }
  }
  
  
  
  1.1                  avalon/merlin/kernel/servlet/src/webapp/WEB-INF/web.xml
  
  Index: web.xml
  ===================================================================
  <!DOCTYPE web-app 
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
      "http://java.sun.com/dtd/web-app_2_3.dtd";>
  
  <web-app>
  
    <display-name>Merlin</display-name>
  
    <description>
      Merlin Servlet is a servlet with an embedded Merlin Kernel.
    </description>
  
    <servlet>
      <servlet-name>merlin</servlet-name>
      <servlet-class>org.apache.avalon.merlin.servlet.MerlinServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
  
    <servlet-mapping>
      <servlet-name>merlin</servlet-name>
      <url-pattern>/*</url-pattern>
    </servlet-mapping>
  
  </web-app>
  
  
  
  1.1                  avalon/merlin/kernel/servlet/src/webapp/css/merlin.css
  
  Index: merlin.css
  ===================================================================
  
  A:link {color: blue}
  A:visited {color: lightslategray}
  A:active {color: darkred}
  
  BODY 
  {
    margin: 0px;
  }
  
  .caption { 
    font-size: 70%;
    margin-top: 2px; margin-bottom: 2px;
    text-transform: uppercase
  }
  .title { 
    font-size: 130%; 
    margin-top: 20px; 
    margin-bottom: 12px;
  }
  .header-desc { 
    margin-top: 3px; margin-bottom: 3px;
  }
  .command,.command-gateway { 
    font-size: 80%; 
    margin-left: 6px; 
    margin-top: 1px; 
    margin-bottom: 1px;
    text-transform: uppercase
  }
  .command-gateway { 
    margin-right: 24px; 
  }
  .banner { 
    margin-top: 3px; margin-bottom: 3px;
  }
  .error { 
    font-size: 70%; 
    margin-top: 0px; 
    margin-bottom: 0px;
  }
  
  .table-row { 
    margin-top: 3px; margin-bottom: 3px;
  }
  
  P{
    font-size: 10pt; 
    font-family: verdana; color: black;  
    margin-left: 3px; 
    margin-right: 3px; 
  }
                        
  .page-title-text { 
    color: #FFFFFF;  
    margin: 3px 5px 5px 3px;
    font-size: large; 
    font-family: Verdana, Arial, Helvetica, sans-serif
  }
  
  
  
  1.1                  avalon/merlin/kernel/servlet/src/webapp/images/Paper.gif
  
        <<Binary file>>
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to