KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > jbi > se > CeltixServiceEngine


1 package org.objectweb.celtix.jbi.se;
2
3
4 import java.io.File JavaDoc;
5 import java.io.FilenameFilter JavaDoc;
6 import java.net.MalformedURLException JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.util.logging.Level JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10
11 import javax.jbi.JBIException;
12 import javax.jbi.component.Component;
13 import javax.jbi.component.ComponentContext;
14 import javax.jbi.component.ComponentLifeCycle;
15 import javax.jbi.component.ServiceUnitManager;
16 import javax.jbi.messaging.DeliveryChannel;
17 import javax.jbi.messaging.MessageExchange;
18 import javax.jbi.servicedesc.ServiceEndpoint;
19 import javax.management.ObjectName JavaDoc;
20
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.DocumentFragment JavaDoc;
23
24 import org.objectweb.celtix.Bus;
25 import org.objectweb.celtix.BusException;
26 import org.objectweb.celtix.jbi.transport.JBITransportFactory;
27
28
29 /** A JBI component. Initializes the Celtix JBI transport
30  */

31 public class CeltixServiceEngine implements ComponentLifeCycle, Component {
32     
33     public static final String JavaDoc JBI_TRANSPORT_ID = "http://celtix.object.org/transport/jbi";
34     private static final String JavaDoc CELTIX_CONFIG_FILE = "celtix-config.xml";
35     private static final String JavaDoc PROVIDER_PROP = "javax.xml.ws.spi.Provider";
36     
37     
38     private static final Logger JavaDoc LOG = Logger.getLogger(CeltixServiceEngine.class.getName());
39     
40     private ComponentContext ctx;
41     private CeltixServiceUnitManager suManager;
42     private Bus bus;
43    
44     public CeltixServiceEngine() {
45     }
46     
47     // Implementation of javax.jbi.component.ComponentLifeCycle
48

49     public final ObjectName JavaDoc getExtensionMBeanName() {
50         return null;
51     }
52     
53     public final void shutDown() throws JBIException {
54         LOG.fine("Shutting down CeltixServiceEngine");
55     }
56     
57     public final void init(final ComponentContext componentContext) throws JBIException {
58         
59         
60         try {
61             Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
62             
63             System.setProperty(PROVIDER_PROP, "org.objectweb.celtix.bus.jaxws.spi.ProviderImpl");
64             ctx = componentContext;
65             
66             File JavaDoc metaInfDir = new File JavaDoc(componentContext.getInstallRoot(), "META-INF");
67             File JavaDoc celtixConfig = new File JavaDoc(metaInfDir, CELTIX_CONFIG_FILE);
68             
69             if (celtixConfig.exists()) {
70                 System.setProperty("celtix.config.file", celtixConfig.toURL().toString());
71                 LOG.fine("set Celtix configuration to: " + System.getProperty("celtix.config.file"));
72             } else {
73                 LOG.severe("could not find Celtix configuration in " + metaInfDir);
74             }
75             
76             ComponentClassLoader loader = createClassLoader();
77             
78             initializeBus();
79             suManager = new CeltixServiceUnitManager(bus, componentContext, loader);
80             registerJBITransport(bus, suManager);
81             
82             LOG.info("Celtix Service Engine installation root:" + componentContext.getInstallRoot());
83             LOG.info("CeltixServiceEngine init complete");
84         } catch (Throwable JavaDoc ex) {
85             ex.printStackTrace();
86             LOG.log(Level.SEVERE, "failed to initilialize bus", ex);
87             throw new JBIException(ex);
88         }
89     }
90     
91     
92     private void initializeBus() throws JBIException {
93         
94         try {
95             LOG.fine("initialising bus");
96             bus = Bus.init();
97             LOG.fine("init complete");
98         } catch (Exception JavaDoc ex) {
99             LOG.log(Level.SEVERE, "bus initialization failed", ex);
100             throw new JBIException(ex);
101         }
102     }
103     
104     
105     public final void start() throws JBIException {
106         
107         try {
108             LOG.fine("CeltixServiceEngine starting");
109             DeliveryChannel chnl = ctx.getDeliveryChannel();
110             configureJBITransportFactory(chnl, suManager);
111             LOG.fine("CeltixServiceEngine startup complete");
112         } catch (BusException ex) {
113             throw new JBIException(ex);
114         }
115     }
116     
117     public final void stop() throws JBIException {
118         LOG.fine("CeltixServiceEngine stopped");
119     }
120     
121     // Implementation of javax.jbi.component.Component
122

123     public final ComponentLifeCycle getLifeCycle() {
124         LOG.fine("CeltixServiceEngine returning life cycle");
125         return this;
126     }
127     
128     public final ServiceUnitManager getServiceUnitManager() {
129         LOG.fine("CeltixServiceEngine return service unit manager");
130         return suManager;
131     }
132     
133     public final Document JavaDoc getServiceDescription(final ServiceEndpoint serviceEndpoint) {
134         Document JavaDoc doc = suManager.getServiceDescription(serviceEndpoint);
135         LOG.fine("CeltixServiceEngine returning service description: " + doc);
136         return doc;
137     }
138     
139     public final boolean isExchangeWithConsumerOkay(final ServiceEndpoint ep,
140                                                     final MessageExchange exchg) {
141         
142         LOG.fine("isExchangeWithConsumerOkay: endpoint: " + ep
143                  + " exchange: " + exchg);
144         return true;
145     }
146     
147     public final boolean isExchangeWithProviderOkay(final ServiceEndpoint ep,
148                                                     final MessageExchange exchng) {
149         LOG.fine("isExchangeWithConsumerOkay: endpoint: " + ep
150                  + " exchange: " + exchng);
151         return true;
152     }
153     
154     public final ServiceEndpoint resolveEndpointReference(final DocumentFragment JavaDoc documentFragment) {
155         return null;
156     }
157     
158     
159     private void configureJBITransportFactory(DeliveryChannel chnl, CeltixServiceUnitManager mgr)
160         throws BusException {
161         getTransportFactory().setDeliveryChannel(chnl);
162     }
163     
164     private void registerJBITransport(Bus argBus, CeltixServiceUnitManager mgr) throws JBIException {
165         try {
166            
167             getTransportFactory().init(argBus);
168             getTransportFactory().setServiceUnitManager(mgr);
169         } catch (Exception JavaDoc ex) {
170             throw new JBIException("failed to register JBI transport factory", ex);
171         }
172     }
173     
174     private JBITransportFactory getTransportFactory() throws BusException {
175         assert bus != null;
176         
177         try {
178             JBITransportFactory transportFactory =
179                 (JBITransportFactory)bus.getTransportFactoryManager()
180                     .getTransportFactory(JBI_TRANSPORT_ID);
181             
182             return transportFactory;
183         } catch (BusException ex) {
184             LOG.log(Level.SEVERE, "error initializing bus", ex);
185             throw ex;
186         }
187     }
188     
189     private ComponentClassLoader createClassLoader() throws JBIException {
190         
191         try {
192             
193             File JavaDoc root = new File JavaDoc(ctx.getInstallRoot());
194             File JavaDoc[] jars = root.listFiles(new FilenameFilter JavaDoc() {
195                 public boolean accept(File JavaDoc f, String JavaDoc name) {
196                     return name.endsWith(".jar");
197                 }
198             });
199             
200             URL JavaDoc urls[] = new URL JavaDoc[jars.length];
201             int i = 0;
202             for (File JavaDoc jar : jars) {
203                 urls[i] = jar.toURL();
204                 i++;
205             }
206             
207             return new ComponentClassLoader(urls, getClass().getClassLoader());
208         } catch (MalformedURLException JavaDoc ex) {
209             throw new JBIException("failed to construct component classloader", ex);
210         }
211     }
212 }
213
Popular Tags