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.geronimo.jetty6;
017:
018: import java.lang.reflect.Field;
019: import java.lang.reflect.Method;
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.ejb.EJB;
024: import javax.naming.Context;
025: import javax.naming.InitialContext;
026: import javax.naming.NamingException;
027: import javax.servlet.Servlet;
028:
029: import org.mortbay.jetty.servlet.ServletHandler;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.geronimo.j2ee.annotation.Injection;
033:
034: /**
035: * @version $Rev$ $Date$
036: */
037: class InjectionServletHandler extends ServletHandler {
038: private static final Log log = LogFactory
039: .getLog(InjectionServletHandler.class);
040:
041: private final Map<String, List<Injection>> injections;
042:
043: public InjectionServletHandler(
044: Map<String, List<Injection>> injections) {
045: this .injections = injections;
046: }
047:
048: public Servlet customizeServlet(Servlet servlet) throws Exception {
049: List<Injection> classInjections = injections.get(servlet
050: .getClass().getName());
051: if (classInjections != null) {
052: for (Injection injection : classInjections) {
053:
054: }
055: }
056: servlet = super .customizeServlet(servlet);
057: processAnnotations(servlet);
058: return servlet;
059: }
060:
061: /**
062: * Inject resources in specified instance.
063: */
064: public void processAnnotations(Object instance) {
065: Context context = null;
066: try {
067: context = (Context) new InitialContext()
068: .lookup("java:comp/env");
069: } catch (Exception e) {
070: return;
071: }
072: if (context == null) {
073: // No resource injection
074: return;
075: }
076:
077: // Initialize fields annotations
078: Field[] fields = instance.getClass().getDeclaredFields();
079: for (int i = 0; i < fields.length; i++) {
080: if (fields[i].isAnnotationPresent(EJB.class)) {
081: EJB annotation = fields[i].getAnnotation(EJB.class);
082: lookupFieldResource(context, instance, fields[i],
083: annotation.name());
084: }
085: }
086:
087: // Initialize methods annotations
088: Method[] methods = instance.getClass().getDeclaredMethods();
089: for (int i = 0; i < methods.length; i++) {
090: if (methods[i].isAnnotationPresent(EJB.class)) {
091: EJB annotation = methods[i].getAnnotation(EJB.class);
092: lookupMethodResource(context, instance, methods[i],
093: annotation.name());
094: }
095: }
096:
097: }
098:
099: /**
100: * Inject resources in specified field.
101: */
102: protected static void lookupFieldResource(
103: javax.naming.Context context, Object instance, Field field,
104: String name) {
105: try {
106: Object lookedupResource = null;
107: boolean accessibility = false;
108:
109: if ((name != null) && (name.length() > 0)) {
110: lookedupResource = context.lookup(name);
111: } else {
112: lookedupResource = context.lookup(instance.getClass()
113: .getName()
114: + "/" + field.getName());
115: }
116:
117: accessibility = field.isAccessible();
118: field.setAccessible(true);
119: field.set(instance, lookedupResource);
120: field.setAccessible(accessibility);
121: } catch (Exception e) {
122: log.error("Error injecting into "
123: + instance.getClass().getName() + "."
124: + field.getName(), e);
125: }
126: }
127:
128: /**
129: * Inject resources in specified method.
130: */
131: protected static void lookupMethodResource(
132: javax.naming.Context context, Object instance,
133: Method method, String name) {
134: try {
135: if (!method.getName().startsWith("set")
136: || method.getParameterTypes().length != 1
137: || !method.getReturnType().getName().equals("void")) {
138: throw new IllegalArgumentException(
139: "Invalid method resource injection annotation");
140: }
141:
142: Object lookedupResource = null;
143: boolean accessibility = false;
144:
145: if ((name != null) && (name.length() > 0)) {
146: lookedupResource = context.lookup(name);
147: } else {
148: lookedupResource = context.lookup(instance.getClass()
149: .getName()
150: + "/" + method.getName().substring(3));
151: }
152:
153: accessibility = method.isAccessible();
154: method.setAccessible(true);
155: method.invoke(instance, lookedupResource);
156: method.setAccessible(accessibility);
157: } catch (Exception e) {
158: log.error("Error injecting into "
159: + instance.getClass().getName() + "."
160: + method.getName() + "()", e);
161: }
162: }
163: }
|