1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
61 if (reference.getBundle() != null) {
62
63
64 Object target = bundleContext.getService(reference);
65 if (target != null)
66 return target;
67 }
68
69 throw new ServiceUnavailableException(reference);
70 }
71
72 public ServiceReference getServiceReference() {
73 return reference;
74 }
75
76 public void destroy() {
77 synchronized (lock) {
78
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 }