You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hivemind.apache.org by ah...@apache.org on 2006/10/04 08:27:42 UTC
svn commit: r452756 - in /hivemind/branches/branch-2-0-annot/annotations: ./
src/ src/java/ src/java/org/ src/java/org/apache/
src/java/org/apache/hivemind/ src/java/org/apache/hivemind/annotations/
src/java/org/apache/hivemind/annotations/internal/ sr...
Author: ahuegen
Date: Tue Oct 3 23:27:40 2006
New Revision: 452756
URL: http://svn.apache.org/viewvc?view=rev&rev=452756
Log:
Basic infrastructure for annotated modules
Added:
hivemind/branches/branch-2-0-annot/annotations/
hivemind/branches/branch-2-0-annot/annotations/pom.xml
hivemind/branches/branch-2-0-annot/annotations/src/
hivemind/branches/branch-2-0-annot/annotations/src/java/
hivemind/branches/branch-2-0-annot/annotations/src/java/org/
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotatedModuleReader.java
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsMessages.java
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsStrings.properties
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Service.java
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/FactoryMethodImplementationConstructor.java
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProvider.java
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProviderImpl.java
hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java
hivemind/branches/branch-2-0-annot/annotations/src/test/
hivemind/branches/branch-2-0-annot/annotations/src/test/org/
hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/
hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/
hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/
hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java
hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java
Added: hivemind/branches/branch-2-0-annot/annotations/pom.xml
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/pom.xml?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/pom.xml (added)
+++ hivemind/branches/branch-2-0-annot/annotations/pom.xml Tue Oct 3 23:27:40 2006
@@ -0,0 +1,75 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.hivemind</groupId>
+ <artifactId>hivemind-annotations</artifactId>
+ <packaging>jar</packaging>
+ <version>2.0.0_M1</version>
+ <parent>
+ <groupId>org.apache.hivemind</groupId>
+ <artifactId>hivemind-project</artifactId>
+ <version>2.0.0_M1</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+ <name>HiveMind Annotations Support</name>
+ <inceptionYear>2006</inceptionYear>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.hivemind</groupId>
+ <artifactId>hivemind</artifactId>
+ <version>2.0.0_M1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.hivemind</groupId>
+ <artifactId>hivemind</artifactId>
+ <version>2.0.0_M1</version>
+ <type>test-jar</type>
+ </dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>easymock</groupId>
+ <artifactId>easymock</artifactId>
+ </dependency>
+ </dependencies>
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>compile</id>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ <optimize>true</optimize>
+ </configuration>
+ </execution>
+ <execution>
+ <id>test-compile</id>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+</project>
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotatedModuleReader.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotatedModuleReader.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotatedModuleReader.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotatedModuleReader.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,88 @@
+package org.apache.hivemind.annotations;
+
+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.annotations.internal.AnnotatedModuleProcessor;
+import org.apache.hivemind.definition.RegistryDefinition;
+import org.apache.hivemind.impl.DefaultClassResolver;
+import org.apache.hivemind.impl.DefaultErrorHandler;
+
+/**
+ * Reads a annotated hivemind module into a {@link RegistryDefinition}. Thus
+ * the defined services configurations and contributions are added to the
+ * registry.
+ * The class delegates the work to {@link AnnotatedModuleProcessor}
+ *
+ * @author Achim Huegen
+ */
+public class AnnotatedModuleReader
+{
+ private static final Log LOG = LogFactory.getLog(AnnotatedModuleReader.class);
+
+ private RegistryDefinition _registryDefinition;
+
+ private AnnotatedModuleProcessor _processor;
+
+ private ErrorHandler _errorHandler;
+
+ private ClassResolver _classResolver;
+
+ public AnnotatedModuleReader(RegistryDefinition registryDefinition)
+ {
+ this(registryDefinition, new DefaultClassResolver(), new DefaultErrorHandler());
+ }
+
+ public AnnotatedModuleReader(RegistryDefinition registryDefinition, ClassResolver classResolver,
+ ErrorHandler errorHandler)
+ {
+ _registryDefinition = registryDefinition;
+ _classResolver = classResolver;
+ _errorHandler = errorHandler;
+ _processor = new AnnotatedModuleProcessor(_registryDefinition, _classResolver, _errorHandler);
+ }
+
+ /**
+ * Reads an annotated module specified by its classname. The module must have a no
+ * argument constructor.
+ *
+ * @param moduleClassName class name of the module
+ */
+ public void readModule(String moduleClassName)
+ {
+ Class moduleClass = findModuleInClassResolver(moduleClassName);
+ readModule(moduleClass);
+ }
+
+ /**
+ * Reads an annotated module.
+ *
+ * @param moduleClass class of the module
+ */
+ public void readModule(Class moduleClass)
+ {
+ try
+ {
+ _processor.processModule(moduleClass);
+ }
+ catch (RuntimeException ex)
+ {
+ _errorHandler.error(LOG, ex.getMessage(), HiveMind.getLocation(ex), ex);
+ }
+ }
+
+ private Class findModuleInClassResolver(String type)
+ {
+ Class result = _classResolver.checkForClass(type);
+
+ if (result == null)
+ throw new ApplicationRuntimeException(AnnotationsMessages.unableToFindModuleClass(
+ type, _classResolver));
+
+ return result;
+ }
+
+}
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsMessages.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsMessages.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsMessages.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsMessages.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,35 @@
+// Copyright 2004, 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.annotations;
+
+import org.apache.hivemind.ClassResolver;
+import org.apache.hivemind.impl.MessageFormatter;
+
+
+/**
+ * Used to format messages used in errors and log output for classes within the impl package.
+ *
+ * @author Howard Lewis Ship
+ */
+class AnnotationsMessages
+{
+ private static final MessageFormatter _formatter = new MessageFormatter(AnnotationsMessages.class,
+ "AnnotationsStrings");
+
+ static String unableToFindModuleClass(String moduleClassName, ClassResolver resolver)
+ {
+ return _formatter.format("unable-to-find-module-class", resolver, moduleClassName);
+ }
+}
\ No newline at end of file
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsStrings.properties
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsStrings.properties?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsStrings.properties (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AnnotationsStrings.properties Tue Oct 3 23:27:40 2006
@@ -0,0 +1,15 @@
+# Copyright 2004, 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.
+
+unable-to-find-module-class=Could not find module class {0} in {1}
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,11 @@
+package org.apache.hivemind.annotations;
+
+public interface Registry
+{
+ public <T> T getService(String serviceId, Class<T> serviceInterface);
+ public <T> T getService(Class<T> serviceInterface);
+ public <T> T getConfiguration(String configurationId, Class<T> configurationType);
+ public <T> T getConfiguration(Class<T> configurationType);
+ public Object getConfiguration(String configurationId);
+ public void autowire(Object target);
+}
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Service.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Service.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Service.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Service.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,17 @@
+package org.apache.hivemind.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.apache.hivemind.internal.ServiceModel;
+
+@Documented
+@Retention(value = RetentionPolicy.RUNTIME)
+@Target(value = ElementType.METHOD)
+public @interface Service {
+ String id();
+ String serviceModel() default ServiceModel.SINGLETON;
+}
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,144 @@
+package org.apache.hivemind.annotations.internal;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hivemind.ClassResolver;
+import org.apache.hivemind.ErrorHandler;
+import org.apache.hivemind.Location;
+import org.apache.hivemind.Resource;
+import org.apache.hivemind.annotations.Registry;
+import org.apache.hivemind.annotations.Service;
+import org.apache.hivemind.definition.ModuleDefinition;
+import org.apache.hivemind.definition.RegistryDefinition;
+import org.apache.hivemind.definition.ServiceImplementationDefinition;
+import org.apache.hivemind.definition.ServicePointDefinition;
+import org.apache.hivemind.definition.construction.ImplementationConstructor;
+import org.apache.hivemind.impl.LocationImpl;
+import org.apache.hivemind.internal.Visibility;
+import org.apache.hivemind.util.ClasspathResource;
+
+/**
+ * Does the work for
+ * {@link org.apache.hivemind.annotations.AnnotatedModuleReader}.
+ *
+ * Process an annotated class and registers the defined extension and extension points
+ * in a registry definition.
+ *
+ * @author Achim Huegen
+ */
+public class AnnotatedModuleProcessor
+{
+ private static final Log _log = LogFactory.getLog(AnnotatedModuleProcessor.class);
+
+ private ClassResolver _classResolver;
+
+ private ErrorHandler _errorHandler;
+
+ private RegistryDefinition _registryDefinition;
+
+ public AnnotatedModuleProcessor(RegistryDefinition registryDefinition, ClassResolver classResolver,
+ ErrorHandler errorHandler)
+ {
+ _registryDefinition = registryDefinition;
+ _classResolver = classResolver;
+ _errorHandler = errorHandler;
+ }
+
+ /**
+ * Processes a module. Inspects the class
+ *
+ * @param moduleClass
+ */
+ public void processModule(Class moduleClass)
+ {
+ ModuleDefinition module = new ModuleDefinition(determineModuleId(moduleClass), createLocation(moduleClass),
+ _classResolver, moduleClass.getPackage().getName());
+
+// processServices(moduleClass);
+
+ Registry registry = null; // new RegistryImpl(null, )
+ ModuleInstanceProvider instanceProvider = new ModuleInstanceProviderImpl(moduleClass, null);
+ processModuleMethods(moduleClass, module, instanceProvider);
+ _registryDefinition.addModule(module);
+
+ }
+
+// /**
+// * Detects all services defined in the module class.
+// *
+// * @param moduleClass
+// */
+// private void processServices(Class moduleClass)
+// {
+// // TODO Auto-generated method stub
+//
+// }
+ public void processModuleMethods(Class moduleClass, ModuleDefinition module, ModuleInstanceProvider instanceProvider)
+ {
+ Method[] methods = moduleClass.getMethods();
+ for (int i = 0; i < methods.length; i++) {
+ Method method = methods[i];
+ processMethod(method, module, instanceProvider);
+ }
+ }
+
+ public void processMethod(Method method, ModuleDefinition module, ModuleInstanceProvider instanceProvider)
+ {
+ Annotation[] annotations = method.getAnnotations();
+ for (int j = 0; j < annotations.length; j++) {
+ Annotation annotation = annotations[j];
+
+ if (Service.class.isAssignableFrom(annotation.annotationType())) {
+ processAnnotatedServiceMethod(method, (Service) annotation, module, instanceProvider);
+ }
+ }
+
+ }
+
+ private void processAnnotatedServiceMethod(Method method, Service service, ModuleDefinition module, ModuleInstanceProvider instanceProvider)
+ {
+ ServicePointDefinition spd = new ServicePointDefinition(module, service.id(),
+ module.getLocation(), Visibility.PUBLIC, method.getReturnType().getName());
+ module.addServicePoint(spd);
+
+ ImplementationConstructor constructor = new FactoryMethodImplementationConstructor(
+ module.getLocation(), method, instanceProvider);
+
+ ServiceImplementationDefinition sid = new ServiceImplementationDefinition(module,
+ module.getLocation(), constructor, service.serviceModel(), true);
+
+ spd.addImplementation(sid);
+
+ }
+
+ /**
+ * Creates a location pointing at the module class. The location contains a fixed
+ * line number 1.
+ * @param moduleClass
+ * @return the location
+ */
+ protected Location createLocation(Class moduleClass)
+ {
+ String path = "/" + moduleClass.getName().replace('.', '/');
+
+ Resource r = new ClasspathResource(_classResolver, path);
+
+ return new LocationImpl(r, 1);
+ }
+
+ /**
+ * Determines the module id of the module defined by the annotated class.
+ *
+ * @param moduleClass
+ * the module class
+ * @return the id
+ */
+ private String determineModuleId(Class moduleClass)
+ {
+ return moduleClass.getName();
+ }
+
+}
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/FactoryMethodImplementationConstructor.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/FactoryMethodImplementationConstructor.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/FactoryMethodImplementationConstructor.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/FactoryMethodImplementationConstructor.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,39 @@
+package org.apache.hivemind.annotations.internal;
+
+import java.lang.reflect.Method;
+
+import org.apache.hivemind.ApplicationRuntimeException;
+import org.apache.hivemind.Location;
+import org.apache.hivemind.definition.construction.ImplementationConstructionContext;
+import org.apache.hivemind.definition.construction.ImplementationConstructor;
+import org.apache.hivemind.internal.AbstractServiceImplementationConstructor;
+
+public class FactoryMethodImplementationConstructor extends AbstractServiceImplementationConstructor implements
+ ImplementationConstructor
+{
+ private Method _factoryMethod;
+
+ private ModuleInstanceProvider _moduleInstanceProvider;
+
+ public FactoryMethodImplementationConstructor(Location location, Method factoryMethod,
+ ModuleInstanceProvider moduleInstanceProvider)
+ {
+ super(location);
+ _factoryMethod = factoryMethod;
+ _moduleInstanceProvider = moduleInstanceProvider;
+ }
+
+ public Object constructCoreServiceImplementation(ImplementationConstructionContext context)
+ {
+ try
+ {
+ Object result = _factoryMethod.invoke(_moduleInstanceProvider.getModuleInstance(), (Object[]) null);
+ return result;
+ }
+ catch (Exception ex)
+ {
+ throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex);
+ }
+ }
+
+}
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProvider.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProvider.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProvider.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProvider.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,6 @@
+package org.apache.hivemind.annotations.internal;
+
+public interface ModuleInstanceProvider
+{
+ public Object getModuleInstance();
+}
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProviderImpl.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProviderImpl.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProviderImpl.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/ModuleInstanceProviderImpl.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,55 @@
+package org.apache.hivemind.annotations.internal;
+
+import org.apache.hivemind.ApplicationRuntimeException;
+import org.apache.hivemind.annotations.Registry;
+import org.apache.hivemind.util.PropertyUtils;
+
+public class ModuleInstanceProviderImpl implements ModuleInstanceProvider
+{
+ private static final String REGISTRY_PROPERTY_NAME = "registry";
+
+ private Class _moduleClass;
+
+ private Object _instance;
+
+ private Registry _registry;
+
+ public ModuleInstanceProviderImpl(Class moduleClass, Registry registry)
+ {
+ _moduleClass = moduleClass;
+ _registry = registry;
+ }
+
+ public synchronized Object getModuleInstance()
+ {
+ if (_instance == null)
+ {
+ try
+ {
+ _instance = _moduleClass.newInstance();
+ injectRegistry(_instance);
+ }
+ catch (Exception ex)
+ {
+ // TODO: more expressive error message
+ throw new ApplicationRuntimeException(ex.getMessage(), ex);
+ }
+ }
+ return _instance;
+ }
+
+ /**
+ * Checks if the module contains a property REGISTRY_PROPERTY_NAME and injects
+ * the registry interface provided to this class during construction.
+ *
+ * @param moduleInstance
+ */
+ private void injectRegistry(Object moduleInstance)
+ {
+ if (PropertyUtils.isWritable(moduleInstance, REGISTRY_PROPERTY_NAME)
+ && PropertyUtils.getPropertyType(moduleInstance, REGISTRY_PROPERTY_NAME).equals(Registry.class)) {
+ PropertyUtils.write(moduleInstance, REGISTRY_PROPERTY_NAME, _registry);
+ }
+ }
+
+}
Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,53 @@
+package org.apache.hivemind.annotations.internal;
+
+import org.apache.hivemind.annotations.Registry;
+import org.apache.hivemind.internal.Module;
+import org.apache.hivemind.internal.RegistryInfrastructure;
+
+public class RegistryImpl implements Registry
+{
+ private Module _callingModule;
+ private RegistryInfrastructure _delegate;
+
+ public RegistryImpl(Module callingModule, RegistryInfrastructure delegate)
+ {
+ _callingModule = callingModule;
+ _delegate = delegate;
+ }
+
+ public void autowire(Object target)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public <T> T getConfiguration(String configurationId, Class<T> configurationType)
+ {
+ Object configuration = _delegate.getConfiguration(configurationId, _callingModule);
+ return (T) configuration;
+ }
+
+ public <T> T getConfiguration(Class<T> configurationType)
+ {
+// Object configuration = _delegate.getConfiguration(configurationId, _callingModule);
+// throw new UnsupportedOperationException();
+ return null;
+ }
+
+ public Object getConfiguration(String configurationId)
+ {
+ return _delegate.getConfiguration(configurationId, _callingModule);
+ }
+
+ public <T> T getService(String serviceId, Class<T> serviceInterface)
+ {
+ Object service = _delegate.getService(serviceId, serviceInterface, _callingModule);
+ return (T) service;
+ }
+
+ public <T> T getService(Class<T> serviceInterface)
+ {
+ Object service = _delegate.getService(serviceInterface, _callingModule);
+ return (T) service;
+ }
+
+}
Added: hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,36 @@
+package org.apache.hivemind.annotations;
+
+import java.util.Locale;
+
+import org.apache.hivemind.ClassResolver;
+import org.apache.hivemind.Registry;
+import org.apache.hivemind.impl.RegistryBuilder;
+import org.apache.hivemind.test.HiveMindTestCase;
+
+public class AnnotatedModuleReaderTest extends HiveMindTestCase
+{
+ protected Registry buildRegistry(String[] moduleClassNames)
+ {
+ ClassResolver resolver = getClassResolver();
+
+ RegistryBuilder builder = new RegistryBuilder();
+ builder.autoDetectModules();
+
+ for (int i = 0; i < moduleClassNames.length; i++)
+ {
+ AnnotatedModuleReader reader = new AnnotatedModuleReader(builder.getRegistryDefinition(),
+ resolver, builder.getErrorHandler());
+ reader.readModule(moduleClassNames[i]);
+ }
+
+ return builder.constructRegistry(Locale.getDefault());
+ }
+
+ public void testSimpleModule()
+ {
+ Registry registry = buildRegistry(new String[] {SimpleAnnotatedModule.class.getName()});
+ Runnable service = (Runnable) registry.getService("org.apache.hivemind.annotations.SimpleAnnotatedModule.Test", Runnable.class);
+ service.run();
+ }
+
+}
Added: hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java?view=auto&rev=452756
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java Tue Oct 3 23:27:40 2006
@@ -0,0 +1,37 @@
+package org.apache.hivemind.annotations;
+
+import java.util.List;
+
+import org.apache.hivemind.impl.ServiceModelContribution;
+
+public class SimpleAnnotatedModule
+{
+ private Registry _registry;
+
+ @Service(id="Test")
+ public Runnable getRunnable()
+ {
+ return new Runnable() {
+
+ public void run()
+ {
+ List<ServiceModelContribution> models = (List<ServiceModelContribution>) getRegistry().getConfiguration("hivemind.ServiceModels");
+ for (ServiceModelContribution model : models)
+ {
+ System.out.println(model.getName());
+ }
+ System.out.println("working!!!");
+ }
+ };
+ }
+
+ protected Registry getRegistry()
+ {
+ return _registry;
+ }
+
+ protected void setRegistry(Registry registry)
+ {
+ _registry = registry;
+ }
+}