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