|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ObjectTranslator.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 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.service.impl;
|
|
16 |
|
|
17 |
import java.util.HashMap;
|
|
18 |
import java.util.Iterator;
|
|
19 |
import java.util.List;
|
|
20 |
import java.util.Map;
|
|
21 |
|
|
22 |
import org.apache.commons.logging.Log;
|
|
23 |
import org.apache.hivemind.ErrorHandler;
|
|
24 |
import org.apache.hivemind.HiveMind;
|
|
25 |
import org.apache.hivemind.Location;
|
|
26 |
import org.apache.hivemind.internal.Module;
|
|
27 |
import org.apache.hivemind.schema.Translator;
|
|
28 |
import org.apache.hivemind.service.ObjectProvider;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Implementation of the indirect translator. This translator allows the contributor,
|
|
32 |
* not the schema, to define where object values come from, and is fully extensible.
|
|
33 |
* Perhaps I'll have an inspiration and find a better name than "indirect".
|
|
34 |
*
|
|
35 |
* @author Howard Lewis Ship
|
|
36 |
*/
|
|
37 |
public class ObjectTranslator implements Translator |
|
38 |
{ |
|
39 |
private ErrorHandler _errorHandler;
|
|
40 |
private Log _log;
|
|
41 |
private List _contributions;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Keyed on prefix, value is an {@link org.apache.hivemind.service.ObjectProvider}.
|
|
45 |
*/
|
|
46 |
private Map _providers = new HashMap(); |
|
47 |
|
|
48 | 100 |
public void initializeService() |
49 |
{ |
|
50 | 100 |
Map locations = new HashMap();
|
51 | 100 |
Iterator i = _contributions.iterator(); |
52 | 100 |
while (i.hasNext())
|
53 |
{ |
|
54 | 387 |
ObjectProviderContribution c = (ObjectProviderContribution) i.next(); |
55 |
|
|
56 | 387 |
String prefix = c.getPrefix(); |
57 |
|
|
58 | 387 |
Location oldLocation = (Location) locations.get(prefix); |
59 |
|
|
60 | 387 |
if (oldLocation != null) |
61 |
{ |
|
62 | 1 |
_errorHandler.error( |
63 |
_log, |
|
64 |
ServiceMessages.duplicateProviderPrefix(prefix, oldLocation), |
|
65 |
c.getLocation(), |
|
66 |
null);
|
|
67 | 1 |
continue;
|
68 |
} |
|
69 |
|
|
70 | 386 |
locations.put(prefix, c.getLocation()); |
71 | 386 |
_providers.put(prefix, c.getProvider()); |
72 |
} |
|
73 |
} |
|
74 |
|
|
75 | 103 |
public Object translate(
|
76 |
Module contributingModule, |
|
77 |
Class propertyType, |
|
78 |
String inputValue, |
|
79 |
Location location) |
|
80 |
{ |
|
81 | 103 |
if (HiveMind.isBlank(inputValue))
|
82 | 1 |
return null; |
83 |
|
|
84 | 102 |
int colonx = inputValue.indexOf(':');
|
85 |
|
|
86 | 102 |
if (colonx < 1)
|
87 |
{ |
|
88 | 1 |
_errorHandler.error( |
89 |
_log, |
|
90 |
ServiceMessages.invalidProviderSelector(inputValue), |
|
91 |
null,
|
|
92 |
null);
|
|
93 |
|
|
94 | 1 |
return null; |
95 |
} |
|
96 |
|
|
97 | 101 |
String prefix = inputValue.substring(0, colonx); |
98 |
|
|
99 | 101 |
ObjectProvider provider = (ObjectProvider) _providers.get(prefix); |
100 |
|
|
101 | 101 |
if (provider == null) |
102 |
{ |
|
103 | 1 |
_errorHandler.error( |
104 |
_log, |
|
105 |
ServiceMessages.unknownProviderPrefix(prefix), |
|
106 |
location, |
|
107 |
null);
|
|
108 |
|
|
109 | 1 |
return null; |
110 |
} |
|
111 |
|
|
112 | 100 |
String locator = inputValue.substring(colonx + 1); |
113 |
|
|
114 | 100 |
return provider.provideObject(contributingModule, propertyType, locator, location);
|
115 |
|
|
116 |
} |
|
117 |
|
|
118 | 100 |
public void setContributions(List list) |
119 |
{ |
|
120 | 100 |
_contributions = list; |
121 |
} |
|
122 |
|
|
123 | 99 |
public void setErrorHandler(ErrorHandler handler) |
124 |
{ |
|
125 | 99 |
_errorHandler = handler; |
126 |
} |
|
127 |
|
|
128 | 99 |
public void setLog(Log log) |
129 |
{ |
|
130 | 99 |
_log = log; |
131 |
} |
|
132 |
|
|
133 |
} |
|
134 |
|
|