View Javadoc

    1   package org.codehaus.xfire.plexus.config;
    2
    3   import java.io.File;
    4   import java.io.FileNotFoundException;
    5   import java.io.FileReader;
    6   import java.io.InputStream;
    7   import java.io.InputStreamReader;
    8   import java.io.Reader;
    9
    10  import org.codehaus.plexus.configuration.PlexusConfiguration;
    11  import org.codehaus.plexus.configuration.PlexusConfigurationException;
    12  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
    13  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Configurable;
    14  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
    15  import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
    16  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
    17  import org.codehaus.xfire.plexus.PlexusXFireComponent;
    18  import org.codehaus.xfire.service.Service;
    19
    20  /***
    21   * Loads in XFire components from the XFire configuration file.
    22   *
    23   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    24   */
    25  public class DefaultConfigurationService
    26      extends PlexusXFireComponent
    27      implements Initializable, ConfigurationService, Configurable
    28  {
    29      private Configurator configurator;
    30
    31      private PlexusConfiguration services;
    32
    33  	public void initialize() throws InitializationException
    34  	{
    35  	    try
    36  	    {
    37              createServices(services);
    38
    39  	        Reader reader = findConfigurationReader();
    40
    41  	        if ( reader == null )
    42  	        {
    43  	           return;
    44  	        }
    45
    46  	        PlexusConfiguration configuration = new XmlPlexusConfiguration( Xpp3DomBuilder.build(reader) );
    47
    48              createServices( configuration.getChild("services") );
    49  	    }
    50  	    catch( Exception e )
    51  	    {
    52  	        getLogger().error("Could not start the configuration service.", e);
    53  	        throw new InitializationException("Could not start configuration service.", e);
    54  	    }
    55  	}
    56
    57  	private void createServices(PlexusConfiguration child)
    58          throws Exception
    59      {
    60          PlexusConfiguration[] service = child.getChildren("service");
    61
    62          for ( int i = 0; i < service.length; i++ )
    63          {
    64              createService( service[i] );
    65          }
    66      }
    67
    68      private void createService(PlexusConfiguration c)
    69          throws Exception
    70      {
    71          Service endpoint = configurator.createService(c);
    72      }
    73
    74  	protected Reader findConfigurationReader() throws FileNotFoundException
    75  	{
    76  		String configFileName = System.getProperty("xfire.config");
    77
    78          Reader reader = null;
    79
    80          if ( configFileName == null )
    81          {
    82              getLogger().info("No configuration file specified. Looking for xfire.xml in the current directory.");
    83              configFileName = "xfire.xml";
    84          }
    85
    86          File file = new File( configFileName );
    87
    88          if ( file.exists() )
    89          {
    90              getLogger().info("Found configuration file " + file.getAbsolutePath());
    91          	reader = new FileReader( file );
    92          }
    93          else
    94          {
    95              getLogger().info("Could not find configuration file " + file.getAbsolutePath() +
    96                               ". Looking in the classpath.");
    97
    98              InputStream is = getClass().getResourceAsStream(configFileName);
    99
    100             if ( is == null )
    101             {
    102                 getLogger().info("Could not find configuration file " + configFileName +
    103                                  " on classpath. Looking for META-INF/xfire/xfire.xml " +
    104                                  "on the classpath");
    105
    106                 is = getClass().getResourceAsStream("META-INF/xfire/xfire.xml");
    107
    108                 if ( is == null )
    109                 {
    110                     getLogger().info("No configuration found.");
    111                     return null;
    112                 }
    113             }
    114
    115             reader = new InputStreamReader( is );
    116         }
    117
    118 		return reader;
    119 	}
    120
    121     /***
    122      * @param arg0
    123      * @throws PlexusConfigurationException
    124      */
    125     public void configure(PlexusConfiguration config)
    126         throws PlexusConfigurationException
    127     {
    128         services = config.getChild("services");
    129     }
    130 }