001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.common;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.apache.servicemix.executors.Executor;
022    import org.w3c.dom.Document;
023    import org.w3c.dom.DocumentFragment;
024    
025    import javax.jbi.component.ComponentContext;
026    import javax.jbi.component.ComponentLifeCycle;
027    import javax.jbi.component.ServiceUnitManager;
028    import javax.jbi.messaging.MessageExchange;
029    import javax.jbi.messaging.MessagingException;
030    import javax.jbi.messaging.MessageExchange.Role;
031    import javax.jbi.servicedesc.ServiceEndpoint;
032    import javax.xml.namespace.QName;
033    
034    /**
035     * Base class for a component.
036     * 
037     * @author Guillaume Nodet
038     * @version $Revision: 514613 $
039     * @since 3.0
040     */
041    public abstract class BaseComponent implements ServiceMixComponent {
042    
043        protected final transient Log logger = LogFactory.getLog(getClass());
044        
045        protected BaseLifeCycle lifeCycle;
046        protected Registry registry;
047        protected BaseServiceUnitManager serviceUnitManager;
048        
049        public BaseComponent() {
050            lifeCycle = createLifeCycle();
051            registry = createRegistry();
052            serviceUnitManager = createServiceUnitManager();
053        }
054        
055        /* (non-Javadoc)
056         * @see javax.jbi.component.Component#getLifeCycle()
057         */
058        public ComponentLifeCycle getLifeCycle() {
059            return lifeCycle;
060        }
061    
062        /* (non-Javadoc)
063         * @see javax.jbi.component.Component#getServiceUnitManager()
064         */
065        public ServiceUnitManager getServiceUnitManager() {
066            return serviceUnitManager;
067        }
068    
069        /* (non-Javadoc)
070         * @see javax.jbi.component.Component#getServiceDescription(javax.jbi.servicedesc.ServiceEndpoint)
071         */
072        public Document getServiceDescription(ServiceEndpoint endpoint) {
073            if (logger.isDebugEnabled()) {
074                logger.debug("Querying service description for " + endpoint);
075            }
076            String key = EndpointSupport.getKey(endpoint);
077            Endpoint ep = this.registry.getEndpoint(key);
078            if (ep != null) {
079                Document doc = ep.getDescription();
080                if (doc == null) {
081                    if (logger.isDebugEnabled()) {
082                        logger.debug("No description found for " + key);
083                    }
084                }
085                return doc;
086            } else {
087                if (logger.isDebugEnabled()) {
088                    logger.debug("No endpoint found for " + key);
089                }
090                return null;
091            }
092        }
093    
094        /* (non-Javadoc)
095         * @see javax.jbi.component.Component#isExchangeWithConsumerOkay(javax.jbi.servicedesc.ServiceEndpoint, javax.jbi.messaging.MessageExchange)
096         */
097        public boolean isExchangeWithConsumerOkay(ServiceEndpoint endpoint, MessageExchange exchange) {
098            String key = EndpointSupport.getKey(endpoint);
099            Endpoint ep = this.registry.getEndpoint(key);
100            if (ep != null) {
101                if (ep.getRole() != Role.PROVIDER) {
102                    if (logger.isDebugEnabled()) {
103                        logger.debug("Endpoint " + key + " is a consumer. Refusing exchange with consumer.");
104                    }
105                    return false;
106                } else {
107                    return ep.isExchangeOkay(exchange);
108                }
109            } else {
110                if (logger.isDebugEnabled()) {
111                    logger.debug("No endpoint found for " + key + ". Refusing exchange with consumer.");
112                }
113                return false;
114            }
115        }
116    
117        /* (non-Javadoc)
118         * @see javax.jbi.component.Component#isExchangeWithProviderOkay(javax.jbi.servicedesc.ServiceEndpoint, javax.jbi.messaging.MessageExchange)
119         */
120        public boolean isExchangeWithProviderOkay(ServiceEndpoint endpoint, MessageExchange exchange) {
121            // TODO: check if the selected endpoint is good for us
122            return true;
123        }
124    
125        /* (non-Javadoc)
126         * @see javax.jbi.component.Component#resolveEndpointReference(org.w3c.dom.DocumentFragment)
127         */
128        public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
129            return null;
130        }
131        
132        /**
133         * Create the life cycle object.
134         * Derived classes should override this method to be able to
135         * use a custom life cycle implementation.
136         * 
137         * @return a life cycle object
138         */
139        protected BaseLifeCycle createLifeCycle() {
140            return new BaseLifeCycle(this);
141        }
142    
143        /**
144         * Create the service unit manager.
145         * Derived classes should override this method and return a 
146         * BaseServiceUnitManager so that the component is able to 
147         * handle service unit deployment.
148         * 
149         * @return a service unit manager
150         */
151        protected BaseServiceUnitManager createServiceUnitManager() {
152            return null;
153        }
154    
155        protected Registry createRegistry() {
156            return new Registry(this);
157        }
158    
159        public ComponentContext getComponentContext() {
160            return lifeCycle.getContext();
161        }
162    
163        public String getComponentName() {
164            if (getComponentContext() == null) {
165                return "Component (" + getClass().getName() + ") not yet initialized";
166            }
167            return getComponentContext().getComponentName();
168        }
169    
170        /**
171         * @return Returns the logger.
172         */
173        public Log getLogger() {
174            return logger;
175        }
176    
177        /**
178         * @return Returns the registry.
179         */
180        public Registry getRegistry() {
181            return registry;
182        }
183    
184        /**
185         * Shortcut to retrieve this component's executor.
186         * 
187         * @return the executor for this component
188         */
189        public Executor getExecutor() {
190            return lifeCycle.getExecutor();
191        }
192    
193        public void sendConsumerExchange(MessageExchange exchange, Endpoint endpoint) throws MessagingException {
194            lifeCycle.sendConsumerExchange(exchange, endpoint);
195        }
196    
197        public void prepareConsumerExchange(MessageExchange exchange, Endpoint endpoint) throws MessagingException {
198            lifeCycle.prepareConsumerExchange(exchange, endpoint);
199        }
200    
201        public QName getEPRElementName() {
202            return null;
203        }
204    
205    }