ObjectEntry.java

biz/hammurapi/ant/ObjectEntry.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 070-A Cyclomatic complexity is too high: 20, maximum allowed is 12 2 100:9
Java Inspector 085 Do not declare runtime exceptions in the throws clause. 2 56:9
Java Inspector 085 Do not declare runtime exceptions in the throws clause. 2 92:9
Java Inspector 085 Do not declare runtime exceptions in the throws clause. 2 96:9
Java Inspector 085 Do not declare runtime exceptions in the throws clause. 2 100:9
Java Inspector 089 Parameter parameter is not documented 2 56:9
Java Inspector 089 Parameter className is not documented 2 69:9
Java Inspector 089 Parameter value is not documented 2 78:9
Java Inspector 089 Method is not properly documented 2 86:9
Java Inspector 089 Method return value is not properly documented 2 86:9
Java Inspector 089 Undocumented method 2 92:9
Java Inspector 089 Undocumented method 2 96:9
Java Inspector 089 Undocumented method 2 100:9
Java Inspector 089 Method is not properly documented 2 175:9
Java Inspector 089 Method return value is not properly documented 2 175:9
Java Inspector 089 Method is not properly documented 2 182:9
Java Inspector 089 Method return value is not properly documented 2 182:9
Java Inspector 089 Method documentation is too short. It is only 1 words. Should be at least 3 words. 2 197:9
Java Inspector 089 Parameter classPath is not documented 2 197:9
Java Inspector 089 Method return value is not documented 2 209:9
Java Inspector 089 Method documentation is too short. It is only 2 words. Should be at least 3 words. 2 221:9
Java Inspector 089 Parameter text is not documented 2 221:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 58:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 109:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 129:84
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 146:133
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 149:98
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 149:141
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 153:92
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 158:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 160:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 162:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 164:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 166:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 166:91

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.ant;
24
25import java.lang.reflect.InvocationTargetException;
26import java.util.Collection;
27import java.util.Iterator;
28import java.util.LinkedList;
29import java.util.List;
30
31import org.apache.tools.ant.AntClassLoader;
32import org.apache.tools.ant.BuildException;
33import org.apache.tools.ant.ProjectComponent;
34import org.apache.tools.ant.types.Path;
35
36import biz.hammurapi.config.ConfigurationException;
37import biz.hammurapi.config.Parameterizable;
38
39/**
40 * Object entry. Base class for configurable objects.
41 * @ant.type name="objectentry" category="Common"
42 * @author Pavel Vlasov
43 * @version $Revision: 1.5 $
44 */
45public class ObjectEntry extends ProjectComponent {
46 private String className;
47 private String value;
48 private List parameters=new LinkedList();
49
50 /**
51 * Configuration parameter. Object entry class must implement biz.hammurapi.config.Parameterizable
52 * @ant.not-required
53 * @param parameter
54 * @throws BuildException
55 */
56 public void addConfiguredParameter(Param parameter) throws BuildException {
57 if (parameter.getName()==null) {
58 throw new BuildException("Unnamed parameter");
59 }
60 parameters.add(parameter);
61 }
62
63 /**
64 * Either value or class name is required. If both class name and value are
65 * specified then that class should have a constructor with one String parameter.
66 * @ant.not-required
67 * @param className
68 */
69 public void setClassName(String className) {
70 this.className = className;
71 }
72
73 /**
74 * Not required if class name is set.
75 * @ant.not-required
76 * @param value
77 */
78 public void setValue(String value) {
79 this.value = value;
80 }
81
82 /**
83 * @ant.ignore
84 * @return
85 */
86 public Collection getParameters() {
87 return parameters;
88 }
89
90 private Object theObject;
91
92 protected void validateClass(Class clazz) throws BuildException {
93
94 }
95
96 protected void validateInstance(Object instance) throws BuildException {
97
98 }
99
100 public Object getObject(ClassLoader masterClassLoader) throws BuildException {
101 if (theObject==null) {
102 if (className==null) {
103 if (parameters.isEmpty()) {
104 if (value==null) {
105 return null;
106 }
107 theObject=value;
108 } else {
109 throw new BuildException("Nested parameters supported only if classname attribute is set");
110 }
111 } else {
112 try {
113 ClassLoader classLoader=masterClassLoader==null ? getClass().getClassLoader() : masterClassLoader;
114 if (classPath!=null) {
115 if (masterClassLoader==null) {
116 classLoader=new AntClassLoader(getProject(), classPath);
117 } else {
118 classLoader=new AntClassLoader(masterClassLoader, getProject(), classPath, false);
119 }
120 }
121
122 if (classLoader==null) {
123 classLoader=getClass().getClassLoader();
124 }
125
126 Class theClass=classLoader.loadClass(className);
127 validateClass(theClass);
128 if (!(parameters.isEmpty() || Parameterizable.class.isAssignableFrom(theClass))) {
129 throw new BuildException(className+" does not support parameters");
130 }
131
132 if (value==null) {
133 theObject=theClass.newInstance();
134 } else {
135 theObject=theClass.getConstructor(new Class[] {String.class}).newInstance(new Object[] {value});
136 }
137
138 if (!parameters.isEmpty()) {
139 // Double check
140 if (theObject instanceof Parameterizable) {
141 Iterator it=parameters.iterator();
142 while (it.hasNext()) {
143 Param param=(Param) it.next();
144 try {
145 if (!((Parameterizable) theObject).setParameter(param.getName(), param.getObject(masterClassLoader))) {
146 throw new BuildException(theObject.getClass().getName()+" does not support parameter "+param.getName());
147 }
148 } catch (ConfigurationException e) {
149 throw new BuildException("Could not set parameter "+param.getName()+" for object entry "+theObject.getClass().getName(), e);
150 }
151 }
152 } else {
153 throw new BuildException(className+" does not support parameters");
154 }
155 }
156 validateInstance(theObject);
157 } catch (ClassNotFoundException e) {
158 throw new BuildException("Class not found: "+className, e);
159 } catch (InstantiationException e) {
160 throw new BuildException("Can not instantiate: "+className, e);
161 } catch (IllegalAccessException e) {
162 throw new BuildException("Can not instantiate: "+className, e);
163 } catch (InvocationTargetException e) {
164 throw new BuildException("Can not instantiate: "+className, e);
165 } catch (NoSuchMethodException e) {
166 throw new BuildException("Constructor "+className+"(String) not found", e);
167 }
168 }
169 }
170 return theObject;
171 }
172 /**
173 * @return
174 */
175 protected String getClassName() {
176 return className;
177 }
178
179 /**
180 * @return
181 */
182 protected String getValue() {
183 return value;
184 }
185
186 /**
187 * Classpath for loading classes
188 * @ant:not-required
189 */
190 private Path classPath;
191
192 /**
193 * Classpath.
194 * @ant.not-required
195 * @param classPath
196 */
197 public void setClassPath(Path classPath) {
198 if (this.classPath == null) {
199 this.classPath = classPath;
200 } else {
201 this.classPath.append(classPath);
202 }
203 }
204
205 /**
206 * Nested classpath element
207 * @ant:not-required
208 */
209 public Path createClasspath() {
210 if (classPath == null) {
211 classPath = new Path(getProject());
212 }
213 return classPath.createPath();
214 }
215
216 /**
217 * Text value.
218 * @ant.not-required
219 * @param text
220 */
221 public void addText(String text) {
222 this.value=text;
223 }
224}
225