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    
021    import org.apache.cxf.Bus;
022    import org.apache.cxf.BusFactory;
023    import org.apache.cxf.bus.spring.SpringBusFactory;
024    import org.apache.servicemix.common.DefaultComponent;
025    import org.apache.servicemix.jbi.security.auth.AuthenticationService;
026    import org.apache.servicemix.jbi.security.auth.impl.JAASAuthenticationService;
027    
028    /**
029     * 
030     * @author gnodet
031     * @org.apache.xbean.XBean element="component" description="a JBI component for hosting endpoints that can use 
032     * either SOAP/HTTP or SOAP/JMS."
033     */
034    public class CxfBcComponent extends DefaultComponent {
035    
036        private CxfBcEndpointType[] endpoints;
037    
038        private Bus bus;
039    
040        private String busCfg;
041        
042        private CxfBcConfiguration configuration = new CxfBcConfiguration();
043        
044        /**
045         * @return the endpoints
046         */
047        public CxfBcEndpointType[] getEndpoints() {
048            return endpoints;
049        }
050    
051        /**
052        * Specifies the list of endpoints hosted by the component.
053         * @param endpoints
054         *            the endpoints to set
055         * @org.apache.xbean.Property description="the list of endpoints hosted by the component"
056         */
057        public void setEndpoints(CxfBcEndpointType[] endpoints) {
058            this.endpoints = endpoints;
059        }
060    
061        @Override
062        protected List getConfiguredEndpoints() {
063            return asList(endpoints);
064        }
065    
066        @Override
067        protected Class[] getEndpointClasses() {
068            return new Class[] {CxfBcProvider.class, CxfBcConsumer.class};
069        }
070    
071        @Override
072        protected void doInit() throws Exception {
073            if (getBusConfig() != null) {
074                SpringBusFactory bf = new SpringBusFactory();
075                bus = bf.createBus(getBusConfig());
076            } else {
077                bus = BusFactory.getDefaultBus();
078            }
079            if (getConfiguration().getAuthenticationService() == null) {
080                try {
081                    String name = getConfiguration().getAuthenticationServiceName();
082                    Object as = context.getNamingContext().lookup(name);
083                    getConfiguration().setAuthenticationService((AuthenticationService) as);
084                } catch (Throwable e) {
085                    getConfiguration().setAuthenticationService(new JAASAuthenticationService());
086                }
087            }
088            super.doInit();
089        }
090        
091        @Override
092        protected void doShutDown() throws Exception {
093            // Bus should no longer be the thread default since the component's threads will end now
094            if (bus != null) {
095                BusFactory.setThreadDefaultBus(null);
096            }
097            super.doShutDown();
098        }
099    
100        public Bus getBus() {
101            return bus;
102        }
103    
104        /**
105            * Specifies the location of the CXF configuraiton file used to configure
106            * the CXF bus. This allows you to access features like JMS runtime 
107            * behavior and WS-RM. The configuration set at the component level is
108            * superceeded by any configuration specified by an endpoint.
109            *
110            * @param busCfg a string containing the relative path to the configuration file
111            * @org.apache.xbean.Property description="the location of the CXF configuration file used to configure the CXF 
112            * bus for all endpoints in the container. Endpoint-specific configuration overrides these settings. This allows 
113            * you to configure features like WS-RM and JMS runtime behavior."
114            **/
115        public void setBusConfig(String busConfig) {
116            this.busCfg = busConfig;
117        }
118    
119        public String getBusConfig() {
120            return busCfg;
121        }
122    
123        public void setConfiguration(CxfBcConfiguration configuration) {
124            this.configuration = configuration;
125        }
126    
127        public CxfBcConfiguration getConfiguration() {
128            return configuration;
129        }
130    }