You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hivemind.apache.org by hl...@apache.org on 2004/07/18 02:37:11 UTC
cvs commit: jakarta-hivemind/framework/src/java/org/apache/hivemind/impl CreateClassServiceConstructor.java
hlship 2004/07/17 17:37:11
Modified: framework/src/test/hivemind/test/services TestServices.java
TestBuilderFactory.java
framework/src/java/org/apache/hivemind/util
ConstructorUtils.java UtilStrings.properties
UtilMessages.java
framework/src/java/org/apache/hivemind/service/impl
ServiceMessages.java BuilderFactory.java
ServiceStrings.properties
framework/src/java/org/apache/hivemind/impl
CreateClassServiceConstructor.java
Added: framework/src/test/hivemind/test/services
BuilderAccessFailure.properties
framework/src/java/org/apache/hivemind/service/impl
BuilderFactoryLogic.java
Log:
Refactor BuilderFactory into BuilderFactory and BuilderFactoryLogic.
Revision Changes Path
1.19 +7 -13 jakarta-hivemind/framework/src/test/hivemind/test/services/TestServices.java
Index: TestServices.java
===================================================================
RCS file: /home/cvs/jakarta-hivemind/framework/src/test/hivemind/test/services/TestServices.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- TestServices.java 25 Jun 2004 20:19:57 -0000 1.18
+++ TestServices.java 18 Jul 2004 00:37:11 -0000 1.19
@@ -294,19 +294,13 @@
// s is a proxy, invoke a service method to force the creation of the
// service (and the error).
- try
- {
- s.getExtensionPointId();
- unreachable();
- }
- catch (ApplicationRuntimeException ex)
- {
- assertExceptionRegexp(
- ex,
- "Unable to construct service hivemind\\.test\\.services\\.BuilderAccessFailure: "
- + "Class hivemind\\.test\\.services\\.impl\\.BuilderAccessImpl "
- + "does not contain a property named 'EVIL'\\.");
- }
+ interceptLogging("hivemind.test.services");
+
+ String result = s.getLocalizedMessage("success");
+
+ assertLoggedMessagePattern("Class hivemind.test.services.impl.BuilderAccessImpl does not contain a property named 'EVIL'.");
+
+ assertEquals("Stumbles, logs error, and continues.", result);
}
public void testConstructorFactory() throws Exception
1.9 +1 -10 jakarta-hivemind/framework/src/test/hivemind/test/services/TestBuilderFactory.java
Index: TestBuilderFactory.java
===================================================================
RCS file: /home/cvs/jakarta-hivemind/framework/src/test/hivemind/test/services/TestBuilderFactory.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- TestBuilderFactory.java 16 Jul 2004 23:15:40 -0000 1.8
+++ TestBuilderFactory.java 18 Jul 2004 00:37:11 -0000 1.9
@@ -184,9 +184,6 @@
MockControl messagesControl = newControl(Messages.class);
Messages messages = (Messages) messagesControl.getMock();
- module.getErrorHandler();
- c.setReturnValue(eh);
-
module.getClassResolver();
c.setReturnValue(cr);
@@ -229,10 +226,7 @@
MockControl c = newControl(Module.class);
Module module = (Module) c.getMock();
-
- module.getErrorHandler();
- c.setReturnValue(newMock(ErrorHandler.class));
-
+
module.getClassResolver();
c.setReturnValue(new DefaultClassResolver());
@@ -263,9 +257,6 @@
MockControl c = newControl(Module.class);
Module module = (Module) c.getMock();
-
- module.getErrorHandler();
- c.setReturnValue(newMock(ErrorHandler.class));
module.getClassResolver();
c.setReturnValue(new DefaultClassResolver());
1.1 jakarta-hivemind/framework/src/test/hivemind/test/services/BuilderAccessFailure.properties
Index: BuilderAccessFailure.properties
===================================================================
#
# Copyright 2004 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.
success=Stumbles, logs error, and continues.
1.3 +7 -14 jakarta-hivemind/framework/src/java/org/apache/hivemind/util/ConstructorUtils.java
Index: ConstructorUtils.java
===================================================================
RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/ConstructorUtils.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ConstructorUtils.java 5 Jun 2004 19:09:12 -0000 1.2
+++ ConstructorUtils.java 18 Jul 2004 00:37:11 -0000 1.3
@@ -56,13 +56,11 @@
*
* @param targetClass the class to be instantiated
* @param parameters the parameters to pass to the constructor (may be null or empty)
- * @param location the location to use in any reported errors (this will be passed
- * to {@link HiveMind#getLocationString(Object)})
* @return the new instance
* @throws ApplicationRuntimeException on any failure
*
*/
- public static Object invokeConstructor(Class targetClass, Object[] parameters, Object location)
+ public static Object invokeConstructor(Class targetClass, Object[] parameters)
{
if (parameters == null)
parameters = new Object[0];
@@ -72,14 +70,13 @@
for (int i = 0; i < parameters.length; i++)
parameterTypes[i] = parameters[i] == null ? null : parameters[i].getClass();
- return invokeMatchingConstructor(targetClass, parameterTypes, parameters, location);
+ return invokeMatchingConstructor(targetClass, parameterTypes, parameters);
}
private static Object invokeMatchingConstructor(
Class targetClass,
Class[] parameterTypes,
- Object[] parameters,
- Object location)
+ Object[] parameters)
{
Constructor[] constructors = targetClass.getConstructors();
@@ -88,12 +85,11 @@
Constructor c = constructors[i];
if (isMatch(c, parameterTypes))
- return invoke(c, parameters, location);
+ return invoke(c, parameters);
}
throw new ApplicationRuntimeException(
- UtilMessages.noMatchingConstructor(targetClass, location),
- HiveMind.getLocation(location),
+ UtilMessages.noMatchingConstructor(targetClass),
null);
}
@@ -134,7 +130,7 @@
return false;
}
- private static Object invoke(Constructor c, Object[] parameters, Object location)
+ private static Object invoke(Constructor c, Object[] parameters)
{
try
{
@@ -142,10 +138,7 @@
}
catch (Exception ex)
{
- throw new ApplicationRuntimeException(
- UtilMessages.invokeFailed(c, location, ex),
- HiveMind.getLocation(location),
- ex);
+ throw new ApplicationRuntimeException(UtilMessages.invokeFailed(c, ex), null, ex);
}
}
}
1.4 +1 -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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- UtilStrings.properties 18 Jun 2004 13:50:33 -0000 1.3
+++ UtilStrings.properties 18 Jul 2004 00:37:11 -0000 1.4
@@ -15,7 +15,7 @@
no-such-property=Class {0} does not contain a property named ''{1}''.
no-matching-constructor=Unable to find a constructor for class {0}.
-invoke-failed=Failure invoking constructor for class {0} (at {1}): {2}
+invoke-failed=Failure invoking constructor for class {0}: {1}
no-writer=Property {0} of object {1} is read-only.
write-failure=Unable to update property {0} of object {1}: {2}
no-reader=Property {0} of object {1} is write-only.
1.6 +4 -11 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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- UtilMessages.java 18 Jun 2004 13:50:33 -0000 1.5
+++ UtilMessages.java 18 Jul 2004 00:37:11 -0000 1.6
@@ -35,21 +35,14 @@
return _formatter.format("no-such-property", target.getClass().getName(), propertyName);
}
- public static String noMatchingConstructor(Class targetClass, Object location)
+ public static String noMatchingConstructor(Class targetClass)
{
- return _formatter.format(
- "no-matching-constructor",
- targetClass.getName(),
- HiveMind.getLocationString(location));
+ return _formatter.format("no-matching-constructor", targetClass.getName());
}
- public static String invokeFailed(Constructor constructor, Object location, Throwable cause)
+ public static String invokeFailed(Constructor constructor, Throwable cause)
{
- return _formatter.format(
- "invoke-failed",
- constructor.getDeclaringClass().getName(),
- HiveMind.getLocationString(location),
- cause);
+ return _formatter.format("invoke-failed", constructor.getDeclaringClass().getName(), cause);
}
public static String noPropertyWriter(String propertyName, Object target)
1.11 +5 -0 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.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- ServiceMessages.java 17 Jul 2004 21:32:16 -0000 1.10
+++ ServiceMessages.java 18 Jul 2004 00:37:11 -0000 1.11
@@ -182,4 +182,9 @@
{
return _formatter.format("invalid-service-property-locator", locator);
}
+
+ public static String failureBuildingService(String serviceId, Throwable cause)
+ {
+ return _formatter.format("failure-building-service", serviceId, cause);
+ }
}
1.13 +7 -187 jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/BuilderFactory.java
Index: BuilderFactory.java
===================================================================
RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/BuilderFactory.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- BuilderFactory.java 16 Jul 2004 23:15:40 -0000 1.12
+++ BuilderFactory.java 18 Jul 2004 00:37:11 -0000 1.13
@@ -14,22 +14,12 @@
package org.apache.hivemind.service.impl;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Iterator;
import java.util.List;
-import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.hivemind.ApplicationRuntimeException;
-import org.apache.hivemind.ClassResolver;
-import org.apache.hivemind.ErrorHandler;
import org.apache.hivemind.HiveMind;
import org.apache.hivemind.ServiceImplementationFactory;
import org.apache.hivemind.internal.Module;
-import org.apache.hivemind.service.EventLinker;
-import org.apache.hivemind.util.ConstructorUtils;
-import org.apache.hivemind.util.PropertyUtils;
/**
* Implementation of {@link org.apache.hivemind.ServiceImplementationFactory}
@@ -59,185 +49,15 @@
{
HiveMind.checkFactoryParameterCount(POINT_ID, parameters, 1);
- Log log = LogFactory.getLog(serviceId);
- ErrorHandler errorHandler = invokingModule.getErrorHandler();
-
BuilderParameter parameter = (BuilderParameter) parameters.get(0);
- Object result = instantiateCoreServiceInstance(serviceId, invokingModule, parameter);
-
- setProperties(serviceId, invokingModule, parameter, result, log);
-
- registerForEvents(log, errorHandler, parameter, result);
-
- invokeInitializer(parameter, result, serviceId, log, invokingModule);
-
- return result;
- }
-
- private void registerForEvents(
- Log log,
- ErrorHandler eventHandler,
- BuilderParameter parameter,
- Object result)
- {
- List eventRegistrations = parameter.getEventRegistrations();
-
- if (eventRegistrations.isEmpty())
- return;
-
- EventLinker linker = new EventLinkerImpl(log, eventHandler);
- Iterator i = eventRegistrations.iterator();
- while (i.hasNext())
- {
- EventRegistration er = (EventRegistration) i.next();
-
- linker.addEventListener(
- er.getProducer(),
- er.getEventSetName(),
- result,
- er.getLocation());
- }
- }
-
- private void setProperties(
- String serviceId,
- Module invokingModule,
- BuilderParameter parameter,
- Object target,
- Log log)
- {
- List properties = parameter.getProperties();
- int count = properties.size();
-
- for (int i = 0; i < count; i++)
- {
- BuilderFacet facet = (BuilderFacet) properties.get(i);
- String propertyName = facet.getPropertyName();
-
- try
- {
- // Facets that can autowire, should.
-
- facet.autowire(target, serviceId, invokingModule);
-
- // There will be a facet for log, messages, service-id, etc. even if no
- // property name is specified, so we skip it here. In many cases, those
- // facets will have just done an autowire.
-
- if (propertyName == null)
- continue;
-
- Class targetType = PropertyUtils.getPropertyType(target, propertyName);
-
- Object value = facet.getFacetValue(serviceId, invokingModule, targetType);
-
- HiveMind.setLocation(value, facet.getLocation());
-
- PropertyUtils.write(target, propertyName, value);
- }
- catch (Exception ex)
- {
- throw new ApplicationRuntimeException(
- ex.getMessage(),
- target,
- facet.getLocation(),
- ex);
- }
- }
- }
-
- private Object instantiateCoreServiceInstance(
- String serviceId,
- Module invokingModule,
- BuilderParameter parameter)
- {
-
- ClassResolver resolver = invokingModule.getClassResolver();
- Class serviceClass = resolver.findClass(parameter.getClassName());
-
- Object[] constructorParameters =
- buildConstructorParameters(parameter, invokingModule, serviceId);
-
- return ConstructorUtils.invokeConstructor(serviceClass, constructorParameters, parameter);
- }
-
- private Object[] buildConstructorParameters(
- BuilderParameter parameter,
- Module invokingModule,
- String serviceId)
- {
- List parameters = parameter.getParameters();
- int count = parameters.size();
-
- if (count == 0)
- return null;
-
- Object[] result = new Object[count];
-
- for (int i = 0; i < count; i++)
- {
- BuilderFacet facet = (BuilderFacet) parameters.get(i);
-
- result[i] = facet.getFacetValue(serviceId, invokingModule, Object.class);
-
- HiveMind.setLocation(result[i], HiveMind.getLocation(facet));
- }
-
- return result;
- }
-
- private void invokeInitializer(
- BuilderParameter parameters,
- Object service,
- String serviceId,
- Log log,
- Module module)
- {
- String methodName = parameters.getInitializeMethod();
-
- boolean allowMissing = HiveMind.isBlank(methodName);
-
- String searchMethodName = allowMissing ? "initializeService" : methodName;
-
- try
- {
- findAndInvokeInitializerMethod(service, searchMethodName, allowMissing);
- }
- catch (Exception ex)
- {
- module.getErrorHandler().error(
- log,
- ServiceMessages.unableToInitializeService(
- serviceId,
- methodName,
- service.getClass(),
- ex),
- parameters.getLocation(),
- ex);
- }
- }
-
- private void findAndInvokeInitializerMethod(
- Object service,
- String methodName,
- boolean allowMissing)
- throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
- {
- Class serviceClass = service.getClass();
-
- try
- {
- Method m = serviceClass.getMethod(methodName, null);
-
- m.invoke(service, null);
- }
- catch (NoSuchMethodException ex)
- {
- if (allowMissing)
- return;
+ BuilderFactoryLogic logic =
+ new BuilderFactoryLogic(
+ invokingModule,
+ LogFactory.getLog(serviceId),
+ serviceId,
+ parameter);
- throw ex;
- }
+ return logic.createService();
}
}
1.7 +1 -0 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.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ServiceStrings.properties 17 Jul 2004 21:32:16 -0000 1.6
+++ ServiceStrings.properties 18 Jul 2004 00:37:11 -0000 1.7
@@ -35,3 +35,4 @@
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}
1.1 jakarta-hivemind/framework/src/java/org/apache/hivemind/service/impl/BuilderFactoryLogic.java
Index: BuilderFactoryLogic.java
===================================================================
// Copyright 2004 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.service.impl;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.hivemind.ClassResolver;
import org.apache.hivemind.ErrorHandler;
import org.apache.hivemind.HiveMind;
import org.apache.hivemind.internal.Module;
import org.apache.hivemind.service.EventLinker;
import org.apache.hivemind.util.ConstructorUtils;
import org.apache.hivemind.util.PropertyUtils;
/**
* Created by {@link org.apache.hivemind.service.impl.BuilderFactory} for each
* service to be created; encapsulates all the direct and indirect parameters
* used to construct a service.
*
* @author Howard Lewis Ship
*/
public class BuilderFactoryLogic
{
private Module _contributingModule;
private Log _log;
private String _serviceId;
private BuilderParameter _parameter;
public BuilderFactoryLogic(
Module contributingModule,
Log log,
String serviceId,
BuilderParameter parameter)
{
_contributingModule = contributingModule;
_log = log;
_serviceId = serviceId;
_parameter = parameter;
}
public Object createService()
{
Object result = null;
try
{
result = instantiateCoreServiceInstance();
setProperties(result);
registerForEvents(result);
invokeInitializer(result);
}
catch (Exception ex)
{
_contributingModule.getErrorHandler().error(
_log,
ServiceMessages.failureBuildingService(_serviceId, ex),
_parameter.getLocation(),
ex);
}
return result;
}
private Object instantiateCoreServiceInstance()
{
ClassResolver resolver = _contributingModule.getClassResolver();
Class serviceClass = resolver.findClass(_parameter.getClassName());
Object[] constructorParameters = buildConstructorParameters();
return ConstructorUtils.invokeConstructor(serviceClass, constructorParameters);
}
private Object[] buildConstructorParameters()
{
List parameters = _parameter.getParameters();
int count = parameters.size();
if (count == 0)
return null;
Object[] result = new Object[count];
for (int i = 0; i < count; i++)
{
BuilderFacet facet = (BuilderFacet) parameters.get(i);
try
{
result[i] = facet.getFacetValue(_serviceId, _contributingModule, Object.class);
HiveMind.setLocation(result[i], HiveMind.getLocation(facet));
}
catch (Exception ex)
{
_contributingModule.getErrorHandler().error(
_log,
ex.getMessage(),
facet.getLocation(),
ex);
}
}
return result;
}
private void invokeInitializer(Object service)
{
String methodName = _parameter.getInitializeMethod();
boolean allowMissing = HiveMind.isBlank(methodName);
String searchMethodName = allowMissing ? "initializeService" : methodName;
try
{
findAndInvokeInitializerMethod(service, searchMethodName, allowMissing);
}
catch (Exception ex)
{
_contributingModule.getErrorHandler().error(
_log,
ServiceMessages.unableToInitializeService(
_serviceId,
methodName,
service.getClass(),
ex),
_parameter.getLocation(),
ex);
}
}
private void findAndInvokeInitializerMethod(
Object service,
String methodName,
boolean allowMissing)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
Class serviceClass = service.getClass();
try
{
Method m = serviceClass.getMethod(methodName, null);
m.invoke(service, null);
}
catch (NoSuchMethodException ex)
{
if (allowMissing)
return;
throw ex;
}
}
private void registerForEvents(Object result)
{
List eventRegistrations = _parameter.getEventRegistrations();
if (eventRegistrations.isEmpty())
return;
EventLinker linker = new EventLinkerImpl(_log, _contributingModule.getErrorHandler());
Iterator i = eventRegistrations.iterator();
while (i.hasNext())
{
EventRegistration er = (EventRegistration) i.next();
// Will log any errors to the errorHandler
linker.addEventListener(
er.getProducer(),
er.getEventSetName(),
result,
er.getLocation());
}
}
private void setProperties(Object service)
{
List properties = _parameter.getProperties();
int count = properties.size();
for (int i = 0; i < count; i++)
{
BuilderFacet facet = (BuilderFacet) properties.get(i);
String propertyName = facet.getPropertyName();
try
{
// Facets that can autowire, should.
facet.autowire(service, _serviceId, _contributingModule);
// There will be a facet for log, messages, service-id, etc. even if no
// property name is specified, so we skip it here. In many cases, those
// facets will have just done an autowire.
if (propertyName == null)
continue;
Class targetType = PropertyUtils.getPropertyType(service, propertyName);
Object value = facet.getFacetValue(_serviceId, _contributingModule, targetType);
HiveMind.setLocation(value, facet.getLocation());
PropertyUtils.write(service, propertyName, value);
}
catch (Exception ex)
{
_contributingModule.getErrorHandler().error(
_log,
ex.getMessage(),
facet.getLocation(),
ex);
}
}
}
}
1.6 +1 -1 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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- CreateClassServiceConstructor.java 16 Jul 2004 23:15:40 -0000 1.5
+++ CreateClassServiceConstructor.java 18 Jul 2004 00:37:11 -0000 1.6
@@ -37,7 +37,7 @@
{
Class instanceClass = getInstanceClass();
- return ConstructorUtils.invokeConstructor(instanceClass, null, this);
+ return ConstructorUtils.invokeConstructor(instanceClass, null);
}
private synchronized Class getInstanceClass()
---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-cvs-help@jakarta.apache.org