PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ironjacamar-1.0.9.Final/core/src/main/java/org/jboss/jca/core/util/Injection.java

#
Java | 381 lines | 262 code | 29 blank | 90 comment | 105 complexity | f3e3d9810ef1dab45339e567ad0d5b67 MD5 | raw file
Possible License(s): LGPL-2.1, MIT
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.jca.core.util;
  23. import java.io.File;
  24. import java.lang.reflect.Constructor;
  25. import java.lang.reflect.Field;
  26. import java.lang.reflect.InvocationTargetException;
  27. import java.lang.reflect.Method;
  28. import java.net.InetAddress;
  29. import java.util.Locale;
  30. /**
  31. * Injection utility which can inject values into objects. This file is a copy
  32. * of the <code>com.github.fungal.api.util.Injection</code> class.
  33. *
  34. * @author <a href="mailto:jesper.pedersen@jboss.org">Jesper Pedersen</a>
  35. */
  36. public class Injection
  37. {
  38. /**
  39. * Constructor
  40. */
  41. public Injection()
  42. {
  43. }
  44. /**
  45. * Inject a value into an object property
  46. * @param object The object
  47. * @param propertyName The property name
  48. * @param propertyValue The property value
  49. * @exception NoSuchMethodException If the property method cannot be found
  50. * @exception IllegalAccessException If the property method cannot be accessed
  51. * @exception InvocationTargetException If the property method cannot be executed
  52. */
  53. @SuppressWarnings("unchecked")
  54. public void inject(Object object, String propertyName, Object propertyValue)
  55. throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
  56. {
  57. inject(object, propertyName, propertyValue, null, false);
  58. }
  59. /**
  60. * Inject a value into an object property
  61. * @param object The object
  62. * @param propertyName The property name
  63. * @param propertyValue The property value
  64. * @param propertyType The property type as a fully quilified class name
  65. * @exception NoSuchMethodException If the property method cannot be found
  66. * @exception IllegalAccessException If the property method cannot be accessed
  67. * @exception InvocationTargetException If the property method cannot be executed
  68. */
  69. @SuppressWarnings("unchecked")
  70. public void inject(Object object, String propertyName, Object propertyValue, String propertyType)
  71. throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
  72. {
  73. inject(object, propertyName, propertyValue, propertyType, false);
  74. }
  75. /**
  76. * Inject a value into an object property
  77. * @param object The object
  78. * @param propertyName The property name
  79. * @param propertyValue The property value
  80. * @param propertyType The property type as a fully quilified class name
  81. * @param includeFields Should fields be included for injection if a method can't be found
  82. * @exception NoSuchMethodException If the property method cannot be found
  83. * @exception IllegalAccessException If the property method cannot be accessed
  84. * @exception InvocationTargetException If the property method cannot be executed
  85. */
  86. @SuppressWarnings("unchecked")
  87. public void inject(Object object,
  88. String propertyName, Object propertyValue, String propertyType,
  89. boolean includeFields)
  90. throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
  91. {
  92. if (object == null)
  93. throw new IllegalArgumentException("Object is null");
  94. if (propertyName == null || propertyName.trim().equals(""))
  95. throw new IllegalArgumentException("PropertyName is undefined");
  96. String methodName = "set" + propertyName.substring(0, 1).toUpperCase(Locale.US);
  97. if (propertyName.length() > 1)
  98. {
  99. methodName += propertyName.substring(1);
  100. }
  101. Method method = findMethod(object.getClass(), methodName, propertyType);
  102. if (method != null)
  103. {
  104. Class<?> parameterClass = method.getParameterTypes()[0];
  105. Object parameterValue = null;
  106. try
  107. {
  108. parameterValue = getValue(propertyName, parameterClass, propertyValue,
  109. object.getClass().getClassLoader());
  110. }
  111. catch (Throwable t)
  112. {
  113. throw new InvocationTargetException(t, t.getMessage());
  114. }
  115. if (!parameterClass.isPrimitive() || parameterValue != null)
  116. method.invoke(object, new Object[] {parameterValue});
  117. }
  118. else
  119. {
  120. if (!includeFields)
  121. throw new NoSuchMethodException("Method " + methodName + " not found");
  122. // Ok, we didn't find a method - assume field
  123. Field field = findField(object.getClass(), propertyName, propertyType);
  124. if (field != null)
  125. {
  126. Class<?> fieldClass = field.getType();
  127. Object fieldValue = null;
  128. try
  129. {
  130. fieldValue = getValue(propertyName, fieldClass, propertyValue,
  131. object.getClass().getClassLoader());
  132. }
  133. catch (Throwable t)
  134. {
  135. throw new InvocationTargetException(t, t.getMessage());
  136. }
  137. field.set(object, fieldValue);
  138. }
  139. else
  140. {
  141. throw new NoSuchMethodException("Field " + propertyName + " not found");
  142. }
  143. }
  144. }
  145. /**
  146. * Find a method
  147. * @param clz The class
  148. * @param methodName The method name
  149. * @param propertyType The property type; can be <code>null</code>
  150. * @return The method; <code>null</code> if not found
  151. */
  152. protected Method findMethod(Class<?> clz, String methodName, String propertyType)
  153. {
  154. while (!clz.equals(Object.class))
  155. {
  156. Method[] methods = clz.getDeclaredMethods();
  157. for (int i = 0; i < methods.length; i++)
  158. {
  159. Method method = methods[i];
  160. if (methodName.equals(method.getName()) && method.getParameterTypes().length == 1)
  161. {
  162. if (propertyType == null || propertyType.equals(method.getParameterTypes()[0].getName()))
  163. return method;
  164. }
  165. }
  166. clz = clz.getSuperclass();
  167. }
  168. return null;
  169. }
  170. /**
  171. * Find a field
  172. * @param clz The class
  173. * @param fieldName The field name
  174. * @param fieldType The field type; can be <code>null</code>
  175. * @return The field; <code>null</code> if not found
  176. */
  177. protected Field findField(Class<?> clz, String fieldName, String fieldType)
  178. {
  179. while (!clz.equals(Object.class))
  180. {
  181. Field[] fields = clz.getDeclaredFields();
  182. for (int i = 0; i < fields.length; i++)
  183. {
  184. Field field = fields[i];
  185. if (fieldName.equals(field.getName()))
  186. {
  187. if (fieldType == null || fieldType.equals(field.getType().getName()))
  188. return field;
  189. }
  190. }
  191. clz = clz.getSuperclass();
  192. }
  193. return null;
  194. }
  195. /**
  196. * Get the value
  197. * @param name The value name
  198. * @param clz The value class
  199. * @param v The value
  200. * @param cl The class loader
  201. * @return The substituted value
  202. * @exception Exception Thrown in case of an error
  203. */
  204. protected Object getValue(String name, Class<?> clz, Object v, ClassLoader cl) throws Exception
  205. {
  206. if (v instanceof String)
  207. {
  208. String substituredValue = getSubstitutionValue((String)v);
  209. if (clz.equals(String.class))
  210. {
  211. v = substituredValue;
  212. }
  213. else if (clz.equals(byte.class) || clz.equals(Byte.class))
  214. {
  215. if (substituredValue != null && !substituredValue.trim().equals(""))
  216. v = Byte.valueOf(substituredValue);
  217. }
  218. else if (clz.equals(short.class) || clz.equals(Short.class))
  219. {
  220. if (substituredValue != null && !substituredValue.trim().equals(""))
  221. v = Short.valueOf(substituredValue);
  222. }
  223. else if (clz.equals(int.class) || clz.equals(Integer.class))
  224. {
  225. if (substituredValue != null && !substituredValue.trim().equals(""))
  226. v = Integer.valueOf(substituredValue);
  227. }
  228. else if (clz.equals(long.class) || clz.equals(Long.class))
  229. {
  230. if (substituredValue != null && !substituredValue.trim().equals(""))
  231. v = Long.valueOf(substituredValue);
  232. }
  233. else if (clz.equals(float.class) || clz.equals(Float.class))
  234. {
  235. if (substituredValue != null && !substituredValue.trim().equals(""))
  236. v = Float.valueOf(substituredValue);
  237. }
  238. else if (clz.equals(double.class) || clz.equals(Double.class))
  239. {
  240. if (substituredValue != null && !substituredValue.trim().equals(""))
  241. v = Double.valueOf(substituredValue);
  242. }
  243. else if (clz.equals(boolean.class) || clz.equals(Boolean.class))
  244. {
  245. if (substituredValue != null && !substituredValue.trim().equals(""))
  246. v = Boolean.valueOf(substituredValue);
  247. }
  248. else if (clz.equals(char.class) || clz.equals(Character.class))
  249. {
  250. if (substituredValue != null && !substituredValue.trim().equals(""))
  251. v = Character.valueOf(substituredValue.charAt(0));
  252. }
  253. else if (clz.equals(InetAddress.class))
  254. {
  255. v = InetAddress.getByName(substituredValue);
  256. }
  257. else if (clz.equals(Class.class))
  258. {
  259. v = Class.forName(substituredValue, true, cl);
  260. }
  261. else
  262. {
  263. try
  264. {
  265. Constructor<?> constructor = clz.getConstructor(String.class);
  266. v = constructor.newInstance(substituredValue);
  267. }
  268. catch (Throwable t)
  269. {
  270. // Try static String valueOf method
  271. try
  272. {
  273. Method valueOf = clz.getMethod("valueOf", String.class);
  274. v = valueOf.invoke((Object)null, substituredValue);
  275. }
  276. catch (Throwable inner)
  277. {
  278. throw new IllegalArgumentException("Unknown property resolution for property " + name);
  279. }
  280. }
  281. }
  282. }
  283. return v;
  284. }
  285. /**
  286. * System property substitution
  287. * @param input The input string
  288. * @return The output
  289. */
  290. protected String getSubstitutionValue(String input)
  291. {
  292. if (input == null || input.trim().equals(""))
  293. return input;
  294. while (input.indexOf("${") != -1)
  295. {
  296. int from = input.indexOf("${");
  297. int to = input.indexOf("}");
  298. int dv = input.indexOf(":", from + 2);
  299. if (dv != -1)
  300. {
  301. if (dv > to)
  302. dv = -1;
  303. }
  304. String systemProperty = "";
  305. String defaultValue = "";
  306. if (dv == -1)
  307. {
  308. String s = input.substring(from + 2, to);
  309. if ("/".equals(s))
  310. {
  311. systemProperty = File.separator;
  312. }
  313. else if (":".equals(s))
  314. {
  315. systemProperty = File.pathSeparator;
  316. }
  317. else
  318. {
  319. systemProperty = SecurityActions.getSystemProperty(s);
  320. }
  321. }
  322. else
  323. {
  324. systemProperty = SecurityActions.getSystemProperty(input.substring(from + 2, dv));
  325. defaultValue = input.substring(dv + 1, to);
  326. }
  327. String prefix = "";
  328. String postfix = "";
  329. if (from != 0)
  330. {
  331. prefix = input.substring(0, from);
  332. }
  333. if (to + 1 < input.length() - 1)
  334. {
  335. postfix = input.substring(to + 1);
  336. }
  337. if (systemProperty != null && !systemProperty.trim().equals(""))
  338. {
  339. input = prefix + systemProperty + postfix;
  340. }
  341. else if (defaultValue != null && !defaultValue.trim().equals(""))
  342. {
  343. input = prefix + defaultValue + postfix;
  344. }
  345. else
  346. {
  347. input = prefix + postfix;
  348. }
  349. }
  350. return input;
  351. }
  352. }