KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.ClassResolver;
21 import org.apache.hivemind.ErrorHandler;
22 import org.apache.hivemind.HiveMind;
23 import org.apache.hivemind.annotations.internal.AnnotatedModuleProcessor;
24 import org.apache.hivemind.definition.RegistryDefinition;
25 import org.apache.hivemind.impl.DefaultClassResolver;
26 import org.apache.hivemind.impl.DefaultErrorHandler;
27
28 /**
29  * Reads a annotated hivemind module into a {@link RegistryDefinition}. Thus
30  * the defined services configurations and contributions are added to the
31  * registry.
32  * The class delegates the work to {@link AnnotatedModuleProcessor}
33  *
34  * @author Achim Huegen
35  */

36 public class AnnotatedModuleReader
37 {
38     private static final Log LOG = LogFactory.getLog(AnnotatedModuleReader.class);
39
40     private RegistryDefinition _registryDefinition;
41
42     private AnnotatedModuleProcessor _processor;
43
44     private ErrorHandler _errorHandler;
45
46     private ClassResolver _classResolver;
47
48     /**
49      * @param registryDefinition the registry definition to which the modules are added.
50      */

51     public AnnotatedModuleReader(RegistryDefinition registryDefinition)
52     {
53         this(registryDefinition, new DefaultClassResolver(), new DefaultErrorHandler());
54     }
55
56     /**
57      * @param registryDefinition the registry definition to which the modules are added.
58      * @param classResolver the {@link ClassResolver} used to resolve all classes referenced from
59      * elements inside this module.
60      * @param errorHandler errorHandler used for handling recoverable errors
61      */

62     public AnnotatedModuleReader(RegistryDefinition registryDefinition, ClassResolver classResolver,
63             ErrorHandler errorHandler)
64     {
65         _registryDefinition = registryDefinition;
66         _classResolver = classResolver;
67         _errorHandler = errorHandler;
68         _processor = new AnnotatedModuleProcessor(_registryDefinition, _classResolver, _errorHandler);
69     }
70
71     /**
72      * Reads an annotated module specified by its classname. The module must have a no
73      * argument constructor.
74      *
75      * @param moduleClassName class name of the module
76      */

77     public void readModule(String JavaDoc moduleClassName)
78     {
79         Class JavaDoc moduleClass = findModuleInClassResolver(moduleClassName);
80         readModule(moduleClass);
81     }
82
83     /**
84      * Reads an annotated module.
85      *
86      * @param moduleClass class of the module
87      */

88     public void readModule(Class JavaDoc moduleClass)
89     {
90         try
91         {
92             _processor.processModule(moduleClass);
93         }
94         catch (RuntimeException JavaDoc ex)
95         {
96             _errorHandler.error(LOG, ex.getMessage(), HiveMind.getLocation(ex), ex);
97         }
98     }
99     
100     private Class JavaDoc findModuleInClassResolver(String JavaDoc type)
101     {
102         Class JavaDoc result = _classResolver.checkForClass(type);
103
104         if (result == null)
105             throw new ApplicationRuntimeException(AnnotationsMessages.unableToFindModuleClass(
106                     type, _classResolver));
107
108         return result;
109     }
110
111 }
112
Popular Tags