KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > impl > DefaultImplementationBuilderImpl


1 // Copyright 2004, 2005 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.lib.impl;
16
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.impl.BaseLocatable;
23 import org.apache.hivemind.lib.DefaultImplementationBuilder;
24 import org.apache.hivemind.service.ClassFab;
25 import org.apache.hivemind.service.ClassFabUtils;
26 import org.apache.hivemind.service.ClassFactory;
27 import org.apache.hivemind.service.MethodIterator;
28
29 /**
30  * Implemenation of {@link org.apache.hivemind.lib.DefaultImplementationBuilder}.
31  *
32  * @author Howard Lewis Ship
33  */

34 public class DefaultImplementationBuilderImpl extends BaseLocatable implements
35         DefaultImplementationBuilder
36 {
37     private Map JavaDoc _instances = Collections.synchronizedMap(new HashMap JavaDoc());
38
39     private ClassFactory _classFactory;
40
41     public Object JavaDoc buildDefaultImplementation(Class JavaDoc interfaceType)
42     {
43         Object JavaDoc result = _instances.get(interfaceType);
44
45         if (result == null)
46         {
47             result = create(interfaceType);
48
49             _instances.put(interfaceType, result);
50         }
51
52         return result;
53     }
54
55     private Object JavaDoc create(Class JavaDoc interfaceType)
56     {
57         Class JavaDoc defaultClass = createClass(interfaceType);
58
59         try
60         {
61             return defaultClass.newInstance();
62         }
63         catch (Exception JavaDoc ex)
64         {
65             throw new ApplicationRuntimeException(ImplMessages.unableToCreateDefaultImplementation(
66                     interfaceType,
67                     ex), ex);
68         }
69     }
70
71     private Class JavaDoc createClass(Class JavaDoc interfaceType)
72     {
73         if (!interfaceType.isInterface())
74             throw new ApplicationRuntimeException(ImplMessages.notAnInterface(interfaceType));
75
76         String JavaDoc name = ClassFabUtils.generateClassName(interfaceType);
77
78         ClassFab cf = _classFactory.newClass(name, Object JavaDoc.class);
79
80         cf.addInterface(interfaceType);
81
82         MethodIterator mi = new MethodIterator(interfaceType);
83
84         while (mi.hasNext())
85         {
86             ClassFabUtils.addNoOpMethod(cf, mi.next());
87         }
88
89         if (!mi.getToString())
90             ClassFabUtils.addToStringMethod(cf, ImplMessages
91                     .defaultImplementationDescription(interfaceType));
92
93         return cf.createClass();
94     }
95
96     public void setClassFactory(ClassFactory factory)
97     {
98         _classFactory = factory;
99     }
100
101 }
Popular Tags