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 org.osgi.framework.BundleContext;
20  import org.osgi.framework.Filter;
21  import org.osgi.framework.ServiceEvent;
22  import org.springframework.osgi.service.importer.OsgiServiceDependency;
23  import org.springframework.osgi.util.OsgiServiceReferenceUtils;
24  
25  /**
26   * Holder/helper class representing an OSGi service dependency
27   * 
28   * @author Costin Leau
29   * @author Hal Hildebrand
30   * @author Andy Piper
31   */
32  public class MandatoryServiceDependency implements OsgiServiceDependency {
33  
34  	protected final Filter filter;
35  
36  	protected final String filterAsString;
37  
38  	protected final boolean isMandatory;
39  
40  	protected final BundleContext bundleContext;
41  
42  	private final String beanName;
43  
44  	private OsgiServiceDependency serviceDependency;
45  
46  
47  	public MandatoryServiceDependency(BundleContext bc, Filter serviceFilter, boolean isMandatory, String beanName) {
48  		filter = serviceFilter;
49  		this.filterAsString = filter.toString();
50  		this.isMandatory = isMandatory;
51  		bundleContext = bc;
52  		this.beanName = beanName;
53  		serviceDependency = new OsgiServiceDependency() {
54  
55  			public String getBeanName() {
56  				return MandatoryServiceDependency.this.beanName;
57  			}
58  
59  			public Filter getServiceFilter() {
60  				return MandatoryServiceDependency.this.filter;
61  			}
62  
63  			public boolean isMandatory() {
64  				return MandatoryServiceDependency.this.isMandatory;
65  			}
66  
67  		};
68  	}
69  
70  	public MandatoryServiceDependency(BundleContext bc, OsgiServiceDependency dependency) {
71  		this(bc, dependency.getServiceFilter(), dependency.isMandatory(), dependency.getBeanName());
72  	}
73  
74  	public boolean matches(ServiceEvent event) {
75  		return filter.match(event.getServiceReference());
76  	}
77  
78  	/**
79  	 * @return
80  	 */
81  	public boolean isServicePresent() {
82  		return (!isMandatory || OsgiServiceReferenceUtils.isServicePresent(bundleContext, filterAsString));
83  	}
84  
85  	public String toString() {
86  		return "Dependency on [" + filterAsString + "] (from bean [" + beanName + "])";
87  	}
88  
89  	/**
90  	 * @return Returns the filter.
91  	 */
92  	public Filter getServiceFilter() {
93  		return filter;
94  	}
95  
96  	/**
97  	 * Returns the beanName.
98  	 * 
99  	 * @return Returns the beanName
100 	 */
101 	public String getBeanName() {
102 		return beanName;
103 	}
104 
105 	public boolean isMandatory() {
106 		return isMandatory;
107 	}
108 
109 	public boolean equals(Object o) {
110 		if (this == o)
111 			return true;
112 		if (o == null || getClass() != o.getClass())
113 			return false;
114 
115 		final MandatoryServiceDependency that = (MandatoryServiceDependency) o;
116 
117 		if (isMandatory != that.isMandatory)
118 			return false;
119 		if (filterAsString != null ? !filterAsString.equals(that.filterAsString) : that.filterAsString != null)
120 			return false;
121 
122 		return true;
123 	}
124 
125 	public int hashCode() {
126 		int result;
127 		result = (filterAsString != null ? filterAsString.hashCode() : 0);
128 		result = 29 * result + (isMandatory ? 1 : 0);
129 		return result;
130 	}
131 
132 	public OsgiServiceDependency getServiceDependency() {
133 		return serviceDependency;
134 	}
135 }