Coverage Report - org.apache.commons.inject.api.PostConstructModule
 
Classes in this File Line Coverage Branch Coverage Complexity
PostConstructModule
80%
17/21
58%
7/12
2,778
PostConstructModule$1
100%
5/5
75%
3/4
2,778
PostConstructModule$2
78%
11/14
50%
2/4
2,778
 
 1  
 /**
 2  
  Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  contributor license agreements.  See the NOTICE file distributed with
 4  
  this work for additional information regarding copyright ownership.
 5  
  The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  (the "License"); you may not use this file except in compliance with
 7  
  the License.  You may obtain a copy of the License at
 8  
 
 9  
       http://www.apache.org/licenses/LICENSE-2.0
 10  
 
 11  
  Unless required by applicable law or agreed to in writing, software
 12  
  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  See the License for the specific language governing permissions and
 15  
  limitations under the License.
 16  
 */
 17  
 package org.apache.commons.inject.api;
 18  
 
 19  
 import java.lang.annotation.Annotation;
 20  
 import java.lang.reflect.Method;
 21  
 
 22  
 import javax.annotation.PostConstruct;
 23  
 import javax.annotation.PreDestroy;
 24  
 
 25  
 import org.apache.commons.inject.api.bind.IBinder;
 26  
 import org.apache.commons.inject.api.bind.IModule;
 27  
 import org.apache.commons.inject.api.bind.IBinder.IInjectionListener;
 28  
 import org.apache.commons.inject.impl.DefaultLifecycleController;
 29  
 import org.apache.commons.inject.util.Exceptions;
 30  
 
 31  14
 public class PostConstructModule implements IModule {
 32  
         private ILifecycleController controller;
 33  
 
 34  
         public ILifecycleController getLifecycleController() {
 35  2
                 if (controller == null) {
 36  2
                         controller = new DefaultLifecycleController();
 37  
                 }
 38  2
                 return controller;
 39  
         }
 40  
 
 41  
         public void setLifecycleController(ILifecycleController pController) {
 42  0
                 controller = pController;
 43  0
         }
 44  
 
 45  
         @Override
 46  
         public void configure(IBinder pBinder) {
 47  2
                 pBinder.add(new IInjectionListener() {
 48  
                         @Override
 49  
                         public void initialized(IKey<?> pKey, Object pObject) {
 50  7
                                 final ILifecycleListener listener = getListenerFor(pObject);
 51  7
                                 if (listener != null  &&  listener != controller) {
 52  5
                                         controller.add(listener);
 53  
                                 }
 54  7
                         }
 55  
                 });
 56  2
                 pBinder.bind(ILifecycleController.class).toInstance(controller);
 57  2
         }
 58  
 
 59  
         protected ILifecycleListener getListenerFor(final Object pObject) {
 60  7
                 if (pObject instanceof ILifecycleListener) {
 61  3
                         return (ILifecycleListener) pObject;
 62  
                 }
 63  4
                 final Method postConstructMethod = findMethod(PostConstruct.class, pObject.getClass());
 64  4
                 final Method preDestroyMethod = findMethod(PreDestroy.class, pObject.getClass());
 65  4
                 if (postConstructMethod == null  &&  preDestroyMethod == null) {
 66  0
                         return null;
 67  
                 }
 68  4
                 return new ILifecycleListener(){
 69  
                         @Override
 70  
                         public void start() {
 71  4
                                 invoke(postConstructMethod, pObject);
 72  4
                         }
 73  
 
 74  
                         @Override
 75  
                         public void shutdown() {
 76  4
                                 invoke(preDestroyMethod, pObject);
 77  4
                         }
 78  
 
 79  
                         private void invoke(Method pMethod, Object pObject) {
 80  8
                                 if (pMethod == null) {
 81  0
                                         return;
 82  
                                 }
 83  
                                 try {
 84  8
                                         if (!pMethod.isAccessible()) {
 85  8
                                                 pMethod.setAccessible(true);
 86  
                                         }
 87  8
                                         pMethod.invoke(pObject);
 88  0
                                 } catch (Throwable t) {
 89  0
                                         throw Exceptions.show(t);
 90  8
                                 }
 91  8
                         }
 92  
                 };
 93  
         }
 94  
 
 95  
         private Method findMethod(Class<? extends Annotation> pAnnotationClass, Class<?> pType) {
 96  8
                 final Method[] methods = pType.getDeclaredMethods();
 97  12
                 for (Method method : methods) {
 98  12
                         if (method.isAnnotationPresent(pAnnotationClass)) {
 99  8
                                 return method;
 100  
                         }
 101  
                 }
 102  0
                 return null;
 103  
         }
 104  
 }