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.cxfbc;
018    
019    import java.util.List;
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import org.apache.cxf.Bus;
024    import org.apache.cxf.BusFactory;
025    import org.apache.cxf.bus.spring.SpringBusFactory;
026    import org.apache.servicemix.common.DefaultComponent;
027    
028    /**
029     * 
030     * @author gnodet
031     * @org.apache.xbean.XBean element="component" description="a JBI component for hosting endpoints that can use either SOAP/HTTP or SOAP/JMS."
032     */
033    public class CxfBcComponent extends DefaultComponent {
034    
035        private CxfBcEndpointType[] endpoints;
036    
037        private Bus bus;
038        
039        private Map<String, Bus> allBuses = new ConcurrentHashMap<String, Bus>();
040    
041        private String busCfg;
042        
043        private CxfBcConfiguration configuration = new CxfBcConfiguration();
044        
045        /**
046         * @return the endpoints
047         */
048        public CxfBcEndpointType[] getEndpoints() {
049            return endpoints;
050        }
051    
052        /**
053        * Specifies the list of endpoints hosted by the component.
054         * @param endpoints
055         *            the endpoints to set
056         * @org.apache.xbean.Property description="the list of endpoints hosted by the component"
057         */
058        public void setEndpoints(CxfBcEndpointType[] endpoints) {
059            this.endpoints = endpoints;
060        }
061    
062        @Override
063        protected List getConfiguredEndpoints() {
064            return asList(endpoints);
065        }
066    
067        @Override
068        protected Class[] getEndpointClasses() {
069            return new Class[] {CxfBcProvider.class, CxfBcConsumer.class};
070        }
071    
072        @Override
073        protected void doInit() throws Exception {
074            //Load configuration
075            configuration.setRootDir(context.getWorkspaceRoot());
076            configuration.setComponentName(context.getComponentName());
077            configuration.load();
078            if (configuration.getBusCfg() != null && configuration.getBusCfg().length() > 0) {
079                SpringBusFactory bf = new SpringBusFactory();
080                bus = bf.createBus(configuration.getBusCfg());
081            } else {
082                bus = BusFactory.newInstance().createBus();
083            }
084            if (getConfiguration().getAuthenticationService() == null) {
085                try {
086                    String name = getConfiguration().getAuthenticationServiceName();
087                    Object as = context.getNamingContext().lookup(name);
088                    getConfiguration().setAuthenticationService(as);
089                } catch (Throwable e) {
090                    try {
091                        Class cl = Class.forName("org.apache.servicemix.jbi.security.auth.impl.JAASAuthenticationService");
092                        getConfiguration().setAuthenticationService(cl.newInstance());
093                    } catch (Throwable t) {
094                        logger.warn("Unable to retrieve or create the authentication service");
095                    }
096                }
097            }
098            super.doInit();
099        }
100        
101        @Override
102        protected void doShutDown() throws Exception {
103            // Bus should no longer be the thread default since the component's threads will end now
104            if (bus != null) {
105                BusFactory.setThreadDefaultBus(null);
106            }
107            super.doShutDown();
108        }
109    
110        public Bus getBus() {
111            while (bus == null) {
112                //wait until bus get initialized
113                //espically when restart osgi container, endpoint bundle may retrieve 
114                //bus before the servicemix-cxf-bc bundle completely init bus
115                try {
116                    Thread.sleep(1000);
117                } catch (InterruptedException e) {
118                    
119                }
120            }
121            return bus;
122        }
123        
124        public Map<String, Bus> getAllBuses() {
125            return this.allBuses;
126        }
127    
128        /**
129            * Specifies the location of the CXF configuraiton file used to configure
130            * the CXF bus. This allows you to access features like JMS runtime 
131            * behavior and WS-RM. The configuration set at the component level is
132            * superceeded by any configuration specified by an endpoint.
133            *
134            * @param busCfg a string containing the relative path to the configuration file
135            * @org.apache.xbean.Property description="the location of the CXF configuration file used to configure the CXF bus for all endpoints in the container. Endpoint-specific configuration overrides these settings. This allows you to configure features like WS-RM and JMS runtime behavior."
136            **/
137        public void setBusConfig(String busConfig) {
138            this.busCfg = busConfig;
139        }
140    
141        public String getBusConfig() {
142            return busCfg;
143        }
144    
145        public void setConfiguration(CxfBcConfiguration configuration) {
146            this.configuration = configuration;
147        }
148    
149        public CxfBcConfiguration getConfiguration() {
150            return configuration;
151        }
152        
153        /**
154         * @return the authenticationService
155         */
156        public Object getAuthenticationService() {
157            return configuration.getAuthenticationService();
158        }
159    
160        /**
161         * @param authenticationService the authenticationService to set
162         * @org.apache.xbean.Property description="the authentication service object used by a component"
163         */
164        public void setAuthenticationService(Object authenticationService) {
165            this.configuration.setAuthenticationService(authenticationService);
166        }
167    
168    }