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.config.annotation;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.util.Map;
23  import java.util.WeakHashMap;
24  import java.util.logging.Level;
25  import java.util.logging.Logger;
26  
27  import javax.faces.FacesException;
28  import javax.faces.context.ExternalContext;
29  import javax.faces.context.FacesContext;
30  import javax.naming.NamingException;
31  
32  import org.apache.myfaces.shared.util.ClassUtils;
33  import org.apache.tomcat.InstanceManager;
34  
35  /**
36   * An annotation lifecycle provider for Tomcat 7.
37   */
38  public class Tomcat7AnnotationLifecycleProvider implements
39          DiscoverableLifecycleProvider, LifecycleProvider2
40  {
41  
42      private static Logger log = Logger.getLogger(Tomcat7AnnotationLifecycleProvider.class.getName());
43      
44      private WeakHashMap<ClassLoader, InstanceManager> instanceManagers = null;
45  
46      public Tomcat7AnnotationLifecycleProvider(ExternalContext externalContext)
47      {
48          instanceManagers = new WeakHashMap<ClassLoader, InstanceManager>();
49      }
50  
51      public Object newInstance(String className)
52              throws ClassNotFoundException, IllegalAccessException,
53              InstantiationException, NamingException, InvocationTargetException
54      {
55          Class<?> clazz = ClassUtils.classForName(className);
56          if (log.isLoggable(Level.FINEST))
57          {
58              log.finest("Creating instance of " + className);
59          }
60          Object object = clazz.newInstance();
61  
62          return object;
63      }
64  
65      public void destroyInstance(Object instance)
66              throws IllegalAccessException, InvocationTargetException
67      {
68          InstanceManager manager = instanceManagers
69                  .get(ClassUtils.getContextClassLoader());
70  
71          if (manager != null)
72          {
73              manager.destroyInstance(instance);
74          }
75      }
76  
77      public void postConstruct(Object instance)
78              throws IllegalAccessException, InvocationTargetException
79      {
80          InstanceManager manager = instanceManagers
81                  .get(ClassUtils.getContextClassLoader());
82          if (manager == null)
83          {
84              //Initialize manager
85              manager = initManager();
86          }
87  
88          //Is initialized
89          if (manager != null)
90          {
91              //Inject resources
92              try 
93              {
94                  manager.newInstance(instance);
95              }
96              catch (NamingException e)
97              {
98                  throw new FacesException(e);
99              }
100         }
101     }
102 
103     public boolean isAvailable()
104     {
105         try
106         {
107             Class.forName("org.apache.tomcat.InstanceManager",
108                     true, ClassUtils.getContextClassLoader());
109             return true;
110         }
111         catch (Exception e)
112         {
113             // ignore
114         }
115 
116         return false;
117     }
118 
119     private InstanceManager initManager()
120     {
121         FacesContext context = FacesContext.getCurrentInstance();
122         if (context == null)
123         {
124             return null;
125         }
126 
127         ExternalContext extCtx = context.getExternalContext();
128         if (extCtx == null)
129         {
130             return null;
131         }
132 
133         // get application map to access ServletContext attributes
134         Map<String, Object> applicationMap = extCtx.getApplicationMap();
135 
136         InstanceManager instanceManager = (InstanceManager)
137                 applicationMap.get(InstanceManager.class.getName());
138         if (instanceManager != null)
139         {
140             instanceManagers.put(ClassUtils.getContextClassLoader(),
141                     instanceManager);
142         }
143 
144         return instanceManager;
145     }
146 
147 }