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.osgi;
018    
019    import java.util.Dictionary;
020    import java.util.Properties;
021    import java.util.Collection;
022    import java.util.Map;
023    import java.util.HashMap;
024    
025    import org.apache.servicemix.common.Endpoint;
026    import org.apache.servicemix.jbi.deployer.DeployedAssembly;
027    import org.osgi.framework.BundleContext;
028    import org.springframework.osgi.context.BundleContextAware;
029    import org.springframework.beans.factory.InitializingBean;
030    import org.springframework.beans.factory.DisposableBean;
031    import org.springframework.context.ApplicationContextAware;
032    import org.springframework.context.ApplicationContext;
033    
034    public class EndpointExporter implements BundleContextAware, ApplicationContextAware, InitializingBean, DisposableBean, DeployedAssembly {
035    
036        private BundleContext bundleContext;
037        private ApplicationContext applicationContext;
038        private Collection<Endpoint> endpoints;
039    
040        public void setBundleContext(BundleContext bundleContext) {
041            this.bundleContext = bundleContext;
042        }
043    
044        public void setApplicationContext(ApplicationContext applicationContext) {
045            this.applicationContext = applicationContext;
046        }
047    
048        public void setEndpoints(Collection<Endpoint> endpoints) {
049            this.endpoints = endpoints;
050        }
051    
052        public Collection<Endpoint> getEndpoints() {
053            Collection<Endpoint> eps = this.endpoints;
054            if (eps == null) {
055                eps = this.applicationContext.getBeansOfType(Endpoint.class).values();
056            }
057            return eps;
058        }
059    
060        public String getName() {
061            return bundleContext.getBundle().getSymbolicName();
062        }
063    
064        public Map<String, String> getServiceUnits() {
065            Map<String, String> sus = new HashMap<String, String>();
066            for (Endpoint ep : getEndpoints()) {
067                sus.put(ep.getServiceUnit().getName(), ep.getServiceUnit().getComponent().getComponentName());
068            }
069            return sus;
070        }
071    
072        public void afterPropertiesSet() throws Exception {
073            Collection<Endpoint> eps = getEndpoints();
074            for (Endpoint ep : eps) {
075                EndpointWrapper wrapper = new EndpointWrapperImpl(ep, applicationContext.getClassLoader());
076                Dictionary props = new Properties();
077                bundleContext.registerService(EndpointWrapper.class.getName(), wrapper, props);
078            }
079            bundleContext.registerService(DeployedAssembly.class.getName(), this, new Properties());
080        }
081    
082        public void destroy() throws Exception {
083        }
084    }