📄 annotationcollection.java
字号:
//========================================================================//$Id: AnnotationCollection.java 3680 2008-09-21 10:37:13Z janb $//Copyright 2006 Mort Bay Consulting Pty. Ltd.//------------------------------------------------------------------------//Licensed under the Apache License, Version 2.0 (the "License");//you may not use this file except in compliance with the License.//You may obtain a copy of the License at //http://www.apache.org/licenses/LICENSE-2.0//Unless required by applicable law or agreed to in writing, software//distributed under the License is distributed on an "AS IS" BASIS,//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.//See the License for the specific language governing permissions and//limitations under the License.//========================================================================package org.mortbay.jetty.annotations;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.util.ArrayList;import java.util.List;import javax.annotation.Resource;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.annotation.Resources;import javax.annotation.security.RunAs;import javax.naming.InitialContext;import javax.naming.NameNotFoundException;import javax.naming.NamingException;import javax.servlet.Servlet;import javax.transaction.UserTransaction;import org.mortbay.jetty.plus.annotation.Injection;import org.mortbay.jetty.plus.annotation.InjectionCollection;import org.mortbay.jetty.plus.annotation.LifeCycleCallbackCollection;import org.mortbay.jetty.plus.annotation.PostConstructCallback;import org.mortbay.jetty.plus.annotation.PreDestroyCallback;import org.mortbay.jetty.plus.annotation.RunAsCollection;import org.mortbay.jetty.plus.naming.EnvEntry;import org.mortbay.jetty.plus.naming.Transaction;import org.mortbay.jetty.servlet.Holder;import org.mortbay.jetty.servlet.ServletHolder;import org.mortbay.jetty.webapp.WebAppContext;import org.mortbay.log.Log;import org.mortbay.util.IntrospectionUtil;import org.mortbay.util.Loader;/** * AnnotationCollection * * An AnnotationCollection represents all of the annotated classes, methods and fields in the * inheritance hierarchy for a class. NOTE that methods and fields in this collection are NOT * just the ones that are inherited by the class, but represent ALL annotations that must be * processed for a single instance of a given class. * * The class to which this collection pertains is obtained by calling * getTargetClass(). * * Using the list of annotated classes, methods and fields, the collection will generate * the appropriate JNDI entries and the appropriate Injection and LifeCycleCallback objects * to be later applied to instances of the getTargetClass(). */public class AnnotationCollection{ private WebAppContext _webApp; //the webapp private Class _targetClass; //the most derived class to which this collection pertains private List _methods = new ArrayList(); //list of methods relating to the _targetClass which have annotations private List _fields = new ArrayList(); //list of fields relating to the _targetClass which have annotations private List _classes = new ArrayList();//list of classes in the inheritance hierarchy that have annotations private static Class[] __envEntryTypes = new Class[] {String.class, Character.class, Integer.class, Boolean.class, Double.class, Byte.class, Short.class, Long.class, Float.class}; public void setWebAppContext(WebAppContext webApp) { _webApp=webApp; } public WebAppContext getWebAppContext() { return _webApp; } /** * Get the class which is the subject of these annotations * @return the clazz */ public Class getTargetClass() { return _targetClass; } /** * Set the class to which this collection pertains * @param clazz the clazz to set */ public void setTargetClass(Class clazz) { _targetClass=clazz; } public void addClass (Class clazz) { if (clazz.getDeclaredAnnotations().length==0) return; _classes.add(clazz); } public void addMethod (Method method) { if (method.getDeclaredAnnotations().length==0) return; _methods.add(method); } public void addField(Field field) { if (field.getDeclaredAnnotations().length==0) return; _fields.add(field); } public List getClasses() { return _classes; } public List getMethods () { return _methods; } public List getFields() { return _fields; } public void processRunAsAnnotations (RunAsCollection runAsCollection) { for (int i=0; i<_classes.size();i++) { Class clazz = (Class)_classes.get(i); //if this implements javax.servlet.Servlet check for run-as if (Servlet.class.isAssignableFrom(clazz)) { RunAs runAs = (RunAs)clazz.getAnnotation(RunAs.class); if (runAs != null) { String role = runAs.value(); if (role != null) { org.mortbay.jetty.plus.annotation.RunAs ra = new org.mortbay.jetty.plus.annotation.RunAs(); ra.setTargetClass(clazz); ra.setRoleName(role); runAsCollection.add(ra); } } } } } /** * Process @Resource annotations at the class, method and field level. * @return */ public InjectionCollection processResourceAnnotations(InjectionCollection injections) { processClassResourceAnnotations(); processMethodResourceAnnotations(injections); processFieldResourceAnnotations(injections); return injections; } /** * Process @PostConstruct and @PreDestroy annotations. * @return */ public LifeCycleCallbackCollection processLifeCycleCallbackAnnotations(LifeCycleCallbackCollection callbacks) { processPostConstructAnnotations(callbacks); processPreDestroyAnnotations(callbacks); return callbacks; } /** * Process @Resources annotation on classes */ public void processResourcesAnnotations () { for (int i=0; i<_classes.size();i++) { Class clazz = (Class)_classes.get(i); Resources resources = (Resources)clazz.getAnnotation(Resources.class); if (resources != null) { Resource[] resArray = resources.value(); if (resArray==null||resArray.length==0) continue; for (int j=0;j<resArray.length;j++) { String name = resArray[j].name(); String mappedName = resArray[j].mappedName(); Resource.AuthenticationType auth = resArray[j].authenticationType(); Class type = resArray[j].type(); boolean shareable = resArray[j].shareable(); if (name==null || name.trim().equals("")) throw new IllegalStateException ("Class level Resource annotations must contain a name (Common Annotations Spec Section 2.3)"); try { //TODO don't ignore the shareable, auth etc etc if (!org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(_webApp, name, mappedName)) if (!org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(_webApp.getServer(), name, mappedName)) throw new IllegalStateException("No resource bound at "+(mappedName==null?name:mappedName)); } catch (NamingException e) { throw new IllegalStateException(e); } } } } } /** * Class level Resource annotations declare a name in the * environment that will be looked up at runtime. They do * not specify an injection. */ private void processClassResourceAnnotations () { for (int i=0; i<_classes.size();i++) { Class clazz = (Class)_classes.get(i); Resource resource = (Resource)clazz.getAnnotation(Resource.class); if (resource != null) { String name = resource.name(); String mappedName = resource.mappedName(); Resource.AuthenticationType auth = resource.authenticationType(); Class type = resource.type(); boolean shareable = resource.shareable(); if (name==null || name.trim().equals("")) throw new IllegalStateException ("Class level Resource annotations must contain a name (Common Annotations Spec Section 2.3)"); try { //TODO don't ignore the shareable, auth etc etc if (!org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(_webApp, name,mappedName)) if (!org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(_webApp.getServer(), name,mappedName)) throw new IllegalStateException("No resource at "+(mappedName==null?name:mappedName)); } catch (NamingException e) { throw new IllegalStateException(e); } } } } /** * Process a Resource annotation on the Methods. * * This will generate a JNDI entry, and an Injection to be * processed when an instance of the class is created. * @param injections */ private void processMethodResourceAnnotations(InjectionCollection webXmlInjections) { //Get the method level Resource annotations for (int i=0;i<_methods.size();i++) { Method m = (Method)_methods.get(i); Resource resource = (Resource)m.getAnnotation(Resource.class); if (resource != null) { //JavaEE Spec 5.2.3: Method cannot be static if (Modifier.isStatic(m.getModifiers())) throw new IllegalStateException(m+" cannot be static"); // Check it is a valid javabean if (!IntrospectionUtil.isJavaBeanCompliantSetter(m)) throw new IllegalStateException(m+" is not a java bean compliant setter method"); //allow default name to be overridden String name = (resource.name()!=null && !resource.name().trim().equals("")? resource.name(): defaultResourceNameFromMethod(m)); //get the mappedName if there is one String mappedName = (resource.mappedName()!=null && !resource.mappedName().trim().equals("")?resource.mappedName():null); Class type = m.getParameterTypes()[0]; //get other parts that can be specified in @Resource Resource.AuthenticationType auth = resource.authenticationType(); boolean shareable = resource.shareable();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -