1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.el.unified.resolver;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24
25 import javax.el.ELContext;
26 import javax.el.ELException;
27 import javax.el.PropertyNotFoundException;
28
29 import org.apache.myfaces.shared.util.ClassUtils;
30
31
32
33
34 public class ImportHandlerResolver extends ScopedAttributeResolver
35 {
36 private static final Class EL_CLASS;
37 private static final Constructor EL_CLASS_CONSTRUCTOR;
38 private static final Method GET_IMPORT_HANDLER_METHOD;
39 private static final Method IMPORT_HANDLER_RESOLVE_CLASS_METHOD;
40
41 static
42 {
43 Class elClass = null;
44 Class importHandlerClass = null;
45 Constructor elClassConstructor = null;
46 Method getImportHandlerMethod = null;
47 Method importHandlerResolveClassMethod = null;
48 try
49 {
50
51 elClass = ClassUtils.classForName("javax.el.ELClass");
52 importHandlerClass = ClassUtils.classForName("javax.el.ImportHandler");
53 getImportHandlerMethod = ELContext.class.getMethod("getImportHandler");
54 if (elClass != null && importHandlerClass != null)
55 {
56 importHandlerResolveClassMethod =
57 importHandlerClass.getDeclaredMethod("resolveClass", new Class[] {String.class});
58 elClassConstructor = elClass.getConstructor(Class.class);
59 }
60 }
61 catch (SecurityException ex)
62 {
63
64 }
65 catch (ClassNotFoundException ex)
66 {
67
68 }
69 catch (NoSuchMethodException e)
70 {
71
72 }
73 EL_CLASS = elClass;
74 GET_IMPORT_HANDLER_METHOD = getImportHandlerMethod;
75 IMPORT_HANDLER_RESOLVE_CLASS_METHOD = importHandlerResolveClassMethod;
76 EL_CLASS_CONSTRUCTOR = elClassConstructor;
77 }
78
79
80
81
82 public ImportHandlerResolver()
83 {
84 }
85
86
87
88
89 @Override
90 public Object getValue(final ELContext context, final Object base, final Object property)
91 throws NullPointerException, PropertyNotFoundException, ELException
92 {
93 if (EL_CLASS != null && EL_CLASS_CONSTRUCTOR != null
94 && GET_IMPORT_HANDLER_METHOD != null && IMPORT_HANDLER_RESOLVE_CLASS_METHOD != null )
95 {
96 Object importHandler = null;
97 try
98 {
99
100 importHandler = GET_IMPORT_HANDLER_METHOD.invoke(context);
101 if (importHandler != null)
102 {
103 Class<?> clazz;
104 clazz = (Class<?>) IMPORT_HANDLER_RESOLVE_CLASS_METHOD
105 .invoke(importHandler, property.toString());
106 if (clazz != null)
107 {
108 context.setPropertyResolved(true);
109 return EL_CLASS_CONSTRUCTOR.newInstance(clazz);
110 }
111 }
112 }
113 catch (IllegalAccessException ex)
114 {
115
116 }
117 catch (IllegalArgumentException ex)
118 {
119
120 }
121 catch (InvocationTargetException ex)
122 {
123
124 }
125 catch (SecurityException ex)
126 {
127
128 }
129 catch (InstantiationException e)
130 {
131
132 }
133 }
134 return null;
135 }
136 }