1 package org.codehaus.xfire.plexus;
2
3 import java.io.File;
4
5 import javax.servlet.ServletException;
6
7 import org.codehaus.xfire.XFire;
8 import org.codehaus.xfire.XFireFactory;
9 import org.codehaus.xfire.service.ServiceRegistry;
10 import org.codehaus.xfire.transport.TransportManager;
11 import org.codehaus.xfire.transport.http.XFireServlet;
12
13 /***
14 * Creates an embedded version of XFire within a servlet. For most
15 * applications this will probably be sufficient. For more advanced
16 * container usages, see the XFireServlet and Plexus documentation.
17 *
18 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
19 */
20 public class StandaloneXFireServlet
21 extends XFireServlet
22 {
23 XFireFactory factory;
24
25 private File webInfPath;
26
27 static
28 {
29
30 XFireFactory.registerFactory(PlexusXFireFactory.class, true);
31 }
32
33 public XFire getXFire() throws ServletException
34 {
35 try
36 {
37 return factory.getXFire();
38 }
39 catch (Exception e)
40 {
41 e.printStackTrace();
42 throw new ServletException("Couldn't find XFire service. Check configuration.", e);
43 }
44 }
45
46 public File getWebappBase()
47 {
48 if (webInfPath == null)
49 {
50 webInfPath = new File(getServletContext().getRealPath("/WEB-INF"));
51 }
52
53 return webInfPath;
54 }
55
56 public XFire createXFire() throws ServletException
57 {
58 File config = new File(getWebappBase(), getInitParameter("config"));
59
60 System.setProperty("xfire.config", config.getAbsolutePath());
61 System.out.println("Configuration file: " + config.getAbsolutePath());
62
63 String plexusConfig = getInitParameter("plexus-config");
64 if ( plexusConfig != null )
65 {
66 System.setProperty("xfire.plexusConfig", plexusConfig);
67 }
68
69 try
70 {
71 return XFireFactory.newInstance().getXFire();
72 }
73 catch (Exception e)
74 {
75 e.printStackTrace();
76 throw new ServletException("Couldn't start XFire service. Check configuration.", e);
77 }
78 }
79
80 /***
81 * @return
82 * @throws Exception
83 */
84 protected TransportManager getTransportManager()
85 throws ServletException
86 {
87 try
88 {
89 return factory.getXFire().getTransportManager();
90 }
91 catch (Exception e)
92 {
93 throw new ServletException("No transport service found!", e);
94 }
95 }
96
97 public ServiceRegistry getServiceRegistry()
98 throws ServletException
99 {
100 try
101 {
102 return factory.getXFire().getServiceRegistry();
103 }
104 catch (Exception e)
105 {
106 throw new ServletException("No service registry found!", e);
107 }
108 }
109
110 public void destroy()
111 {
112 factory = null;
113
114 super.destroy();
115 }
116 }