avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craft...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/servlet ServletContainer.java ServletContainer.roles ServletContainer.xconf ServletContainer.xlog servlet.java
Date Thu, 04 Jul 2002 10:24:00 GMT
crafterm    2002/07/04 03:24:00

  Modified:    fortress/examples build.xml
  Added:       fortress/examples/conf web.xml
               fortress/examples/src/java/org/apache/excalibur/fortress/examples/servlet
                        ServletContainer.java ServletContainer.roles
                        ServletContainer.xconf ServletContainer.xlog
                        servlet.java
  Log:
  First draft servlet based Fortress container example.
  
  Revision  Changes    Path
  1.3       +24 -2     jakarta-avalon-excalibur/fortress/examples/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/fortress/examples/build.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- build.xml	4 Jul 2002 09:43:24 -0000	1.2
  +++ build.xml	4 Jul 2002 10:23:59 -0000	1.3
  @@ -111,6 +111,11 @@
       <!-- Initialize custom tasks                                             -->
       <!-- =================================================================== -->
       <target name="init" depends="collect-jars">
  +
  +        <available property="servlet.present" classname="javax.servlet.Servlet">
  +            <classpath refid="project.class.path"/>
  +        </available>
  +
       </target>
   
       <!-- =================================================================== -->
  @@ -126,6 +131,8 @@
               debug="on"
               optimize="off">
               <classpath refid="project.class.path" />
  +            <include name="**/*.java"/>
  +            <exclude name="**/servlet/*.java" unless="servlet.present"/>
           </javac>
   	
           <!-- copy resources to same location as .class files -->
  @@ -146,7 +153,22 @@
       
       </target>
       
  -    <target name="install" depends="jar">
  +    <target name="webapp" depends="jar">
  +    
  +    	<mkdir dir="${build.dir}/servlet"/>
  +	<mkdir dir="${build.dir}/servlet/WEB-INF"/>
  +	<mkdir dir="${build.dir}/servlet/WEB-INF/lib"/>
  +	
  +	<copy todir="${build.dir}/servlet/WEB-INF/lib">
  +	    <fileset dir="${lib.dir}">
  +	        <include name="*.jar"/>
  +		<exclude name="servlet*"/>
  +	    </fileset>
  +	</copy>
  +	
  +	<copy todir="${build.dir}/servlet/WEB-INF"
  +	      file="conf/web.xml"/>
  +	      
       </target>
   
       <!-- =================================================================== -->
  @@ -164,7 +186,7 @@
       <!-- =================================================================== -->
       <!-- Main target                                                         -->
       <!-- =================================================================== -->
  -    <target name="main" depends="install" description="build the runnable application">
  +    <target name="main" depends="jar" description="build the runnable application">
       </target>
   
       <!-- =================================================================== -->
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/conf/web.xml
  
  Index: web.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <!-- $Id: web.xml,v 1.1 2002/07/04 10:23:59 crafterm Exp $ -->
  
  <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
      "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
  
  <web-app>
  	<display-name>Fortress-Example</display-name>
  
  	<description>
  		A Fortress-based servlet container example
  	</description>
  
  	<servlet>
  		<servlet-name>ServletContainer</servlet-name>
  		<servlet-class>org.apache.excalibur.fortress.examples.servlet.servlet</servlet-class>
  		<load-on-startup>1</load-on-startup>
  	</servlet>
  
  	<!-- Map all documents to container servlet -->
  	<servlet-mapping>
  		<servlet-name>ServletContainer</servlet-name>
  		<url-pattern>/</url-pattern>
  	</servlet-mapping>
  
  </web-app>
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/servlet/ServletContainer.java
  
  Index: ServletContainer.java
  ===================================================================
  /*
  * Copyright (C) The Apache Software Foundation. All rights reserved.
  *
  * This software is published under the terms of the Apache Software License
  * version 1.1, a copy of which has been included with this distribution in
  * the LICENSE.txt file.
  */
  package org.apache.excalibur.fortress.examples.servlet;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.excalibur.fortress.AbstractContainer;
  
  import org.apache.excalibur.fortress.examples.components.Translator;
  
  /**
   * Fortress based servlet example. Presents a simple page to the user
   * displaying the possible languages they can see the text 'hello world'
   * written in.
   *
   * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/07/04 10:24:00 $
   */
  public final class ServletContainer extends AbstractContainer
  {
      public static final String KEY = "hello-world";
  
      private ComponentManager m_manager;
      private Translator m_translator;
  
      /**
       * Initialize this component
       *
       * @exception Exception if an error occurs
       */
      public void initialize()
          throws Exception
      {
          super.initialize();
  
          // REVISIT: shouldn't this work in compose() ?
          m_manager = getComponentManager();
          m_translator = ( Translator ) m_manager.lookup( Translator.ROLE );
      }
  
      /**
       * Simple method to handle requests sent to the container from the
       * controlling servlet. This container simply displays a page containing
       * a list of possible languages the user can see the text 'hello world' 
       * written in.
       *
       * @param request a <code>ServletRequest</code> instance
       * @param response a <code>ServletResponse</code> instance
       * @exception ServletException if a servlet error occurs
       * @exception IOException if an IO error occurs
       */
      public void handleRequest( ServletRequest request, ServletResponse response )
          throws ServletException, IOException
      {
          PrintWriter out = response.getWriter();
          String selected = request.getParameter( "language" );
          String[] languages = m_translator.getSupportedLanguages( KEY );
  
          out.println( "<html>" );
          out.println( "<head><title>Hello World!</title></head>"
);
          out.println( "<body>" );
          out.println( "<hr>" );
  
          out.println( "<h1>" );
  
          if ( selected == null )
          {
              out.println( "Please select your language" );
          }
          else
          {
              out.println( m_translator.getTranslation( KEY, selected ) );
          }
  
          out.println( "</h1>" );
          out.println( "<hr>" );
  
          out.println( "Available languages:" );
  
          out.println( "<form action='' name='languagelist'>" );
          out.println( "<select size='1' name='language'>" );
  
          for ( int i = 0; i < languages.length; ++i )
          {
              String lang = languages[ i ];
              out.print( "<option value='" + lang + "'" );
  
              // preselect chosen language
  
              if ( lang.equals( selected ) )
              {
                  out.print( " selected" );
              }
  
              out.println( ">" + lang + "</option>" );
          }
  
          out.println( "</select>" );
          out.println( "<input value='OK' type='submit'>" );
          out.println( "</form>" );
  
          out.println( "</body>" );
          out.println( "</html>" );
  
          out.close();
      }
  
      /**
       * Release resources
       */
      public void dispose()
      {
          super.dispose();
  
          if ( m_translator != null )
              m_manager.release( m_translator );
      }
  }
  
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/servlet/ServletContainer.roles
  
  Index: ServletContainer.roles
  ===================================================================
  <fortress-roles>
  
    <role name="org.apache.excalibur.fortress.examples.components.Translator">
      <component shorthand="translator"
                 class="org.apache.excalibur.fortress.examples.components.TranslatorImpl"
                 handler="org.apache.excalibur.fortress.handler.ThreadSafeComponentHandler"/>
    </role>
  
  </fortress-roles>
  
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/servlet/ServletContainer.xconf
  
  Index: ServletContainer.xconf
  ===================================================================
  <?xml version="1.0" encoding="iso-8859-1" ?>
  
  <!-- Example Fortress configuration file.
  
       This file contains sample component configurations for the Fortress 'servlet'
       example.
   -->
  <fortress-example>
  
    <!-- Simple translation component. This component maintains translations for a
         given key in different languages.
     -->
    <translator id="translator" logger="translator">
  	<dictionary>
  	  <translation key="hello-world">
  	    <value language="Deutsch">Hallo Welt</value>
  	    <value language="English">Hello World</value>
  	    <value language="Franšais">Bonjour la monde</value>
  	    <value language="Indonesia">Apa kabar Dunia</value>
  	    <value language="Espan§l">Hola Mundo</value>
  	    <value language="Italiano">Ciao Mondo</value>
  	  </translation>
  	</dictionary>
    </translator>
  
  </fortress-example>
  
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/servlet/ServletContainer.xlog
  
  Index: ServletContainer.xlog
  ===================================================================
  <logkit>
      <factories>
        <factory type="file" class="org.apache.avalon.excalibur.logger.factory.FileTargetFactory"/>
      </factories>
  
      <targets>
         <file id="root">
          <filename>fortress-examples.log</filename>
          <format type="extended">
            %7.7{priority} %5.5{time}   [%8.8{category}] (%{context}): %{message}\n%{throwable}
          </format>
         </file>
       </targets>
  
       <categories>
         <category name="" log-level="DEBUG">
           <log-target id-ref="root"/>
         </category>
       </categories>
  
  </logkit>
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/examples/src/java/org/apache/excalibur/fortress/examples/servlet/servlet.java
  
  Index: servlet.java
  ===================================================================
  /*
  * Copyright (C) The Apache Software Foundation. All rights reserved.
  *
  * This software is published under the terms of the Apache Software License
  * version 1.1, a copy of which has been included with this distribution in
  * the LICENSE.txt file.
  */
  package org.apache.excalibur.fortress.examples.servlet;
  
  import java.io.IOException;
  
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServlet;
  
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.DefaultContext;
  import org.apache.avalon.framework.logger.LogKitLogger;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.excalibur.fortress.ContainerManager;
  import org.apache.excalibur.fortress.DefaultContainerManager;
  import org.apache.excalibur.fortress.util.ContextBuilder;
  import org.apache.excalibur.fortress.util.ContextManager;
  
  /**
   * Servlet based Fortress container example.
   *
   * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
   * @version $Id: servlet.java,v 1.1 2002/07/04 10:24:00 crafterm Exp $
   */
  public final class servlet extends HttpServlet
  {
      private ServletContainer m_container;
      private ContainerManager m_containerManager;
  
      /**
       * Initializes Servlet and creates a <code>ServletContainer</code> instance
       *
       * @exception ServletException if an error occurs
       */
      public void init()
          throws ServletException
      {
          super.init();
  
          try
          {
              ContextBuilder contextBuilder = new ContextBuilder();
              contextBuilder.setContainerClass( "org.apache.excalibur.fortress.examples.servlet.ServletContainer"
);
              contextBuilder.setContextDirectory( "./" );
              contextBuilder.setWorkDirectory( "./" );
              contextBuilder.setContainerConfiguration( "resource://org/apache/excalibur/fortress/examples/servlet/ServletContainer.xconf"
);
              contextBuilder.setLoggerManagerConfiguration( "resource://org/apache/excalibur/fortress/examples/servlet/ServletContainer.xlog"
);
              contextBuilder.setRoleManagerConfiguration( "resource://org/apache/excalibur/fortress/examples/servlet/ServletContainer.roles"
);
  
              ContextManager contextManager = new ContextManager( contextBuilder.getContext(),
null );
              contextManager.initialize();
  
              m_containerManager = new DefaultContainerManager( contextManager );
              m_containerManager.initialize();
  
              m_container = ( ServletContainer ) m_containerManager.getContainer();
          }
          catch ( Exception e )
          {
              throw new ServletException( "Error during initialization", e );
          }
      }
  
      /**
       * Pass all servlet requests through to container to be handled. In a more
       * complex system, there could be multiple containers that handle different
       * requests, or a main controlling container with subcontainers for different
       * requests.
       *
       * @param request a <code>ServletRequest</code> instance
       * @param response a <code>ServletResponse</code> instance
       * @exception IOException if an IO error occurs
       * @exception ServletException if a servlet error occurs
       */
      public void service( ServletRequest request, ServletResponse response )
          throws IOException, ServletException
      {
          m_container.handleRequest( request, response );
      }
  
      /**
       * Disposes of container manager and container instance.
       */
      public void destroy()
      {
          m_containerManager.dispose();
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message