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 8 import org.codehaus.plexus.embed.Embedder; 9 import org.codehaus.xfire.XFire; 10 import org.codehaus.xfire.XFireFactory; 11 12 /*** 13 * <p> 14 * The StandaloneXFire class allows you to embed a Plexus managed version 15 * of XFire within your application. Use the XFireFactory to access it. 16 * </p> 17 * <p> 18 * If you are not using the StandaloneXFireServlet or PlexusXFireServlet, 19 * you must register this factory: 20 * </p> 21 * <pre> 22 * XFireFactory.register(PlexusXFireFactory.class, true); 23 * </pre> 24 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> 25 */ 26 public class PlexusXFireFactory 27 extends XFireFactory 28 { 29 private static PlexusXFireFactory standalone; 30 31 protected Embedder embed; 32 33 protected PlexusXFireFactory() 34 throws Exception 35 { 36 URL resource = getPlexusConfiguration(); 37 38 embed = new Embedder(); 39 40 embed.setConfiguration( resource ); 41 42 Properties contextProperties = new Properties(); 43 44 embed.setProperties(contextProperties); 45 46 embed.start(); 47 } 48 49 public static XFireFactory createInstance() 50 throws Exception 51 { 52 if ( standalone == null ) 53 { 54 synchronized( PlexusXFireFactory.class ) 55 { 56 standalone = new PlexusXFireFactory(); 57 } 58 } 59 60 return standalone; 61 } 62 63 /*** 64 * @return 65 */ 66 private URL getPlexusConfiguration() 67 { 68 URL resource = null; 69 70 String configFileName = System.getProperty("xfire.plexusConfig"); 71 72 if ( configFileName != null ) 73 { 74 File file = new File(configFileName); 75 if ( file.exists() ) 76 { 77 try 78 { 79 resource = file.toURL(); 80 } 81 catch (MalformedURLException e) 82 { 83 throw new RuntimeException("Couldn't get plexus configuration.", e); 84 } 85 } 86 else 87 { 88 resource = getClass().getResource(configFileName); 89 } 90 } 91 92 if ( resource == null ) 93 { 94 resource = getClass().getResource("/org/codehaus/xfire/plexus/StandaloneXFire.xml"); 95 } 96 97 return resource; 98 } 99 100 public XFire getXFire() throws Exception 101 { 102 return (XFire) embed.lookup( XFire.ROLE ); 103 } 104 105 protected void finalize() throws Throwable 106 { 107 embed.stop(); 108 109 super.finalize(); 110 } 111 }