1 /*
2 wsmo4j - a WSMO API and Reference Implementation
3
4 Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5
6 This library is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the Free
8 Software Foundation; either version 2.1 of the License, or (at your option)
9 any later version.
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 details.
14 You should have received a copy of the GNU Lesser General Public License along
15 with this library; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /**
20 * <p>Title: WSMO4J</p>
21 * <p>Description: WSMO API and a Reference Implementation</p>
22 * <p>Copyright: Copyright (c) 2004-2005</p>
23 * <p>Company: OntoText Lab. / SIRMA </p>
24 */
25
26 package org.wsmo.locator;
27
28 import java.lang.reflect.*;
29 import java.util.*;
30
31 import org.wsmo.common.*;
32 import org.wsmo.factory.*;
33
34 /**
35 * The locator manager responsible to validate and manage
36 * a queue of locators.
37 *
38 * @author not attributable
39 * @version $Revision: 1946 $ $Date: 2007-04-02 15:13:28 +0300 (Mon, 02 Apr 2007) $
40 */
41 public final class LocatorManager {
42
43 private static final String LOCATOR = "org.deri.wsmo4j.locator.LocatorImpl";
44
45 private LinkedList <Locator> locList = new LinkedList <Locator> ();
46
47 /**
48 * Adds new locator to the queue of locators
49 * @param locator The locator to add
50 */
51 public void addLocator(Locator locator) {
52 if (locator == null) {
53 throw new IllegalArgumentException();
54 }
55 if (locList.contains(locator)) {
56 throw new IllegalArgumentException("The locator was already added!");
57 }
58 locList.addLast(locator);
59 }
60
61
62 /**
63 * Attempts to find an entity based on its IRI.
64 * If the first locator in the queue is unable to locate the entity
65 * then it will pass the request to
66 * the next locator in the locator chain
67 * @param id The ID of the entity to find
68 * @return The entity corresponding to the supplied ID or <i>null</i> if none found
69 * @throws org.wsmo.common.exception.SynchronisationException
70 * @see #setNext(Locator loc)
71 * @see Entity
72 */
73 public Set lookup(Identifier id) {
74 Set entities = new HashSet();
75 for (int i = 0; i < locList.size(); i++) {
76 entities = locList.get(i).lookup(id);
77 if (entities != null) {
78 break;
79 }
80 }
81 return entities;
82 }
83
84 /**
85 * Attempts to find an entity based on its IRI.
86 * If the first locator in the queue is unable to locate the entity
87 * then it will pass the request to
88 * the next locator in the locator chain
89 * @param id The ID of the entity to find
90 * @param clazz The type of the identifier
91 * @return The entity corresponding to the supplied ID or <i>null</i> if none found
92 * @throws org.wsmo.common.exception.SynchronisationException
93 * @see #setNext(Locator loc)
94 */
95 public Entity lookup(Identifier id, Class clazz) {
96 Entity entity = null;
97 for (int i = 0; i < locList.size(); i++) {
98 entity = locList.get(i).lookup(id, clazz);
99 if (entity != null) {
100 break;
101 }
102 }
103 return entity;
104 }
105
106 /**
107 * Removes a locator from the queue
108 * @param locator The locator to remove
109 */
110 public void removeLocator(Locator locator) {
111 if (locator == null) {
112 throw new IllegalArgumentException();
113 }
114 locList.remove(locator);
115 }
116
117 /**
118 * Creates a locator based on the supplied preferences.
119 * @param properties the preferences for the locator. Such preferences
120 * should provide all the necessary information for the locator
121 * initialisation (e.g. provider class, type...)
122 * @return a Locator
123 */
124 public static Locator createLocator(Map <String, Object> map) {
125 String className = null;
126 if (null == map || false == map.containsKey(Factory.PROVIDER_CLASS)) {
127 className = LOCATOR;
128 }
129
130 if (className == null) {
131 className = (String) map.get(Factory.PROVIDER_CLASS);
132 }
133 Object locator = null;
134
135 try {
136 Class providerLocator = Class.forName(className);
137 Constructor locatorConstructor =
138 providerLocator.getConstructor(new Class[] {java.util.Map.class});
139 locator = locatorConstructor.newInstance(new Object[] {map});
140 }
141 catch (ClassNotFoundException e) {
142 throw new RuntimeException("Provider's class not found in classpath...", e);
143 }
144 catch (NoSuchMethodException nsme) {
145 throw new RuntimeException(
146 "The provider class should have a constuctor which takes a single java.util.Map argument...",
147 nsme);
148 }
149 catch (Exception e) {
150 throw new RuntimeException("Can not instantiate locator", e);
151 }
152
153 assert locator != null;
154
155 if (false == locator instanceof Locator) {
156 throw new RuntimeException(map.get(Factory.PROVIDER_CLASS) +
157 " does not implement the org.wsmo.locator.Locator interface");
158 }
159 return (Locator)locator;
160 }
161 }
162
163 /*
164 * $Log$
165 * Revision 1.10 2007/04/02 12:13:15 morcen
166 * Generics support added to wsmo-api, wsmo4j and wsmo-test
167 *
168 * Revision 1.9 2006/08/22 16:25:48 nathaliest
169 * added locator implementation and set it as default locator at the locator manager
170 *
171 * Revision 1.8 2006/01/09 12:32:45 vassil_momtchev
172 * locator manager class changed to final
173 *
174 * Revision 1.7 2005/10/18 09:16:47 marin_dimitrov
175 * lookup() returns a Set (was Entity[])
176 *
177 * Revision 1.6 2005/10/18 09:13:01 marin_dimitrov
178 * lookup() returns a Set (was Entity[])
179 *
180 * Revision 1.5 2005/10/18 08:48:06 vassil_momtchev
181 * Entity lookup(Identifier) changed to Entity[] lookup(Identifier) and Entity lookup(Identifier, Class)
182 *
183 * Revision 1.4 2005/09/14 08:54:13 marin_dimitrov
184 * removed getLocatorCount()
185 *
186 * Revision 1.3 2005/09/09 10:40:49 marin_dimitrov
187 * formatting
188 *
189 */