KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > component > context > ComponentContextImpl


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.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: ComponentContextImpl.java 1191 2006-11-02 11:16:29Z msauvage $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.component.context;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.MissingResourceException JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import javax.jbi.JBIException;
32 import javax.jbi.component.Component;
33 import javax.jbi.management.MBeanNames;
34 import javax.jbi.messaging.DeliveryChannel;
35 import javax.jbi.messaging.MessagingException;
36 import javax.jbi.servicedesc.ServiceEndpoint;
37 import javax.management.MBeanServer JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.xml.namespace.QName JavaDoc;
40
41 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle;
42 import org.objectweb.petals.jbi.management.service.EndpointService;
43 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService;
44 import org.objectweb.petals.jbi.messaging.DeliveryChannelImpl;
45 import org.objectweb.petals.jbi.registry.AbstractEndpoint;
46 import org.objectweb.petals.jbi.registry.ConsumerEndpoint;
47 import org.objectweb.petals.jbi.registry.InternalEndpoint;
48 import org.objectweb.petals.jbi.routing.Router;
49 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor;
50 import org.objectweb.petals.util.PetalsRuntimeException;
51 import org.objectweb.petals.util.StringHelper;
52 import org.w3c.dom.Document JavaDoc;
53 import org.w3c.dom.DocumentFragment JavaDoc;
54
55 /**
56  * @version $Revision: 1191 $ $Date: 2006-08-04 12:11:16 +0200 (ven., 04 août
57  * 2006) $
58  * @since Petals 1.0
59  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
60  * @author wjoseph - eBMWebsourcing
61  */

