/modules/enterprise/binding/src/main/java/org/rhq/bindings/util/ConfigurationClassBuilder.java

https://github.com/ccrouch/rhq · Java · 158 lines · 116 code · 18 blank · 24 comment · 39 complexity · d943cede028a8b13f4ce5715bc977a88 MD5 · raw file

  1. /*
  2. * RHQ Management Platform
  3. * Copyright (C) 2005-2008 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. package org.rhq.bindings.util;
  20. import java.util.LinkedHashMap;
  21. import javassist.ClassPool;
  22. import javassist.CtClass;
  23. import javassist.NotFoundException;
  24. import org.rhq.core.domain.configuration.Configuration;
  25. import org.rhq.core.domain.configuration.PropertySimple;
  26. import org.rhq.core.domain.configuration.definition.ConfigurationDefinition;
  27. import org.rhq.core.domain.configuration.definition.PropertyDefinition;
  28. import org.rhq.core.domain.configuration.definition.PropertyDefinitionSimple;
  29. /**
  30. * @author Greg Hinkle
  31. */
  32. public class ConfigurationClassBuilder {
  33. public static LinkedHashMap<String, CtClass> translateParameters(ConfigurationDefinition def)
  34. throws NotFoundException {
  35. LinkedHashMap<String, CtClass> result = new LinkedHashMap<String, CtClass>();
  36. if (def == null || def.getPropertyDefinitions() == null) {
  37. return result;
  38. }
  39. for (PropertyDefinition pd : def.getPropertyDefinitions().values()) {
  40. if (pd instanceof PropertyDefinitionSimple) {
  41. PropertyDefinitionSimple simple = (PropertyDefinitionSimple) pd;
  42. String name = simpleName(simple.getDisplayName());
  43. CtClass paramType = getSimpleTypeClass(simple);
  44. result.put(name, paramType);
  45. }
  46. }
  47. return result;
  48. }
  49. private static CtClass getSimpleTypeClass(PropertyDefinitionSimple simple) throws NotFoundException {
  50. Class<?> paramType = null;
  51. switch (simple.getType()) {
  52. case STRING:
  53. case LONG_STRING:
  54. case PASSWORD:
  55. case FILE:
  56. case DIRECTORY:
  57. paramType = String.class;
  58. break;
  59. case BOOLEAN:
  60. paramType = Boolean.TYPE;
  61. break;
  62. case INTEGER:
  63. paramType = Integer.TYPE;
  64. break;
  65. case LONG:
  66. paramType = Long.TYPE;
  67. break;
  68. case FLOAT:
  69. paramType = Float.TYPE;
  70. break;
  71. case DOUBLE:
  72. paramType = Double.TYPE;
  73. break;
  74. }
  75. return ClassPool.getDefault().get(paramType.getName());
  76. }
  77. public static CtClass translateConfiguration(ConfigurationDefinition def) throws NotFoundException {
  78. if (def == null) {
  79. return CtClass.voidType;
  80. } else if (def.getPropertyDefinitionSimple("operationResult") != null) {
  81. // Its a simple type
  82. return getSimpleTypeClass(def.getPropertyDefinitionSimple("operationResult"));
  83. } else {
  84. // TODO GH: Build a custom type?
  85. return ClassPool.getDefault().get(Configuration.class.getName());
  86. }
  87. }
  88. private static String simpleName(String name) {
  89. return decapitalize(name.replaceAll("\\W", ""));
  90. }
  91. private static String decapitalize(String name) {
  92. return Character.toLowerCase(name.charAt(0)) + name.substring(1, name.length());
  93. }
  94. public static Configuration translateParametersToConfig(ConfigurationDefinition parametersConfigurationDefinition,
  95. Object[] args) throws NotFoundException {
  96. LinkedHashMap<String, CtClass> translateParameters = translateParameters(parametersConfigurationDefinition);
  97. Configuration config = new Configuration();
  98. int index = 0;
  99. for (String key : translateParameters.keySet()) {
  100. config.put(new PropertySimple(key, args[index++]));
  101. }
  102. return config;
  103. }
  104. public static Object translateResults(ConfigurationDefinition resultsConfigurationDefinition, Configuration result)
  105. throws NotFoundException {
  106. CtClass expectedReturn = translateConfiguration(resultsConfigurationDefinition);
  107. if (expectedReturn.equals(ClassPool.getDefault().get(Configuration.class.getName()))) {
  108. return result;
  109. } else {
  110. //bail on translation if Configuration passed in is null
  111. if (result == null)
  112. return result;
  113. PropertySimple simple = result.getSimple("operationResult");
  114. if (simple != null) {
  115. if (expectedReturn.getName().equals(String.class.getName())) {
  116. return simple.getStringValue();
  117. } else if (expectedReturn.getName().equals(Boolean.class.getName())
  118. || expectedReturn.getName().equals(Boolean.TYPE.getName())) {
  119. return simple.getBooleanValue();
  120. } else if (expectedReturn.getName().equals(Integer.class.getName())
  121. || expectedReturn.getName().equals(Integer.TYPE.getName())) {
  122. return simple.getIntegerValue();
  123. } else if (expectedReturn.getName().equals(Long.class.getName())
  124. || expectedReturn.getName().equals(Long.TYPE.getName())) {
  125. return simple.getLongValue();
  126. } else if (expectedReturn.getName().equals(Float.class.getName())
  127. || expectedReturn.getName().equals(Float.TYPE.getName())) {
  128. return simple.getFloatValue();
  129. } else if (expectedReturn.getName().equals(Double.class.getName())
  130. || expectedReturn.getName().equals(Double.TYPE.getName())) {
  131. return simple.getDoubleValue();
  132. } else if (expectedReturn.getName().equals(Boolean.class.getName())
  133. || expectedReturn.getName().equals(Boolean.TYPE.getName())) {
  134. return simple.getBooleanValue();
  135. }
  136. }
  137. }
  138. return null;
  139. }
  140. }