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.common.xbean;
018
019 import java.io.File;
020 import java.util.ArrayList;
021 import java.util.Collections;
022 import java.util.Iterator;
023 import java.util.List;
024
025 import javax.jbi.messaging.MessageExchange;
026
027 import org.apache.servicemix.common.Endpoint;
028 import org.apache.servicemix.common.packaging.Consumes;
029 import org.apache.servicemix.common.packaging.Provides;
030 import org.apache.servicemix.common.packaging.ServiceUnitAnalyzer;
031 import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
032 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
033
034 public abstract class AbstractXBeanServiceUnitAnalyzer implements
035 ServiceUnitAnalyzer {
036
037 List<Consumes> consumes = new ArrayList<Consumes>();
038
039 List<Provides> provides = new ArrayList<Provides>();
040
041 /*
042 * (non-Javadoc)
043 *
044 * @see org.apache.servicemix.common.packaging.ServiceUnitAnalyzer#getConsumes()
045 */
046 public List<Consumes> getConsumes() {
047 return consumes;
048 }
049
050 protected abstract List<Consumes> getConsumes(Endpoint endpoint);
051
052 /*
053 * (non-Javadoc)
054 *
055 * @see org.apache.servicemix.common.packaging.ServiceUnitAnalyzer#getProvides()
056 */
057 public List<Provides> getProvides() {
058 return provides;
059 }
060
061 protected List<Provides> getProvides(Endpoint endpoint) {
062 List<Provides> providesList = new ArrayList<Provides>();
063 if (endpoint.getRole().equals(MessageExchange.Role.PROVIDER)) {
064 Provides newProvide = new Provides();
065 newProvide.setEndpointName(endpoint.getEndpoint());
066 newProvide.setInterfaceName(endpoint.getInterfaceName());
067 newProvide.setServiceName(endpoint.getService());
068 providesList.add(newProvide);
069 }
070
071 return providesList;
072 }
073
074 protected abstract String getXBeanFile();
075
076 /*
077 * (non-Javadoc)
078 *
079 * @see org.apache.servicemix.common.packaging.ServiceUnitAnalyzer#init(java.io.File)
080 */
081 public void init(File explodedServiceUnitRoot) {
082 FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
083 new String[] { "file:///" + explodedServiceUnitRoot.getAbsolutePath() + "/" + getXBeanFile() },
084 false);
085 List beanFactoryPostProcessors = getBeanFactoryPostProcessors(explodedServiceUnitRoot.getAbsolutePath());
086 for (Iterator iter = beanFactoryPostProcessors.iterator(); iter.hasNext();) {
087 BeanFactoryPostProcessor processor = (BeanFactoryPostProcessor) iter.next();
088 context.addBeanFactoryPostProcessor(processor);
089 }
090 context.refresh();
091 for (int i = 0; i < context.getBeanDefinitionNames().length; i++) {
092 Object bean = context.getBean(context.getBeanDefinitionNames()[i]);
093 if (isValidEndpoint(bean)) {
094 // The provides are generic while the consumes need to
095 // be handled by the implementation
096 Endpoint endpoint = (Endpoint) bean;
097 provides.addAll(getProvides(endpoint));
098 consumes.addAll(getConsumes(endpoint));
099 }
100 }
101 }
102
103 protected List getBeanFactoryPostProcessors(String absolutePath) {
104 return Collections.EMPTY_LIST;
105 }
106
107 protected abstract boolean isValidEndpoint(Object bean);
108
109 }