hlship 2005/02/19 09:57:41
Modified: framework/src/test/hivemind/test TestMisc.java
framework/src/java/org/apache/hivemind/schema/rules
ClassTranslator.java RulesMessages.java
InstanceTranslator.java CreateObjectRule.java
RulesStrings.properties
src/documentation/content/xdocs descriptor.xml site.xml
rules.xml links.ent
framework/src/java/org/apache/hivemind/util
UtilMessages.java UtilStrings.properties
framework/src/java/org/apache/hivemind/service/impl
ServiceStrings.properties
ObjectInstanceObjectProvider.java
ServiceMessages.java
framework/src/descriptor/META-INF hivemodule.xml
. status.xml
framework/src/documentation/content/xdocs/hivemind
ObjectProviders.xml
framework/src/test/org/apache/hivemind/service/impl
TestObjectProviders.java
framework/src/java/org/apache/hivemind/impl
CreateClassServiceConstructor.java
Added: framework/src/test/hivemind/test SimpleBean.java
src/documentation/content/xdocs instance-initialization.xml
framework/src/test/org/apache/hivemind/util
TestInstanceCreationUtils.java
framework/src/java/org/apache/hivemind/util
InstanceCreationUtils.java
framework/src/test/org/apache/hivemind/schema/rules
TestCreateObjectRule.java Bean.java
Log:
HIVEMIND-93: Allow initialization in create-instance, rules, etc.
Revision Changes Path
1.24 +18 -1
jakarta-hivemind/framework/src/test/hivemind/test/TestMisc.java
Index: TestMisc.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/test/hivemind/test/TestMisc.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- TestMisc.java 10 Feb 2005 01:04:35 -0000 1.23
+++ TestMisc.java 19 Feb 2005 17:57:40 -0000 1.24
@@ -127,6 +127,23 @@
assertTrue(o2 instanceof HashMap);
}
+ /** @since 1.1 */
+
+ public void testCreateInstanceWithInitializer()
+ {
+ Module m = newModule();
+
+ CreateClassServiceConstructor c = new
CreateClassServiceConstructor();
+
+ c.setContributingModule(m);
+
+ c.setInstanceClassName(SimpleBean.class.getName() +
",value=HiveMind");
+
+ SimpleBean b = (SimpleBean) c.constructCoreServiceImplementation();
+
+ assertEquals("HiveMind", b.getValue());
+ }
+
public void testCreateClassServiceConstructorFailure()
{
Module m = newModule();
@@ -146,7 +163,7 @@
{
assertExceptionSubstring(
ex,
- "Unable to find a constructor for class
hivemind.test.PrivateBean.");
+ "Unable to instantiate instance of class
hivemind.test.PrivateBean");
}
}
1.1
jakarta-hivemind/framework/src/test/hivemind/test/SimpleBean.java
Index: SimpleBean.java
===================================================================
// Copyright 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hivemind.test;
/**
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class SimpleBean
{
public String _value;
public String getValue()
{
return _value;
}
public void setValue(String value)
{
_value = value;
}
}
1.9 +0 -10
jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/ClassTranslator.java
Index: ClassTranslator.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/ClassTranslator.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ClassTranslator.java 10 Feb 2005 01:04:33 -0000 1.8
+++ ClassTranslator.java 19 Feb 2005 17:57:40 -0000 1.9
@@ -14,7 +14,6 @@
package org.apache.hivemind.schema.rules;
-import org.apache.hivemind.ClassResolver;
import org.apache.hivemind.HiveMind;
import org.apache.hivemind.Location;
import org.apache.hivemind.internal.Module;
@@ -30,15 +29,6 @@
public Object translate(Module contributingModule, Class propertyType,
String inputValue,
Location location)
{
- return extractClass(contributingModule, inputValue);
- }
-
- /**
- * Converts the value from a class name to an instance of the class.
Null input returns null. If
- * an exception is thrown, then it is logged and null is returned.
- */
- protected Class extractClass(Module contributingModule, String
inputValue)
- {
if (HiveMind.isBlank(inputValue))
return null;
1.8 +0 -10
jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/RulesMessages.java
Index: RulesMessages.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/RulesMessages.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- RulesMessages.java 5 Jan 2005 18:03:41 -0000 1.7
+++ RulesMessages.java 19 Feb 2005 17:57:40 -0000 1.8
@@ -59,11 +59,6 @@
return _formatter.format("invalid-boolean-value", inputValue);
}
- public static String errorCreatingObject(String className, Location
location, Throwable cause)
- {
- return _formatter.format("error-creating-object", className,
location, cause);
- }
-
public static String invalidDoubleValue(String inputValue)
{
return _formatter.format("invalid-double-value", inputValue);
@@ -178,9 +173,4 @@
propertyType.getName(),
cause);
}
-
- public static String unableToInstantiateInstanceOfClass(Class clazz,
Throwable cause)
- {
- return _formatter.format("unable-to-instantiate-instance-of-class",
clazz.getName(), cause);
- }
}
\ No newline at end of file
1.6 +4 -29
jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/InstanceTranslator.java
Index: InstanceTranslator.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/InstanceTranslator.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- InstanceTranslator.java 19 Feb 2005 02:40:54 -0000 1.5
+++ InstanceTranslator.java 19 Feb 2005 17:57:40 -0000 1.6
@@ -14,18 +14,18 @@
package org.apache.hivemind.schema.rules;
-import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.HiveMind;
import org.apache.hivemind.Location;
import org.apache.hivemind.internal.Module;
-import org.apache.hivemind.util.PropertyUtils;
+import org.apache.hivemind.schema.Translator;
+import org.apache.hivemind.util.InstanceCreationUtils;
/**
* Used to translate from a class name to an instance of the class.
*
* @author Howard Lewis Ship
*/
-public class InstanceTranslator extends ClassTranslator
+public class InstanceTranslator implements Translator
{
public Object translate(Module contributingModule, Class propertyType,
String inputValue,
Location location)
@@ -33,32 +33,7 @@
if (HiveMind.isBlank(inputValue))
return null;
- int commax = inputValue.indexOf(',');
-
- String className = (commax < 0) ? inputValue :
inputValue.substring(0, commax);
-
- Class objectClass = extractClass(contributingModule, className);
-
- try
- {
- Object result = objectClass.newInstance();
-
- if (commax > 0)
- PropertyUtils.configureProperties(result,
inputValue.substring(commax + 1));
-
- HiveMind.setLocation(result, location);
-
- return result;
- }
- catch (Exception ex)
- {
- // JDK 1.4 produces a good message here, but JDK 1.3 does not,
so we
- // create our own.
-
- throw new
ApplicationRuntimeException(RulesMessages.unableToInstantiateInstanceOfClass(
- objectClass,
- ex), location, ex);
- }
+ return InstanceCreationUtils.createInstance(contributingModule,
inputValue, location);
}
}
\ No newline at end of file
1.8 +5 -19
jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/CreateObjectRule.java
Index: CreateObjectRule.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/CreateObjectRule.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- CreateObjectRule.java 10 Feb 2005 01:04:33 -0000 1.7
+++ CreateObjectRule.java 19 Feb 2005 17:57:40 -0000 1.8
@@ -14,11 +14,10 @@
package org.apache.hivemind.schema.rules;
-import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Element;
-import org.apache.hivemind.HiveMind;
import org.apache.hivemind.LocationHolder;
import org.apache.hivemind.schema.SchemaProcessor;
+import org.apache.hivemind.util.InstanceCreationUtils;
/**
* Basic [EMAIL PROTECTED] org.apache.hivemind.schema.Rule}for creating a
new object. Created from the the
@@ -47,23 +46,10 @@
*/
public void begin(SchemaProcessor processor, Element element)
{
- Object object = null;
-
- try
- {
- Class objectClass =
processor.getDefiningModule().resolveType(_className);
-
- object = objectClass.newInstance();
- }
- catch (Exception ex)
- {
- throw new
ApplicationRuntimeException(RulesMessages.errorCreatingObject(
- _className,
- getLocation(),
- ex), getLocation(), ex);
- }
-
- HiveMind.setLocation(object, element.getLocation());
+ Object object = InstanceCreationUtils.createInstance(
+ processor.getDefiningModule(),
+ _className,
+ element.getLocation());
processor.push(object);
}
1.5 +1 -3
jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/RulesStrings.properties
Index: RulesStrings.properties
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/schema/rules/RulesStrings.properties,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- RulesStrings.properties 6 Jan 2005 01:45:08 -0000 1.4
+++ RulesStrings.properties 19 Feb 2005 17:57:40 -0000 1.5
@@ -15,7 +15,6 @@
unable-to-set-element-property=Unable to update property {0} of {1} (in
element {2}, at {3}): {4}
unable-to-set-property=Unable set property {0} of {1}: {2}
invalid-boolean-value=''{0}'' is not a boolean value (which should be either
''true'' or ''false'').
-error-creating-object=Error creating instance of class {0} (at {1}): {2}
invalid-double-value=''{0}'' is not a double value.
min-double-value=Value {0} is less than minimum value {1}.
@@ -40,5 +39,4 @@
invalid-initializer=Initializer string (''{0}'') is not in proper format
(key=value[,key=value]*).
no-property-editor=No property editor for {0}.
smart-translator-error=Unable to translate ''{0}'' to type {1}: {2}
-
-unable-to-instantiate-instance-of-class=Unable to instantiate instance of
class {0}: {1}
+`
\ No newline at end of file
1.22 +1 -1
jakarta-hivemind/src/documentation/content/xdocs/descriptor.xml
Index: descriptor.xml
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/src/documentation/content/xdocs/descriptor.xml,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- descriptor.xml 17 Feb 2005 19:46:44 -0000 1.21
+++ descriptor.xml 19 Feb 2005 17:57:40 -0000 1.22
@@ -218,7 +218,7 @@
<td>class</td>
<td>class name</td>
<td>yes</td>
- <td>Fully qualified class name to
instantiate.</td>
+ <td>The class name to instantiate. May
use &instance-initializer; syntax.</td>
</tr>
<tr>
<td>model</td>
1.42 +1 -0 jakarta-hivemind/src/documentation/content/xdocs/site.xml
Index: site.xml
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/src/documentation/content/xdocs/site.xml,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -r1.41 -r1.42
--- site.xml 11 Feb 2005 15:29:51 -0000 1.41
+++ site.xml 19 Feb 2005 17:57:40 -0000 1.42
@@ -37,6 +37,7 @@
<dependencies label="Dependencies" href="dependencies.html"/>
<conditional label="Conditional Contributions"
href="conditional.html"/>
<serial label="Serializing Services" href="serializing.html"/>
+ <instance-initialization label="Instance Initialization"
href="instance-initialization.html"/>
</reference>
<project-info label="Project Information">
1.10 +4 -16
jakarta-hivemind/src/documentation/content/xdocs/rules.xml
Index: rules.xml
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/src/documentation/content/xdocs/rules.xml,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- rules.xml 19 Feb 2005 02:40:54 -0000 1.9
+++ rules.xml 19 Feb 2005 17:57:40 -0000 1.10
@@ -79,7 +79,8 @@
<td>yes</td>
<td>The complete class name of
the object to create. The class must
be public, and have a
no-arguments public constructor. The class name may
- be abbreviated; it is resolved by the module containing the
&schema; element.</td>
+ be abbreviated; it is resolved by the module containing the
&schema; element.
+ You may use &instance-initializer; syntax.</td>
</tr>
</table>
</section>
@@ -369,21 +370,8 @@
<p>The class name may be abbreviated.</p>
<p>
- The instance translator may also initialize properties of the
object. Following the
- class name, a comma-seperated list of property names may be
provided. For example:
<code>org.example.MyBean,firstProperty=firstValue,secondProperty=secondValue</code>.
- </p>
-
- <p>
- The instance translator will convert the values into the
appropriate type (typically, number types).
- </p>
-
- <p>
- For boolean properties, you may omit the equals sign and value to
set the property to true. Example:
- <code>org.example.MyBean,booleanProperty</code>. Alternately, the
property name may be prefixed with
- an esclamation point to set the value to false. Example:
- <code>org.example.MyBean,!booleanProperty</code>.
- </p>
-
+ The instance translator may also initialize properties using
&instance-initializer; syntax.
+ </p>
</section>
<section>
<title>int</title>
1.24 +1 -0
jakarta-hivemind/src/documentation/content/xdocs/links.ent
Index: links.ent
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/src/documentation/content/xdocs/links.ent,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- links.ent 11 Feb 2005 15:29:51 -0000 1.23
+++ links.ent 19 Feb 2005 17:57:40 -0000 1.24
@@ -140,6 +140,7 @@
<!-- Entities for common services and configurations. -->
<!ENTITY object-reference '<link href="site:hivemind.ObjectProviders">object
reference</link>'>
+<!ENTITY instance-initializer '<link
href="site:instance-initialization">instance initializer</link>'>
<!ENTITY hivemind.ApplicationDefaults '<link
href="site:hivemind.ApplicationDefaults">hivemind.ApplicationDefaults</link>'>
<!ENTITY hivemind.BuilderFactory '<link
href="site:hivemind.BuilderFactory">hivemind.BuilderFactory</link>'>
1.1
jakarta-hivemind/src/documentation/content/xdocs/instance-initialization.xml
Index: instance-initialization.xml
===================================================================
<?xml version="1.0"?>
<!--
Copyright 2005 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.3//EN"
"http://xml.apache.org/forrest/dtd/document-v13.dtd" [
<!ENTITY % common-links SYSTEM "links.ent">
%common-links;
]>
<document>
<header>
<title>Lightweight Instance Initialization</title>
</header>
<body>
<p> There are frequently situations in which you want HiveMind to create
an instance of an object for you, and initialize some of its properties. </p>
<p> The &hivemind.BuilderFactory; can do this ... but it means dressing
up your instance as a complete service, which can be cumbersome and overkill.
</p>
<p> In HiveMind 1.0, you didn't have many other options. Starting with
HiveMind 1.1, you do: <em>lightweight instance initialization</em>. </p>
<p> In certain places where you specify a class name (&create-instance;,
&create-object;, the <link href="rules.html#instance">instance
translator</link> and the <link href="site:hivemind.ObjectProviders">instance
object provider</link>), you can further identify a list of properties and
values. The values are always strings and are converted to appropriate types
for assignment. </p>
<p> The class name is seperated from the property values by a comma, and
a comma seperates each property value pair. An equals sign seperates each
property from its value. The end result is quite readable:
<code>com.example.MyBean,name=Bean,priority=10</code>. </p>
<p> For properties that are boolean, you can omit the equals sign and
value. The value 'true' is assumed. Example:
<code>com.example.MyBean,required</code> would set the required property of the
bean to true. </p>
<p> Alternately, you can prefix the boolean property name with an
exclamation point to set the value to false. Example:
<code>com.example.MyBean,!required</code> </p>
<p> These object instantiations and initializations occur within a
reference module; you can omit the module's package from the class name. </p>
</body>
</document>
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestInstanceCreationUtils.java
Index: TestInstanceCreationUtils.java
===================================================================
// Copyright 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.util;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Location;
import org.apache.hivemind.impl.BaseLocatable;
import org.apache.hivemind.internal.Module;
import org.apache.hivemind.test.HiveMindTestCase;
import org.easymock.MockControl;
/**
* Tests for [EMAIL PROTECTED]
org.apache.hivemind.util.InstanceCreationUtils}.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class TestInstanceCreationUtils extends HiveMindTestCase
{
private static class Bean
{
private int _value;
public int getValue()
{
return _value;
}
public void setValue(int value)
{
_value = value;
}
}
private Module newModule(String name, Class returnValue)
{
MockControl control = newControl(Module.class);
Module module = (Module) control.getMock();
module.resolveType(name);
control.setReturnValue(returnValue);
return module;
}
public void testSimple()
{
Module module = newModule("Bean", Bean.class);
replayControls();
Bean bean = (Bean) InstanceCreationUtils.createInstance(module,
"Bean", null);
assertNotNull(bean);
verifyControls();
}
public void testComplex()
{
Module module = newModule("Bean", Bean.class);
replayControls();
Bean bean = (Bean) InstanceCreationUtils.createInstance(module,
"Bean,value=42", null);
assertEquals(42, bean.getValue());
verifyControls();
}
public void testSetLocation()
{
Location l = fabricateLocation(11);
Module module = newModule("Holder", BaseLocatable.class);
replayControls();
BaseLocatable holder = (BaseLocatable)
InstanceCreationUtils.createInstance(
module,
"Holder",
l);
assertSame(l, holder.getLocation());
verifyControls();
}
public void testFailure()
{
Location l = fabricateLocation(11);
Module module = newModule("Bean", Bean.class);
replayControls();
try
{
InstanceCreationUtils.createInstance(module, "Bean,value=fred",
l);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertExceptionSubstring(
ex,
"Unable to instantiate instance of class
org.apache.hivemind.util.TestInstanceCreationUtils$Bean");
assertSame(l, ex.getLocation());
}
verifyControls();
}
}
1.11 +6 -0
jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilMessages.java
Index: UtilMessages.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilMessages.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- UtilMessages.java 19 Feb 2005 02:40:53 -0000 1.10
+++ UtilMessages.java 19 Feb 2005 17:57:40 -0000 1.11
@@ -99,4 +99,10 @@
return _formatter.format("unable-to-convert", new Object[]
{ value, propertyType.getName(), propertyName, target, cause });
}
+
+ /** @since 1.1 */
+ public static String unableToInstantiateInstanceOfClass(Class clazz,
Throwable cause)
+ {
+ return _formatter.format("unable-to-instantiate-instance-of-class",
clazz.getName(), cause);
+ }
}
\ No newline at end of file
1.8 +2 -1
jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilStrings.properties
Index: UtilStrings.properties
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilStrings.properties,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- UtilStrings.properties 19 Feb 2005 02:40:53 -0000 1.7
+++ UtilStrings.properties 19 Feb 2005 17:57:40 -0000 1.8
@@ -24,4 +24,5 @@
bad-file-url=Error retrieving URL for file {0}: {1}
unable-to-reference-context-path=Unable to reference context path ''{0}''.
no-property-editor=No property editor exists for property {0} of class {1}.
-unable-to-convert=Unable to convert ''{0}'' to type {1} (for property {2} of
object {3}): {4}
\ No newline at end of file
+unable-to-convert=Unable to convert ''{0}'' to type {1} (for property {2} of
object {3}): {4}
+unable-to-instantiate-instance-of-class=Unable to instantiate instance of
class {0}: {1}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/util/InstanceCreationUtils.java
Index: InstanceCreationUtils.java
===================================================================
// Copyright 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.util;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.HiveMind;
import org.apache.hivemind.Location;
import org.apache.hivemind.internal.Module;
/**
* Code for creating an instance, possibly setting its [EMAIL PROTECTED]
org.apache.hivemind.Location}, and
* possibly initializing its properties.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class InstanceCreationUtils
{
/**
* Creates an instance.
*
* @param module
* the referencing module, used to resolve partial class names
* @param initializer
* The name of the class to instantiate, possibly followed by
a list of properties
* and values. I.e.
<code>com.examples.MyBean,property=value</code>.
* @param location
* The location to assign to the newly created instance, if it
implements
* [EMAIL PROTECTED] org.apache.hivemind.LocationHolder}. Also
the location used to report
* errors creating or configuring the instance.
*/
public static Object createInstance(Module module, String initializer,
Location location)
{
int commax = initializer.indexOf(',');
String className = (commax < 0) ? initializer :
initializer.substring(0, commax);
Class objectClass = module.resolveType(className);
try
{
Object result = objectClass.newInstance();
HiveMind.setLocation(result, location);
if (commax > 0)
PropertyUtils.configureProperties(result,
initializer.substring(commax + 1));
return result;
}
catch (Exception ex)
{
// JDK 1.4 produces a good message here, but JDK 1.3 does not, so
we
// create our own.
throw new
ApplicationRuntimeException(UtilMessages.unableToInstantiateInstanceOfClass(
objectClass,
ex), location, ex);
}
}
}
1.13 +0 -1
jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/ServiceStrings.properties
Index: ServiceStrings.properties
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/ServiceStrings.properties,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- ServiceStrings.properties 17 Feb 2005 19:46:44 -0000 1.12
+++ ServiceStrings.properties 19 Feb 2005 17:57:40 -0000 1.13
@@ -32,7 +32,6 @@
invalid-provider-selector=Object provider selector ''{0}'' is not properly
formatted.
unknown-provider-prefix=No object provider exists for prefix ''{0}''.
duplicate-provider-prefix=Object provider prefix ''{0}'' duplicates a
previous prefix at {1}.
-error-instantiating-instance=Error instantiating instance of class {0}: {1}
invalid-service-property-locator=''{0}'' is not a valid locator for use with
the service-property object provider. It should be the id of a service, a
comma, and the name of a property provided by that service.
failure-building-service=Error building service {0}: {1}
autowire-property-failure=Unable to autowire property {0} of service {1}: {2}
1.5 +2 -18
jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/ObjectInstanceObjectProvider.java
Index: ObjectInstanceObjectProvider.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/ObjectInstanceObjectProvider.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ObjectInstanceObjectProvider.java 10 Feb 2005 01:04:34 -0000 1.4
+++ ObjectInstanceObjectProvider.java 19 Feb 2005 17:57:40 -0000 1.5
@@ -14,11 +14,10 @@
package org.apache.hivemind.service.impl;
-import org.apache.hivemind.ApplicationRuntimeException;
-import org.apache.hivemind.HiveMind;
import org.apache.hivemind.Location;
import org.apache.hivemind.internal.Module;
import org.apache.hivemind.service.ObjectProvider;
+import org.apache.hivemind.util.InstanceCreationUtils;
/**
* Implementation of [EMAIL PROTECTED]
org.apache.hivemind.service.ObjectProvider}that instantiates a new
@@ -32,22 +31,7 @@
public Object provideObject(Module contributingModule, Class
propertyType, String locator,
Location location)
{
- try
- {
- Class instanceClass = contributingModule.resolveType(locator);
-
- Object result = instanceClass.newInstance();
-
- HiveMind.setLocation(result, location);
-
- return result;
- }
- catch (Exception ex)
- {
- throw new
ApplicationRuntimeException(ServiceMessages.errorInstantiatingInstance(
- locator,
- ex), location, ex);
- }
+ return InstanceCreationUtils.createInstance(contributingModule,
locator, location);
}
}
\ No newline at end of file
1.18 +0 -5
jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/ServiceMessages.java
Index: ServiceMessages.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/ServiceMessages.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- ServiceMessages.java 17 Feb 2005 19:46:44 -0000 1.17
+++ ServiceMessages.java 19 Feb 2005 17:57:40 -0000 1.18
@@ -140,11 +140,6 @@
return _formatter.format("duplicate-provider-prefix", prefix,
priorLocation);
}
- public static String errorInstantiatingInstance(String className,
Throwable cause)
- {
- return _formatter.format("error-instantiating-instance", className,
cause);
- }
-
public static String invalidServicePropertyLocator(String locator)
{
return _formatter.format("invalid-service-property-locator",
locator);
1.26 +1 -3
jakarta-hivemind/framework/src/descriptor/META-INF/hivemodule.xml
Index: hivemodule.xml
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/descriptor/META-INF/hivemodule.xml,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- hivemodule.xml 17 Feb 2005 19:46:45 -0000 1.25
+++ hivemodule.xml 19 Feb 2005 17:57:40 -0000 1.26
@@ -131,8 +131,7 @@
</attribute>
<rules>
- <create-object class="service.MethodContribution"/>
- <set-property property="include" value="true"/>
+ <create-object class="service.MethodContribution,include"/>
<read-attribute attribute="method" property="methodPattern"/>
<invoke-parent method="addElement"/>
</rules>
@@ -146,7 +145,6 @@
<rules>
<create-object class="service.MethodContribution"/>
- <set-property property="include" value="false"/>
<read-attribute attribute="method" property="methodPattern"/>
<invoke-parent method="addElement"/>
</rules>
1.103 +1 -0 jakarta-hivemind/status.xml
Index: status.xml
===================================================================
RCS file: /home/cvs/jakarta-hivemind/status.xml,v
retrieving revision 1.102
retrieving revision 1.103
diff -u -r1.102 -r1.103
--- status.xml 19 Feb 2005 16:28:09 -0000 1.102
+++ status.xml 19 Feb 2005 17:57:40 -0000 1.103
@@ -39,6 +39,7 @@
<action type="update" dev="HLS"> Allow service interfaces to be
ordinary non-interface classes. </action>
<action type="update" dev="HLS"> Extend instance translator to
configure properties of the instantiated instance. </action>
<action type="fix" dev="KW" fixes-bug="HIVEMIND-60" due-to="Trent
McClenahan"> service: object provider throws NPE with empty locator </action>
+ <action type="fix" dev="HLS" fixes-bug="HIVEMIND-93"> Allow
initialization in create-instance, rules, etc. </action>
</release>
<release version="1.1-alpha-1" date="Jan 19 2005">
<action type="add" dev="KW"> Added <dependency> construct
providing support for dependencies between modules. Refactoring of
RegistryBuilder: Now constructs Registry using a ModuleProvider. </action>
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/schema/rules/TestCreateObjectRule.java
Index: TestCreateObjectRule.java
===================================================================
// Copyright 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.schema.rules;
import org.apache.hivemind.Element;
import org.apache.hivemind.Location;
import org.apache.hivemind.internal.Module;
import org.apache.hivemind.schema.SchemaProcessor;
import org.apache.hivemind.test.AggregateArgumentsMatcher;
import org.apache.hivemind.test.ArgumentMatcher;
import org.apache.hivemind.test.HiveMindTestCase;
import org.easymock.MockControl;
/**
* Tests for [EMAIL PROTECTED]
org.apache.hivemind.schema.rules.CreateObjectRule}.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class TestCreateObjectRule extends HiveMindTestCase
{
private Module newModule(String className, Class result)
{
MockControl control = newControl(Module.class);
Module module = (Module) control.getMock();
module.resolveType(className);
control.setReturnValue(result);
return module;
}
private Element newElement(Location location)
{
MockControl control = newControl(Element.class);
Element element = (Element) control.getMock();
element.getLocation();
control.setReturnValue(location);
return element;
}
public void testCreateWithInitializer()
{
final Location l = fabricateLocation(123);
Module module = newModule("Bean", Bean.class);
Element element = newElement(l);
MockControl control = newControl(SchemaProcessor.class);
SchemaProcessor processor = (SchemaProcessor) control.getMock();
processor.getDefiningModule();
control.setReturnValue(module);
processor.push(new Bean());
control.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher()
{
public boolean matches(Object expected, Object actual)
{
Bean b = (Bean) actual;
assertEquals("HiveMind", b.getValue());
assertSame(l, b.getLocation());
return true;
}
}));
replayControls();
CreateObjectRule rule = new CreateObjectRule("Bean,value=HiveMind");
rule.begin(processor, element);
verifyControls();
processor.pop();
control.setReturnValue(null);
replayControls();
rule.end(processor, element);
verifyControls();
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/schema/rules/Bean.java
Index: Bean.java
===================================================================
// Copyright 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.schema.rules;
import org.apache.hivemind.impl.BaseLocatable;
/**
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class Bean extends BaseLocatable
{
private String _value;
public String getValue()
{
return _value;
}
public void setValue(String value)
{
_value = value;
}
}
1.11 +2 -1
jakarta-hivemind/framework/src/documentation/content/xdocs/hivemind/ObjectProviders.xml
Index: ObjectProviders.xml
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/documentation/content/xdocs/hivemind/ObjectProviders.xml,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- ObjectProviders.xml 11 Feb 2005 18:15:21 -0000 1.10
+++ ObjectProviders.xml 19 Feb 2005 17:57:40 -0000 1.11
@@ -87,7 +87,8 @@
<tr>
<td>instance</td>
- <td>The locator is a fully qualified class name, which must have a
public no arguments contructor.</td>
+ <td>The locator is a class name, which must have a public no arguments
contructor.
+ You may use &instance-initializer; syntax.</td>
<td>
instance:com.example.MyObject
</td>
1.9 +1 -1
jakarta-hivemind/framework/src/test/org/apache/hivemind/service/impl/TestObjectProviders.java
Index: TestObjectProviders.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/test/org/apache/hivemind/service/impl/TestObjectProviders.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- TestObjectProviders.java 19 Feb 2005 16:28:09 -0000 1.8
+++ TestObjectProviders.java 19 Feb 2005 17:57:40 -0000 1.9
@@ -124,7 +124,7 @@
}
catch (ApplicationRuntimeException ex)
{
- assertExceptionSubstring(ex, "Error instantiating instance of
class java.util.List");
+ assertExceptionSubstring(ex, "Unable to instantiate instance of
class java.util.List");
}
}
1.9 +5 -24
jakarta-hivemind/framework/src/java/org/apache/hivemind/impl/CreateClassServiceConstructor.java
Index: CreateClassServiceConstructor.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/impl/CreateClassServiceConstructor.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- CreateClassServiceConstructor.java 10 Feb 2005 01:04:33 -0000
1.8
+++ CreateClassServiceConstructor.java 19 Feb 2005 17:57:41 -0000
1.9
@@ -14,10 +14,9 @@
package org.apache.hivemind.impl;
-import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.internal.Module;
import org.apache.hivemind.internal.ServiceImplementationConstructor;
-import org.apache.hivemind.util.ConstructorUtils;
+import org.apache.hivemind.util.InstanceCreationUtils;
/**
* Constructs a service by instantiating a class.
@@ -31,30 +30,12 @@
private String _instanceClassName;
- private Class _instanceClass;
-
public Object constructCoreServiceImplementation()
{
- Class instanceClass = getInstanceClass();
-
- return ConstructorUtils.invokeConstructor(instanceClass, null);
- }
-
- private synchronized Class getInstanceClass()
- {
- if (_instanceClass == null)
- {
- try
- {
- _instanceClass =
_contributingModule.resolveType(_instanceClassName);
- }
- catch (Exception ex)
- {
- throw new ApplicationRuntimeException(ex.getMessage(),
getLocation(), ex);
- }
- }
-
- return _instanceClass;
+ return InstanceCreationUtils.createInstance(
+ _contributingModule,
+ _instanceClassName,
+ getLocation());
}
public Module getContributingModule()
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]