KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.HiveMind;
19 import org.apache.hivemind.Location;
20 import org.apache.hivemind.internal.Module;
21 import org.apache.hivemind.lib.BeanFactory;
22 import org.apache.hivemind.service.ObjectProvider;
23
24 /**
25  * An {@link org.apache.hivemind.service.ObjectProvider}
26  * that references a named (or named and initialized) bean from a
27  * {@link org.apache.hivemind.lib.BeanFactory}. The translator string is of the form:
28  * <code>service-id:name[,initializer]</code>. That is, the text after the colon
29  * is an initializer passed to {@link org.apache.hivemind.lib.BeanFactory#get(String)}.
30  *
31  * @author Howard Lewis Ship
32  */

33 public class BeanFactoryObjectProvider implements ObjectProvider
34 {
35     public Object JavaDoc provideObject(
36         Module contributingModule,
37         Class JavaDoc propertyType,
38         String JavaDoc inputValue,
39         Location location)
40     {
41         if (HiveMind.isBlank(inputValue))
42             return null;
43
44         int colonx = inputValue.indexOf(':');
45
46         if (colonx < 0)
47             throw new ApplicationRuntimeException(
48                 FactoryMessages.invalidBeanTranslatorFormat(inputValue),
49                 location,
50                 null);
51
52         String JavaDoc serviceId = inputValue.substring(0, colonx);
53
54         if (serviceId.length() == 0)
55             throw new ApplicationRuntimeException(
56                 FactoryMessages.invalidBeanTranslatorFormat(inputValue),
57                 location,
58                 null);
59
60         String JavaDoc locator = inputValue.substring(colonx + 1);
61
62         if (locator.length() == 0)
63             throw new ApplicationRuntimeException(
64                 FactoryMessages.invalidBeanTranslatorFormat(inputValue),
65                 location,
66                 null);
67
68         BeanFactory f = (BeanFactory) contributingModule.getService(serviceId, BeanFactory.class);
69
70         return f.get(locator);
71     }
72
73 }
74
Popular Tags