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.lib;
    20
    21  /**
    22   * <p>
    23   * Internal helper class for some class management.
    24   * </p>
    25   * <p>
    26   * <b>Not for use outside of Orchestra</b>
    27   * </p>
    28   */
    29  public final class _ClassUtils
    30  {
    31      private _ClassUtils()
    32      {
    33      }
    34
    35      /**
    36       * Create a new instance for a class by its name
    37       *
    38       * @since 1.1
    39       */
    40      public static Object newInstance(Class clazz)
    41      {
    42          try
    43          {
    44              return clazz.newInstance();
    45          }
    46          catch(NoClassDefFoundError e)
    47          {
    48              throw new OrchestraException(e);
    49          }
    50          catch (InstantiationException e)
    51          {
    52              throw new OrchestraException(e);
    53          }
    54          catch (IllegalAccessException e)
    55          {
    56              throw new OrchestraException(e);
    57          }
    58      }
    59
    60      /**
    61       * create a new instance for a class by its name
    62       */
    63      public static Object newInstance(String className)
    64      {
    65          try
    66          {
    67              Class clazz = classForName(className);
    68              return clazz.newInstance();
    69          }
    70          catch(NoClassDefFoundError e)
    71          {
    72              throw new OrchestraException(e);
    73          }
    74          catch (InstantiationException e)
    75          {
    76              throw new OrchestraException(e);
    77          }
    78          catch (IllegalAccessException e)
    79          {
    80              throw new OrchestraException(e);
    81          }
    82          catch (ClassNotFoundException e)
    83          {
    84              throw new OrchestraException(e);
    85          }
    86      }
    87
    88      /**
    89       * Lookup class using the webapp classloader first and the the classloader which loaded
    90       * this class.
    91       */
    92      public static Class classForName(String className) throws ClassNotFoundException
    93      {
    94          if (className == null)
    95          {
    96              throw new NullPointerException("className");
    97          }
    98
    99          try
    100         {
    101             // Try WebApp ClassLoader first
    102             return Class.forName(className,
    103                 false, // do not initialize for faster startup
    104                 Thread.currentThread().getContextClassLoader());
    105         }
    106         catch (ClassNotFoundException ignore)
    107         {
    108             // fallback: Try ClassLoader for this class (i.e. the myfaces.jar lib)
    109             return Class.forName(className,
    110                 false, // do not initialize for faster startup
    111                 _ClassUtils.class.getClassLoader());
    112         }
    113     }
    114 }