⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 annotationcollection.java

📁 jetty SERVER連接資料庫用的軟體
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                //if @Resource specifies a type, check it is compatible with setter param                if ((resource.type() != null)                         &&                         !resource.type().equals(Object.class)                        &&                        (!IntrospectionUtil.isTypeCompatible(type, resource.type(), false)))                    throw new IllegalStateException("@Resource incompatible type="+resource.type()+ " with method param="+type+ " for "+m);                               //check if an injection has already been setup for this target by web.xml                Injection webXmlInjection = webXmlInjections.getInjection(getTargetClass(), m);                                if (webXmlInjection == null)                {                    try                    {                        //try binding name to environment                        //try the webapp's environment first                        boolean bound = org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(_webApp, name, mappedName);                                                //try the server's environment                        if (!bound)                            bound = org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(_webApp.getServer(), name, mappedName);                                                //try the jvm's environment                        if (!bound)                            bound = org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(null, name, mappedName);                                                //TODO if it is an env-entry from web.xml it can be injected, in which case there will be no                        //NamingEntry, just a value bound in java:comp/env                        if (!bound)                        {                            try                            {                                InitialContext ic = new InitialContext();                                String nameInEnvironment = (mappedName!=null?mappedName:name);                                ic.lookup("java:comp/env/"+nameInEnvironment);                                                               bound = true;                            }                            catch (NameNotFoundException e)                            {                                bound = false;                            }                        }                                                if (bound)                        {                            Log.debug("Bound "+(mappedName==null?name:mappedName) + " as "+ name);                            //   Make the Injection for it                            Injection injection = new Injection();                            injection.setTargetClass(getTargetClass());                            injection.setJndiName(name);                            injection.setMappingName(mappedName);                            injection.setTarget(m);                            webXmlInjections.add(injection);                        }                        else if (!isEnvEntryType(type))                        {                            //if this is an env-entry type resource and there is no value bound for it, it isn't                            //an error, it just means that perhaps the code will use a default value instead                            // JavaEE Spec. sec 5.4.1.3                               throw new IllegalStateException("No resource at "+(mappedName==null?name:mappedName));                        }                    }                    catch (NamingException e)                    {                                                throw new IllegalStateException(e);                    }                }                else                {                    //if an injection is already set up for this name, then the types must be compatible                    //JavaEE spec sec 5.2.4                    try                    {                         Object value = webXmlInjection.lookupInjectedValue();                         if (!IntrospectionUtil.isTypeCompatible(type, value.getClass(), false))                             throw new IllegalStateException("Type of field="+type+" is not compatible with Resource type="+value.getClass());                    }                    catch (NamingException e)                    {                        throw new IllegalStateException(e);                    }                }            }        }    }            /**     * Process @Resource annotation for a Field. These will both set up a     * JNDI entry and generate an Injection. Or they can be the equivalent     * of env-entries with default values     *      * @param injections     */    private void processFieldResourceAnnotations (InjectionCollection webXmlInjections)    {        for (int i=0;i<_fields.size();i++)        {            Field f = (Field)_fields.get(i);            Resource resource = (Resource)f.getAnnotation(Resource.class);            if (resource != null)            {                //JavaEE Spec 5.2.3: Field cannot be static                if (Modifier.isStatic(f.getModifiers()))                    throw new IllegalStateException(f+" cannot be static");                                //JavaEE Spec 5.2.3: Field cannot be final                if (Modifier.isFinal(f.getModifiers()))                    throw new IllegalStateException(f+" cannot be final");                                //work out default name                String name = f.getDeclaringClass().getCanonicalName()+"/"+f.getName();                //allow @Resource name= to override the field name                name = (resource.name()!=null && !resource.name().trim().equals("")? resource.name(): name);                                //get the type of the Field                Class type = f.getType();                //if @Resource specifies a type, check it is compatible with field type                if ((resource.type() != null)                        &&                         !resource.type().equals(Object.class)                        &&                        (!IntrospectionUtil.isTypeCompatible(type, resource.type(), false)))                    throw new IllegalStateException("@Resource incompatible type="+resource.type()+ " with field type ="+f.getType());                                //get the mappedName if there is one                String mappedName = (resource.mappedName()!=null && !resource.mappedName().trim().equals("")?resource.mappedName():null);                //get other parts that can be specified in @Resource                Resource.AuthenticationType auth = resource.authenticationType();                boolean shareable = resource.shareable();                            //check if an injection has already been setup for this target by web.xml                Injection webXmlInjection = webXmlInjections.getInjection(getTargetClass(), f);                if (webXmlInjection == null)                {                    try                    {                        boolean bound = org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(_webApp, name, mappedName);                        if (!bound)                            bound = org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(_webApp.getServer(), name, mappedName);                        if (!bound)                            bound =  org.mortbay.jetty.plus.naming.NamingEntryUtil.bindToENC(null, name, mappedName);                         if (!bound)                        {                            //see if there is an env-entry value been bound from web.xml                            try                            {                                InitialContext ic = new InitialContext();                                String nameInEnvironment = (mappedName!=null?mappedName:name);                                ic.lookup("java:comp/env/"+nameInEnvironment);                                                               bound = true;                            }                            catch (NameNotFoundException e)                            {                                bound = false;                            }                        }                        //Check there is a JNDI entry for this annotation                         if (bound)                        {                             Log.debug("Bound "+(mappedName==null?name:mappedName) + " as "+ name);                            //   Make the Injection for it if the binding succeeded                            Injection injection = new Injection();                            injection.setTargetClass(getTargetClass());                            injection.setJndiName(name);                            injection.setMappingName(mappedName);                            injection.setTarget(f);                            webXmlInjections.add(injection);                         }                        else if (!isEnvEntryType(type))                        {                            //if this is an env-entry type resource and there is no value bound for it, it isn't                            //an error, it just means that perhaps the code will use a default value instead                            // JavaEE Spec. sec 5.4.1.3                            throw new IllegalStateException("No resource at "+(mappedName==null?name:mappedName));                        }                    }                    catch (NamingException e)                    {                        throw new IllegalStateException(e);                    }                }                else                {                    //if an injection is already set up for this name, then the types must be compatible                    //JavaEE spec sec 5.2.4                    try                    {                         Object value = webXmlInjection.lookupInjectedValue();                         if (!IntrospectionUtil.isTypeCompatible(type, value.getClass(), false))                             throw new IllegalStateException("Type of field="+type+" is not compatible with Resource type="+value.getClass());                    }                    catch (NamingException e)                    {                        throw new IllegalStateException(e);                    }                }            }        }      }            /**     * Find @PostConstruct annotations.     *      * The spec says (Common Annotations Sec 2.5) that only ONE method     * may be adorned with the PostConstruct annotation, however this does     * not clarify how this works with inheritance.     *      * TODO work out what to do with inherited PostConstruct annotations     *      * @param callbacks     */    private void processPostConstructAnnotations (LifeCycleCallbackCollection callbacks)    {        //      TODO: check that the same class does not have more than one        for (int i=0; i<_methods.size(); i++)        {            Method m = (Method)_methods.get(i);            if (m.isAnnotationPresent(PostConstruct.class))            {                if (m.getParameterTypes().length != 0)                    throw new IllegalStateException(m+" has parameters");                if (m.getReturnType() != Void.TYPE)                    throw new IllegalStateException(m+" is not void");                if (m.getExceptionTypes().length != 0)                    throw new IllegalStateException(m+" throws checked exceptions");                if (Modifier.isStatic(m.getModifiers()))                    throw new IllegalStateException(m+" is static");                                                PostConstructCallback callback = new PostConstructCallback();                callback.setTargetClass(getTargetClass());                callback.setTarget(m);                callbacks.add(callback);            }        }    }        /**     * Find @PreDestroy annotations.     *      * The spec says (Common Annotations Sec 2.5) that only ONE method     * may be adorned with the PreDestroy annotation, however this does     * not clarify how this works with inheritance.     *      * TODO work out what to do with inherited PreDestroy annotations     * @param callbacks     */    private void processPreDestroyAnnotations (LifeCycleCallbackCollection callbacks)    {        //TODO: check that the same class does not have more than one                for (int i=0; i<_methods.size(); i++)        {            Method m = (Method)_methods.get(i);            if (m.isAnnotationPresent(PreDestroy.class))            {                if (m.getParameterTypes().length != 0)                    throw new IllegalStateException(m+" has parameters");                if (m.getReturnType() != Void.TYPE)                    throw new IllegalStateException(m+" is not void");                if (m.getExceptionTypes().length != 0)                    throw new IllegalStateException(m+" throws checked exceptions");                if (Modifier.isStatic(m.getModifiers()))                    throw new IllegalStateException(m+" is static");                                PreDestroyCallback callback = new PreDestroyCallback();                 callback.setTargetClass(getTargetClass());                callback.setTarget(m);                callbacks.add(callback);            }        }    }         private static boolean isEnvEntryType (Class type)    {        boolean result = false;        for (int i=0;i<__envEntryTypes.length && !result;i++)        {            result = (type.equals(__envEntryTypes[i]));        }        return result;    }        private static Class getNamingEntryType (Class type)    {        if (type==null)            return null;                if (isEnvEntryType(type))            return EnvEntry.class;                if (type.getName().equals("javax.transaction.UserTransaction"))                return Transaction.class;        else            return org.mortbay.jetty.plus.naming.Resource.class;    }        private String defaultResourceNameFromMethod (Method m)    {        String name = m.getName().substring(3);        name = name.substring(0,1).toLowerCase()+name.substring(1);        return m.getDeclaringClass().getCanonicalName()+"/"+name;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -