01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.util;
18:
19: import java.util.ArrayList;
20: import java.util.Collection;
21: import java.util.Map;
22: import java.util.TreeMap;
23: import java.util.Collections;
24: import java.net.URI;
25:
26: public class LinkResolver<E> {
27: private final Map<String, E> byFullName = new TreeMap<String, E>();
28: private final Map<String, Collection<E>> byShortName = new TreeMap<String, Collection<E>>();
29:
30: public boolean add(String moduleId, String name, E value) {
31: String fullName = moduleId + "#" + name;
32: if (byFullName.containsKey(fullName)) {
33: // entry already exists
34: return false;
35: }
36:
37: // Full name: moduleId#name -> value
38: byFullName.put(fullName, value);
39:
40: // Short name: name -> List(values)
41: Collection<E> values = byShortName.get(name);
42: if (values == null) {
43: values = new ArrayList<E>();
44: byShortName.put(name, values);
45: }
46: values.add(value);
47:
48: return true;
49: }
50:
51: public Collection<E> values() {
52: return byFullName.values();
53: }
54:
55: public Collection<E> values(String shortName) {
56: Collection<E> es = byShortName.get(shortName);
57: return es != null ? es : Collections.EMPTY_LIST;
58: }
59:
60: public E resolveLink(String link, String moduleId) {
61: URI moduleURI = URI.create(moduleId);
62: return resolveLink(link, moduleURI);
63: }
64:
65: public E resolveLink(String link, URI moduleUri) {
66: if (!link.contains("#")) {
67: // check for a name in the current module
68: E value = byFullName.get(moduleUri.toString() + "#" + link);
69: if (value != null) {
70: return value;
71: }
72:
73: // check for single value using short name
74: Collection<E> values = byShortName.get(link);
75: if (values == null || values.size() != 1) {
76: return null;
77: }
78: value = values.iterator().next();
79: return value;
80: } else if (moduleUri != null) {
81: // full (absolute) name
82: String fullName = moduleUri.resolve(link).toString();
83: E value = byFullName.get(fullName);
84: return value;
85: } else {
86: // Absolute reference in a standalone module
87: return null;
88: }
89: }
90:
91: protected E getUniqueMember() {
92: if (byFullName.size() == 1) {
93: return byFullName.values().iterator().next();
94: } else {
95: return null;
96: }
97: }
98:
99: }
|