View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.osgi.extender.internal.dependencies.startup;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import org.osgi.framework.BundleContext;
24  import org.osgi.framework.BundleException;
25  import org.osgi.framework.InvalidSyntaxException;
26  import org.springframework.beans.BeansException;
27  import org.springframework.beans.factory.BeanFactory;
28  import org.springframework.beans.factory.BeanFactoryUtils;
29  import org.springframework.beans.factory.SmartFactoryBean;
30  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
31  import org.springframework.osgi.extender.OsgiServiceDependencyFactory;
32  import org.springframework.osgi.service.importer.DefaultOsgiServiceDependency;
33  import org.springframework.osgi.service.importer.OsgiServiceDependency;
34  import org.springframework.osgi.service.importer.support.OsgiServiceCollectionProxyFactoryBean;
35  import org.springframework.osgi.service.importer.support.OsgiServiceProxyFactoryBean;
36  import org.springframework.util.StringUtils;
37  
38  /**
39   * Default mandatory importer dependency factory.
40   * 
41   * @author Costin Leau
42   * 
43   */
44  public class MandatoryImporterDependencyFactory implements OsgiServiceDependencyFactory {
45  
46  	public Collection getServiceDependencies(BundleContext bundleContext, ConfigurableListableBeanFactory beanFactory)
47  			throws BeansException, InvalidSyntaxException, BundleException {
48  
49  		String[] singleBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory,
50  			OsgiServiceProxyFactoryBean.class, true, false);
51  
52  		String[] collectionBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory,
53  			OsgiServiceCollectionProxyFactoryBean.class, true, false);
54  
55  		String[] beans = StringUtils.concatenateStringArrays(singleBeans, collectionBeans);
56  
57  		List beansCollections = new ArrayList(beans.length);
58  
59  		for (int i = 0; i < beans.length; i++) {
60  			String beanName = (beans[i].startsWith(BeanFactory.FACTORY_BEAN_PREFIX) ? beans[i]
61  					: BeanFactory.FACTORY_BEAN_PREFIX + beans[i]);
62  
63  			SmartFactoryBean reference = (SmartFactoryBean) beanFactory.getBean(beanName);
64  
65  			OsgiServiceDependency dependency;
66  			if (reference instanceof OsgiServiceProxyFactoryBean) {
67  				OsgiServiceProxyFactoryBean importer = (OsgiServiceProxyFactoryBean) reference;
68  
69  				dependency = new DefaultOsgiServiceDependency(beanName, importer.getUnifiedFilter(),
70  					importer.getCardinality().isMandatory());
71  			}
72  			else {
73  				OsgiServiceCollectionProxyFactoryBean importer = (OsgiServiceCollectionProxyFactoryBean) reference;
74  
75  				dependency = new DefaultOsgiServiceDependency(beanName, importer.getUnifiedFilter(),
76  					importer.getCardinality().isMandatory());
77  			}
78  
79  			beansCollections.add(dependency);
80  		}
81  
82  		return beansCollections;
83  	}
84  }