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 import java.util.Hashtable;
10 import java.util.Iterator;
11 import java.util.List;
12
13 import org.codehaus.plexus.PlexusConstants;
14 import org.codehaus.plexus.PlexusContainer;
15 import org.codehaus.plexus.configuration.PlexusConfiguration;
16 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
17 import org.codehaus.plexus.context.Context;
18 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
19 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
20 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
21 import org.codehaus.xfire.plexus.PlexusXFireComponent;
22 import org.codehaus.xfire.service.Service;
23
24 /***
25 * Loads in XFire components from the XFire configuration file.
26 *
27 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
28 */
29 public class DefaultConfigurationService
30 extends PlexusXFireComponent
31 implements Initializable, Contextualizable, ConfigurationService
32 {
33 private PlexusContainer container;
34
35 private Hashtable configurators = new Hashtable();
36
37 public void initialize() throws Exception
38 {
39 try
40 {
41 List confs = getServiceLocator().lookupList(Configurator.ROLE);
42
43 for ( Iterator itr = confs.iterator(); itr.hasNext(); )
44 {
45 Configurator conf = (Configurator) itr.next();
46
47 register( conf );
48 }
49
50 Reader reader = findConfigurationReader();
51
52 if ( reader == null )
53 {
54 return;
55 }
56
57 PlexusConfiguration configuration = new XmlPlexusConfiguration( Xpp3DomBuilder.build(reader) );
58 createServices( configuration.getChild("services") );
59 }
60 catch( Exception e )
61 {
62 getLogger().error("Could not start the configuration service.", e);
63 throw e;
64 }
65 }
66
67 private void createServices(PlexusConfiguration child)
68 throws Exception
69 {
70 PlexusConfiguration[] service = child.getChildren("service");
71
72 for ( int i = 0; i < service.length; i++ )
73 {
74 createService( service[i] );
75 }
76 }
77
78 private void createService(PlexusConfiguration c)
79 throws Exception
80 {
81 String type = c.getChild("type").getValue("simple");
82
83 if ( type == null )
84 {
85 getLogger().error("Service " + c.getAttribute("name")
86 + " has no type.");
87 return;
88 }
89
90 getLogger().info("Creating service " + c.getChild("name").getValue() + " with type " + type);
91 Configurator builder =
92 (Configurator) getServiceLocator().lookup( Configurator.ROLE, type );
93
94 if ( builder == null )
95 {
96 getLogger().error("Error creating service " + c.getAttribute("name") + ": No Configurator.");
97 return;
98 }
99 else
100 {
101 Service service = builder.createService(c);
102 }
103 }
104
105 protected Reader findConfigurationReader() throws FileNotFoundException
106 {
107 String configFileName = System.getProperty("xfire.config");
108
109 Reader reader = null;
110
111 if ( configFileName == null )
112 {
113 getLogger().info("No configuration file specified.");
114 configFileName = "xfire.xml";
115 }
116
117 File file = new File( configFileName );
118
119 if ( file.exists() )
120 {
121 getLogger().info("Found configuration file " + file.getAbsolutePath());
122 reader = new FileReader( file );
123 }
124 else
125 {
126 getLogger().info("Could not find configuration file " + file.getAbsolutePath());
127 getLogger().info("Looking in the classpath.");
128
129 InputStream is = getClass().getResourceAsStream(configFileName);
130
131 if ( is == null )
132 {
133 is = getClass().getResourceAsStream("META-INF/xfire/xfire.xml");
134
135 if ( is == null )
136 return null;
137 }
138
139 reader = new InputStreamReader( is );
140 }
141
142 return reader;
143 }
144
145 /***
146 * @see org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable#contextualize(org.codehaus.plexus.context.Context)
147 */
148 public void contextualize(Context context) throws Exception
149 {
150 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
151 }
152
153 /***
154 * @see org.codehaus.xfire.plexus.config.ConfigurationService#register(org.codehaus.xfire.plexus.config.ServiceConfigurator)
155 */
156 public void register( Configurator configurator )
157 {
158 configurators.put( configurator.getServiceType(),
159 configurator );
160 }
161 }