View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * See JSF 2.2 section 5.6.2.8
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              // These classes will only be available with EL 3+
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              //No op
64          }
65          catch (ClassNotFoundException ex)
66          {
67              //No op
68          } 
69          catch (NoSuchMethodException e) 
70          {
71              // No op
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       * Creates a new instance of ImportHandlerResolver
81       */
82      public ImportHandlerResolver()
83      {
84      }
85  
86      /*
87       * Handle the EL case for the name of an import class
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                  // In an EL 3+ environment, the ELContext will have a getImportHandler() method
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                 //No op
116             } 
117             catch (IllegalArgumentException ex) 
118             {
119                 //No op
120             } 
121             catch (InvocationTargetException ex) 
122             {
123                 //No op
124             } 
125             catch (SecurityException ex) 
126             {
127                 //No op
128             } 
129             catch (InstantiationException e) 
130             {
131                 //No op
132             }
133         }
134         return null;
135     }
136 }