source: trunk/java-client/org/nzdl/gsdl/service/NzdlServiceClient.java@ 2254

Last change on this file since 2254 was 2220, checked in by say1, 23 years ago

change int-> long in a number of places, new createNzdlService method taking only an IOR, added the start of a hli (high level interface

  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1/*
2 * NzdlServiceClient.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
21package org.nzdl.gsdl.service;
22
23//import java.io.*;
24//import java.util.*;
25import java.util.Map;
26import java.util.Set;
27import java.util.List;
28import java.util.HashSet;
29import java.util.Properties;
30
31/* jdk 1.3 orb */
32import org.omg.CORBA.ORB;
33/* gsdl corba stuff */
34import org.nzdl.gsdl.corba.gsdlInterface.*;
35import org.nzdl.gsdl.util.*;
36
37/**
38 * Used to implement the {@link NzdlService NzdlService} object.
39 * NzdlService is an object that services CORBA client requests to a
40 * Greenstone Digital Library.
41 *
42 * @author Stuart Yeates ([email protected])
43 * @author Aziz Mahoui ([email protected])
44 * @author Gordon Paynter ([email protected])
45 * @author Brett Sheeran ([email protected]) (comments)
46 * @version $Revision: 2220 $
47 */
48public class NzdlServiceClient
49 extends java.lang.Object
50 implements NzdlService {
51
52 private corbaiface m_nzdlServer = null;
53
54 /**
55 * Creates an instance of Java's ORB (Object Request Broker) to interface
56 * to a Greenstone Digital Library.
57 * @param <b>args</b> command-line arguments from the application's main method, or
58 * alternatively, the name of the applet. This parameter is passed to
59 * <TT>ORB.init( _args, _props)</TT>. Args may be a <TT>null</TT>.
60 * @param <b>props</b> application specific properties.
61 * This parameter is passed to
62 * <TT>ORB.init( _args, _props)</TT>. props may be a <TT>null</TT>.
63 * @param <b>IOR</b> the Interoperable Object Reference, a string that identifies
64 * the computer on which corbaserver is running and the interface it
65 * supports.
66 * @see The Java documentation on the ORB class.
67 */
68 public NzdlServiceClient( String [] _args, Properties _props, String _IOR) {
69 corbaComErrorHolder error = NzdlCorbaFactory.createComErrorHolder();
70
71 try {
72
73 // getting a reference to the remote NZDL server
74 org.omg.CORBA.ORB orb = ORB.init( _args, _props);
75 org.omg.CORBA.Object obj = orb.string_to_object(_IOR) ;
76 m_nzdlServer = corbaifaceHelper.narrow(obj);
77
78 // basic setup
79 if (m_nzdlServer.initialise(error)) {
80 } else {
81 throw new Error("NzdlServiceClient initialisation unsuccessful.");
82 }
83
84 } catch (Exception e) {
85 System.err.println("Exception in NzdlServiceClient initialisation: \n" + e) ;
86 System.err.println("corbaComError value: " + error.value.value());
87 System.err.println("NzdlServiceClient initialisation unsuccessful.");
88 throw new Error("NzdlServiceClient initialisation unsuccessful.");
89 }
90 if (error.value.value() != corbaComError._corbaNoError) {
91 System.err.println("corbaComError value: " + error.value.value());
92 }
93
94 System.out.println("NzdlServiceClient initialisation successful.");
95 }
96
97
98 /**
99 * Allow the client to configure a CORBA server.
100 * @param key the parameter to be set.
101 * @param values what that parameter will be set to.
102 */
103 public void configure( String _key, Set _values ) {
104 if (_key == null || _key.equals("")){
105 throw new Error ("null or empty collection _key");
106 }
107 if (_values == null ){
108 throw new Error ("null or empty collection _values");
109 }
110 corbaComErrorHolder error = NzdlCorbaFactory.createComErrorHolder();
111 m_nzdlServer.configure( NzdlCorbaFactory.toCorbaText( _key ),
112 NzdlCorbaFactory.toCorbaTextArray( _values ),
113 error);
114 if (error.value.value() != corbaComError._corbaNoError) {
115 System.err.println("CORBA error:" + error.value.value());
116 }
117 }
118
119 /**
120 * Obtain the set of collection names for a Greenstone library.
121 * @return A set of collection names in string format.
122 */
123 public Set getCollectionSet( ) {
124 corbaComErrorHolder error = NzdlCorbaFactory.createComErrorHolder();
125 corbatext_tarrayHolder c_collSet = NzdlCorbaFactory.createCorbaTextArrayHolder();
126 m_nzdlServer.collectionList(c_collSet, error);
127 Set collSet = new HashSet();
128 for (int i=0; i<c_collSet.value.length; i++) {
129 collSet.add( NzdlCorbaFactory.toString( c_collSet.value[i] ) );
130 }
131 if (error.value.value() != corbaComError._corbaNoError) {
132 System.err.println("CORBA error:" + error.value.value());
133 }
134 return collSet ;
135 }
136
137 /**
138 * Check if ORB knows of this collection.
139 * @param name the name of a collecion to be searched for.
140 * @return boolean <TT>true</TT> if collection is found, else <TT>false</TT>.
141 */
142 public boolean hasCollection( String _name ) {
143 if (_name == null || _name.equals("")){
144 throw new Error ("null or empty collection _name");
145 }
146 corbaComErrorHolder c_err = NzdlCorbaFactory.createComErrorHolder();
147 org.omg.CORBA.BooleanHolder c_has = new org.omg.CORBA.BooleanHolder();
148 m_nzdlServer.hasCollection( NzdlCorbaFactory.toCorbaText(_name), c_has, c_err );
149 if (c_err.value.value() != 0)
150 System.err.println("hasCollection error " + c_err.value.value());
151 return c_has.value;
152 }
153
154
155 /**
156 * Check if ORB can communicate with of this collection.
157 * @param name the name of a collecion to be pinged.
158 * @return boolean <TT>true</TT> if collection returned ping, else
159 * <TT>false</TT>.
160 */
161 public boolean pingCollection( String _name ) {
162 if (_name == null || _name.equals("")){
163 throw new Error ("null or empty collection _name");
164 }
165 corbaComErrorHolder c_err = NzdlCorbaFactory.createComErrorHolder();
166 org.omg.CORBA.BooleanHolder c_ping = new org.omg.CORBA.BooleanHolder();
167 m_nzdlServer.ping( NzdlCorbaFactory.toCorbaText(_name), c_ping, c_err );
168 if (c_err.value.value() != 0)
169 System.err.println("pingCollection() error " + c_err.value.value());
170 return c_ping.value;
171 }
172
173 /**
174 * Extract information on this collection into a
175 * {@link NzdlCollectionInfo NzdlCollectionInfo} object
176 * @param name the collection to be retrieved.
177 * @return an instance of
178 * {@link NzdlCollectionInfo NzdlCollectionInfo} containing data on a
179 * collection
180 */
181 public NzdlCollectionInfo getCollectionInfo( String _name ) {
182 if (_name == null || _name.equals("")){
183 throw new Error ("null or empty collection _name");
184 }
185 corbaComErrorHolder c_err = NzdlCorbaFactory.createComErrorHolder();
186 corbaColInfoResponseHolder c_info = NzdlCorbaFactory.createColInfoResponseHolder();
187 m_nzdlServer.getCollectInfo( NzdlCorbaFactory.toCorbaText(_name), c_info, c_err );
188 if (c_err.value.value() != 0)
189 System.err.println("getCollectionInfo() error " + c_err.value.value());
190 return new NzdlCollectionInfo( c_info.value );
191 }
192
193 /**
194 * Returns the set of filters for the service object.
195 * @param name the collection name
196 * @return a set of strings that name the filters, for example
197 * BrowseFilter, QueryFilter and NullFilter.
198 */
199 public Set getFilterSet( String _name ) {
200 if (_name == null || _name.equals("")){
201 throw new Error ("null or empty filter _name");
202 }
203 corbaComErrorHolder c_err = NzdlCorbaFactory.createComErrorHolder();
204 corbatext_t c_name = NzdlCorbaFactory.toCorbaText( _name );
205 corbatext_tarrayHolder c_collSet = NzdlCorbaFactory.createCorbaTextArrayHolder();
206 m_nzdlServer.getFilterInfo(c_name, c_collSet,c_err);
207 if (c_err.value.value() != 0)
208 System.err.println("getCollectionInfo() error " + c_err.value.value());
209
210 Set collSet = new HashSet();
211 for (int i=0; i<c_collSet.value.length; i++) {
212 collSet.add( NzdlCorbaFactory.toString( c_collSet.value[i] ) );
213 }
214 return collSet ;
215
216 }
217
218 /**
219 * Not Currently implemented.
220 */
221 public Set getMetaTagSet( String _name ) {
222 if (_name == null || _name.equals("")){
223 throw new Error ("null or empty _name");
224 }
225 System.err.println("TO BE IMPLEMENTED: DON'T KNOW HOW YET !!!");
226 return null ;
227 }
228
229 /**
230 * Returns the Greenstone Markup Language (GML) text for a particular
231 * document from a collection.
232 * @param name the name of the collection.
233 * @param docID the document identity string
234 * @return the GML document in the form of a string.
235 */
236 public String getDocument( String _name, String _docID ) {
237 if (_name == null || _name.equals("")){
238 throw new Error ("null or empty _name");
239 }
240 if (_docID == null || _docID.equals("")){
241 throw new Error ("null or empty _docID");
242 }
243 corbatext_t c_name = NzdlCorbaFactory.toCorbaText( _name );
244 corbaDocRequestHolder c_request = NzdlCorbaFactory.createDocRequestHolder( _docID );
245 corbaDocResponseHolder c_response = NzdlCorbaFactory.createDocResponseHolder( );
246 corbaComErrorHolder c_err = NzdlCorbaFactory.createComErrorHolder();
247 m_nzdlServer.getDocument( c_name, c_request, c_response, c_err );
248 if (c_err.value.value() != 0)
249 System.err.println("getDocument() error " + c_err.value.value());
250 return NzdlCorbaFactory.toString( c_response.value.doc );
251 }
252
253 /**
254 * Returns the set of value(s) for a metatag from a particular document.
255 * @param name collection name
256 * @param docID the document identifier.
257 * @param metaTag the metatag name such as: Title, Author, Date and Images.
258 * @return The set of value(s) for the requested metatag and document. In
259 * cases such as Title, Author, Date there will probably be only one value
260 * per document.
261 */
262 public Set getMetaData( String _name, String _docID, String _metaTag ) {
263 if (_name == null || _name.equals("")){
264 throw new Error ("null or empty _name");
265 }
266 if (_docID == null || _docID.equals("")){
267 throw new Error ("null or empty _docID");
268 }
269 if (_metaTag == null || _metaTag.equals("")){
270 throw new Error ("null or empty _metaTag");
271 }
272 NzdlRequest request = new NzdlRequest( _docID, _metaTag );
273 NzdlResponse response = new NzdlResponse( );
274 service( _name, request, response );
275 NzdlResultSet resultSet = response.getResultSet();
276 /*
277 * Because a result set may contain several hits, we only return
278 * the set of metadata values of the specifed meta tag for the
279 * specified document ID.
280 */
281 return resultSet.getMetaData( _docID, _metaTag );
282 }
283
284 /**
285 * Returns a map of value(s) for a metatag from a list of documents.
286 * @param name collection name
287 * @param docIDs a list of document identifier strings.
288 * @param metaTag the metatag name such as: Title, Author, Date and Images.
289 * @return The map of value(s) for the requested metatag and document. In
290 * cases such as Title, Author, Date there will probably be only one value
291 * per document.
292 */
293 public Map getMetaData( String _name, List _docIDs, String _metaTag ) {
294 if (_name == null || _name.equals("")){
295 throw new Error ("null or empty _name");
296 }
297 if (_docIDs == null){
298 throw new Error ("null or empty _docID");
299 }
300 if (_metaTag == null || _metaTag.equals("")){
301 throw new Error ("null or empty _metaTag");
302 }
303 NzdlRequest request = new NzdlRequest( _docIDs, _metaTag );
304 NzdlResponse response = new NzdlResponse( );
305 service( _name, request, response );
306 NzdlResultSet resultSet = response.getResultSet();
307 //return resultSet.getMetaData( _docID, _metaTag );
308 return resultSet.getMetaData( _metaTag );
309 //return resultSet.getAllMetaData( );
310 }
311
312 /**
313 * Services a {@link NzdlRequest NzdlRequest} to a collection and places
314 * the results into a {@link NzdlResponse NzdlResponse} object. <br>
315 * Pre: Created instances of NzdlRequest and NzdlResponse. <br>
316 * Post: Placed the result data into NzdlResponse object.
317 * @param name the collection name.
318 * @param request an instance of NzdlRequest with query data to be serviced
319 * @param response an instance of NzdlResponse. where the results of
320 * sevciving the query will be placed
321 */
322 public void service( String _name, NzdlRequest _request, NzdlResponse _response ) {
323 if (_name == null || _name.equals("")){
324 throw new Error ("null or empty _name");
325 }
326 if (_request == null || _request.equals("")){
327 throw new Error ("null or empty _request");
328 }
329 if (_response == null || _response.equals("")){
330 throw new Error ("null or empty _response");
331 }
332 corbatext_t c_name = NzdlCorbaFactory.toCorbaText( _name );
333 corbaComErrorHolder c_err = NzdlCorbaFactory.createComErrorHolder();
334 m_nzdlServer.filter( c_name, _request.getFilter(), _response.getHolder(), c_err );
335 if (c_err.value.value() != 0)
336 System.err.println("service() error " + c_err.value.value());
337 }
338
339}
Note: See TracBrowser for help on using the repository browser.