001: package org.objectweb.celtix.jbi.se;
002:
003: import java.io.File;
004: import java.io.FilenameFilter;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.util.logging.Level;
008: import java.util.logging.Logger;
009:
010: import javax.jbi.JBIException;
011: import javax.jbi.component.Component;
012: import javax.jbi.component.ComponentContext;
013: import javax.jbi.component.ComponentLifeCycle;
014: import javax.jbi.component.ServiceUnitManager;
015: import javax.jbi.messaging.DeliveryChannel;
016: import javax.jbi.messaging.MessageExchange;
017: import javax.jbi.servicedesc.ServiceEndpoint;
018: import javax.management.ObjectName;
019:
020: import org.w3c.dom.Document;
021: import org.w3c.dom.DocumentFragment;
022:
023: import org.objectweb.celtix.Bus;
024: import org.objectweb.celtix.BusException;
025: import org.objectweb.celtix.jbi.transport.JBITransportFactory;
026:
027: /** A JBI component. Initializes the Celtix JBI transport
028: */
029: public class CeltixServiceEngine implements ComponentLifeCycle,
030: Component {
031:
032: public static final String JBI_TRANSPORT_ID = "http://celtix.object.org/transport/jbi";
033: private static final String CELTIX_CONFIG_FILE = "celtix-config.xml";
034: private static final String PROVIDER_PROP = "javax.xml.ws.spi.Provider";
035:
036: private static final Logger LOG = Logger
037: .getLogger(CeltixServiceEngine.class.getName());
038:
039: private ComponentContext ctx;
040: private CeltixServiceUnitManager suManager;
041: private Bus bus;
042:
043: public CeltixServiceEngine() {
044: }
045:
046: // Implementation of javax.jbi.component.ComponentLifeCycle
047:
048: public final ObjectName getExtensionMBeanName() {
049: return null;
050: }
051:
052: public final void shutDown() throws JBIException {
053: LOG.fine("Shutting down CeltixServiceEngine");
054: }
055:
056: public final void init(final ComponentContext componentContext)
057: throws JBIException {
058:
059: try {
060: Thread.currentThread().setContextClassLoader(
061: getClass().getClassLoader());
062:
063: System.setProperty(PROVIDER_PROP,
064: "org.objectweb.celtix.bus.jaxws.spi.ProviderImpl");
065: ctx = componentContext;
066:
067: File metaInfDir = new File(componentContext
068: .getInstallRoot(), "META-INF");
069: File celtixConfig = new File(metaInfDir, CELTIX_CONFIG_FILE);
070:
071: if (celtixConfig.exists()) {
072: System.setProperty("celtix.config.file", celtixConfig
073: .toURL().toString());
074: LOG.fine("set Celtix configuration to: "
075: + System.getProperty("celtix.config.file"));
076: } else {
077: LOG.severe("could not find Celtix configuration in "
078: + metaInfDir);
079: }
080:
081: ComponentClassLoader loader = createClassLoader();
082:
083: initializeBus();
084: suManager = new CeltixServiceUnitManager(bus,
085: componentContext, loader);
086: registerJBITransport(bus, suManager);
087:
088: LOG.info("Celtix Service Engine installation root:"
089: + componentContext.getInstallRoot());
090: LOG.info("CeltixServiceEngine init complete");
091: } catch (Throwable ex) {
092: ex.printStackTrace();
093: LOG.log(Level.SEVERE, "failed to initilialize bus", ex);
094: throw new JBIException(ex);
095: }
096: }
097:
098: private void initializeBus() throws JBIException {
099:
100: try {
101: LOG.fine("initialising bus");
102: bus = Bus.init();
103: LOG.fine("init complete");
104: } catch (Exception ex) {
105: LOG.log(Level.SEVERE, "bus initialization failed", ex);
106: throw new JBIException(ex);
107: }
108: }
109:
110: public final void start() throws JBIException {
111:
112: try {
113: LOG.fine("CeltixServiceEngine starting");
114: DeliveryChannel chnl = ctx.getDeliveryChannel();
115: configureJBITransportFactory(chnl, suManager);
116: LOG.fine("CeltixServiceEngine startup complete");
117: } catch (BusException ex) {
118: throw new JBIException(ex);
119: }
120: }
121:
122: public final void stop() throws JBIException {
123: LOG.fine("CeltixServiceEngine stopped");
124: }
125:
126: // Implementation of javax.jbi.component.Component
127:
128: public final ComponentLifeCycle getLifeCycle() {
129: LOG.fine("CeltixServiceEngine returning life cycle");
130: return this ;
131: }
132:
133: public final ServiceUnitManager getServiceUnitManager() {
134: LOG.fine("CeltixServiceEngine return service unit manager");
135: return suManager;
136: }
137:
138: public final Document getServiceDescription(
139: final ServiceEndpoint serviceEndpoint) {
140: Document doc = suManager.getServiceDescription(serviceEndpoint);
141: LOG.fine("CeltixServiceEngine returning service description: "
142: + doc);
143: return doc;
144: }
145:
146: public final boolean isExchangeWithConsumerOkay(
147: final ServiceEndpoint ep, final MessageExchange exchg) {
148:
149: LOG.fine("isExchangeWithConsumerOkay: endpoint: " + ep
150: + " exchange: " + exchg);
151: return true;
152: }
153:
154: public final boolean isExchangeWithProviderOkay(
155: final ServiceEndpoint ep, final MessageExchange exchng) {
156: LOG.fine("isExchangeWithConsumerOkay: endpoint: " + ep
157: + " exchange: " + exchng);
158: return true;
159: }
160:
161: public final ServiceEndpoint resolveEndpointReference(
162: final DocumentFragment documentFragment) {
163: return null;
164: }
165:
166: private void configureJBITransportFactory(DeliveryChannel chnl,
167: CeltixServiceUnitManager mgr) throws BusException {
168: getTransportFactory().setDeliveryChannel(chnl);
169: }
170:
171: private void registerJBITransport(Bus argBus,
172: CeltixServiceUnitManager mgr) throws JBIException {
173: try {
174:
175: getTransportFactory().init(argBus);
176: getTransportFactory().setServiceUnitManager(mgr);
177: } catch (Exception ex) {
178: throw new JBIException(
179: "failed to register JBI transport factory", ex);
180: }
181: }
182:
183: private JBITransportFactory getTransportFactory()
184: throws BusException {
185: assert bus != null;
186:
187: try {
188: JBITransportFactory transportFactory = (JBITransportFactory) bus
189: .getTransportFactoryManager().getTransportFactory(
190: JBI_TRANSPORT_ID);
191:
192: return transportFactory;
193: } catch (BusException ex) {
194: LOG.log(Level.SEVERE, "error initializing bus", ex);
195: throw ex;
196: }
197: }
198:
199: private ComponentClassLoader createClassLoader()
200: throws JBIException {
201:
202: try {
203:
204: File root = new File(ctx.getInstallRoot());
205: File[] jars = root.listFiles(new FilenameFilter() {
206: public boolean accept(File f, String name) {
207: return name.endsWith(".jar");
208: }
209: });
210:
211: URL urls[] = new URL[jars.length];
212: int i = 0;
213: for (File jar : jars) {
214: urls[i] = jar.toURL();
215: i++;
216: }
217:
218: return new ComponentClassLoader(urls, getClass()
219: .getClassLoader());
220: } catch (MalformedURLException ex) {
221: throw new JBIException(
222: "failed to construct component classloader", ex);
223: }
224: }
225: }
|