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.bean;
018    
019    import java.net.URI;
020    import java.util.ArrayList;
021    import java.util.List;
022    import java.util.Map;
023    
024    import javax.jbi.servicedesc.ServiceEndpoint;
025    
026    import org.apache.servicemix.common.BaseServiceUnitManager;
027    import org.apache.servicemix.common.DefaultComponent;
028    import org.apache.servicemix.common.Deployer;
029    import org.apache.servicemix.common.Endpoint;
030    import org.apache.servicemix.jbi.util.IntrospectionSupport;
031    import org.apache.servicemix.jbi.util.URISupport;
032    import org.springframework.beans.BeansException;
033    import org.springframework.context.ApplicationContext;
034    import org.springframework.context.ApplicationContextAware;
035    
036    /**
037     * A JBI component for binding beans to the JBI bus which work directly off of the JBI messages
038     * without requiring any SOAP Processing. If you require support for SOAP, JAX-WS, JSR-181 then you
039     * should use the servicemix-jsr181 module instead.
040     *
041     * @version $Revision: $
042     * @org.apache.xbean.XBean element="component" description="Bean Component"
043     */
044    public class BeanComponent extends DefaultComponent implements ApplicationContextAware {
045    
046        private BeanEndpoint[] endpoints;
047        private String[] searchPackages;
048        private ApplicationContext applicationContext;
049    
050        /* (non-Javadoc)
051         * @see org.servicemix.common.BaseComponent#createServiceUnitManager()
052         */
053        @Override
054        public BaseServiceUnitManager createServiceUnitManager() {
055            Deployer[] deployers = new Deployer[] {new BeanXBeanDeployer(this) };
056            return new BaseServiceUnitManager(this, deployers);
057        }
058    
059        public BeanEndpoint[] getEndpoints() {
060            return endpoints;
061        }
062    
063        public void setEndpoints(BeanEndpoint[] endpoints) {
064            this.endpoints = endpoints;
065        }
066    
067        public ApplicationContext getApplicationContext() {
068            return applicationContext;
069        }
070    
071        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
072            this.applicationContext = applicationContext;
073        }
074    
075        public String[] getSearchPackages() {
076            return searchPackages;
077        }
078    
079        public void setSearchPackages(String[] searchPackages) {
080            this.searchPackages = searchPackages;
081        }
082    
083        @SuppressWarnings("unchecked")
084        protected List getConfiguredEndpoints() {
085            List list = new ArrayList(asList(getEndpoints()));
086            if (searchPackages != null) {
087                EndpointFinder finder = new EndpointFinder(this);
088                finder.addEndpoints(list);
089            }
090            return list;
091        }
092    
093        protected Class[] getEndpointClasses() {
094            return new Class[]{BeanEndpoint.class};
095        }
096    
097        protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception {
098            // We receive an exchange for an EPR that has not been used yet.
099            // Register a provider endpoint and restart processing.
100            BeanEndpoint endpoint = new BeanEndpoint(this, ep);
101    
102            // TODO
103            //endpoint.setRole(MessageExchange.Role.PROVIDER);
104    
105            // lets use a URL to parse the path
106            URI uri = new URI(ep.getEndpointName());
107    
108            String beanName = null;
109            // lets try the host first for hierarchial URIs
110            if (uri.getHost() != null) {
111                // it must start bean://host/path
112                beanName = uri.getHost();
113            } else {
114                // it must be an absolute URI of the form bean:name
115                beanName = uri.getSchemeSpecificPart();
116            }
117            if (beanName != null) {
118                endpoint.setBeanName(beanName);
119            } else {
120                throw new IllegalArgumentException("No bean name defined for URI: "
121                        + uri + ". Please use a URI of bean:name or bean://name?property=value");
122            }
123    
124            Map map = URISupport.parseQuery(uri.getQuery());
125            if (endpoint.getBean() == null) {
126                endpoint.setBean(endpoint.createBean());
127            }
128            IntrospectionSupport.setProperties(endpoint.getBean(), map);
129    
130            endpoint.activate();
131            return endpoint;
132        }
133    
134        /**
135         * Adds a new component dynamically
136         */
137        public void addEndpoint(BeanEndpoint endpoint) throws Exception {
138            super.addEndpoint(endpoint);
139        }
140    }