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.components.util;
018    
019    import javax.jbi.JBIException;
020    import javax.jbi.component.Component;
021    import javax.jbi.component.ComponentContext;
022    import javax.jbi.component.ComponentLifeCycle;
023    import javax.jbi.component.ServiceUnitManager;
024    import javax.jbi.messaging.MessageExchange;
025    import javax.jbi.servicedesc.ServiceEndpoint;
026    import javax.xml.namespace.QName;
027    
028    import org.w3c.dom.Document;
029    import org.w3c.dom.DocumentFragment;
030    
031    /**
032     * A simple adaptor which can be used to turn any instance of
033     * a {@link ComponentLifeCycle} into a fully fledged JBI {@link Component}
034     *
035     * @version $Revision: 564374 $
036     */
037    public class ComponentAdaptor implements Component {
038    
039        private ComponentLifeCycle lifeCycle;
040        private QName service;
041        private String endpoint;
042        private ComponentContext context;
043        private ServiceUnitManager serviceManager;
044    
045    
046        public ComponentAdaptor(ComponentLifeCycle lifeCycle) {
047            this.lifeCycle = lifeCycle;
048        }
049    
050        public ComponentAdaptor(ComponentLifeCycle lifeCycle, QName service, String endpoint) {
051            this.lifeCycle = lifeCycle;
052            this.service = service;
053            this.endpoint = endpoint;
054        }
055    
056        /**
057         * Called when the Component is initialized
058         *
059         * @param ctx
060         * @throws javax.jbi.JBIException
061         */
062        public void init(ComponentContext ctx) throws JBIException {
063            this.context = ctx;
064            if (service != null && endpoint != null) {
065                ctx.activateEndpoint(service, endpoint);
066            }
067        }
068    
069        /**
070         * @return the lifecycel control implementation
071         */
072        public ComponentLifeCycle getLifeCycle() {
073            return lifeCycle;
074        }
075    
076        public ServiceUnitManager getServiceUnitManager() {
077            initializeServiceUnitManager();
078            return serviceManager;
079        }
080    
081        public void setServiceManager(ServiceUnitManager serviceManager) {
082            this.serviceManager = serviceManager;
083        }
084    
085    
086        public ComponentContext getContext() {
087            return context;
088        }
089    
090        public String toString() {
091            return getClass().getName() + " for " + lifeCycle;
092        }
093    
094        protected synchronized void initializeServiceUnitManager() {
095            if (this.serviceManager == null) {
096                this.serviceManager = createServiceUnitManager();
097            }
098        }
099    
100        protected ServiceUnitManager createServiceUnitManager() {
101            return new ServiceUnitManagerSupport();
102        }
103    
104    
105        public Document getServiceDescription(ServiceEndpoint ep) {
106            // TODO Auto-generated method stub
107            return null;
108        }
109    
110    
111        public boolean isExchangeWithConsumerOkay(ServiceEndpoint ep, MessageExchange exchange) {
112            // TODO Auto-generated method stub
113            return true;
114        }
115    
116    
117        public boolean isExchangeWithProviderOkay(ServiceEndpoint ep, MessageExchange exchange) {
118            // TODO Auto-generated method stub
119            return true;
120        }
121    
122    
123        public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
124            // TODO Auto-generated method stub
125            return null;
126        }
127    
128    }