RowsetConfigurableContainer.java

biz/hammurapi/sql/RowsetConfigurableContainer.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 049 Use a Collection instead of arrays Object[] 2 69:52
Java Inspector 089 Undocumented constructor 2 44:9
Java Inspector 089 Undocumented constructor 2 48:9
Java Inspector 089 Undocumented method 2 55:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 56:43
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 57:53
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 58:54
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 88:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 88:97
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 44:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.sql;
24
25import java.io.StringReader;
26import java.lang.reflect.Constructor;
27import java.sql.ResultSet;
28import java.sql.SQLException;
29
30import biz.hammurapi.config.DomConfigurable;
31import biz.hammurapi.config.GenericContainer;
32import biz.hammurapi.convert.ConvertingService;
33import biz.hammurapi.xml.dom.DOMUtils;
34
35
36/**
37 * Container, which reads component definitions from rowset.
38 * @author Pavel Vlasov
39 * @revision $Revision$
40 */
41public class RowsetConfigurableContainer extends GenericContainer implements RowProcessor {
42 private ClassLoader classLoader;
43
44 public RowsetConfigurableContainer() {
45 // Default constructor
46 }
47
48 public RowsetConfigurableContainer(ClassLoader classLoader) {
49 super();
50 this.classLoader = classLoader;
51 }
52
53
54
55 public boolean process(ResultSet rs) throws SQLException {
56 String value=rs.getString("PARAMETER_VALUE");
57 String componentName = rs.getString("NAME");
58 String componentClass = rs.getString("CLASS_NAME");
59 try {
60 // Default class is String
61 if (componentClass==null || componentClass.trim().length()==0) {
62 addComponent(componentName, value);
63 } else {
64 Class clazz = Class.forName(componentClass);
65 if (value==null || value.trim().length()==0) {
66 addComponent(componentName, clazz.newInstance());
67 } else {
68 // Attempt to construct class from value
69 Constructor[] constructors=clazz.getConstructors();
70 for (int i=0; i<constructors.length; i++) {
71 if (constructors[i].getParameterTypes().length==1 && constructors[i].getParameterTypes()[0].equals(String.class)) {
72 addComponent(componentName, constructors[i].newInstance(new Object[] {value}));
73 return true;
74 }
75 }
76
77 Object instance = clazz.newInstance();
78
79 if (DomConfigurable.class.isAssignableFrom(clazz)) {
80 ((DomConfigurable) instance).configure(DOMUtils.parse(new StringReader(value)).getDocumentElement(), this, classLoader);
81 } else {
82 instance = ConvertingService.convert(instance, clazz);
83 }
84 addComponent(componentName, instance);
85 }
86 }
87 } catch (Exception e) {
88 throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"' -> "+componentClass, e);
89 }
90 return true;
91 }
92}
93