Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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   package org.apache.kahadb.util;
   18   
   19   import java.beans.PropertyEditor;
   20   import java.beans.PropertyEditorManager;
   21   import java.lang.reflect.Field;
   22   import java.lang.reflect.Method;
   23   import java.lang.reflect.Modifier;
   24   import java.net.URI;
   25   import java.net.URISyntaxException;
   26   import java.util.Arrays;
   27   import java.util.HashMap;
   28   import java.util.Iterator;
   29   import java.util.LinkedHashMap;
   30   import java.util.Map;
   31   import java.util.Set;
   32   import java.util.Map.Entry;
   33   
   34   public final class IntrospectionSupport {
   35       
   36       private IntrospectionSupport() {
   37       }
   38   
   39       public static boolean getProperties(Object target, Map props, String optionPrefix) {
   40   
   41           boolean rc = false;
   42           if (target == null) {
   43               throw new IllegalArgumentException("target was null.");
   44           }
   45           if (props == null) {
   46               throw new IllegalArgumentException("props was null.");
   47           }
   48   
   49           if (optionPrefix == null) {
   50               optionPrefix = "";
   51           }
   52   
   53           Class clazz = target.getClass();
   54           Method[] methods = clazz.getMethods();
   55           for (int i = 0; i < methods.length; i++) {
   56               Method method = methods[i];
   57               String name = method.getName();
   58               Class type = method.getReturnType();
   59               Class params[] = method.getParameterTypes();
   60               if ((name.startsWith("is") || name.startsWith("get")) && params.length == 0 && type != null && isSettableType(type)) {
   61   
   62                   try {
   63   
   64                       Object value = method.invoke(target, new Object[] {});
   65                       if (value == null) {
   66                           continue;
   67                       }
   68   
   69                       String strValue = convertToString(value, type);
   70                       if (strValue == null) {
   71                           continue;
   72                       }
   73                       if (name.startsWith("get")) {
   74                           name = name.substring(3, 4).toLowerCase()
   75                                   + name.substring(4);
   76                       } else {
   77                           name = name.substring(2, 3).toLowerCase()
   78                                   + name.substring(3);
   79                       }
   80                       props.put(optionPrefix + name, strValue);
   81                       rc = true;
   82   
   83                   } catch (Throwable ignore) {
   84                       ignore.printStackTrace();
   85                   }
   86   
   87               }
   88           }
   89   
   90           return rc;
   91       }
   92   
   93       public static boolean setProperties(Object target, Map props, String optionPrefix) {
   94           boolean rc = false;
   95           if (target == null) {
   96               throw new IllegalArgumentException("target was null.");
   97           }
   98           if (props == null) {
   99               throw new IllegalArgumentException("props was null.");
  100           }
  101   
  102           for (Iterator<String> iter = props.keySet().iterator(); iter.hasNext();) {
  103               String name = iter.next();
  104               if (name.startsWith(optionPrefix)) {
  105                   Object value = props.get(name);
  106                   name = name.substring(optionPrefix.length());
  107                   if (setProperty(target, name, value)) {
  108                       iter.remove();
  109                       rc = true;
  110                   }
  111               }
  112           }
  113           return rc;
  114       }
  115   
  116       public static Map<String, Object> extractProperties(Map props, String optionPrefix) {
  117           if (props == null) {
  118               throw new IllegalArgumentException("props was null.");
  119           }
  120   
  121           HashMap<String, Object> rc = new HashMap<String, Object>(props.size());
  122   
  123           for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
  124               String name = (String)iter.next();
  125               if (name.startsWith(optionPrefix)) {
  126                   Object value = props.get(name);
  127                   name = name.substring(optionPrefix.length());
  128                   rc.put(name, value);
  129                   iter.remove();
  130               }
  131           }
  132   
  133           return rc;
  134       }
  135   
  136       public static boolean setProperties(Object target, Map props) {
  137           boolean rc = false;
  138   
  139           if (target == null) {
  140               throw new IllegalArgumentException("target was null.");
  141           }
  142           if (props == null) {
  143               throw new IllegalArgumentException("props was null.");
  144           }
  145   
  146           for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) {
  147               Map.Entry entry = (Entry)iter.next();
  148               if (setProperty(target, (String)entry.getKey(), entry.getValue())) {
  149                   iter.remove();
  150                   rc = true;
  151               }
  152           }
  153   
  154           return rc;
  155       }
  156   
  157       public static boolean setProperty(Object target, String name, Object value) {
  158           try {
  159               Class clazz = target.getClass();
  160               Method setter = findSetterMethod(clazz, name);
  161               if (setter == null) {
  162                   return false;
  163               }
  164   
  165               // If the type is null or it matches the needed type, just use the
  166               // value directly
  167               if (value == null || value.getClass() == setter.getParameterTypes()[0]) {
  168                   setter.invoke(target, new Object[] {value});
  169               } else {
  170                   // We need to convert it
  171                   setter.invoke(target, new Object[] {convert(value, setter.getParameterTypes()[0])});
  172               }
  173               return true;
  174           } catch (Throwable ignore) {
  175               return false;
  176           }
  177       }
  178   
  179       private static Object convert(Object value, Class type) throws URISyntaxException {
  180           PropertyEditor editor = PropertyEditorManager.findEditor(type);
  181           if (editor != null) {
  182               editor.setAsText(value.toString());
  183               return editor.getValue();
  184           }
  185           if (type == URI.class) {
  186               return new URI(value.toString());
  187           }
  188           return null;
  189       }
  190   
  191       private static String convertToString(Object value, Class type) throws URISyntaxException {
  192           PropertyEditor editor = PropertyEditorManager.findEditor(type);
  193           if (editor != null) {
  194               editor.setValue(value);
  195               return editor.getAsText();
  196           }
  197           if (type == URI.class) {
  198               return ((URI)value).toString();
  199           }
  200           return null;
  201       }
  202   
  203       private static Method findSetterMethod(Class clazz, String name) {
  204           // Build the method name.
  205           name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
  206           Method[] methods = clazz.getMethods();
  207           for (int i = 0; i < methods.length; i++) {
  208               Method method = methods[i];
  209               Class params[] = method.getParameterTypes();
  210               if (method.getName().equals(name) && params.length == 1 ) {
  211                   return method;
  212               }
  213           }
  214           return null;
  215       }
  216   
  217       private static boolean isSettableType(Class clazz) {
  218           if (PropertyEditorManager.findEditor(clazz) != null) {
  219               return true;
  220           }
  221           if (clazz == URI.class) {
  222               return true;
  223           }
  224           if (clazz == Boolean.class) {
  225               return true;
  226           }
  227           return false;
  228       }
  229   
  230       public static String toString(Object target) {
  231           return toString(target, Object.class);
  232       }
  233   
  234       public static String toString(Object target, Class stopClass) {
  235           LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
  236           addFields(target, target.getClass(), stopClass, map);
  237           StringBuffer buffer = new StringBuffer(simpleName(target.getClass()));
  238           buffer.append(" {");
  239           Set entrySet = map.entrySet();
  240           boolean first = true;
  241           for (Iterator iter = entrySet.iterator(); iter.hasNext();) {
  242               Map.Entry entry = (Map.Entry)iter.next();
  243               if (first) {
  244                   first = false;
  245               } else {
  246                   buffer.append(", ");
  247               }
  248               buffer.append(entry.getKey());
  249               buffer.append(" = ");
  250               appendToString(buffer, entry.getValue());
  251           }
  252           buffer.append("}");
  253           return buffer.toString();
  254       }
  255   
  256       protected static void appendToString(StringBuffer buffer, Object value) {
  257           buffer.append(value);
  258       }
  259   
  260       public static String simpleName(Class clazz) {
  261           String name = clazz.getName();
  262           int p = name.lastIndexOf(".");
  263           if (p >= 0) {
  264               name = name.substring(p + 1);
  265           }
  266           return name;
  267       }
  268   
  269       private static void addFields(Object target, Class startClass, Class<Object> stopClass, LinkedHashMap<String, Object> map) {
  270   
  271           if (startClass != stopClass) {
  272               addFields(target, startClass.getSuperclass(), stopClass, map);
  273           }
  274   
  275           Field[] fields = startClass.getDeclaredFields();
  276           for (int i = 0; i < fields.length; i++) {
  277               Field field = fields[i];
  278               if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())
  279                   || Modifier.isPrivate(field.getModifiers())) {
  280                   continue;
  281               }
  282   
  283               try {
  284                   field.setAccessible(true);
  285                   Object o = field.get(target);
  286                   if (o != null && o.getClass().isArray()) {
  287                       try {
  288                           o = Arrays.asList((Object[])o);
  289                       } catch (Throwable e) {
  290                       }
  291                   }
  292                   map.put(field.getName(), o);
  293               } catch (Throwable e) {
  294                   e.printStackTrace();
  295               }
  296           }
  297   
  298       }
  299   
  300   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]