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: 695836 $
039     * @since 3.0
040     */
041    @Deprecated
042    public abstract class BaseComponent implements ServiceMixComponent {
043    
044            protected final transient Log logger = LogFactory.getLog(getClass());
045        
046        protected BaseLifeCycle lifeCycle;
047        protected Registry registry;
048        protected BaseServiceUnitManager serviceUnitManager;
049        
050        public BaseComponent() {
051            lifeCycle = createLifeCycle();
052            registry = createRegistry();
053            serviceUnitManager = createServiceUnitManager();
054        }
055        
056        /* (non-Javadoc)
057         * @see javax.jbi.component.Component#getLifeCycle()
058         */
059        public ComponentLifeCycle getLifeCycle() {
060            return lifeCycle;
061        }
062    
063        /* (non-Javadoc)
064         * @see javax.jbi.component.Component#getServiceUnitManager()
065         */
066        public ServiceUnitManager getServiceUnitManager() {
067            return serviceUnitManager;
068        }
069    
070        /* (non-Javadoc)
071         * @see javax.jbi.component.Component#getServiceDescription(javax.jbi.servicedesc.ServiceEndpoint)
072         */
073        public Document getServiceDescription(ServiceEndpoint endpoint) {
074            if (logger.isDebugEnabled()) {
075                logger.debug("Querying service description for " + endpoint);
076            }
077            String key = EndpointSupport.getKey(endpoint);
078            Endpoint ep = this.registry.getEndpoint(key);
079            if (ep != null) {
080                Document doc = ep.getDescription();
081                if (doc == null) {
082                    if (logger.isDebugEnabled()) {
083                        logger.debug("No description found for " + key);
084                    }
085                }
086                return doc;
087            } else {
088                if (logger.isDebugEnabled()) {
089                    logger.debug("No endpoint found for " + key);
090                }
091                return null;
092            }
093        }
094    
095        /* (non-Javadoc)
096         * @see javax.jbi.component.Component#isExchangeWithConsumerOkay(javax.jbi.servicedesc.ServiceEndpoint, javax.jbi.messaging.MessageExchange)
097         */
098        public boolean isExchangeWithConsumerOkay(ServiceEndpoint endpoint, MessageExchange exchange) {
099            String key = EndpointSupport.getKey(endpoint);
100            Endpoint ep = this.registry.getEndpoint(key);
101            if (ep != null) {
102                if (ep.getRole() != Role.PROVIDER) {
103                    if (logger.isDebugEnabled()) {
104                        logger.debug("Endpoint " + key + " is a consumer. Refusing exchange with consumer.");
105                    }
106                    return false;
107                } else {
108                    return ep.isExchangeOkay(exchange);
109                }
110            } else {
111                if (logger.isDebugEnabled()) {
112                    logger.debug("No endpoint found for " + key + ". Refusing exchange with consumer.");
113                }
114                return false;
115            }
116        }
117    
118        /* (non-Javadoc)
119         * @see javax.jbi.component.Component#isExchangeWithProviderOkay(javax.jbi.servicedesc.ServiceEndpoint, javax.jbi.messaging.MessageExchange)
120         */
121        public boolean isExchangeWithProviderOkay(ServiceEndpoint endpoint, MessageExchange exchange) {
122            // TODO: check if the selected endpoint is good for us
123            return true;
124        }
125    
126        /* (non-Javadoc)
127         * @see javax.jbi.component.Component#resolveEndpointReference(org.w3c.dom.DocumentFragment)
128         */
129        public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
130            return null;
131        }
132        
133        /**
134         * Create the life cycle object.
135         * Derived classes should override this method to be able to
136         * use a custom life cycle implementation.
137         * 
138         * @return a life cycle object
139         */
140        protected BaseLifeCycle createLifeCycle() {
141            return new BaseLifeCycle(this);
142        }
143    
144        /**
145         * Create the service unit manager.
146         * Derived classes should override this method and return a 
147         * BaseServiceUnitManager so that the component is able to 
148         * handle service unit deployment.
149         * 
150         * @return a service unit manager
151         */
152        protected BaseServiceUnitManager createServiceUnitManager() {
153            return null;
154        }
155    
156        protected Registry createRegistry() {
157            return new Registry(this);
158        }
159    
160        public ComponentContext getComponentContext() {
161            return lifeCycle.getContext();
162        }
163    
164        public String getComponentName() {
165            if (getComponentContext() == null) {
166                return "Component (" + getClass().getName() + ") not yet initialized";
167            }
168            return getComponentContext().getComponentName();
169        }
170    
171        /**
172         * @return Returns the logger.
173         */
174        public Log getLogger() {
175            return logger;
176        }
177    
178        /**
179         * @return Returns the registry.
180         */
181        public Registry getRegistry() {
182            return registry;
183        }
184    
185        /**
186         * Shortcut to retrieve this component's executor.
187         * 
188         * @return the executor for this component
189         */
190        public Executor getExecutor() {
191            return lifeCycle.getExecutor();
192        }
193    
194        public Object getSmx3Container() {
195            return lifeCycle.getSmx3Container();
196        }
197    
198        public void prepareExchange(MessageExchange exchange, Endpoint endpoint) throws MessagingException {
199            lifeCycle.prepareExchange(exchange, endpoint);
200        }
201    
202        public QName getEPRElementName() {
203            return null;
204        }
205    
206        public Container getContainer() {
207            return lifeCycle.getContainer();
208        }
209    
210    
211    }