KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > annotations > AnnotatedRegistryBuilder


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;
16
17 import java.util.Locale JavaDoc;
18
19 import org.apache.hivemind.ClassResolver;
20 import org.apache.hivemind.ErrorHandler;
21 import org.apache.hivemind.annotations.internal.TypedRegistryImpl;
22 import org.apache.hivemind.definition.RegistryDefinition;
23 import org.apache.hivemind.definition.impl.RegistryDefinitionImpl;
24 import org.apache.hivemind.events.RegistryInitializationListener;
25 import org.apache.hivemind.impl.DefaultClassResolver;
26 import org.apache.hivemind.impl.DefaultErrorHandler;
27 import org.apache.hivemind.impl.RegistryBuilder;
28 import org.apache.hivemind.internal.RegistryInfrastructure;
29
30 /**
31  * Helper class for defining hivemind registries that mainly base on
32  * annotated modules.
33  *
34  * @author Achim Huegen
35  */

36 public class AnnotatedRegistryBuilder
37 {
38     private ClassResolver _classResolver;
39     private ErrorHandler _errorHandler;
40     private Locale JavaDoc _locale;
41     
42     /**
43      * Constructor that uses default implementations of ClassResolver, ErrorHandler and Locale.
44      */

45     public AnnotatedRegistryBuilder()
46     {
47         this(new DefaultClassResolver(), new DefaultErrorHandler(), Locale.getDefault());
48     }
49     
50     /**
51      * Creates a new instance.
52      * @param classResolver the {@link ClassResolver} used to resolve all classes referenced from
53      * elements inside this module.
54      * @param errorHandler errorHandler used for handling recoverable errors
55      * @param locale the locale to use for the registry
56      */

57     public AnnotatedRegistryBuilder(ClassResolver classResolver, ErrorHandler errorHandler,
58             Locale JavaDoc locale)
59     {
60         _classResolver = classResolver;
61         _errorHandler = errorHandler;
62         _locale = locale;
63     }
64     
65     /**
66      * Constructs a registry from a couple of annotated module classes specified by their name.
67      * @param moduleClassNames the annotated module class names
68      * @return the registry
69      */

70     public TypedRegistry constructRegistry(String JavaDoc ... moduleClassNames)
71     {
72         RegistryDefinition definition = constructRegistryDefinition(moduleClassNames);
73         return constructRegistry(definition);
74     }
75
76     /**
77      * Constructs a registry from a couple of annotated module classes specified by their class definitions.
78      * @param moduleClasses the annotated module classes
79      * @return the registry
80      */

81    public TypedRegistry constructRegistry(Class JavaDoc ... moduleClasses)
82     {
83         RegistryDefinition definition = constructRegistryDefinition(moduleClasses);
84         return constructRegistry(definition);
85     }
86     
87     private RegistryDefinition constructRegistryDefinition(String JavaDoc ... moduleClassNames)
88     {
89         RegistryDefinition definition = new RegistryDefinitionImpl();
90
91         for (int i = 0; i < moduleClassNames.length; i++)
92         {
93             AnnotatedModuleReader reader = new AnnotatedModuleReader(definition,
94                     _classResolver, _errorHandler);
95             reader.readModule(moduleClassNames[i]);
96         }
97
98         return definition;
99     }
100     
101     private RegistryDefinition constructRegistryDefinition(Class JavaDoc ... moduleClasses)
102     {
103         RegistryDefinition definition = new RegistryDefinitionImpl();
104
105         for (int i = 0; i < moduleClasses.length; i++)
106         {
107             AnnotatedModuleReader reader = new AnnotatedModuleReader(definition,
108                     _classResolver, _errorHandler);
109             reader.readModule(moduleClasses[i]);
110         }
111
112         return definition;
113     }
114     
115     private TypedRegistry constructRegistry(RegistryDefinition definition)
116     {
117         // Register a listener that obtains a reference to RegistryInfrastructure
118
// which is not visible by other means.
119
RegistryInfrastructureHolder infrastructureHolder = new RegistryInfrastructureHolder();
120             
121         definition.addRegistryInitializationListener(infrastructureHolder);
122         
123         RegistryBuilder.constructRegistry(definition, _errorHandler, _locale);
124         // Now the RegistryInfrastructureHolder has access to the registry
125
return new TypedRegistryImpl(null, infrastructureHolder.getInfrastructure());
126     }
127
128     final class RegistryInfrastructureHolder implements RegistryInitializationListener
129     {
130         private RegistryInfrastructure _infrastructure;
131
132         public void registryInitialized(RegistryInfrastructure registry)
133         {
134             _infrastructure = registry;
135         }
136
137         public RegistryInfrastructure getInfrastructure()
138         {
139             return _infrastructure;
140         }
141     }
142 }
143
Popular Tags