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/11/11 11:24:00 UTC

cvs commit: jakarta-avalon-excalibur/meta/src/java/org/apache/excalibur/meta/model Profile.java

mcconnell    2002/11/11 02:24:00

  Added:       meta/src/java/org/apache/excalibur/meta/model Profile.java
  Log:
  Initial commit of the Profile class.  The class represents the aggregation of
  the state information about a particular deployment scenario.  The profile
  does not include information pertaining to association with other profiles as
  this is a container concern.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/meta/src/java/org/apache/excalibur/meta/model/Profile.java
  
  Index: Profile.java
  ===================================================================
  /* ==================================================================== 
   * The Apache Software License, Version 1.1 
   * 
   * Copyright (c) 2002 The Apache Software Foundation. All rights 
   * reserved. 
   * 
   * Redistribution and use in source and binary forms, with or without 
   * modification, 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", 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 apache@apache.org.
   *
   * 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 (INCLUDING, 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.excalibur.meta.model;
  
  import java.io.InputStream;
  import java.io.Serializable;
  import java.util.Hashtable;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.excalibur.configuration.CascadingConfiguration;
  import org.apache.excalibur.configuration.ConfigurationUtil;
  import org.apache.excalibur.meta.ConfigurationBuilder;
  import org.apache.excalibur.meta.info.StageDescriptor;
  import org.apache.excalibur.meta.info.Type;
  import org.xml.sax.InputSource;
  
  /**
   * Definition of the criteria for an explicit component profile.  A profile, when
   * included within the scope of a container declaration will be instantiated in
   * the model as an EXPLICIT component profile resulting in the initiation of
   * dependency resolution relative to the component as the target deployment
   * objective.  Multiple supplementary profiles may be packaged in a .xprofiles
   * resources and will be assigned to the container automatically.  In the absence
   * of explicit or packaged profile directives, an implicit profile will be created
   * for any component types declared under a jar manifest.
   *
   * <p><b>XML</b></p>
   * <p>A component element declares the profile to be applied during the instantiation
   * of a component type.  It includes a name and class declaration, logging directives
   * (resolved relative to the component's container), context creation criteria,
   * together with configuration or parameters information.</p>
   *
   * <pre>
   <font color="gray"><i>&lt;!--
   Declaration of the services hosted by this container.  Service container here
   will be managed relative to other provider components at the same level and
   may be serviced by components declared in parent container.
   --&gt;</i></font>
  
   &lt;component name="<font color="darkred">complex</font>" class="<font color="darkred">org.apache.excalibur.playground.ComplexComponent</font>" activation="<font color="darkred">startup</font>"&gt;
  
   <font color="gray"><i>&lt;!--
   Priority and target assignments for component specific logging categrories.
   --&gt;</i></font>
  
   &lt;categories priority="<font color="darkred">DEBUG</font>"&gt;
   &lt;category name="<font color="darkred">init</font>" priority="<font color="darkred">DEBUG</font>" /&gt;
   &lt;/categories&gt;
  
   <font color="gray"><i>&lt;!--
   Include the following context value in the context supplied a component using this
   profile.  Context entries are normally only required in the case where the component
   type declares a required context type and entry values. Generally speaking, a component
   will normally qualify it's instantiation criteria through a configuration declaration.
   Any context values defined at this level will override context values supplied by the
   container.
   --&gt;</i></font>
  
   &lt;context&gt;
   &lt;entry name="<font color="darkred">location</font>" value="<font color="darkred">Paris</font>"/&gt;
   &lt;/context&gt;
  
   <font color="gray"><i>&lt;!--
   Apply the following configuration when instantiating the component.  This configuration
   will be applied as the primary configuration in a cascading configuration chain.  A
   type may declare a default configuration under a "classname".xconfig file that will be
   used to dereference any configuration requests not resolvable by the configuration
   supplied here.
   --&gt;</i></font>
  
   &lt;configuration&gt;
   &lt;message value="<font color="darkred">Hello</font>"/&gt;
   &lt;/configuration&gt;
  
   <font color="gray"><i>&lt;!--
   The parameterization criteria from this instance of the component type.
   --&gt;</i></font>
  
   &lt;parameters/&gt;
  
   &lt;/component&gt;
   * </pre>
   *
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/11 10:24:00 $
   */
  public class Profile implements Serializable
  {
  
      /**
       * Constant indicating that the profile was implicitly created.
       */
      public static final int IMPLICIT = 0;
  
      /**
       * Constant indicating that the profile was created based on a profile packaged with the type.
       */
      public static final int PACKAGED = 1;
  
      /**
       * Constant indicating that the profile was explicitly declared under an assembly directive.
       */
      public static final int EXPLICIT = 2;
  
      /**
       * The name of the component profile. This is an
       * abstract name used during assembly.
       */
      private final String m_name;
  
      /**
       * The assigned logging categories.
       */
      private final LoggingDirective m_categories;
  
      /**
       * The info object for component type.
       */
      private final Type m_type;
  
      /**
       * The parameters for component (if any).
       */
      private final Parameters m_parameters;
  
      /**
       * The configuration for component (if any).
       */
      private final Configuration m_configuration;
  
      /**
       * The derived configuration
       */
      private Configuration m_config;
  
      /**
       * The configuration for component (if any).
       */
      private final ContextDirective m_context;
  
      /**
       * The creation mode.
       */
      private final int m_mode;
  
      /**
       * The enabled status of the profile.  Normally a profile is enabled however, a
       * failure to assemble will result in the disabling of the profile.
       */
      private boolean m_enabled = true;
  
      /**
       * The activation policy - TRUE if activation on startup, FALSE to activate on request.
       */
      private final boolean m_activation;
  
      /**
       * Create an Profile instance, enabled, lazy activation and explicit
       * status.
       *
       * @param name the abstract name of the profile
       * @param type the type of component that this profile qualifies
       * @param categories the logging categoories descriptor
       */
      public Profile( final String name,
                      final Type type,
                      final LoggingDirective categories )
      {
          this( name, null, null, null, categories, type, true, false, EXPLICIT );
      }
  
      /**
       * Create a Profile instance without parameters.
       *
       * @param name the abstract name of the profile
       * @param configuration the configuration instance to use during type instantiation
       * @param context the context instance to use during type instantiation
       * @param categories the logging categories descriptor
       * @param type the type of component that this profile qualifies
       * @param enabled the enabled state of the component profile
       * @param activation TRUE if activation on startup, FALSE to activate on request
       * @param mode the creation mode (either IMPLICIT, PACKAGED, or EXPLICIT)
       */
      public Profile( final String name,
                      final Configuration configuration,
                      final ContextDirective context,
                      final LoggingDirective categories,
                      final Type type,
                      final boolean enabled,
                      final boolean activation,
                      final int mode )
      {
          this( name, null, configuration, context, categories, type, enabled, activation, mode );
      }
  
      /**
       * Create a Profile instance without parameters.
       *
       * @param name the abstract name of profile
       * @param activation the profile activation policy
       * @param template the profile to use as a template
       */
      public Profile( final String name, boolean activation, final Profile template )
      {
          this(
              name,
              template.getParameters(),
              template.getConfiguration(),
              template.getContext(),
              template.getCategories(),
              template.getType(),
              template.isEnabled(),
              activation,
              EXPLICIT );
      }
  
      /**
       * Create a Profile instance.
       *
       * @param name the abstract name of component meta data instance
       * @param parameters the parameters instance to use during type instantiation
       * @param configuration the configuration instance to use during type instantiation
       * @param context the context instance to use during type instantiation
       * @param categories the logging categories descriptor
       * @param type the type of component that this profile qualifies
       * @param enabled the enabled state of the component profile
       * @param activation TRUE if activation on startup, FALSE to activate on request
       * @param mode the creation mode (either IMPLICIT, PACKAGED, or EXPLICIT)
       */
      public Profile( final String name,
                      final Parameters parameters,
                      final Configuration configuration,
                      final ContextDirective context,
                      final LoggingDirective categories,
                      final Type type,
                      final boolean enabled,
                      final boolean activation,
                      final int mode )
      {
          if( null == type )
          {
              throw new NullPointerException( "type" );
          }
  
          if( null == name )
          {
              m_name = type.getInfo().getName() + "-" + System.identityHashCode( this );
          }
          else
          {
              if( mode != EXPLICIT )
              {
                  m_name = name + "-" + System.identityHashCode( this );
              }
              else
              {
                  m_name = name;
              }
          }
  
          if( null == categories )
          {
              throw new NullPointerException( "categories" );
          }
  
          m_parameters = parameters;
          m_configuration = configuration;
          m_categories = categories;
          m_type = type;
          m_context = context;
          m_enabled = enabled;
          m_activation = activation;
          m_mode = mode;
      }
  
      /**
       * Test is this profile is enabled.  A profile is enabled unless explicitly disabled by an
       * assembly directive, or implicity disabled as a result of an assembly failure.
       *
       * @return TRUE if the profile is enabled.
       * @see #setEnabled( boolean )
       */
      public boolean isEnabled()
      {
          return m_enabled;
      }
  
      /**
       * Set the enabled status of the profile to the supplied value.
       * @param value the enabled status - TRUE or FALSE
       */
      public void setEnabled( boolean value )
      {
          m_enabled = value;
      }
  
      /**
       * Return the name of component metadata instance.
       *
       * @return the name of the component.
       */
      public String getName()
      {
          return m_name;
      }
  
      /**
       * Return the logging categories for the profile.
       *
       * @return the logger
       */
      public LoggingDirective getCategories()
      {
          return m_categories;
      }
  
      /**
       * Return the type info for component.
       *
       * @return the info for component type.
       */
      public Type getType()
      {
          return m_type;
      }
  
      /**
       * Return the context directive for the profile.
       *
       * @return the ContextDirective for the profile.
       */
      public ContextDirective getContext()
      {
          return m_context;
      }
  
      /**
       * Return the Parameters for the profile.
       *
       * @return the Parameters for Component (if any).
       */
      public Parameters getParameters()
      {
          return m_parameters;
      }
  
      /**
       * Return the base Configuration for the profile.
       *
       * @return the base Configuration for profile.
       */
      public Configuration getConfiguration()
      {
          return m_configuration;
      }
  
      /**
       * Return the derived Configuration for the profile as a
       * {@link CascadingConfiguration}. The primary configuration
       * is either the base configuration of a configuration referenced
       * by the attribute <code>src</code> and the default configuration
       * is resolved from the &lt;classname&gt;.xconfig resourse.
       *
       * @param classloader the classloader to use
       * @return the Configuration for profile.
       * @exception Exception if an error occurs while resolving the configuration
       */
      public Configuration getConfiguration( ClassLoader classloader ) throws Exception
      {
          if( m_config != null )
          {
              return m_config;
          }
  
          final Class type = classloader.loadClass(
              getType().getInfo().getImplementationKey() );
          if( !Configurable.class.isAssignableFrom( type ) )
          {
              return null;
          }
  
          final Configuration defaults = getType().getConfiguration( classloader );
          Configuration base = null;
          if( getConfiguration().getAttribute( "src", null ) != null )
          {
              String src = getConfiguration().getAttribute( "src" );
              if( src.startsWith( "resource://" ) )
              {
                  final String url = src.substring( 11 );
                  final InputStream stream =
                      classloader.getResourceAsStream( url );
                  if( null == stream )
                  {
                      final String error =
                          "Requested configuration source does not exist: " + src;
                      throw new ConfigurationException( error );
                  }
                  final InputSource source = new InputSource( stream );
                  base = ConfigurationBuilder.build( source );
              }
              else
              {
                  try
                  {
                      base = ConfigurationBuilder.build( src );
                  }
                  catch( Throwable e )
                  {
                      final String error =
                          "Unexpected exception while attempting to resolve configuration from src : "
                          + src;
                      throw new ConfigurationException( error );
                  }
              }
          }
          else
          {
              base = getConfiguration();
          }
  
          if( null == defaults )
          {
              m_config = base;
          }
          else
          {
              m_config = new CascadingConfiguration( base, defaults );
          }
          return m_config;
      }
  
      /**
       * Returns the creation mode for this profile.
       * @return a value of EXPLICIT, PACKAGED or IMPLICIT
       */
      public int getMode()
      {
          return m_mode;
      }
  
      /**
       * Return the activation policy for the component.  If TRUE, activation
       * will occur at startup.  If false, activation will be deferred to
       * the first lookup invocation if any (i.e. lazy activation).
       *
       * @return the activation policy
       */
      public boolean getActivationPolicy()
      {
          return m_activation;
      }
  
      /**
       * Returns a string representation of the profile.
       * @return a string representation
       */
      public String toString()
      {
          return "Profile name: " + getName()
              + ", type: " + getType().getInfo().getName()
              + ", mode: " + modeToString( getMode() );
      }
  
      /**
       * Provide a textual report on the profile.
       * @return the formatted profile report
       */
      public String list()
      {
          StringBuffer buffer = new StringBuffer();
          buffer.append( "PROFILE REPORT\n" );
          buffer.append( "\n  name: " + getName() );
          buffer.append( "\n  base: " + getType().getInfo().getName() );
          buffer.append( "\n  key: " + getType().getInfo().getImplementationKey() );
          buffer.append( "\n  context: " + getContext() );
          buffer.append( "\n  configuration:" );
          if( getConfiguration() != null )
          {
              buffer.append( ConfigurationUtil.list( getConfiguration() ) );
          }
          else
          {
              buffer.append( " null" );
          }
          buffer.append( "\n  parameters: " + getParameters() );
          buffer.append( "\n\n" );
          return buffer.toString();
      }
  
     /**
      * Returns a string representation of a mode value.
      * @param mode the mode value
      * @return the string representation
      */
      public static String modeToString( int mode )
      {
          if( mode == IMPLICIT )
          {
              return "IMPLICIT";
          }
          else if( mode == PACKAGED )
          {
              return "PACKAGED";
          }
          else if( mode == EXPLICIT )
          {
              return "EXPLICIT";
          }
          else
          {
              return "?";
          }
      }
  }
  
  
  

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