1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.lifecycle.config; |
20 | |
21 | import static com.google.inject.matcher.Matchers.any; |
22 | |
23 | import java.io.Closeable; |
24 | import java.io.IOException; |
25 | import java.lang.reflect.InvocationTargetException; |
26 | import java.lang.reflect.Method; |
27 | import java.util.Arrays; |
28 | import java.util.HashSet; |
29 | import java.util.Set; |
30 | import java.util.concurrent.ExecutorService; |
31 | |
32 | import javax.annotation.PostConstruct; |
33 | import javax.annotation.PreDestroy; |
34 | import javax.inject.Inject; |
35 | import javax.inject.Named; |
36 | |
37 | import org.jclouds.Constants; |
38 | import org.jclouds.lifecycle.Closer; |
39 | |
40 | import com.google.inject.AbstractModule; |
41 | import com.google.inject.ProvisionException; |
42 | import com.google.inject.TypeLiteral; |
43 | import com.google.inject.spi.InjectionListener; |
44 | import com.google.inject.spi.TypeEncounter; |
45 | import com.google.inject.spi.TypeListener; |
46 | |
47 | /** |
48 | * This associates java lifecycle annotations with guice hooks. For example, we invoke |
49 | * {@link PostConstruct} after injection, and Associate {@link PreDestroy} with a global |
50 | * {@link Closer} object. |
51 | * |
52 | * @author Adrian Cole |
53 | */ |
54 | public class LifeCycleModule extends AbstractModule { |
55 | |
56 | protected void configure() { |
57 | |
58 | Closeable executorCloser = new Closeable() { |
59 | @Inject |
60 | @Named(Constants.PROPERTY_USER_THREADS) |
61 | ExecutorService userExecutor; |
62 | @Inject |
63 | @Named(Constants.PROPERTY_IO_WORKER_THREADS) |
64 | ExecutorService ioExecutor; |
65 | |
66 | public void close() throws IOException { |
67 | assert userExecutor != null; |
68 | userExecutor.shutdownNow(); |
69 | assert ioExecutor != null; |
70 | ioExecutor.shutdownNow(); |
71 | } |
72 | }; |
73 | |
74 | binder().requestInjection(executorCloser); |
75 | Closer closer = new Closer(); |
76 | closer.addToClose(executorCloser); |
77 | bind(Closer.class).toInstance(closer); |
78 | bindPostInjectionInvoke(closer); |
79 | } |
80 | |
81 | protected void bindPostInjectionInvoke(final Closer closer) { |
82 | bindListener(any(), new TypeListener() { |
83 | public <I> void hear(TypeLiteral<I> injectableType, TypeEncounter<I> encounter) { |
84 | Set<Method> methods = new HashSet<Method>(); |
85 | Class<? super I> type = injectableType.getRawType(); |
86 | while (type != null) { |
87 | methods.addAll(Arrays.asList(type.getDeclaredMethods())); |
88 | type = type.getSuperclass(); |
89 | } |
90 | for (final Method method : methods) { |
91 | invokePostConstructMethodAfterInjection(encounter, method); |
92 | associatePreDestroyWithCloser(closer, encounter, method); |
93 | } |
94 | } |
95 | |
96 | private <I> void associatePreDestroyWithCloser(final Closer closer, |
97 | TypeEncounter<I> encounter, final Method method) { |
98 | PreDestroy preDestroy = method.getAnnotation(PreDestroy.class); |
99 | if (preDestroy != null) { |
100 | encounter.register(new InjectionListener<I>() { |
101 | public void afterInjection(final I injectee) { |
102 | closer.addToClose(new Closeable() { |
103 | public void close() throws IOException { |
104 | try { |
105 | method.invoke(injectee); |
106 | } catch (InvocationTargetException ie) { |
107 | Throwable e = ie.getTargetException(); |
108 | throw new IOException(e.getMessage()); |
109 | } catch (IllegalAccessException e) { |
110 | throw new IOException(e.getMessage()); |
111 | } |
112 | } |
113 | }); |
114 | |
115 | } |
116 | }); |
117 | } |
118 | } |
119 | |
120 | private <I> void invokePostConstructMethodAfterInjection(TypeEncounter<I> encounter, |
121 | final Method method) { |
122 | PostConstruct postConstruct = method.getAnnotation(PostConstruct.class); |
123 | if (postConstruct != null) { |
124 | encounter.register(new InjectionListener<I>() { |
125 | public void afterInjection(I injectee) { |
126 | try { |
127 | method.invoke(injectee); |
128 | } catch (InvocationTargetException ie) { |
129 | Throwable e = ie.getTargetException(); |
130 | throw new ProvisionException(e.getMessage(), e); |
131 | } catch (IllegalAccessException e) { |
132 | throw new ProvisionException(e.getMessage(), e); |
133 | } |
134 | } |
135 | }); |
136 | } |
137 | } |
138 | }); |
139 | } |
140 | |
141 | } |