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.jbi.util.IntrospectionSupport;
029 import org.apache.servicemix.jbi.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
049 public BeanEndpoint[] getEndpoints() {
050 return endpoints;
051 }
052
053 public void setEndpoints(BeanEndpoint[] endpoints) {
054 this.endpoints = endpoints;
055 }
056
057 public ApplicationContext getApplicationContext() {
058 return applicationContext;
059 }
060
061 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
062 this.applicationContext = applicationContext;
063 }
064
065 public String[] getSearchPackages() {
066 return searchPackages;
067 }
068
069 public void setSearchPackages(String[] searchPackages) {
070 this.searchPackages = searchPackages;
071 }
072
073 @SuppressWarnings("unchecked")
074 protected List getConfiguredEndpoints() {
075 List list = new ArrayList(asList(getEndpoints()));
076 if (searchPackages != null) {
077 EndpointFinder finder = new EndpointFinder(this);
078 finder.addEndpoints(list);
079 }
080 return list;
081 }
082
083 protected Class[] getEndpointClasses() {
084 return new Class[]{BeanEndpoint.class};
085 }
086
087 protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception {
088 // We receive an exchange for an EPR that has not been used yet.
089 // Register a provider endpoint and restart processing.
090 BeanEndpoint endpoint = new BeanEndpoint(this, ep);
091
092 // TODO
093 //endpoint.setRole(MessageExchange.Role.PROVIDER);
094
095 // lets use a URL to parse the path
096 URI uri = new URI(ep.getEndpointName());
097
098 String beanName = null;
099 // lets try the host first for hierarchial URIs
100 if (uri.getHost() != null) {
101 // it must start bean://host/path
102 beanName = uri.getHost();
103 } else {
104 // it must be an absolute URI of the form bean:name
105 beanName = uri.getSchemeSpecificPart();
106 }
107 if (beanName != null) {
108 endpoint.setBeanName(beanName);
109 } else {
110 throw new IllegalArgumentException("No bean name defined for URI: "
111 + uri + ". Please use a URI of bean:name or bean://name?property=value");
112 }
113
114 Map map = URISupport.parseQuery(uri.getQuery());
115 if (endpoint.getBean() == null) {
116 endpoint.setBean(endpoint.createBean());
117 }
118 IntrospectionSupport.setProperties(endpoint.getBean(), map);
119
120 endpoint.activate();
121 return endpoint;
122 }
123
124 /**
125 * Adds a new component dynamically
126 */
127 public void addEndpoint(BeanEndpoint endpoint) throws Exception {
128 super.addEndpoint(endpoint);
129 }
130 }