View Javadoc

1   package org.codehaus.xfire.plexus;
2   
3   import java.io.File;
4   import java.net.MalformedURLException;
5   import java.net.URL;
6   import java.util.Properties;
7   import org.codehaus.plexus.embed.Embedder;
8   import org.codehaus.xfire.XFire;
9   import org.codehaus.xfire.service.ServiceRegistry;
10  import org.codehaus.xfire.transport.TransportManager;
11  
12  /***
13   * <p>
14   * The StandaloneXFire class allows you to embed XFire any which way
15   * within your apps.  Grab an instance of the <code>StandaloneXFire</code>
16   * instance via <code>getInstance()</code> then access such components
17   * as the ServiceRegistry or TransportService.
18   * </p>
19   * <p>
20   * This class assumes one XFire instance per JVM. To create many XFire instances
21   * you must use Plexus directly.
22   * </p>
23   * <p>
24   * To use a non-standard plexus configuration for XFire, set the
25   * "xfire.plexusConfig" system property to the location of the configuration
26   * file.  This can be in the classpath or in the filesystem.
27   * </p>
28   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
29   */
30  public class StandaloneXFire
31  {
32      private static StandaloneXFire standalone;
33      
34      protected Embedder embed;
35  
36      private StandaloneXFire() 
37          throws Exception
38      {
39          URL resource = getPlexusConfiguration();
40          
41          embed = new Embedder();
42  
43          embed.setConfiguration( resource );
44  
45          Properties contextProperties = new Properties();
46  
47          embed.setProperties(contextProperties);
48          
49          embed.start();
50      }
51  
52      /***
53  	 * @return
54  	 */
55  	private URL getPlexusConfiguration()
56  	{
57  		URL resource = null;
58          
59          String configFileName = System.getProperty("xfire.plexusConfig");
60          
61          if ( configFileName != null ) 
62          {
63              File file = new File(configFileName);
64              if ( file.exists() )
65              {
66                  try
67      			{
68      				resource = file.toURL();
69      			}
70      			catch (MalformedURLException e)
71      			{
72                      throw new RuntimeException("Couldn't get plexus configuration.", e);
73      			}
74              }
75              else
76              {
77                  resource = getClass().getResource(configFileName);
78              }
79          }
80  
81          if ( resource == null )
82          {
83              resource = getClass().getResource("/org/codehaus/xfire/plexus/StandaloneXFire.xml");
84          }
85          
86  		return resource;
87  	}
88  
89  	public static StandaloneXFire getInstance() 
90          throws Exception
91      {
92          if (standalone == null)
93  		{
94  			synchronized (StandaloneXFire.class)
95  			{
96  				standalone = new StandaloneXFire();
97  			}
98  		}
99  		return standalone;
100     }
101     
102     public ServiceRegistry getServiceRegistry() throws Exception
103     {
104         return (ServiceRegistry) embed.lookup( ServiceRegistry.ROLE );
105     }
106     
107     public XFire getXFire() throws Exception
108     {
109         return (XFire) embed.lookup( XFire.ROLE );
110     }
111     
112     public TransportManager getTransportService() throws Exception
113     {
114         return (TransportManager) embed.lookup( TransportManager.ROLE );
115     }
116     
117 	protected void finalize() throws Throwable
118 	{
119 		embed.stop();
120         
121         super.finalize();
122 	}
123 }