/** * Process the annotations for the listeners. */ protected static void loadApplicationListenerAnnotations(Context context) { Class<?> classClass = null; String[] applicationListeners = context.findApplicationListeners(); for (int i = 0; i < applicationListeners.length; i++) { classClass = Introspection.loadClass(context, applicationListeners[i]); if (classClass == null) { continue; } loadClassAnnotation(context, classClass); loadFieldsAnnotation(context, classClass); loadMethodsAnnotation(context, classClass); } }
/** * Process the annotations for the filters. */ protected static void loadApplicationFilterAnnotations(Context context) { Class<?> classClass = null; FilterDef[] filterDefs = context.findFilterDefs(); for (int i = 0; i < filterDefs.length; i++) { classClass = Introspection.loadClass(context, (filterDefs[i]).getFilterClass()); if (classClass == null) { continue; } loadClassAnnotation(context, classClass); loadFieldsAnnotation(context, classClass); loadMethodsAnnotation(context, classClass); } }
protected static void loadMethodsAnnotation(Context context, Class<?> classClass) { // Initialize the annotations Method[] methods = Introspection.getDeclaredMethods(classClass); if (methods != null && methods.length > 0) { for (Method method : methods) { Resource annotation = method.getAnnotation(Resource.class); if (annotation != null) { if (!Introspection.isValidSetter(method)) { throw new IllegalArgumentException(sm.getString( "webAnnotationSet.invalidInjection")); } String defaultName = classClass.getName() + SEPARATOR + Introspection.getPropertyName(method); Class<?> defaultType = (method.getParameterTypes()[0]); addResource(context, annotation, defaultName, defaultType); } } } }
private static Method findLifecycleCallback(Method currentMethod, String methodNameFromXml, Method method, Class<? extends Annotation> annotation) { Method result = currentMethod; if (methodNameFromXml != null) { if (method.getName().equals(methodNameFromXml)) { if (!Introspection.isValidLifecycleCallback(method)) { throw new IllegalArgumentException( "Invalid " + annotation.getName() + " annotation"); } result = method; } } else { if (method.isAnnotationPresent(annotation)) { if (currentMethod != null || !Introspection.isValidLifecycleCallback(method)) { throw new IllegalArgumentException( "Invalid " + annotation.getName() + " annotation"); } result = method; } } return result; }
protected static void loadMethodsAnnotation(Context context, Class<?> classClass) { // Initialize the annotations Method[] methods = Introspection.getDeclaredMethods(classClass); if (methods != null && methods.length > 0) { for (Method method : methods) { Resource annotation = method.getAnnotation(Resource.class); if (annotation != null) { if (!Introspection.isValidSetter(method)) { throw new IllegalArgumentException(sm.getString("webAnnotationSet.invalidInjection")); } String defaultName = classClass.getName() + SEPARATOR + Introspection.getPropertyName(method); Class<?> defaultType = (method.getParameterTypes()[0]); addResource(context, annotation, defaultName, defaultType); } } } }
/** * Inject resources in specified method. * * @param context * jndi context to extract value from * @param instance * object to inject into * @param method * field target for injection * @param name * jndi name value is bound under * @param clazz * class annotation is defined in * @throws IllegalAccessException * if method is inaccessible * @throws javax.naming.NamingException * if value is not accessible in naming context * @throws java.lang.reflect.InvocationTargetException * if setter call fails */ protected static void lookupMethodResource(Context context, Object instance, Method method, String name, Class<?> clazz) throws NamingException, IllegalAccessException, InvocationTargetException { if (!Introspection.isValidSetter(method)) { throw new IllegalArgumentException(sm.getString("defaultInstanceManager.invalidInjection")); } Object lookedupResource; boolean accessibility; String normalizedName = normalize(name); if ((normalizedName != null) && (normalizedName.length() > 0)) { lookedupResource = context.lookup(normalizedName); } else { lookedupResource = context.lookup(clazz.getName() + "/" + Introspection.getPropertyName(method)); } synchronized (method) { accessibility = method.isAccessible(); method.setAccessible(true); method.invoke(instance, lookedupResource); method.setAccessible(accessibility); } }
private static Method findLifecycleCallback(Method currentMethod, String methodNameFromXml, Method method, Class<? extends Annotation> annotation) { Method result = currentMethod; if (methodNameFromXml != null) { if (method.getName().equals(methodNameFromXml)) { if (!Introspection.isValidLifecycleCallback(method)) { throw new IllegalArgumentException("Invalid " + annotation.getName() + " annotation"); } result = method; } } else { if (method.isAnnotationPresent(annotation)) { if (currentMethod != null || !Introspection.isValidLifecycleCallback(method)) { throw new IllegalArgumentException("Invalid " + annotation.getName() + " annotation"); } result = method; } } return result; }
/** * Process the annotations for the listeners. */ protected static void loadApplicationListenerAnnotations(Context context) { Class<?> classClass = null; @SuppressWarnings("deprecation") String[] applicationListeners = context.findApplicationListeners(); for (int i = 0; i < applicationListeners.length; i++) { classClass = Introspection.loadClass(context, applicationListeners[i]); if (classClass == null) { continue; } loadClassAnnotation(context, classClass); loadFieldsAnnotation(context, classClass); loadMethodsAnnotation(context, classClass); } }
protected static void loadFieldsAnnotation(Context context, Class<?> classClass) { // Initialize the annotations Field[] fields = Introspection.getDeclaredFields(classClass); if (fields != null && fields.length > 0) { for (Field field : fields) { if (field.isAnnotationPresent(Resource.class)) { Resource annotation = field.getAnnotation(Resource.class); String defaultName = classClass.getName() + SEPARATOR + field.getName(); Class<?> defaultType = field.getType(); addResource(context, annotation, defaultName, defaultType); } } } }
protected static void loadMethodsAnnotation(Context context, Class<?> classClass) { // Initialize the annotations Method[] methods = Introspection.getDeclaredMethods(classClass); if (methods != null && methods.length > 0) { for (Method method : methods) { if (method.isAnnotationPresent(Resource.class)) { Resource annotation = method.getAnnotation(Resource.class); if (!Introspection.isValidSetter(method)) { throw new IllegalArgumentException(sm.getString( "webAnnotationSet.invalidInjection")); } String defaultName = classClass.getName() + SEPARATOR + Introspection.getPropertyName(method); Class<?> defaultType = (method.getParameterTypes()[0]); addResource(context, annotation, defaultName, defaultType); } } } }
protected static void loadFieldsAnnotation(Context context, Class<?> classClass) { // Initialize the annotations Field[] fields = Introspection.getDeclaredFields(classClass); if (fields != null && fields.length > 0) { for (Field field : fields) { Resource annotation = field.getAnnotation(Resource.class); if (annotation != null) { String defaultName = classClass.getName() + SEPARATOR + field.getName(); Class<?> defaultType = field.getType(); addResource(context, annotation, defaultName, defaultType); } } } }
private static String getType(Resource annotation, Class<?> defaultType) { Class<?> type = annotation.type(); if (type == null || type.equals(Object.class)) { if (defaultType != null) { type = defaultType; } } return Introspection.convertPrimitiveType(type).getCanonicalName(); }
/** * Checks that the configuration of the type for the specified resource is * consistent with any injection targets and if the type is not specified, * tries to configure the type based on the injection targets * * @param resource The resource to check * * @return <code>true</code> if the type for the resource is now valid (if * previously <code>null</code> this means it is now set) or * <code>false</code> if the current resource type is inconsistent * with the injection targets and/or cannot be determined */ private boolean checkResourceType(ResourceBase resource) { if (!(container instanceof Context)) { // Only Context's will have injection targets return true; } if (resource.getInjectionTargets() == null || resource.getInjectionTargets().size() == 0) { // No injection targets so use the defined type for the resource return true; } Context context = (Context) container; String typeName = resource.getType(); Class<?> typeClass = null; if (typeName != null) { typeClass = Introspection.loadClass(context, typeName); if (typeClass == null) { // Can't load the type - will trigger a failure later so don't // fail here return true; } } Class<?> compatibleClass = getCompatibleType(context, resource, typeClass); if (compatibleClass == null) { // Indicates that a compatible type could not be identified that // worked for all injection targets return false; } resource.setType(compatibleClass.getCanonicalName()); return true; }
private Class<?> getSetterType(Class<?> clazz, String name) { Method[] methods = Introspection.getDeclaredMethods(clazz); if (methods != null && methods.length > 0) { for (Method method : methods) { if (Introspection.isValidSetter(method) && Introspection.getPropertyName(method).equals(name)) { return method.getParameterTypes()[0]; } } } return null; }
private Class<?> getFieldType(Class<?> clazz, String name) { Field[] fields = Introspection.getDeclaredFields(clazz); if (fields != null && fields.length > 0) { for (Field field : fields) { if (field.getName().equals(name)) { return field.getType(); } } } return null; }
/** * Inject resources in specified method. * * @param context jndi context to extract value from * @param instance object to inject into * @param method field target for injection * @param name jndi name value is bound under * @param clazz class annotation is defined in * @throws IllegalAccessException if method is inaccessible * @throws javax.naming.NamingException if value is not accessible in naming context * @throws java.lang.reflect.InvocationTargetException * if setter call fails */ protected static void lookupMethodResource(Context context, Object instance, Method method, String name, Class<?> clazz) throws NamingException, IllegalAccessException, InvocationTargetException { if (!Introspection.isValidSetter(method)) { throw new IllegalArgumentException( sm.getString("defaultInstanceManager.invalidInjection")); } Object lookedupResource; boolean accessibility; String normalizedName = normalize(name); if ((normalizedName != null) && (normalizedName.length() > 0)) { lookedupResource = context.lookup(normalizedName); } else { lookedupResource = context.lookup( clazz.getName() + "/" + Introspection.getPropertyName(method)); } synchronized (method) { accessibility = method.isAccessible(); method.setAccessible(true); method.invoke(instance, lookedupResource); method.setAccessible(accessibility); } }