KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > annotations > internal > ModuleInstanceProviderImpl


1 // Copyright 2007 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.annotations.internal;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.annotations.TypedRegistry;
19 import org.apache.hivemind.internal.Module;
20 import org.apache.hivemind.internal.RegistryInfrastructure;
21 import org.apache.hivemind.util.Defense;
22 import org.apache.hivemind.util.PropertyUtils;
23
24 /**
25  * Implementation of {@link ModuleInstanceProvider}.
26  *
27  * @author Achim Huegen
28  */

29 public class ModuleInstanceProviderImpl implements ModuleInstanceProvider
30 {
31     private static final String JavaDoc REGISTRY_PROPERTY_NAME = "registry";
32
33     private Class JavaDoc _moduleClass;
34
35     private Object JavaDoc _instance;
36
37     private String JavaDoc _moduleId;
38     
39     public ModuleInstanceProviderImpl(Class JavaDoc moduleClass, String JavaDoc moduleId)
40     {
41         _moduleClass = moduleClass;
42         _moduleId = moduleId;
43     }
44
45     public Object JavaDoc getModuleInstance()
46     {
47         Defense.fieldNotNull(_instance, "instance");
48         return _instance;
49     }
50     
51     private void createModuleInstance(RegistryInfrastructure _registry)
52     {
53         try
54         {
55             _instance = _moduleClass.newInstance();
56             injectRegistry(_instance, _registry);
57         }
58         catch (Exception JavaDoc ex)
59         {
60             // TODO: more expressive error message
61
throw new ApplicationRuntimeException(ex.getMessage(), ex);
62         }
63     }
64
65     /**
66      * Checks if the module contains a property REGISTRY_PROPERTY_NAME and injects
67      * the registry interface provided to this class during construction.
68      *
69      * @param moduleInstance
70      */

71     private void injectRegistry(Object JavaDoc moduleInstance, RegistryInfrastructure _registry)
72     {
73         if (PropertyUtils.isWritable(moduleInstance, REGISTRY_PROPERTY_NAME)
74                 && PropertyUtils.getPropertyType(moduleInstance, REGISTRY_PROPERTY_NAME).equals(TypedRegistry.class)) {
75             
76             Module callingModule = _registry.getModule(_moduleId);
77             TypedRegistry annotatedRegistry = new TypedRegistryImpl(callingModule, _registry);
78             PropertyUtils.write(moduleInstance, REGISTRY_PROPERTY_NAME, annotatedRegistry);
79         }
80     }
81
82     /**
83      * Called after initialization of the registry infrastructure.
84      * This is a good moment to create the module instance. If any service defined in the module
85      * is initialized during startup (by the EagerLoad service) it will find the registry
86      * reference in place.
87      *
88      * @see org.apache.hivemind.events.RegistryInitializationListener#registryInitialized(org.apache.hivemind.internal.RegistryInfrastructure)
89      */

90     public void registryInitialized(RegistryInfrastructure registry)
91     {
92         createModuleInstance(registry);
93     }
94
95 }
96
Popular Tags