KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > factory > BeanFactoryImpl


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.factory;
16
17 import java.lang.reflect.Constructor JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.ErrorLog;
25 import org.apache.hivemind.HiveMind;
26 import org.apache.hivemind.impl.BaseLocatable;
27 import org.apache.hivemind.lib.BeanFactory;
28
29 /**
30  * Implementation of {@link org.apache.hivemind.lib.BeanFactory}.
31  *
32  * @author Howard Lewis Ship
33  */

34 public class BeanFactoryImpl extends BaseLocatable implements BeanFactory
35 {
36     private ErrorLog _errorLog;
37
38     private Class JavaDoc _vendType;
39
40     private Map JavaDoc _contributions = new HashMap JavaDoc();
41
42     private Map JavaDoc _cache = new HashMap JavaDoc();
43
44     private boolean _defaultCacheable;
45
46     public BeanFactoryImpl(ErrorLog errorLog, Class JavaDoc vendType, List JavaDoc contributions,
47             boolean defaultCacheable)
48     {
49         _errorLog = errorLog;
50         _vendType = vendType;
51         _defaultCacheable = defaultCacheable;
52
53         processContributions(contributions);
54     }
55
56     public boolean contains(String JavaDoc locator)
57     {
58         int commax = locator.indexOf(',');
59
60         String JavaDoc name = commax < 0 ? locator.trim() : locator.substring(0, commax);
61
62         return _contributions.containsKey(name);
63     }
64
65     private void processContributions(List JavaDoc list)
66     {
67         Iterator JavaDoc i = list.iterator();
68
69         while (i.hasNext())
70         {
71             BeanFactoryContribution c = (BeanFactoryContribution) i.next();
72
73             Class JavaDoc beanClass = c.getBeanClass();
74
75             if (beanClass.isInterface() || beanClass.isArray() || beanClass.isPrimitive())
76             {
77                 _errorLog.error(FactoryMessages.invalidContributionClass(c), c.getLocation(), null);
78                 continue;
79             }
80
81             if (!_vendType.isAssignableFrom(beanClass))
82             {
83                 _errorLog.error(FactoryMessages.wrongContributionType(c, _vendType), c
84                         .getLocation(), null);
85                 continue;
86             }
87
88             _contributions.put(c.getName(), c);
89         }
90     }
91
92     public synchronized Object JavaDoc get(String JavaDoc locator)
93     {
94         Object JavaDoc result = _cache.get(locator);
95
96         if (result == null)
97             result = create(locator);
98
99         return result;
100     }
101
102     // Implicitly synchronized by get()
103

104     private Object JavaDoc create(String JavaDoc locator)
105     {
106         int commax = locator.indexOf(',');
107
108         String JavaDoc name = commax < 0 ? locator.trim() : locator.substring(0, commax);
109         String JavaDoc initializer = commax < 0 ? null : locator.substring(commax + 1).trim();
110
111         BeanFactoryContribution c = (BeanFactoryContribution) _contributions.get(name);
112
113         if (c == null)
114             throw new ApplicationRuntimeException(FactoryMessages.unknownContribution(name));
115
116         Object JavaDoc result = construct(c, initializer);
117
118         if (c.getStoreResultInCache(_defaultCacheable))
119             _cache.put(locator, result);
120
121         return result;
122     }
123
124     private Object JavaDoc construct(BeanFactoryContribution contribution, String JavaDoc initializer)
125     {
126         Class JavaDoc beanClass = contribution.getBeanClass();
127
128         try
129         {
130             if (HiveMind.isBlank(initializer))
131                 return beanClass.newInstance();
132
133             Constructor JavaDoc c = beanClass.getConstructor(new Class JavaDoc[]
134             { String JavaDoc.class });
135
136             return c.newInstance(new Object JavaDoc[]
137             { initializer });
138         }
139         catch (Exception JavaDoc ex)
140         {
141             throw new ApplicationRuntimeException(FactoryMessages
142                     .unableToInstantiate(beanClass, ex), contribution.getLocation(), ex);
143
144         }
145     }
146
147 }
Popular Tags