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.camel.component.cxf.cxfbean;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import javax.jws.WebService;
023    
024    import org.apache.camel.component.cxf.CxfHeaderFilterStrategy;
025    import org.apache.camel.impl.ProcessorEndpoint;
026    import org.apache.camel.spi.HeaderFilterStrategy;
027    import org.apache.camel.spi.HeaderFilterStrategyAware;
028    import org.apache.camel.util.CamelContextHelper;
029    import org.apache.cxf.Bus;
030    import org.apache.cxf.BusFactory;
031    import org.apache.cxf.endpoint.Server;
032    import org.apache.cxf.feature.LoggingFeature;
033    import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
034    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
035    import org.apache.cxf.transport.ConduitInitiatorManager;
036    import org.apache.cxf.transport.DestinationFactoryManager;
037    
038    /**
039     * CXF Bean Endpoint is a {@link ProcessorEndpoint} which associated with 
040     * a {@link CxfBeanDestination}.  It delegates the processing of Camel 
041     * Exchanges to the associated CxfBeanDestination.
042     *  
043     * @version $Revision: 19197 $
044     */
045    public class CxfBeanEndpoint extends ProcessorEndpoint implements HeaderFilterStrategyAware {
046        private static final String URI_PREFIX = "cxfbean";
047        private Server server;
048        private Bus bus;
049        private boolean isSetDefaultBus;
050        private CxfBeanBinding cxfBeanBinding = new DefaultCxfBeanBinding();
051        private HeaderFilterStrategy headerFilterStrategy = new CxfHeaderFilterStrategy();
052        private boolean loggingFeatureEnabled;
053        private boolean populateFromClass = true;
054    
055        public CxfBeanEndpoint(String remaining, CxfBeanComponent component) {
056            super(remaining, component);
057        }
058        
059        public void stop() {
060            server.stop();
061        }
062        
063        public void start() {
064            server.start();
065        }
066    
067        @SuppressWarnings("unchecked")
068        public void init() {
069            Object obj = CamelContextHelper.mandatoryLookup(getCamelContext(), getEndpointUri());
070            
071            List<Object> serviceBeans;
072            if (obj instanceof List) {
073                serviceBeans = (List)obj;
074            } else {
075                serviceBeans = new ArrayList<Object>();
076                serviceBeans.add(obj);
077            }
078            
079            if (bus == null) {
080                bus = BusFactory.newInstance().createBus();
081            }
082            
083            if (isSetDefaultBus) {
084                BusFactory.setDefaultBus(bus);
085            }
086            
087            registerTransportFactory((CxfBeanComponent)this.getComponent());       
088            
089            createServer(serviceBeans);
090        }
091        
092        @Override
093        protected String createEndpointUri() {
094            return URI_PREFIX + ":" + getEndpointUri();
095        }
096        
097        private void createServer(List<Object> serviceBeans) {
098            Object obj = serviceBeans.get(0).getClass().getAnnotation(WebService.class);
099    
100            if (obj != null) {
101                JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
102                bean.setTransportId(CxfBeanTransportFactory.TRANSPORT_ID);
103                bean.setServiceClass(serviceBeans.get(0).getClass());
104                if (bean.getServiceFactory() != null) {
105                    bean.getServiceFactory().setPopulateFromClass(isPopulateFromClass());
106                }
107                bean.setBus(bus);
108                bean.setStart(true);
109                bean.setAddress("camel://" + createEndpointUri());
110                if (loggingFeatureEnabled) {
111                    bean.getFeatures().add(new LoggingFeature());
112                }            
113                server = bean.create();
114            } else {
115                JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
116                bean.setServiceBeans(serviceBeans);
117                bean.setAddress("camel://" + createEndpointUri());
118                bean.setStart(true);
119                bean.setTransportId(CxfBeanTransportFactory.TRANSPORT_ID);
120                bean.setBus(bus);
121                if (loggingFeatureEnabled) {
122                    bean.getFeatures().add(new LoggingFeature());
123                }  
124                server = bean.create();
125            }
126        }
127        
128        /**
129         * @param cxfBeanComponent 
130         * 
131         */
132        private void registerTransportFactory(CxfBeanComponent cxfBeanComponent) {
133            
134            CxfBeanTransportFactory transportFactory = new CxfBeanTransportFactory();
135            transportFactory.setCxfBeanComponent(cxfBeanComponent);
136            transportFactory.setBus(bus);
137            
138            // register the conduit initiator
139            ConduitInitiatorManager cim = bus.getExtension(ConduitInitiatorManager.class);
140            cim.registerConduitInitiator(CxfBeanTransportFactory.TRANSPORT_ID, transportFactory);
141            
142            // register the destination factory
143            DestinationFactoryManager dfm = bus.getExtension(DestinationFactoryManager.class);
144            dfm.registerDestinationFactory(CxfBeanTransportFactory.TRANSPORT_ID, transportFactory);    
145            
146        }
147    
148        // Properties
149        // -------------------------------------------------------------------------
150    
151        public Bus getBus() {
152            return bus;
153        }
154    
155        public void setBus(Bus bus) {
156            this.bus = bus;
157        }
158    
159        public void setSetDefaultBus(boolean isSetDefaultBus) {
160            this.isSetDefaultBus = isSetDefaultBus;
161        }
162    
163        public boolean isSetDefaultBus() {
164            return isSetDefaultBus;
165        }
166    
167        public void setCxfBeanBinding(CxfBeanBinding cxfBeanBinding) {
168            this.cxfBeanBinding = cxfBeanBinding;
169        }
170    
171        public CxfBeanBinding getCxfBeanBinding() {
172            return cxfBeanBinding;
173        }
174    
175        public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
176            this.headerFilterStrategy = headerFilterStrategy;
177        }
178    
179        public HeaderFilterStrategy getHeaderFilterStrategy() {
180            return headerFilterStrategy;
181        }
182        
183        public void setLoggingFeatureEnabled(boolean loggingFeatureEnabled) {
184            this.loggingFeatureEnabled = loggingFeatureEnabled;
185        }
186    
187        public boolean isLoggingFeatureEnabled() {
188            return loggingFeatureEnabled;
189        }     
190    
191        public void setPopulateFromClass(boolean populateFromClass) {
192            this.populateFromClass = populateFromClass;
193        }
194    
195        public boolean isPopulateFromClass() {
196            return populateFromClass;
197        }
198    }