injectintrospector.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 866 行 · 第 1/2 页
JAVA
866 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.config.j2ee;import com.caucho.config.program.SingletonGenerator;import com.caucho.config.program.ComponentValueGenerator;import com.caucho.config.program.FieldGeneratorProgram;import com.caucho.config.program.MethodGeneratorProgram;import com.caucho.config.program.ValueGenerator;import com.caucho.amber.manager.*;import com.caucho.config.program.ConfigProgram;import com.caucho.config.ConfigException;import com.caucho.config.program.ConfigProgram;import com.caucho.naming.Jndi;import com.caucho.util.L10N;import com.caucho.util.Log;import com.caucho.webbeans.component.ComponentImpl;import com.caucho.webbeans.manager.WebBeansContainer;import javax.annotation.*;import javax.ejb.EJB;import javax.ejb.EJBs;import javax.naming.*;import javax.interceptor.*;import javax.persistence.*;import javax.webbeans.*;//import javax.xml.ws.WebServiceRef;import java.beans.Introspector;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.*;import java.util.logging.Logger;import java.util.concurrent.*;/** * Analyzes a bean for @Inject tags. */public class InjectIntrospector { private static final L10N L = new L10N(InjectIntrospector.class); private static final Logger log = Log.open(InjectIntrospector.class); private static HashMap<Class,Class> _primitiveTypeMap = new HashMap<Class,Class>(); public static InjectProgram introspectProgram(Class type) { ArrayList<ConfigProgram> injectList = new ArrayList<ConfigProgram>(); introspectInject(injectList, type); introspectInit(injectList, type); return new InjectProgram(injectList); } public static void introspectInit(ArrayList<ConfigProgram> initList, Class type) throws ConfigException { if (type == null || type.equals(Object.class)) return; introspectInit(initList, type.getSuperclass()); for (Method method : type.getDeclaredMethods()) { if (! method.isAnnotationPresent(PostConstruct.class)) continue; if (method.getParameterTypes().length == 1 && InvocationContext.class.equals(method.getParameterTypes()[0])) continue; if (method.getParameterTypes().length != 0) throw new ConfigException(location(method) + L.l("{0}: @PostConstruct is requires zero arguments")); PostConstructProgram initProgram = new PostConstructProgram(method); if (! initList.contains(initProgram)) initList.add(initProgram); } } public static void introspectDestroy(ArrayList<ConfigProgram> destroyList, Class type) throws ConfigException { if (type == null || type.equals(Object.class)) return; introspectDestroy(destroyList, type.getSuperclass()); for (Method method : type.getDeclaredMethods()) { if (method.isAnnotationPresent(PreDestroy.class)) { Class []types = method.getParameterTypes(); if (types.length == 0) { } else if (types.length == 1 && types[0].equals(InvocationContext.class)) { // XXX: continue; } else throw new ConfigException(location(method) + L.l("@PreDestroy is requires zero arguments")); PreDestroyInject destroyProgram = new PreDestroyInject(method); if (! destroyList.contains(destroyProgram)) destroyList.add(destroyProgram); } } } public static void introspectConstruct(ArrayList<ConfigProgram> initList, Class type) throws ConfigException { if (type == null || type.equals(Object.class)) return; for (Method method : type.getDeclaredMethods()) { if (method.isAnnotationPresent(PostConstruct.class)) { if (method.getParameterTypes().length != 0) throw new ConfigException(L.l("{0}: @PostConstruct is requires zero arguments", method.getName())); PostConstructProgram initProgram = new PostConstructProgram(method); if (! initList.contains(initProgram)) initList.add(initProgram); } if (method.isAnnotationPresent(PreDestroy.class)) { if (method.getParameterTypes().length != 0) throw new ConfigException(L.l("{0}: @PreDestroy is requires zero arguments", method.getName())); initList.add(new PreDestroyProgram(method)); } } introspectConstruct(initList, type.getSuperclass()); } public static void introspectInject(ArrayList<ConfigProgram> injectList, Class type) throws ConfigException { try { introspectInjectImpl(injectList, type); } catch (ClassNotFoundException e) { log.warning(type + " injection " + e); } catch (NoClassDefFoundError e) { log.warning(type + " injection " + e); } } private static void introspectInjectImpl(ArrayList<ConfigProgram> injectList, Class type) throws ConfigException, ClassNotFoundException { if (type == null || type.equals(Object.class)) return; introspectInjectImpl(injectList, type.getSuperclass()); configureClassResources(injectList, type); for (Field field : type.getDeclaredFields()) { if (hasBindingAnnotation(field)) { WebBeansContainer webBeans = WebBeansContainer.create(); boolean isOptional = isBindingOptional(field); webBeans.createProgram(injectList, field, isOptional); continue; } introspect(injectList, field); } for (Method method : type.getDeclaredMethods()) { String fieldName = method.getName(); Class []param = method.getParameterTypes(); if (hasBindingAnnotation(method)) { WebBeansContainer webBeans = WebBeansContainer.create(); webBeans.createProgram(injectList, method); continue; } if (param.length != 1) continue; /* if (fieldName.startsWith("set") && fieldName.length() > 3) { fieldName = fieldName.substring(3); char ch = fieldName.charAt(0); if (Character.isUpperCase(ch) && (fieldName.length() == 1 || Character.isLowerCase(fieldName.charAt(1)))) { fieldName = Character.toLowerCase(ch) + fieldName.substring(1); } } */ introspect(injectList, method); } } public static void configureClassResources(ArrayList<ConfigProgram> initList, Class type) throws ConfigException { String location = type.getName() + ": "; Resources resources = (Resources) type.getAnnotation(Resources.class); if (resources != null) { for (Resource resource : resources.value()) { introspectClassResource(initList, type, resource); } } Resource resource = (Resource) type.getAnnotation(Resource.class); if (resource != null) { introspectClassResource(initList, type, resource); } PersistenceContext pc = (PersistenceContext) type.getAnnotation(PersistenceContext.class); if (pc != null) introspectClassPersistenceContext(initList, type, pc); // ejb/0f66 EJB ejb = (EJB) type.getAnnotation(EJB.class); // ejb/0f67 EJBs ejbs = (EJBs) type.getAnnotation(EJBs.class); if (ejb != null && ejbs != null) { throw new ConfigException(L.l("{0} cannot have both @EJBs and @EJB", type.getName())); } else if (ejb != null) { if (Object.class.equals(ejb.beanInterface())) throw new ConfigException(location + L.l("@EJB at the class level must have a beanInterface()")); if ("".equals(ejb.name())) throw new ConfigException(location + L.l("@EJB at the class level must have a name()")); generateEjb(location, Object.class, "", ejb); } else if (ejbs != null) { for (EJB childEjb : ejbs.value()) { if (Object.class.equals(childEjb.beanInterface())) throw new ConfigException(location + L.l("@EJB at the class level must have a beanInterface()")); if ("".equals(childEjb.name())) throw new ConfigException(location + L.l("@EJB at the class level must have a name()")); generateEjb(location, Object.class, "", childEjb); } } } private static void introspectClassResource(ArrayList<ConfigProgram> initList, Class type, Resource resource) throws ConfigException { String name = resource.name(); Field field = findField(type, name); if (field != null) { ValueGenerator gen = generateResource(location(field), field.getType(), "", resource); return; } Method method = findMethod(type, name); if (method != null) { ValueGenerator gen = generateResource(location(method), method.getParameterTypes()[0], "", resource); return; } } private static void introspectClassPersistenceContext(ArrayList<ConfigProgram> initList, Class type, PersistenceContext pContext) throws ConfigException { String location = type.getSimpleName() + ": "; ValueGenerator gen = generatePersistenceContext(location, EntityManager.class, "", pContext); } private static void introspect(ArrayList<ConfigProgram> injectList, Field field) throws ConfigException { String location = location(field); ValueGenerator gen = null; // default jndiName String jndiName = field.getDeclaringClass().getName() + "/" + field.getName(); if (field.isAnnotationPresent(Resource.class)) { Resource resource = field.getAnnotation(Resource.class); gen = generateResource(location, field.getType(), jndiName, resource); } else if (field.isAnnotationPresent(EJB.class)) { EJB ejb = field.getAnnotation(EJB.class); gen = generateEjb(location, field.getType(), jndiName, ejb); } else if (field.isAnnotationPresent(PersistenceUnit.class)) { PersistenceUnit pUnit = field.getAnnotation(PersistenceUnit.class); gen = generatePersistenceUnit(location, field.getType(), jndiName, pUnit); } else if (field.isAnnotationPresent(PersistenceContext.class)) { PersistenceContext pContext = field.getAnnotation(PersistenceContext.class); gen = generatePersistenceContext(location, field.getType(), jndiName, pContext); } /* else if (field.isAnnotationPresent(WebServiceRef.class)) { WebServiceRef webService = field.getAnnotation(WebServiceRef.class); gen = generateWebService(location, field.getType(), jndiName, webService); } */ else if (hasBindingAnnotation(field)) introspectWebBean(injectList, field); if (gen != null) injectList.add(new FieldGeneratorProgram(field, gen)); } private static void introspect(ArrayList<ConfigProgram> injectList, Method method) throws ConfigException { String location = location(method); ValueGenerator gen = null; Class type = null; // default jndi name String jndiName = method.getDeclaringClass().getName() + "/" + method.getName(); if (method.getParameterTypes().length > 0) type = method.getParameterTypes()[0]; if (method.isAnnotationPresent(Resource.class)) { Resource resource = method.getAnnotation(Resource.class); gen = generateResource(location, type, jndiName, resource); } else if (method.isAnnotationPresent(EJB.class)) { EJB ejb = method.getAnnotation(EJB.class); gen = generateEjb(location, type, jndiName, ejb); } else if (method.isAnnotationPresent(PersistenceUnit.class)) { PersistenceUnit pUnit = method.getAnnotation(PersistenceUnit.class); gen = generatePersistenceUnit(location, type, jndiName, pUnit); } else if (method.isAnnotationPresent(PersistenceContext.class)) { PersistenceContext pContext = method.getAnnotation(PersistenceContext.class); gen = generatePersistenceContext(location, type, jndiName, pContext); } /*
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?