View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.util;
    19
    20  import org.apache.commons.logging.Log;
    21  import org.apache.commons.logging.LogFactory;
    22
    23  import java.lang.reflect.Method;
    24  import java.util.Map;
    25
    26  /***
    27   * Some bean utility methods
    28   *
    29   * @version $Revision: 1.1 $
    30   */
    31  public class BeanUtils {
    32      private static final Log log = LogFactory.getLog(BeanUtils.class);
    33
    34      protected static final String NAME = "org.apache.commons.beanutils.BeanUtils";
    35      protected static final Class[] PARAMETER_TYPES = new Class[]{Object.class, Map.class};
    36
    37      public static void populate(Object bean, Map properties) {
    38          // use reflection to avoid runtime dependnecy if this method is never called
    39          Class beanUtils = null;
    40          try {
    41              beanUtils = Thread.currentThread().getContextClassLoader().loadClass(NAME);
    42          }
    43          catch (ClassNotFoundException e) {
    44              try {
    45                  beanUtils = BeanUtils.class.getClassLoader().loadClass(NAME);
    46              }
    47              catch (ClassNotFoundException e1) {
    48                  log.trace("Cannot find: " + NAME + ". Reason: " + e, e);
    49              }
    50          }
    51          if (beanUtils == null) {
    52              log.warn("Could not find class: " + NAME + " so cannot configure bean using introspection: " + bean);
    53          }
    54          else {
    55              Method method = null;
    56              try {
    57                  method = beanUtils.getMethod("populate", PARAMETER_TYPES);
    58              }
    59              catch (NoSuchMethodException e) {
    60                  log.trace("Could not find populate", e);
    61              }
    62              if (method == null) {
    63                  log.warn("Could not find populate() method so cannot configure bean using introspection: " + bean);
    64              }
    65              else {
    66                  try {
    67                      method.invoke(null, new Object[]{bean, properties});
    68                  }
    69                  catch (Exception e) {
    70                      log.warn("Could not populate the bean via introspection: " + bean + ". Reason: " + e, e);
    71                  }
    72              }
    73          }
    74      }
    75  }