62 public class ComponentContextImpl implements
63         javax.jbi.component.ComponentContext {
64
65     protected ConsumerEndpoint address;
66
67     protected MBeanNames beanNames;
68
69     protected MBeanServer JavaDoc beanServer;
70
71     protected DeliveryChannelImpl deliveryChannel;
72
73     protected JBIDescriptor descriptor;
74
75     /**
76      * The endpoint service is used to resolved the interface name in a
77      * distributed context
78      */

79     protected EndpointService endpointService;
80
81     /**
82      * references of registered external serviceEndpoints
83      */

84     protected Set JavaDoc<ServiceEndpoint> externalEndpoints;
85
86     protected String JavaDoc installationRoot;
87
88     /**
89      * references of registered internal serviceEndpoints
90      */

91     protected Set JavaDoc<AbstractEndpoint> internalEndpoints;
92
93     protected Component jbiComponent;
94
95     /**
96      * Monolog Logger instance.
97      */

98     protected org.objectweb.util.monolog.api.Logger logger;
99
100     protected LifeCycleManagerService manager;
101
102     protected Logger JavaDoc rootLogger;
103
104     protected String JavaDoc workspaceRoot;
105
106     protected InitialContext JavaDoc namingContext;
107
108     /**
109      * The container router instance.
110      */

111     private Router router;
112
113     public ComponentContextImpl() {
114     }
115
116     /**
117      * Constructor of the ComponentContextImpl class
118      *
119      * @param descriptor
120      * component descriptor
121      * @param installationRoot
122      * Path to the component installation root
123      * @param workspaceRoot
124      * path to the component workspace
125      * @param beanNames
126      * @param beanServer
127      * @param manager
128      * current lifeCycleManagerService
129      * @param address
130      * consumer endpoint
131      * @param endpointService
132      * @param logger
133      * InstallationService monolog logger
134      * @param router
135      * Petals router
136      */

137     public ComponentContextImpl(JBIDescriptor descriptor,
138             String JavaDoc installationRoot, String JavaDoc workspaceRoot,
139             MBeanNames beanNames, MBeanServer JavaDoc beanServer,
140             LifeCycleManagerService manager, ConsumerEndpoint address,
141             EndpointService endpointService,
142             org.objectweb.util.monolog.api.Logger logger, Router router,
143             InitialContext JavaDoc namingContext) {
144         super();
145         if (installationRoot == null) {
146             throw new IllegalArgumentException JavaDoc(
147                     "installationRoot must not be null");
148         } else {
149             if (installationRoot.length() == 0) {
150                 throw new IllegalArgumentException JavaDoc(
151                         "installationRoot must not be empty String");
152             }
153         }
154         if (beanNames == null) {
155             throw new IllegalArgumentException JavaDoc("beanNames must not be null");
156         }
157         if (beanServer == null) {
158             throw new IllegalArgumentException JavaDoc("beanServer must not be null");
159         }
160         if (workspaceRoot == null) {
161             throw new IllegalArgumentException JavaDoc("workspaceRoot must not be null");
162         } else {
163             if (workspaceRoot.length() == 0) {
164                 throw new IllegalArgumentException JavaDoc(
165                         "workspaceRoot must not be empty String");
166             }
167         }
168         this.descriptor = descriptor;
169         this.installationRoot = installationRoot;
170         this.beanNames = beanNames;
171         this.beanServer = beanServer;
172         this.manager = manager;
173         this.address = address;
174         this.internalEndpoints = new HashSet JavaDoc<AbstractEndpoint>();
175         this.externalEndpoints = new HashSet JavaDoc<ServiceEndpoint>();
176         this.endpointService = endpointService;
177         this.logger = logger;
178         this.router = router;
179         this.namingContext = namingContext;
180     }
181
182     /**
183      * @see javax.jbi.component.ComponentContext#activateEndpoint(javax.xml.namespace.QName,
184      * java.lang.String)
185      */

186     public ServiceEndpoint activateEndpoint(QName JavaDoc serviceName,
187             String JavaDoc endpointName) throws JBIException {
188         if (serviceName == null) {
189             throw new IllegalArgumentException JavaDoc("serviceName must be non-null");
190         }
191         if (endpointName == null) {
192             throw new IllegalArgumentException JavaDoc("endpointName must be non-null");
193         }
194         InternalEndpoint se = endpointService.activateEndpoint(serviceName,
195                 endpointName, address);
196         internalEndpoints.add(se);
197         return se;
198     }
199
200     /**
201      * @see javax.jbi.component.ComponentContext#deactivateEndpoint(javax.jbi.servicedesc.ServiceEndpoint)
202      */

203     public void deactivateEndpoint(ServiceEndpoint endpoint)
204         throws JBIException {
205         if (endpoint == null) {
206             throw new IllegalArgumentException JavaDoc("endpoint must be non-null");
207         }
208         endpointService.deactivateEndpoint(endpoint);
209         internalEndpoints.remove(endpoint);
210     }
211
212     /**
213      * deregister all endpoints (internal and external)
214      *
215      */

216     public void deregisterAllEndpoints() throws JBIException {
217         for (AbstractEndpoint se : internalEndpoints) {
218             endpointService.deactivateEndpoint(se);
219             internalEndpoints.remove(se);
220         }
221         for (ServiceEndpoint se : externalEndpoints) {
222             endpointService.deregisterExternalEndpoint(se);
223             externalEndpoints.remove(se);
224         }
225     }
226
227     /**
228      * @see javax.jbi.component.ComponentContext#deregisterExternalEndpoint(javax.jbi.servicedesc.ServiceEndpoint)
229      */

230     public void deregisterExternalEndpoint(ServiceEndpoint externalEndpoint)
231         throws JBIException {
232         endpointService.deregisterExternalEndpoint(externalEndpoint);
233         externalEndpoints.remove(externalEndpoint);
234     }
235
236     public ConsumerEndpoint getAddress() {
237         return address;
238     }
239
240     public Component getComponent() {
241         return jbiComponent;
242     }
243
244     /**
245      * @see javax.jbi.component.ComponentContext#getComponentName()
246      */

247     public String JavaDoc getComponentName() {
248         return descriptor.getComponent().getIdentification().getName();
249     }
250
251     /**
252      * @see javax.jbi.component.ComponentContext#getDeliveryChannel()
253      */

254     public DeliveryChannel getDeliveryChannel() throws MessagingException {
255         if (deliveryChannel == null) {
256             deliveryChannel = createDeliveryChannel();
257         } else {
258             if (deliveryChannel.isOpened()) {
259                 throw new MessagingException(
260                         "The previous DeliveryChannel is still opened");
261             } else {
262                 deliveryChannel = createDeliveryChannel();
263             }
264         }
265         return deliveryChannel;
266     }
267
268     public DeliveryChannelImpl getDeliveryChannelImpl() {
269         return deliveryChannel;
270     }
271
272     /**
273      * @see javax.jbi.component.ComponentContext#getEndpoint(javax.xml.namespace.QName,
274      * java.lang.String)
275      */

276     public ServiceEndpoint getEndpoint(QName JavaDoc service, String JavaDoc name) {
277         if (service == null) {
278             throw new IllegalArgumentException JavaDoc("service must not be null");
279         }
280         if (name == null) {
281             throw new IllegalArgumentException JavaDoc("name must not be null");
282         }
283         return endpointService.getEndpoint(service, name, false);
284     }
285
286     /**
287      * @see javax.jbi.component.ComponentContext#getEndpointDescriptor(javax.jbi.servicedesc.ServiceEndpoint)
288      */

289     public Document JavaDoc getEndpointDescriptor(ServiceEndpoint endpoint)
290         throws JBIException {
291         Document JavaDoc result = null;
292         if (endpoint == null) {
293             throw new IllegalArgumentException JavaDoc("endpoint must not be null");
294         }
295         if (getEndpoint(endpoint.getServiceName(), endpoint.getEndpointName()) != null) {
296             /*
297              * Checks if the endpoint exist and if it is activated
298              */

299             AbstractEndpoint abstractEndpoint = (AbstractEndpoint) endpoint;
300             if (abstractEndpoint.getEndpointService() == null) {
301                 abstractEndpoint.setEndpointService(endpointService);
302             }
303             result = abstractEndpoint.getEndpointDescriptor();
304         }
305         return result;
306     }
307
308     /**
309      * @see javax.jbi.component.ComponentContext#getEndpoints(javax.xml.namespace.QName)
310      */

311     public ServiceEndpoint[] getEndpoints(QName JavaDoc interfaceName) {
312         return endpointService.getInternalEndpointsForInterface(interfaceName);
313     }
314
315     /**
316      * @see javax.jbi.component.ComponentContext#getEndpointsForService(javax.xml.namespace.QName)
317      */

318     public ServiceEndpoint[] getEndpointsForService(QName JavaDoc serviceName) {
319         if (serviceName == null) {
320             throw new IllegalArgumentException JavaDoc("Service name must be non null");
321         }
322         return endpointService.getInternalEndpointsForService(serviceName);
323     }
324
325     /**
326      * @see javax.jbi.component.ComponentContext#getExternalEndpoints(javax.xml.namespace.QName)
327      */

328     public ServiceEndpoint[] getExternalEndpoints(QName JavaDoc interfaceName) {
329         if (interfaceName == null) {
330             throw new IllegalArgumentException JavaDoc(
331                     "Interface name must be non null");
332         }
333         return endpointService.getExternalEndpointsForInterface(interfaceName);
334     }
335
336     /**
337      * @see javax.jbi.component.ComponentContext#getExternalEndpointsForService(javax.xml.namespace.QName)
338      */

339     public ServiceEndpoint[] getExternalEndpointsForService(QName JavaDoc serviceName) {
340         if (serviceName == null) {
341             throw new IllegalArgumentException JavaDoc("Service name must be non null");
342         }
343         return endpointService.getExternalEndpointsForService(serviceName);
344     }
345
346     /**
347      * @see javax.jbi.component.ComponentContext#getInstallRoot()
348      */

349     public String JavaDoc getInstallRoot() {
350         if (StringHelper.isEmpty(installationRoot)) {
351             throw new PetalsRuntimeException(
352                     "Installation root must be non-null and non empty "
353                             + "for a correctly installed component. Check component installation !");
354         }
355         return installationRoot;
356     }
357
358     /**
359      * @see javax.jbi.component.ComponentContext#getLogger(java.lang.String,
360      * java.lang.String)
361      */

362     public Logger JavaDoc getLogger(String JavaDoc suffix, String JavaDoc resourceBundleName)
363         throws MissingResourceException JavaDoc, JBIException {
364         String JavaDoc loggerName = rootLogger.getName();
365         if (suffix == null) {
366             throw new IllegalArgumentException JavaDoc("suffix must not be null");
367         } else {
368             if (suffix.length() > 0) {
369                 loggerName += "." + suffix;
370             }
371         }
372         Logger JavaDoc l = Logger.getLogger(loggerName, resourceBundleName);
373         if (l == null) {
374             l = Logger.getAnonymousLogger();
375         }
376         if (l == null) {
377             throw new JBIException("No logger can be created");
378         }
379         return l;
380
381     }
382
383     /**
384      * @see javax.jbi.component.ComponentContext#getMBeanNames()
385      */

386     public MBeanNames getMBeanNames() {
387         return beanNames;
388     }
389
390     /**
391      * @see javax.jbi.component.ComponentContext#getMBeanServer()
392      */

393     public MBeanServer JavaDoc getMBeanServer() {
394         return beanServer;
395     }
396
397     /**
398      * @see javax.jbi.component.ComponentContext#getNamingContext()
399      */

400     public InitialContext JavaDoc getNamingContext() {
401         return namingContext;
402     }
403
404     /**
405      * TODO not implemented
406      *
407      * @see javax.jbi.component.ComponentContext#getTransactionManager()
408      */

409     public Object JavaDoc getTransactionManager() {
410         throw new NoSuchMethodError JavaDoc("not yet implemented in Petals");
411     }
412
413     /**
414      * @see javax.jbi.component.ComponentContext#getWorkspaceRoot()
415      */

416     public String JavaDoc getWorkspaceRoot() {
417         return workspaceRoot;
418     }
419
420     /**
421      * @see javax.jbi.component.ComponentContext#registerExternalEndpoint(javax.jbi.servicedesc.ServiceEndpoint)
422      */

423     public void registerExternalEndpoint(ServiceEndpoint externalEndpoint)
424         throws JBIException {
425         if (externalEndpoint == null) {
426             throw new IllegalArgumentException JavaDoc(
427                     "externalEndpoint must not be null");
428         }
429         endpointService.registerExternalEndpoint(externalEndpoint);
430         externalEndpoints.add(externalEndpoint);
431     }
432
433     // //////////////////////////////////////////////////////
434
// Implementation specific
435
// //////////////////////////////////////////////////////
436

437     /**
438      * TODO resolveEndpointReference : refactor
439      *
440      * @see javax.jbi.component.ComponentContext#resolveEndpointReference(org.w3c.dom.DocumentFragment)
441      */

442     public ServiceEndpoint resolveEndpointReference(DocumentFragment JavaDoc epr) {
443         if (epr == null) {
444             throw new IllegalArgumentException JavaDoc("epr must not be null");
445         }
446         ServiceEndpoint result = null;
447         // Search for all engines components if one can resolve the EPR.
448
Map JavaDoc engines = manager.getEngineCompoLifeCycles();
449
450         synchronized (engines) {
451             for (Iterator JavaDoc iter = engines.values().iterator(); iter.hasNext()
452                     && result == null;) {
453                 ComponentLifeCycle lifeCycle = (ComponentLifeCycle) iter.next();
454
455                 result = lifeCycle.getComponent().resolveEndpointReference(epr);
456             }
457         }
458
459         if (result == null) {
460             // Search for all binding components if one can resolve the EPR.
461
Map JavaDoc bindings = manager.getBindingCompoLifeCycles();
462
463             synchronized (bindings) {
464                 for (Iterator JavaDoc iter = bindings.values().iterator(); iter
465                         .hasNext()
466                         && result == null;) {
467                     ComponentLifeCycle lifeCycle = (ComponentLifeCycle) iter
468                             .next();
469
470                     result = lifeCycle.getComponent().resolveEndpointReference(
471                             epr);
472                 }
473             }
474         }
475         return result;
476
477     }
478
479     public void setComponent(Component component) {
480         this.jbiComponent = component;
481     }
482
483     public void setDeliveryChannel(DeliveryChannelImpl deliveryChannel) {
484         this.deliveryChannel = deliveryChannel;
485     }
486
487     public void setRootLogger(Logger JavaDoc logger) {
488         rootLogger = logger;
489     }
490
491     protected DeliveryChannelImpl createDeliveryChannel() {
492         return new DeliveryChannelImpl(this, router, logger);
493     }
494     
495     public JBIDescriptor getJBIDescriptor() {
496         return this.descriptor;
497     }
498
499 }
500
Popular Tags