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.service.importer.support.internal.aop;
18  
19  import org.osgi.framework.BundleContext;
20  import org.osgi.framework.ServiceReference;
21  import org.springframework.osgi.service.ServiceUnavailableException;
22  import org.springframework.osgi.service.importer.ServiceProxyDestroyedException;
23  import org.springframework.util.Assert;
24  
25  /**
26   * Interceptor offering static behaviour around an OSGi service. If the OSGi
27   * becomes unavailable, no look up or retries will be executed, the interceptor
28   * throwing an exception.
29   * 
30   * @author Costin Leau
31   * 
32   */
33  public class ServiceStaticInterceptor extends ServiceInvoker {
34  
35  	private static final int hashCode = ServiceStaticInterceptor.class.hashCode() * 13;
36  
37  	private boolean destroyed = false;
38  
39  	/** private lock */
40  	private final Object lock = new Object();
41  
42  	private final ServiceReference reference;
43  
44  	private final BundleContext bundleContext;
45  
46  
47  	public ServiceStaticInterceptor(BundleContext context, ServiceReference reference) {
48  		Assert.notNull(context);
49  		Assert.notNull(reference, "a not null service reference is required");
50  		this.bundleContext = context;
51  		this.reference = reference;
52  	}
53  
54  	protected Object getTarget() {
55  		synchronized (lock) {
56  			if (destroyed)
57  				throw new ServiceProxyDestroyedException();
58  		}
59  
60  		// check if the service is alive first
61  		if (reference.getBundle() != null) {
62  			// since requesting for a service requires additional work
63  			// from the OSGi platform
64  			Object target = bundleContext.getService(reference);
65  			if (target != null)
66  				return target;
67  		}
68  		// throw exception
69  		throw new ServiceUnavailableException(reference);
70  	}
71  
72  	public ServiceReference getServiceReference() {
73  		return reference;
74  	}
75  
76  	public void destroy() {
77  		synchronized (lock) {
78  			// set this flag first to make sure after destruction, the OSGi service is not used any more
79  			destroyed = true;
80  		}
81  	}
82  
83  	public boolean equals(Object other) {
84  		if (this == other)
85  			return true;
86  		if (other instanceof ServiceStaticInterceptor) {
87  			ServiceStaticInterceptor oth = (ServiceStaticInterceptor) other;
88  			return reference.equals(oth.reference) && bundleContext.equals(oth.bundleContext);
89  		}
90  		return false;
91  	}
92  
93  	public int hashCode() {
94  		return hashCode;
95  	}
96  }