001 /**
002 Licensed to the Apache Software Foundation (ASF) under one or more
003 contributor license agreements. See the NOTICE file distributed with
004 this work for additional information regarding copyright ownership.
005 The ASF licenses this file to You under the Apache License, Version 2.0
006 (the "License"); you may not use this file except in compliance with
007 the License. You may obtain a copy of the License at
008
009 http://www.apache.org/licenses/LICENSE-2.0
010
011 Unless required by applicable law or agreed to in writing, software
012 distributed under the License is distributed on an "AS IS" BASIS,
013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 See the License for the specific language governing permissions and
015 limitations under the License.
016 */
017 package org.apache.commons.inject.api;
018
019 import java.lang.annotation.Annotation;
020 import java.lang.reflect.Method;
021
022 import javax.annotation.PostConstruct;
023 import javax.annotation.PreDestroy;
024
025 import org.apache.commons.inject.api.bind.IBinder;
026 import org.apache.commons.inject.api.bind.IModule;
027 import org.apache.commons.inject.api.bind.IBinder.IInjectionListener;
028 import org.apache.commons.inject.impl.DefaultLifecycleController;
029 import org.apache.commons.inject.util.Exceptions;
030
031 public class PostConstructModule implements IModule {
032 private ILifecycleController controller;
033
034 public ILifecycleController getLifecycleController() {
035 if (controller == null) {
036 controller = new DefaultLifecycleController();
037 }
038 return controller;
039 }
040
041 public void setLifecycleController(ILifecycleController pController) {
042 controller = pController;
043 }
044
045 @Override
046 public void configure(IBinder pBinder) {
047 pBinder.add(new IInjectionListener() {
048 @Override
049 public void initialized(IKey<?> pKey, Object pObject) {
050 final ILifecycleListener listener = getListenerFor(pObject);
051 if (listener != null && listener != controller) {
052 controller.add(listener);
053 }
054 }
055 });
056 pBinder.bind(ILifecycleController.class).toInstance(controller);
057 }
058
059 protected ILifecycleListener getListenerFor(final Object pObject) {
060 if (pObject instanceof ILifecycleListener) {
061 return (ILifecycleListener) pObject;
062 }
063 final Method postConstructMethod = findMethod(PostConstruct.class, pObject.getClass());
064 final Method preDestroyMethod = findMethod(PreDestroy.class, pObject.getClass());
065 if (postConstructMethod == null && preDestroyMethod == null) {
066 return null;
067 }
068 return new ILifecycleListener(){
069 @Override
070 public void start() {
071 invoke(postConstructMethod, pObject);
072 }
073
074 @Override
075 public void shutdown() {
076 invoke(preDestroyMethod, pObject);
077 }
078
079 private void invoke(Method pMethod, Object pObject) {
080 if (pMethod == null) {
081 return;
082 }
083 try {
084 if (!pMethod.isAccessible()) {
085 pMethod.setAccessible(true);
086 }
087 pMethod.invoke(pObject);
088 } catch (Throwable t) {
089 throw Exceptions.show(t);
090 }
091 }
092 };
093 }
094
095 private Method findMethod(Class<? extends Annotation> pAnnotationClass, Class<?> pType) {
096 final Method[] methods = pType.getDeclaredMethods();
097 for (Method method : methods) {
098 if (method.isAnnotationPresent(pAnnotationClass)) {
099 return method;
100 }
101 }
102 return null;
103 }
104 }