1 | // Copyright 2004-2007 Jean-Francois Poilpret |
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 net.sourceforge.hiveutils.service.impl; |
16 | |
17 | import java.util.Iterator; |
18 | import java.util.List; |
19 | import java.util.Map; |
20 | import java.util.StringTokenizer; |
21 | |
22 | import org.apache.hivemind.ApplicationRuntimeException; |
23 | import org.apache.hivemind.HiveMind; |
24 | import org.apache.hivemind.Location; |
25 | import org.apache.hivemind.internal.Module; |
26 | import org.apache.hivemind.service.ObjectProvider; |
27 | import org.apache.hivemind.util.PropertyUtils; |
28 | |
29 | /** |
30 | * An <code>org.apache.hivemind.service.ObjectProvider</code> |
31 | * that extracts a contribution item from a global configuration, referenced by |
32 | * a unique id. The translator string is of the form: |
33 | * <code>config-id:id-property:id-value</code>. The id-property name defines |
34 | * the name of a property of the contribution object that is supposed unique and |
35 | * will be used to find the required contribution. |
36 | * |
37 | * @author Jean-Francois Poilpret |
38 | */ |
39 | public class ContributionObjectProvider implements ObjectProvider |
40 | { |
41 | public Object provideObject(Module contributingModule, |
42 | Class propertyType, |
43 | String inputValue, |
44 | Location location) |
45 | { |
46 | if (HiveMind.isBlank(inputValue)) |
47 | { |
48 | return null; |
49 | } |
50 | StringTokenizer tokenizer = new StringTokenizer(inputValue, ":"); |
51 | |
52 | if (tokenizer.countTokens() == NUM_TOKENS_CFG_PROP_ID) |
53 | { |
54 | String idConfig = tokenizer.nextToken(); |
55 | String property = tokenizer.nextToken(); |
56 | String id = tokenizer.nextToken(); |
57 | List config = contributingModule.getConfiguration(idConfig); |
58 | Iterator i = config.iterator(); |
59 | while (i.hasNext()) |
60 | { |
61 | Object contrib = i.next(); |
62 | if (id.equals(PropertyUtils.read(contrib, property))) |
63 | { |
64 | return contrib; |
65 | } |
66 | } |
67 | return null; |
68 | } |
69 | else if (tokenizer.countTokens() == NUM_TOKENS_CFG_ID) |
70 | { |
71 | String idConfig = tokenizer.nextToken(); |
72 | String id = tokenizer.nextToken(); |
73 | if (contributingModule.isConfigurationMappable(idConfig)) |
74 | { |
75 | Map config = contributingModule.getConfigurationAsMap(idConfig); |
76 | return config.get(id); |
77 | } |
78 | } |
79 | throw new ApplicationRuntimeException( |
80 | "Bad input value <" + inputValue + ">", location, null); |
81 | } |
82 | |
83 | static final private int NUM_TOKENS_CFG_PROP_ID = 3; |
84 | static final private int NUM_TOKENS_CFG_ID = 2; |
85 | } |