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: */package org.apache.openejb.tomcat.common;
017:
018: /*
019: * Licensed to the Apache Software Foundation (ASF) under one or more
020: * contributor license agreements. See the NOTICE file distributed with
021: * this work for additional information regarding copyright ownership.
022: * The ASF licenses this file to You under the Apache License, Version 2.0
023: * (the "License"); you may not use this file except in compliance with
024: * the License. You may obtain a copy of the License at
025: *
026: * http://www.apache.org/licenses/LICENSE-2.0
027: *
028: * Unless required by applicable law or agreed to in writing, software
029: * distributed under the License is distributed on an "AS IS" BASIS,
030: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
031: * See the License for the specific language governing permissions and
032: * limitations under the License.
033: */
034:
035: import java.lang.reflect.Field;
036: import java.lang.reflect.InvocationTargetException;
037: import java.lang.reflect.Method;
038: import java.lang.reflect.Modifier;
039:
040: import javax.annotation.PostConstruct;
041: import javax.annotation.PreDestroy;
042: import javax.annotation.Resource;
043: import javax.ejb.EJB;
044: import javax.naming.NamingException;
045: import javax.persistence.PersistenceContext;
046: import javax.persistence.PersistenceUnit;
047: import javax.xml.ws.WebServiceRef;
048:
049: public class LegacyAnnotationProcessor {
050:
051: protected javax.naming.Context context = null;
052:
053: public LegacyAnnotationProcessor(javax.naming.Context context) {
054: this .context = context;
055: }
056:
057: /**
058: * Call postConstruct method on the specified instance.
059: */
060: public void postConstruct(Object instance)
061: throws IllegalAccessException, InvocationTargetException {
062:
063: Method[] methods = instance.getClass().getDeclaredMethods();
064: Method postConstruct = null;
065: for (Method method : methods) {
066: if (method.isAnnotationPresent(PostConstruct.class)) {
067: if ((postConstruct != null)
068: || (method.getParameterTypes().length != 0)
069: || (Modifier.isStatic(method.getModifiers()))
070: || (method.getExceptionTypes().length > 0)
071: || (!method.getReturnType().getName().equals(
072: "void"))) {
073: throw new IllegalArgumentException(
074: "Invalid PostConstruct annotation");
075: }
076: postConstruct = method;
077: }
078: }
079:
080: // At the end the postconstruct annotated
081: // method is invoked
082: if (postConstruct != null) {
083: boolean accessibility = postConstruct.isAccessible();
084: postConstruct.setAccessible(true);
085: postConstruct.invoke(instance);
086: postConstruct.setAccessible(accessibility);
087: }
088: }
089:
090: /**
091: * Call preDestroy method on the specified instance.
092: */
093: public void preDestroy(Object instance)
094: throws IllegalAccessException, InvocationTargetException {
095:
096: Method[] methods = instance.getClass().getDeclaredMethods();
097: Method preDestroy = null;
098: for (Method method : methods) {
099: if (method.isAnnotationPresent(PreDestroy.class)) {
100: if ((preDestroy != null)
101: || (method.getParameterTypes().length != 0)
102: || (Modifier.isStatic(method.getModifiers()))
103: || (method.getExceptionTypes().length > 0)
104: || (!method.getReturnType().getName().equals(
105: "void"))) {
106: throw new IllegalArgumentException(
107: "Invalid PreDestroy annotation");
108: }
109: preDestroy = method;
110: }
111: }
112:
113: // At the end the postconstruct annotated
114: // method is invoked
115: if (preDestroy != null) {
116: boolean accessibility = preDestroy.isAccessible();
117: preDestroy.setAccessible(true);
118: preDestroy.invoke(instance);
119: preDestroy.setAccessible(accessibility);
120: }
121:
122: }
123:
124: /**
125: * Inject resources in specified instance.
126: */
127: public void processAnnotations(Object instance)
128: throws IllegalAccessException, InvocationTargetException,
129: NamingException {
130:
131: if (context == null) {
132: // No resource injection
133: return;
134: }
135:
136: // Initialize fields annotations
137: Field[] fields = instance.getClass().getDeclaredFields();
138: for (Field field : fields) {
139: if (field.isAnnotationPresent(Resource.class)) {
140: Resource annotation = field
141: .getAnnotation(Resource.class);
142: lookupFieldResource(context, instance, field,
143: annotation.name());
144: }
145: if (field.isAnnotationPresent(EJB.class)) {
146: EJB annotation = field.getAnnotation(EJB.class);
147: lookupFieldResource(context, instance, field,
148: annotation.name());
149: }
150: if (field.isAnnotationPresent(WebServiceRef.class)) {
151: WebServiceRef annotation = field
152: .getAnnotation(WebServiceRef.class);
153: lookupFieldResource(context, instance, field,
154: annotation.name());
155: }
156: if (field.isAnnotationPresent(PersistenceContext.class)) {
157: PersistenceContext annotation = field
158: .getAnnotation(PersistenceContext.class);
159: lookupFieldResource(context, instance, field,
160: annotation.name());
161: }
162: if (field.isAnnotationPresent(PersistenceUnit.class)) {
163: PersistenceUnit annotation = field
164: .getAnnotation(PersistenceUnit.class);
165: lookupFieldResource(context, instance, field,
166: annotation.name());
167: }
168: }
169:
170: // Initialize methods annotations
171: Method[] methods = instance.getClass().getDeclaredMethods();
172: for (Method method : methods) {
173: if (method.isAnnotationPresent(Resource.class)) {
174: Resource annotation = method
175: .getAnnotation(Resource.class);
176: lookupMethodResource(context, instance, method,
177: annotation.name());
178: }
179: if (method.isAnnotationPresent(EJB.class)) {
180: EJB annotation = method.getAnnotation(EJB.class);
181: lookupMethodResource(context, instance, method,
182: annotation.name());
183: }
184: if (method.isAnnotationPresent(WebServiceRef.class)) {
185: WebServiceRef annotation = method
186: .getAnnotation(WebServiceRef.class);
187: lookupMethodResource(context, instance, method,
188: annotation.name());
189: }
190: if (method.isAnnotationPresent(PersistenceContext.class)) {
191: PersistenceContext annotation = method
192: .getAnnotation(PersistenceContext.class);
193: lookupMethodResource(context, instance, method,
194: annotation.name());
195: }
196: if (method.isAnnotationPresent(PersistenceUnit.class)) {
197: PersistenceUnit annotation = method
198: .getAnnotation(PersistenceUnit.class);
199: lookupMethodResource(context, instance, method,
200: annotation.name());
201: }
202: }
203:
204: }
205:
206: /**
207: * Inject resources in specified field.
208: */
209: protected static void lookupFieldResource(
210: javax.naming.Context context, Object instance, Field field,
211: String name) throws NamingException, IllegalAccessException {
212:
213: Object lookedupResource;
214: boolean accessibility;
215:
216: if ((name != null) && (name.length() > 0)) {
217: lookedupResource = context.lookup(name);
218: } else {
219: lookedupResource = context.lookup(instance.getClass()
220: .getName()
221: + "/" + field.getName());
222: }
223:
224: accessibility = field.isAccessible();
225: field.setAccessible(true);
226: field.set(instance, lookedupResource);
227: field.setAccessible(accessibility);
228: }
229:
230: /**
231: * Inject resources in specified method.
232: */
233: protected static void lookupMethodResource(
234: javax.naming.Context context, Object instance,
235: Method method, String name) throws NamingException,
236: IllegalAccessException, InvocationTargetException {
237:
238: if (!method.getName().startsWith("set")
239: || method.getParameterTypes().length != 1
240: || !method.getReturnType().getName().equals("void")) {
241: throw new IllegalArgumentException(
242: "Invalid method resource injection annotation");
243: }
244:
245: Object lookedupResource;
246: boolean accessibility;
247:
248: if ((name != null) && (name.length() > 0)) {
249: lookedupResource = context.lookup(name);
250: } else {
251: lookedupResource = context.lookup(instance.getClass()
252: .getName()
253: + "/" + method.getName().substring(3));
254: }
255:
256: accessibility = method.isAccessible();
257: method.setAccessible(true);
258: method.invoke(instance, lookedupResource);
259: method.setAccessible(accessibility);
260: }
261:
262: }
|