You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2002/09/12 08:03:10 UTC

cvs commit: jakarta-avalon-excalibur/meta/src/java/org/apache/excalibur/meta/info/builder SerializedServiceCreator.java ServiceBuilder.java

mcconnell    2002/09/11 23:03:10

  Added:       meta/src/java/org/apache/excalibur/meta/info/builder
                        SerializedServiceCreator.java ServiceBuilder.java
  Log:
  Addition of serialized service info handlers.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/meta/src/java/org/apache/excalibur/meta/info/builder/SerializedServiceCreator.java
  
  Index: SerializedServiceCreator.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.meta.info.builder;
  
  import org.apache.excalibur.meta.info.Service;
  import java.io.InputStream;
  import java.io.ObjectInputStream;
  
  /**
   * Create {@link Service} from stream made up of a serialized object.
   *
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/12 06:03:10 $
   */
  public class SerializedServiceCreator
      implements ServiceCreator
  {
  
     /**
      * Create of a service instance from a serialized form.
      * @param key parameter not used
      * @param input the input stream
      * @return the meta-info instance that describes the component type
      * @exception Exception if an error occurs
      */
      public Service createService( final String key, final InputStream input )
          throws Exception
      {
          final ObjectInputStream ois = new ObjectInputStream( input );
          return (Service)ois.readObject();
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/meta/src/java/org/apache/excalibur/meta/info/builder/ServiceBuilder.java
  
  Index: ServiceBuilder.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.meta.info.builder;
  
  import java.io.InputStream;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.excalibur.meta.info.Service;
  
  /**
   * A ServiceBuilder is responsible for building {@link Service}
   * objects from Configuration objects.
   *
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/12 06:03:10 $
   */
  public final class ServiceBuilder
      extends AbstractLogEnabled
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( ServiceBuilder.class );
  
      private final ServiceCreator m_xmlServiceCreator = createXMLServiceCreator();
      private final ServiceCreator m_serialServiceCreator = new SerializedServiceCreator();
  
      /**
       * Setup logging for all subcomponents
       * @param logger the logging channel
       */
      public void enableLogging( final Logger logger )
      {
          super.enableLogging( logger );
          setupLogger( m_serialServiceCreator );
          if( null != m_xmlServiceCreator )
          {
              setupLogger( m_xmlServiceCreator );
          }
      }
  
      /**
       * Create a {@link Service} object for specified Class.
       *
       * @param clazz The class of Component
       * @return the created Service
       * @throws Exception if an error occurs
       */
      public Service build( final Class clazz )
          throws Exception
      {
          return build( clazz.getName(), clazz.getClassLoader() );
      }
  
      /**
       * Create a {@link Service} object for specified
       * classname, in specified ClassLoader.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created Service
       * @throws Exception if an error occurs
       */
      public Service build( final String classname,
                                  final ClassLoader classLoader )
          throws Exception
      {
          final Service info = buildFromSerDescriptor( classname, classLoader );
          if( null != info )
          {
              return info;
          }
          else
          {
              return buildFromXMLDescriptor( classname, classLoader );
          }
      }
  
      /**
       * Build Service from the XML descriptor format.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created Service
       * @throws Exception if an error occurs
       */
      private Service buildFromSerDescriptor( final String classname,
                                                    final ClassLoader classLoader )
          throws Exception
      {
          final String xinfo =
              classname.replace( '.', '/' ) + ".sinfo";
          final InputStream inputStream =
              classLoader.getResourceAsStream( xinfo );
          if( null == inputStream )
          {
              return null;
          }
  
          return m_serialServiceCreator.createService( classname, inputStream );
      }
  
      /**
       * Build Service from the XML descriptor format.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created Service
       * @throws Exception if an error occurs
       */
      private Service buildFromXMLDescriptor( final String classname,
                                                    final ClassLoader classLoader )
          throws Exception
      {
          //
          // get the input stream for the .xservice resource
          //
  
          final String xservice =
              classname.replace( '.', '/' ) + ".xservice";
          final InputStream inputStream =
              classLoader.getResourceAsStream( xservice );
  
          if( null == inputStream )
          {
              return new Service( null );
          }
  
          //
          // build the type
          //
  
          final ServiceCreator xmlServiceCreator = getXMLServiceCreator( classname );
          return xmlServiceCreator.createService( classname, inputStream );
      }
  
      /**
       * Utility to get xml info builder, else throw
       * an exception if missing descriptor.
       *
       * @return the ServiceCreator
       */
      private ServiceCreator getXMLServiceCreator( final String classname )
          throws Exception
      {
          if( null != m_xmlServiceCreator )
          {
              return m_xmlServiceCreator;
          }
          else
          {
              final String message =
                  REZ.getString( "builder.missing-xml-creator.error",
                                 classname );
              throw new Exception( message );
          }
      }
  
      /**
       * Utility to get XMLServiceCreator if XML files are on
       * ClassPath.
       *
       * @return the XML {@link ServiceCreator}
       */
      private static ServiceCreator createXMLServiceCreator()
      {
          ServiceCreator xmlServiceCreator = null;
          try
          {
              xmlServiceCreator = new XMLServiceCreator();
          }
          catch( final Exception e )
          {
              //Ignore it if ClassNot found due to no
              //XML Classes on classpath
          }
          return xmlServiceCreator;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>