1 | /*
|
---|
2 | * NzdlMultiWayWrapper.java
|
---|
3 | * Copyright (C) 2001 New Zealand Digital Library Project
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 |
|
---|
20 | //the package we're in
|
---|
21 | package org.nzdl.gsdl.service;
|
---|
22 |
|
---|
23 | import java.util.Map;
|
---|
24 | import java.util.Set;
|
---|
25 | import java.util.HashSet;
|
---|
26 | import java.util.Hashtable;
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.Date;
|
---|
29 | import java.util.Hashtable;
|
---|
30 | import java.util.Properties;
|
---|
31 | import java.util.Enumeration;
|
---|
32 | import java.util.Iterator;
|
---|
33 | import java.io.Writer;
|
---|
34 | import java.io.FileWriter;
|
---|
35 | import org.nzdl.gsdl.util.NzdlServiceFactory;
|
---|
36 | import org.nzdl.gsdl.util.NzdlPreferences;
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * A NzdlMultiWayWrapper is a wrapper around multiple NzdlServices.
|
---|
41 | * It attempts to connect to as many servers as possible and present
|
---|
42 | * them to the user a single collection.
|
---|
43 | *
|
---|
44 | * When two collections are found to be the same, then only the
|
---|
45 | * collection with the fastest response time for a getCollectionInfo()
|
---|
46 | * call is added.
|
---|
47 | *
|
---|
48 | * @author Brett Sheeran ([email protected]) (comments)
|
---|
49 | * @author Stuart Yeates ([email protected])
|
---|
50 | * @see org.nzdl.gsdl.service.NzdlCollectionInfo
|
---|
51 | * @version $Revision: 2284 $
|
---|
52 | */
|
---|
53 | public class NzdlMultiWayWrapper
|
---|
54 | extends NzdlWrapper {
|
---|
55 |
|
---|
56 | Hashtable services;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * The bean constructor ...
|
---|
60 | */
|
---|
61 | public NzdlMultiWayWrapper() {
|
---|
62 | super(null);
|
---|
63 | services = new Hashtable();
|
---|
64 | }
|
---|
65 | /**
|
---|
66 | * The normal constructor
|
---|
67 | */
|
---|
68 | public NzdlMultiWayWrapper(String [] _args,
|
---|
69 | Properties _props,
|
---|
70 | String _URL,
|
---|
71 | String _filename,
|
---|
72 | String _IOR) {
|
---|
73 | super(null);
|
---|
74 | services = new Hashtable();
|
---|
75 |
|
---|
76 | NzdlPreferences.saving = false;
|
---|
77 |
|
---|
78 | Hashtable servers = NzdlServiceFactory.findIORs(_args,_props,_URL,_filename,_IOR);
|
---|
79 |
|
---|
80 | Enumeration e = servers.keys() ;
|
---|
81 | for (int i = 0; e.hasMoreElements(); i++) {
|
---|
82 | String key = (String) e.nextElement();
|
---|
83 | NzdlService server = (NzdlService) servers.get(key);
|
---|
84 |
|
---|
85 | if (NzdlPreferences.getBoolean("Log at the NzdlService level"))
|
---|
86 | server = new NzdlLogWrapper(server,
|
---|
87 | "log.file.after.split." + i + ".cache",
|
---|
88 | "AFTER:" + key + ": ");
|
---|
89 | services.put("" +i, server);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | static String findServer(String _name) {
|
---|
94 | int separator = _name.indexOf(':');
|
---|
95 | String result = _name.substring(0,separator);
|
---|
96 | return result;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static String findCollection(String _name) {
|
---|
100 | String result = "";
|
---|
101 | try {
|
---|
102 | int separator = _name.indexOf(':');
|
---|
103 | result = _name.substring(separator+1,_name.length());
|
---|
104 | } catch (Throwable t) {
|
---|
105 | System.err.println("Throwable: "+ t);
|
---|
106 | }
|
---|
107 | return result;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Allow the client to configure a CORBA server.
|
---|
112 | * @param key the parameter to be set.
|
---|
113 | * @param values what that parameter will be set to.
|
---|
114 | */
|
---|
115 | public void configure( String _key, Set _values ) {
|
---|
116 | Enumeration e = services.keys();
|
---|
117 | for (int i = 0; e.hasMoreElements(); i++) {
|
---|
118 | NzdlService server = (NzdlService) e.nextElement();
|
---|
119 | server.configure(_key,_values);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Obtain the set of collection names for a Greenstone library.
|
---|
125 | * @return A set of collection names in string format.
|
---|
126 | */
|
---|
127 | public Set getCollectionSet( ) {
|
---|
128 | Set results = new HashSet();
|
---|
129 | Hashtable intermediate = new Hashtable();
|
---|
130 |
|
---|
131 | Enumeration e = services.keys();
|
---|
132 | for (int i = 0; e.hasMoreElements(); i++) {
|
---|
133 | String name = (String) e.nextElement();
|
---|
134 | NzdlService server = (NzdlService) services.get(name);
|
---|
135 | Set set = server.getCollectionSet();
|
---|
136 | Iterator s = set.iterator();
|
---|
137 | while (s.hasNext()) {
|
---|
138 | String coll = (String) s.next();
|
---|
139 | String collname = "" + i + ":" + coll;
|
---|
140 | boolean hit = false;
|
---|
141 | for (int j = 0; j < i; j++) {
|
---|
142 | String oldcollname = "" + j + ":" + coll;
|
---|
143 | if (intermediate.get(oldcollname) != null) {
|
---|
144 |
|
---|
145 | NzdlService oldServer = (NzdlService) services.get( "" + j );
|
---|
146 | Date startOld = new Date();
|
---|
147 | NzdlCollectionInfo oldColl = oldServer.getCollectionInfo(coll);
|
---|
148 | Date endOld = new Date();
|
---|
149 | Date startNew = new Date();
|
---|
150 | NzdlCollectionInfo newColl = server.getCollectionInfo(coll);
|
---|
151 | Date endNew = new Date();
|
---|
152 |
|
---|
153 | if (oldColl.getBuildDate() == newColl.getBuildDate() &&
|
---|
154 | oldColl.getNumOfDocs() == newColl.getNumOfDocs() &&
|
---|
155 | oldColl.getNumOfWords() == newColl.getNumOfWords() &&
|
---|
156 | oldColl.getNumOfBytes() == newColl.getNumOfBytes() &&
|
---|
157 | oldColl.isPublic() == newColl.isPublic() )
|
---|
158 | {
|
---|
159 | if ((endOld.getTime() - startOld.getTime()) >
|
---|
160 | (endNew.getTime() - startNew.getTime()))
|
---|
161 | {
|
---|
162 | results.remove(oldcollname);
|
---|
163 | results.add(collname);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | hit = true;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | if (hit == false) {
|
---|
170 | intermediate.put(collname,collname);
|
---|
171 | results.add("" + i + ":" + coll);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 | return results;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Check if ORB knows of this collection.
|
---|
180 | * @param name the name of a collecion to be searched for.
|
---|
181 | * @return boolean <TT>true</TT> if collection is found, else <TT>false</TT>.
|
---|
182 | */
|
---|
183 | public boolean hasCollection( String _name ) {
|
---|
184 |
|
---|
185 | NzdlService server = (NzdlService) services.get( findServer(_name));
|
---|
186 | return server.hasCollection(findCollection(_name));
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Check if ORB can communicate with of this collection.
|
---|
191 | * @param name the name of a collecion to be pinged.
|
---|
192 | * @return boolean <TT>true</TT> if collection returned ping, else
|
---|
193 | * <TT>false</TT>.
|
---|
194 | */
|
---|
195 | public boolean pingCollection( String _name ) {
|
---|
196 | NzdlService server = (NzdlService) services.get( findServer(_name));
|
---|
197 | return server.pingCollection(findCollection(_name));
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Extract information on this collection into a
|
---|
202 | * {@link NzdlCollectionInfo NzdlCollectionInfo} object
|
---|
203 | * @param name the collection to be retrieved.
|
---|
204 | * @return an instance of
|
---|
205 | * {@link NzdlCollectionInfo NzdlCollectionInfo} containing data on a
|
---|
206 | * collection
|
---|
207 | */
|
---|
208 | public NzdlCollectionInfo getCollectionInfo( String _name ) {
|
---|
209 | NzdlService server = (NzdlService) services.get( findServer(_name));
|
---|
210 | NzdlCollectionInfo result = server.getCollectionInfo(findCollection(_name));
|
---|
211 | return result;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Returns the set of filters for the service object.
|
---|
216 | * @param name the collection name
|
---|
217 | * @return a set of strings that name the filters, for example
|
---|
218 | * BrowseFilter, QueryFilter and NullFilter.
|
---|
219 | */
|
---|
220 | public Set getFilterSet( String _name ) {
|
---|
221 | NzdlService server = (NzdlService) services.get( findServer(_name));
|
---|
222 | Set result = server.getFilterSet(findCollection(_name));
|
---|
223 | return result;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * @param name the collection name
|
---|
228 | * @return the Set of (name, value) value pairs for all metadata
|
---|
229 | * associated with a collection
|
---|
230 | */
|
---|
231 | public Set getMetaTagSet( String _name ) {
|
---|
232 | NzdlService server = (NzdlService) services.get( findServer(_name));
|
---|
233 | Set result = server.getMetaTagSet(findCollection(_name));
|
---|
234 | return result;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Returns the Greenstone Markup Language (GML) text for a particular
|
---|
239 | * document from a collection.
|
---|
240 | * @param name the name of the collection.
|
---|
241 | * @param docID the document identity string
|
---|
242 | * @return the GML document in the form of a string.
|
---|
243 | */
|
---|
244 | public String getDocument( String _name, String _docID ) {
|
---|
245 | NzdlService server = (NzdlService) services.get( findServer(_name));
|
---|
246 | String result = server.getDocument(findCollection(_name), _docID);
|
---|
247 | return result;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Returns the set of value(s) for a metatag from a particular document.
|
---|
252 | * @param name collection name
|
---|
253 | * @param docID the document identifier.
|
---|
254 | * @param metaTag the metatag name such as: Title, Author, Date and Images.
|
---|
255 | * @return The set of value(s) for the requested metatag and document. In
|
---|
256 | * cases such as Title, Author, Date there will probably be only one value
|
---|
257 | * per document.
|
---|
258 | */
|
---|
259 | public Set getMetaData( String _name, String _docID, String _metaTag ) {
|
---|
260 | NzdlService server = (NzdlService) services.get( findServer(_name));
|
---|
261 | Set result = server.getMetaData( findCollection(_name), _docID, _metaTag );
|
---|
262 | return result;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Returns a map of value(s) for a metatag from a list of documents.
|
---|
267 | * @param name collection name
|
---|
268 | * @param docIDs a list of document identifier strings.
|
---|
269 | * @param metaTag the metatag name such as: Title, Author, Date and Images.
|
---|
270 | * @return The map of value(s) for the requested metatag and document. In
|
---|
271 | * cases such as Title, Author, Date there will probably be only one value
|
---|
272 | * per document.
|
---|
273 | */
|
---|
274 | public Map getMetaData( String _name, List _docIDs, String _metaTag ) {
|
---|
275 | NzdlService server = (NzdlService) services.get( findServer(_name));
|
---|
276 | Map result = server.getMetaData( findCollection(_name), _docIDs, _metaTag );
|
---|
277 | return result;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Services a {@link NzdlRequest NzdlRequest} to a collection and places
|
---|
282 | * the results into a {@link NzdlResponse NzdlResponse} object. <br>
|
---|
283 | * Pre: Created instances of NzdlRequest and NzdlResponse. <br>
|
---|
284 | * Post: Placed the result data into NzdlResponse object.
|
---|
285 | * @param name the collection name.
|
---|
286 | * @param request an instance of NzdlRequest with query data to be serviced
|
---|
287 | * @param response an instance of NzdlResponse. where the results of
|
---|
288 | * sevciving the query will be placed
|
---|
289 | */
|
---|
290 | public void service( String _name, NzdlRequest _request,
|
---|
291 | NzdlResponse _response ) {
|
---|
292 | NzdlService server = (NzdlService) services.get( findServer(_name));
|
---|
293 | server.service(findCollection(_name),_request,_response);
|
---|
294 | }
|
---|
295 |
|
---|
296 | }
|
---|