KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Method JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.definition.Contribution;
22 import org.apache.hivemind.definition.ContributionContext;
23
24 /**
25  * Contributes to a configuration point by passing the configuration container
26  * to a method defined in an annotated module by use of the {@link Contribution} annotation.
27  *
28  * @author Achim Huegen
29  */

30 public class MethodCallContributionConstructor implements
31         Contribution
32 {
33     private Method JavaDoc _templateMethod;
34
35     private ModuleInstanceProvider _moduleInstanceProvider;
36
37     private Location _location;
38
39     public MethodCallContributionConstructor(Location location, Method JavaDoc factoryMethod,
40             ModuleInstanceProvider moduleInstanceProvider)
41     {
42         _location = location;
43         _templateMethod = factoryMethod;
44         _moduleInstanceProvider = moduleInstanceProvider;
45     }
46
47     public Location getLocation()
48     {
49         return _location;
50     }
51
52     public void contribute(ContributionContext context)
53     {
54         try
55         {
56             if (_templateMethod.getParameterTypes().length == 0) {
57                 Object JavaDoc result = _templateMethod.invoke(_moduleInstanceProvider.getModuleInstance());
58                 // a null contribution means: nothing to contribute. This happens for example
59
// in configuration point definitions
60
if (result != null) {
61                     context.mergeContribution(result);
62                 }
63             } else if (_templateMethod.getParameterTypes().length == 1) {
64                 Object JavaDoc[] params = new Object JavaDoc[] {context.getConfigurationData()};
65                 _templateMethod.invoke(_moduleInstanceProvider.getModuleInstance(), params);
66             } else {
67                 // TODO: Throw Exception
68
}
69         }
70         catch (Exception JavaDoc ex)
71         {
72             throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex);
73         }
74         
75     }
76
77 }
78
Popular Tags