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