View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jboss.managed.util;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.modeshape.common.text.Inflector;
30  import org.modeshape.common.util.CheckArg;
31  import org.modeshape.common.util.Logger;
32  import org.modeshape.common.util.Reflection;
33  import org.modeshape.common.util.Reflection.Property;
34  import org.modeshape.jboss.managed.JBossManagedI18n;
35  import org.modeshape.jboss.managed.ManagedEngine.Component;
36  import org.modeshape.jboss.managed.ManagedEngine.ManagedProperty;
37  
38  /**
39   * Class for common utility methods used for ModeShape Managed Objects
40   */
41  public class ManagedUtils { 
42  
43      private static final Logger LOGGER = Logger.getLogger(ManagedUtils.class);
44  
45      public static List<ManagedProperty> getProperties( Component objectType,
46                                                         Object object ) {
47          Reflection reflection = new Reflection(object.getClass());
48          List<ManagedProperty> managedProps = new ArrayList<ManagedProperty>();
49          List<Property> props = new ArrayList<Property>();
50          boolean allInferred = true;
51          try {
52          	props = reflection.getAllPropertiesOn(object);
53              for (Property prop : props) {
54                  if (prop.isInferred()) continue;
55                  allInferred = false;
56                  if (prop.isPrimitive() || prop.getType().toString().contains("java.lang.String")){
57                  	String valueAsString = reflection.getPropertyAsString(object, prop);
58                      managedProps.add(new ManagedProperty(prop, valueAsString));
59                  }
60              }
61          
62              //If all properties are inferred, then we will loop again and just use them all (as long as they are)
63  	        if (allInferred){
64  	        	for (Property prop : props) {
65  	                if (prop.isPrimitive() || prop.getType().toString().contains("java.lang.String")){
66  	                	String valueAsString = reflection.getPropertyAsString(object, prop);
67  	                    managedProps.add(new ManagedProperty(prop, valueAsString));
68  	                }
69  	            }
70  	        }
71  
72          } catch (Throwable e) {
73              LOGGER.error(e, JBossManagedI18n.errorGettingPropertiesFromManagedObject, objectType);
74          }
75          
76          return managedProps;
77      }
78      
79      /**
80       * Set the human-readable label for the property. If null, this will be set to the
81       * {@link Inflector#humanize(String, String...) humanized} form of the name}.
82       * 
83       * @param name the label for the property; may not be null
84       * @return label
85       */
86      public static String createLabel( String name ) {
87         
88      	CheckArg.isNotNull(name, "name");
89      	name = name.replaceFirst("option.", "");
90      	name = name.replaceFirst("custom.", "");
91      	name = name.replace(".", " ");
92      	String label = null;
93      	Inflector inflector = Inflector.getInstance();
94      	
95      	if (name != null) {
96              label = inflector.titleCase(inflector.humanize(name));
97              label = label.replaceFirst("Jcr", "JCR");
98          }
99          return label;
100     }
101 
102 }