EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][org.jclouds.lifecycle.config]

COVERAGE SUMMARY FOR SOURCE FILE [LifeCycleModule.java]

nameclass, %method, %block, %line, %
LifeCycleModule.java67%  (4/6)75%  (12/16)63%  (159/254)66%  (33.2/50)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class LifeCycleModule$2$10%   (0/1)0%   (0/2)0%   (0/21)0%   (0/3)
LifeCycleModule$2$1 (LifeCycleModule$2, Closer, Method): void 0%   (0/1)0%   (0/12)0%   (0/1)
afterInjection (Object): void 0%   (0/1)0%   (0/9)0%   (0/2)
     
class LifeCycleModule$2$1$10%   (0/1)0%   (0/2)0%   (0/37)0%   (0/9)
LifeCycleModule$2$1$1 (LifeCycleModule$2$1, Object): void 0%   (0/1)0%   (0/9)0%   (0/1)
close (): void 0%   (0/1)0%   (0/28)0%   (0/8)
     
class LifeCycleModule$2$2100% (1/1)100% (2/2)49%  (18/37)44%  (4/9)
afterInjection (Object): void 100% (1/1)32%  (9/28)38%  (3/8)
LifeCycleModule$2$2 (LifeCycleModule$2, Method): void 100% (1/1)100% (9/9)100% (1/1)
     
class LifeCycleModule$1100% (1/1)100% (3/3)76%  (31/41)85%  (5.1/6)
close (): void 100% (1/1)70%  (19/27)85%  (4.2/5)
<static initializer> 100% (1/1)75%  (6/8)75%  (0.8/1)
LifeCycleModule$1 (LifeCycleModule): void 100% (1/1)100% (6/6)100% (1/1)
     
class LifeCycleModule$2100% (1/1)100% (4/4)90%  (73/81)94%  (17/18)
associatePreDestroyWithCloser (Closer, TypeEncounter, Method): void 100% (1/1)50%  (8/16)75%  (3/4)
LifeCycleModule$2 (LifeCycleModule, Closer): void 100% (1/1)100% (9/9)100% (1/1)
hear (TypeLiteral, TypeEncounter): void 100% (1/1)100% (41/41)100% (9/9)
invokePostConstructMethodAfterInjection (TypeEncounter, Method): void 100% (1/1)100% (15/15)100% (4/4)
     
class LifeCycleModule100% (1/1)100% (3/3)100% (37/37)100% (10/10)
LifeCycleModule (): void 100% (1/1)100% (3/3)100% (1/1)
bindPostInjectionInvoke (Closer): void 100% (1/1)100% (9/9)100% (2/2)
configure (): void 100% (1/1)100% (25/25)100% (7/7)

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 */
19package org.jclouds.lifecycle.config;
20 
21import static com.google.inject.matcher.Matchers.any;
22 
23import java.io.Closeable;
24import java.io.IOException;
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27import java.util.Arrays;
28import java.util.HashSet;
29import java.util.Set;
30import java.util.concurrent.ExecutorService;
31 
32import javax.annotation.PostConstruct;
33import javax.annotation.PreDestroy;
34import javax.inject.Inject;
35import javax.inject.Named;
36 
37import org.jclouds.Constants;
38import org.jclouds.lifecycle.Closer;
39 
40import com.google.inject.AbstractModule;
41import com.google.inject.ProvisionException;
42import com.google.inject.TypeLiteral;
43import com.google.inject.spi.InjectionListener;
44import com.google.inject.spi.TypeEncounter;
45import 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 */
54public 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}

[all classes][org.jclouds.lifecycle.config]
EMMA 2.0.5312 (C) Vladimir Roubtsov