KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > common > BaseComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.common;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.DocumentFragment JavaDoc;
23
24 import javax.jbi.component.Component;
25 import javax.jbi.component.ComponentContext;
26 import javax.jbi.component.ComponentLifeCycle;
27 import javax.jbi.component.ServiceUnitManager;
28 import javax.jbi.messaging.MessageExchange;
29 import javax.jbi.messaging.MessageExchange.Role;
30 import javax.jbi.servicedesc.ServiceEndpoint;
31 import javax.resource.spi.work.WorkManager JavaDoc;
32
33 /**
34  * Base class for a component.
35  *
36  * @author Guillaume Nodet
37  * @version $Revision: 426415 $
38  * @since 3.0
39  */

40 public abstract class BaseComponent implements Component {
41
42     protected final transient Log logger = LogFactory.getLog(getClass());
43     
44     protected BaseLifeCycle lifeCycle;
45     protected Registry registry;
46     protected BaseServiceUnitManager serviceUnitManager;
47     
48     public BaseComponent() {
49         lifeCycle = createLifeCycle();
50         registry = createRegistry();
51         serviceUnitManager = createServiceUnitManager();
52     }
53     
54     /* (non-Javadoc)
55      * @see javax.jbi.component.Component#getLifeCycle()
56      */

57     public ComponentLifeCycle getLifeCycle() {
58         return lifeCycle;
59     }
60
61     /* (non-Javadoc)
62      * @see javax.jbi.component.Component#getServiceUnitManager()
63      */

64     public ServiceUnitManager getServiceUnitManager() {
65         return serviceUnitManager;
66     }
67
68     /* (non-Javadoc)
69      * @see javax.jbi.component.Component#getServiceDescription(javax.jbi.servicedesc.ServiceEndpoint)
70      */

71     public Document JavaDoc getServiceDescription(ServiceEndpoint endpoint) {
72         if (logger.isDebugEnabled()) {
73             logger.debug("Querying service description for " + endpoint);
74         }
75         String JavaDoc key = EndpointSupport.getKey(endpoint);
76         Endpoint ep = this.registry.getEndpoint(key);
77         if (ep != null) {
78             Document JavaDoc doc = ep.getDescription();
79             if (doc == null) {
80                 if (logger.isDebugEnabled()) {
81                     logger.debug("No description found for " + key);
82                 }
83             }
84             return doc;
85         } else {
86             if (logger.isDebugEnabled()) {
87                 logger.debug("No endpoint found for " + key);
88             }
89             return null;
90         }
91     }
92
93     /* (non-Javadoc)
94      * @see javax.jbi.component.Component#isExchangeWithConsumerOkay(javax.jbi.servicedesc.ServiceEndpoint, javax.jbi.messaging.MessageExchange)
95      */

96     public boolean isExchangeWithConsumerOkay(ServiceEndpoint endpoint, MessageExchange exchange) {
97         String JavaDoc key = EndpointSupport.getKey(endpoint);
98         Endpoint ep = this.registry.getEndpoint(key);
99         if (ep != null) {
100             if (ep.getRole() != Role.PROVIDER) {
101                 if (logger.isDebugEnabled()) {
102                     logger.debug("Endpoint " + key + " is a consumer. Refusing exchange with consumer.");
103                 }
104                 return false;
105             } else {
106                 return ep.isExchangeOkay(exchange);
107             }
108         } else {
109             if (logger.isDebugEnabled()) {
110                 logger.debug("No endpoint found for " + key + ". Refusing exchange with consumer.");
111             }
112             return false;
113         }
114     }
115
116     /* (non-Javadoc)
117      * @see javax.jbi.component.Component#isExchangeWithProviderOkay(javax.jbi.servicedesc.ServiceEndpoint, javax.jbi.messaging.MessageExchange)
118      */

119     public boolean isExchangeWithProviderOkay(ServiceEndpoint endpoint, MessageExchange exchange) {
120         // TODO: check if the selected endpoint is good for us
121
return true;
122     }
123
124     /* (non-Javadoc)
125      * @see javax.jbi.component.Component#resolveEndpointReference(org.w3c.dom.DocumentFragment)
126      */

127     public ServiceEndpoint resolveEndpointReference(DocumentFragment JavaDoc epr) {
128         return null;
129     }
130     
131     /**
132      * Create the life cycle object.
133      * Derived classes should override this method to be able to
134      * use a custom life cycle implementation.
135      *
136      * @return a life cycle object
137      */

138     protected BaseLifeCycle createLifeCycle() {
139         return new BaseLifeCycle(this);
140     }
141
142     /**
143      * Create the service unit manager.
144      * Derived classes should override this method and return a
145      * BaseServiceUnitManager so that the component is able to
146      * handle service unit deployment.
147      *
148      * @return a service unit manager
149      */

150     protected BaseServiceUnitManager createServiceUnitManager() {
151         return null;
152     }
153     
154     protected Registry createRegistry() {
155         return new Registry(this);
156     }
157
158     public ComponentContext getComponentContext() {
159         return lifeCycle.getContext();
160     }
161     
162     public String JavaDoc getComponentName() {
163         if (getComponentContext() == null) {
164             return "Component (" + getClass().getName() + ") not yet initialized";
165         }
166         return getComponentContext().getComponentName();
167     }
168     
169     public WorkManager JavaDoc getWorkManager() {
170         return lifeCycle.workManager;
171     }
172     
173     /**
174      * @return Returns the logger.
175      */

176     public Log getLogger() {
177         return logger;
178     }
179
180     /**
181      * @return Returns the registry.
182      */

183     public Registry getRegistry() {
184         return registry;
185     }
186     
187 }
188
Popular Tags