001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.lifecycle;
020:
021: import java.lang.annotation.Annotation;
022: import java.lang.reflect.InvocationTargetException;
023: import java.lang.reflect.Method;
024:
025: import javax.annotation.PostConstruct;
026: import javax.annotation.PreDestroy;
027:
028: import org.apache.axis2.jaxws.i18n.Messages;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: public abstract class BaseLifecycleManager {
033:
034: private static final Log log = LogFactory
035: .getLog(BaseLifecycleManager.class);
036:
037: protected Object instance;
038:
039: public void invokePostConstruct() throws LifecycleException {
040: if (instance == null) {
041: throw new LifecycleException(Messages
042: .getMessage("EndpointLifecycleManagerImplErr1"));
043: }
044: Method method = getPostConstructMethod();
045: if (method != null) {
046: invokePostConstruct(method);
047: }
048: }
049:
050: protected void invokePostConstruct(Method method)
051: throws LifecycleException {
052: if (log.isDebugEnabled()) {
053: log.debug("Invoking Method with @PostConstruct annotation");
054: }
055: invokeMethod(method, null);
056: if (log.isDebugEnabled()) {
057: log
058: .debug("Completed invoke on Method with @PostConstruct annotation");
059: }
060: }
061:
062: public void invokePreDestroy() throws LifecycleException {
063: if (instance == null) {
064: throw new LifecycleException(Messages
065: .getMessage("EndpointLifecycleManagerImplErr1"));
066: }
067: Method method = getPreDestroyMethod();
068: if (method != null) {
069: invokePreDestroy(method);
070: }
071: }
072:
073: protected void invokePreDestroy(Method method)
074: throws LifecycleException {
075: if (log.isDebugEnabled()) {
076: log.debug("Invoking Method with @PreDestroy annotation");
077: }
078: invokeMethod(method, null);
079: if (log.isDebugEnabled()) {
080: log
081: .debug("Completed invoke on Method with @PreDestroy annotation");
082: }
083: }
084:
085: protected void invokeMethod(Method m, Object[] params)
086: throws LifecycleException {
087: try {
088: m.invoke(instance, params);
089: } catch (InvocationTargetException e) {
090: throw new LifecycleException(e);
091: } catch (IllegalAccessException e) {
092: throw new LifecycleException(e);
093: }
094: }
095:
096: protected Method getPostConstructMethod() {
097: // REVIEW: This method should not be called in performant situations.
098: // Plus the super class methods are not being considered
099:
100: //return Method with @PostConstruct Annotation.
101: if (instance != null) {
102: Class endpointClazz = instance.getClass();
103: Method[] methods = endpointClazz.getMethods();
104:
105: for (Method method : methods) {
106: if (isPostConstruct(method)) {
107: return method;
108: }
109: }
110: }
111: return null;
112: }
113:
114: protected Method getPreDestroyMethod() {
115: // REVIEW: This method should not be called in performant situations.
116: // Plus the super class methods are not being considered
117: //return Method with @PreDestroy Annotation
118: if (instance != null) {
119: Class endpointClazz = instance.getClass();
120: Method[] methods = endpointClazz.getMethods();
121:
122: for (Method method : methods) {
123: if (isPreDestroy(method)) {
124: return method;
125: }
126: }
127: }
128: return null;
129: }
130:
131: protected boolean isPostConstruct(Method method) {
132: Annotation[] annotations = method.getDeclaredAnnotations();
133: for (Annotation annotation : annotations) {
134: return PostConstruct.class.isAssignableFrom(annotation
135: .annotationType());
136: }
137: return false;
138: }
139:
140: protected boolean isPreDestroy(Method method) {
141: Annotation[] annotations = method.getDeclaredAnnotations();
142: for (Annotation annotation : annotations) {
143: return PreDestroy.class.isAssignableFrom(annotation
144: .annotationType());
145: }
146: return false;
147: }
148:
149: }
|