package org.codehaus.xfire.plexus;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import org.codehaus.plexus.embed.Embedder;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;

/**
 * <p>
 * The StandaloneXFire class allows you to embed a Plexus managed version
 * of XFire within your application. Use the XFireFactory to access it.
 * </p>
 * <p>
 * If you are not using the StandaloneXFireServlet or PlexusXFireServlet,
 * you must register this factory:
 * </p>
 * <pre>
 * XFireFactory.register(PlexusXFireFactory.class, true);
 * </pre>
 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
 */
public class PlexusXFireFactory
	extends XFireFactory
{
    private static PlexusXFireFactory standalone;

    protected Embedder embed;

    protected PlexusXFireFactory() 
        throws Exception
    {
        URL resource = getPlexusConfiguration();
        
        embed = new Embedder();

        embed.setConfiguration( resource );

        Properties contextProperties = new Properties();

        embed.setProperties(contextProperties);
        
        embed.start();
    }

    public static XFireFactory createInstance() 
    	throws Exception
    {
        if ( standalone == null )
        {
            synchronized( PlexusXFireFactory.class )
            {
                standalone = new PlexusXFireFactory();
            }
        }
        
        return standalone;
    }

    /**
	 * @return
	 */
	private URL getPlexusConfiguration()
	{
		URL resource = null;
        
        String configFileName = System.getProperty("xfire.plexusConfig");
        
        if ( configFileName != null ) 
        {
            File file = new File(configFileName);
            if ( file.exists() )
            {
                try
    			{
    				resource = file.toURL();
    			}
    			catch (MalformedURLException e)
    			{
                    throw new RuntimeException("Couldn't get plexus configuration.", e);
    			}
            }
            else
            {
                resource = getClass().getResource(configFileName);
            }
        }

        if ( resource == null )
        {
            resource = getClass().getResource("/org/codehaus/xfire/plexus/StandaloneXFire.xml");
        }
        
		return resource;
	}

    public XFire getXFire() throws Exception
    {
        return (XFire) embed.lookup( XFire.ROLE );
    }

	protected void finalize() throws Throwable
	{
		embed.stop();
        
        super.finalize();
	}
}

@@ -136,24 +136,108 @@
 roperties;%0A%0A
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;%0A
 import org.c
@@ -339,16 +339,65 @@
 Factory;
+%0Aimport org.codehaus.xfire.XFireRuntimeException;
 %0A%0A/**%0A *
@@ -1011,29 +1011,26 @@
     
-    throws Exception%0A
+%7B%0A        try%0A    
     
@@ -1039,16 +1039,20 @@
         
+    
 URL reso
@@ -1084,33 +1084,41 @@
 tion();%0A        
-%0A
+    %0A    
         embed = 
@@ -1134,32 +1134,36 @@
 der();%0A%0A        
+ 
+   
 embed.setConfigu
@@ -1191,16 +1191,20 @@
         
+   
+ 
 Properti
@@ -1245,32 +1245,36 @@
 ies();%0A%0A        
+    
 embed.setPropert
@@ -1297,33 +1297,41 @@
 rties);%0A        
+ 
-%0A
+   %0A    
         embed.st
@@ -1333,24 +1333,164 @@
 ed.start();%0A
+        %7D%0A        catch (Exception e)%0A        %7B%0A            throw new XFireRuntimeException(%22Couldn't load plexus embedder.%22, e);%0A        %7D%0A
     %7D%0A%0A    p
@@ -1537,30 +1537,8 @@
 () %0A
-    %09throws Exception%0A
     
@@ -2665,32 +2665,41 @@
 re()
- throws Exception%0A
+%0A    %7B%0A        try%0A    
     %7B%0A
+    
     
@@ -2745,16 +2745,173 @@
 ROLE );%0A
+        %7D%0A        catch (ComponentLookupException e)%0A        %7B%0A            throw new XFireRuntimeException(%22Couldn't lookup xfire component.%22, e);%0A        %7D%0A
     %7D%0A%0A%09

Fix breakage due to signature change on XFireFactory.