1
2 package org.springframework.osgi.service.util.internal.aop;
3
4 import org.aopalliance.intercept.MethodInterceptor;
5 import org.aopalliance.intercept.MethodInvocation;
6 import org.springframework.util.ObjectUtils;
7
8 /**
9 * Simple interceptor for dealing with ThreadContextClassLoader(TCCL)
10 * management.
11 *
12 * @author Hal Hildebrand
13 * @author Costin Leau
14 */
15 public class ServiceTCCLInterceptor implements MethodInterceptor {
16
17 private static final int hashCode = ServiceTCCLInterceptor.class.hashCode() * 13;
18
19 /** classloader to set the TCCL during invocation */
20 private final ClassLoader loader;
21
22
23 /**
24 * Constructs a new <code>OsgiServiceTCCLInterceptor</code> instance.
25 *
26 * @param loader classloader to use for TCCL during invocation. Can be null.
27 */
28 public ServiceTCCLInterceptor(ClassLoader loader) {
29 this.loader = loader;
30 }
31
32 public Object invoke(MethodInvocation invocation) throws Throwable {
33 ClassLoader previous = Thread.currentThread().getContextClassLoader();
34 try {
35 Thread.currentThread().setContextClassLoader(loader);
36 return invocation.proceed();
37 }
38 finally {
39 Thread.currentThread().setContextClassLoader(previous);
40 }
41 }
42
43 public boolean equals(Object other) {
44 if (this == other)
45 return true;
46 if (other instanceof ServiceTCCLInterceptor) {
47 ServiceTCCLInterceptor oth = (ServiceTCCLInterceptor) other;
48 return (ObjectUtils.nullSafeEquals(loader, oth.loader));
49 }
50 return false;
51 }
52
53 public int hashCode() {
54 return hashCode;
55 }
56 }