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.orchestra.dynaForm.jsf.guiBuilder.impl.jsf;
20  
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.myfaces.orchestra.dynaForm.jsf.guiBuilder.impl.myfaces.MyFacesCheck;
23  import org.apache.myfaces.orchestra.lib.OrchestraException;
24  import org.apache.myfaces.shared_orchestra.util.ClassUtils;
25  
26  import javax.faces.context.FacesContext;
27  import java.lang.reflect.Constructor;
28  import java.lang.reflect.InvocationTargetException;
29  
30  public class JsfGuiBuilderFactory
31  {
32      public final static String CONTEXT_GUI_BUILDER = "org.apache.myfaces.custom.dynaForm.GUI_BUILDER";
33  
34      private JsfGuiBuilderFactory()
35      {
36      }
37  
38      public static JsfGuiBuilder create(final FacesContext facesContext)
39      {
40          JsfGuiBuilder guiBuilder = null;
41  
42          String guiBuilderName = facesContext.getExternalContext().getInitParameter(CONTEXT_GUI_BUILDER);
43          if (guiBuilderName == null)
44          {
45              guiBuilder = createGuiBuilderInternal();
46          }
47          else
48          {
49              try
50              {
51                  @SuppressWarnings("unchecked")
52                  Class<? extends JsfGuiBuilder> guiBuilderClass =
53                      (Class<? extends JsfGuiBuilder>) Class.forName(guiBuilderName);
54                  try
55                  {
56                      // try to find a decorator constructor
57                      Constructor<? extends JsfGuiBuilder> decoratorConst =
58                          guiBuilderClass.getConstructor(new Class[]{JsfGuiBuilder.class});
59                      return decoratorConst.newInstance(new Object[]{
60                          createGuiBuilderInternal()
61                      });
62                  }
63                  catch (NoSuchMethodException e)
64                  {
65                      // not - so letz create a plain instance
66                      return (JsfGuiBuilder) guiBuilderClass.newInstance();
67                  }
68              }
69              catch (IllegalArgumentException e)
70              {
71                  throw new OrchestraException(e);
72              }
73              catch (InvocationTargetException e)
74              {
75                  throw new OrchestraException(e);
76              }
77              catch (SecurityException e)
78              {
79                  throw new OrchestraException(e);
80              }
81              catch (InstantiationException e)
82              {
83                  throw new OrchestraException(e);
84              }
85              catch (IllegalAccessException e)
86              {
87                  throw new OrchestraException(e);
88              }
89              catch (ClassNotFoundException e)
90              {
91                  throw new OrchestraException(e);
92              }
93          }
94          return guiBuilder;
95      }
96  
97      protected static JsfGuiBuilder createGuiBuilderInternal()
98      {
99          JsfGuiBuilder guiBuilder = null;
100         if (MyFacesCheck.isTomahawkAvailable())
101         {
102             try
103             {
104                 // See whether the sandbox implementation is also in the classpath. This
105                 // generates nicer output...
106                 String myfacesImpl =
107                     "org.apache.myfaces.orchestra.dynaForm.jsf.guiBuilder.impl.myfaces.MyFacesGuiBuilder";
108                 @SuppressWarnings("unchecked")
109                 Class<? extends JsfGuiBuilder> myfacesGuiBuilder =
110                     ClassUtils.classForName(myfacesImpl);
111                 guiBuilder = (JsfGuiBuilder) myfacesGuiBuilder.newInstance();
112             }
113             catch (ClassNotFoundException e)
114             {
115                 LogFactory.getLog(JsfGuiBuilderFactory.class)
116                     .warn("consider using the myfaces-orchestra-sandbox for better gui support", e);
117             }
118             catch (InstantiationException e)
119             {
120                 LogFactory.getLog(JsfGuiBuilderFactory.class)
121                     .warn("can't create the myfaces gui builder - reverting to plain JSF", e);
122             }
123             catch (IllegalAccessException e)
124             {
125                 LogFactory.getLog(JsfGuiBuilderFactory.class)
126                     .warn("can't create the myfaces gui builder - reverting to plain JSF", e);
127             }
128         }
129 
130         if (guiBuilder == null)
131         {
132             guiBuilder = new JsfGuiBuilder();
133         }
134         return guiBuilder;
135     }
136 }