KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > xquarebc > XQuareBC


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id : $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.xquarebc;
23
24 import java.io.File JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29
30 import javax.jbi.JBIException;
31 import javax.jbi.component.Component;
32 import javax.jbi.component.ComponentContext;
33 import javax.jbi.component.ComponentLifeCycle;
34 import javax.jbi.component.ServiceUnitManager;
35 import javax.jbi.messaging.DeliveryChannel;
36 import javax.jbi.messaging.MessageExchange;
37 import javax.jbi.servicedesc.ServiceEndpoint;
38 import javax.management.ObjectName JavaDoc;
39 import javax.xml.namespace.QName JavaDoc;
40
41 import org.objectweb.petals.binding.xquarebc.listeners.XQuareBCJBIProcessor;
42 import org.objectweb.petals.binding.xquarebc.listeners.XQuareBCListener;
43 import org.objectweb.petals.component.common.listener.MessageExchangeListener;
44 import org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager;
45 import org.objectweb.petals.component.common.util.ComponentLogger;
46 import org.objectweb.petals.component.common.util.XMLHelper;
47 import org.w3c.dom.Document JavaDoc;
48 import org.w3c.dom.DocumentFragment JavaDoc;
49 import org.w3c.dom.Node JavaDoc;
50
51 /**
52  * Represents an XQuare BC JBI Component.
53  * <p>
54  * Holds :
55  * <ul>
56  * <li>the XQuareBCJBIProcessor allowing to interact with the
57  * database (insert and query) using JBI messages,</li>
58  * <li>and the
59  * XQuareBCListener which may emit "new data" event-like JBI messages
60  * according to their configuration (see their javadoc for details).</li>
61  * </ul>
62  * When starting, registers a single XQuareSUHandler to handle service units
63  * deployed onto the XQuare BC.
64  *
65  * @version $Rev: 250 $Date: {date}
66  * @since Petals 1.0
67  * @author Marc Dutoo - Open Wide
68  *
69  */

70 public class XQuareBC implements Component, ComponentLifeCycle {
71
72     protected ComponentContext context;
73     protected Logger JavaDoc logger;
74     protected HashMap JavaDoc<String JavaDoc, Properties JavaDoc> serviceToPropertiesMap;
75     protected DeliveryChannel channel;
76     
77     protected PetalsServiceUnitManager suManager;
78     protected MessageExchangeListener jbiListener;
79
80     protected XQuareBCListener xquareListener;
81     protected XQuareSUHandler xquareSUHandler;
82     
83
84     public ObjectName JavaDoc getExtensionMBeanName() {
85         return null;
86     }
87
88     public ComponentLifeCycle getLifeCycle() {
89         return this;
90     }
91
92     public Document JavaDoc getServiceDescription(ServiceEndpoint arg0) {
93         return suManager.getServiceDescription(arg0);
94     }
95
96     public ServiceUnitManager getServiceUnitManager() {
97         return this.suManager;
98     }
99
100     public void init(ComponentContext context) throws JBIException {
101         try {
102             this.context = context;
103             Logger JavaDoc log = context.getLogger("", null);
104             logger = new ComponentLogger(log, log.getName(), log
105                 .getResourceBundleName(), context.getComponentName());
106
107             this.channel = context.getDeliveryChannel(); //TODO use Internal if not out only
108
this.serviceToPropertiesMap = new HashMap JavaDoc<String JavaDoc, Properties JavaDoc>();
109
110         } catch (Exception JavaDoc e) {
111             throw new JBIException(e);
112         }
113         logger.log(Level.INFO, "init");
114     }
115
116     public boolean isExchangeWithConsumerOkay(ServiceEndpoint arg0,
117         MessageExchange arg1) {
118         return true;
119     }
120
121     public boolean isExchangeWithProviderOkay(ServiceEndpoint arg0,
122         MessageExchange arg1) {
123         return true;
124     }
125
126     public ServiceEndpoint resolveEndpointReference(DocumentFragment JavaDoc arg0) {
127         ServiceEndpoint result = null;
128         if (arg0 != null) {
129             String JavaDoc prefix = arg0.getPrefix();
130             if (prefix == null) {
131                 prefix = new String JavaDoc();
132             } else {
133                 prefix += ":";
134             }
135             Node JavaDoc service = XMLHelper.findChild(arg0, prefix + "service", true);
136
137             if (service != null) {
138                 String JavaDoc serviceRequested = XMLHelper.getAttributeValue(service,
139                 "name");
140                 if (serviceRequested != null) {
141                     result = suManager.findEndpointForService(QName
142                         .valueOf(serviceRequested));
143                 }
144             }
145         }
146         return result;
147     }
148
149     public void shutDown() throws JBIException {
150         logger.log(Level.INFO, "shutDown");
151     }
152
153     public void start() throws JBIException {
154         logger.log(Level.INFO, "start");
155
156         XQuareBCJBIProcessor processor = new XQuareBCJBIProcessor(context, channel, logger,
157             serviceToPropertiesMap);
158         jbiListener = new MessageExchangeListener(channel, processor,
159             MessageExchangeListener.IgnoredStatus.DONE_AND_ERROR_IGNORED, 0);
160         jbiListener.listen(); // start listening for incoming JBI requests
161

162         xquareListener = new XQuareBCListener(context, channel, logger,
163             serviceToPropertiesMap, processor);
164         new Thread JavaDoc(xquareListener).start(); // start "listening" for new data in the db
165

166         xquareSUHandler = new XQuareSUHandler(context, logger,
167             serviceToPropertiesMap);
168         this.suManager = new PetalsServiceUnitManager(context, logger);
169         suManager.addNewHandler(xquareSUHandler);
170     }
171
172     public void stop() throws JBIException {
173         logger.log(Level.INFO, "stop");
174         jbiListener.terminate();
175         xquareListener.stopProcessing();
176     }
177
178     protected String JavaDoc getDirectoryRoot() {
179         String JavaDoc baseDir = context.getInstallRoot();
180         baseDir = baseDir.substring(0, baseDir.length() - 7);
181         baseDir += "work" + File.separator;
182         return baseDir;
183     }
184
185 }
186
Popular Tags