summaryrefslogtreecommitdiff
path: root/usr/src/lib/libslp/javalib/com/sun/slp/UARequester.java
blob: b64f2b6fcffe538f8524de8cfa9d27465c62350f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1999 by Sun Microsystems, Inc.
 * All rights reserved.
 *
 */

//  UARequester.java: Requester operations for UA.
//  Author:           James Kempf
//  Created On:       Thu Jan  8 15:17:35 1998
//  Last Modified By: James Kempf
//  Last Modified On: Mon Feb 22 13:47:06 1999
//  Update Count:     78
//

package com.sun.slp;

import java.util.*;

/**
 * The URequester class implements the Locator interface.
 * It handles the request for the API.  If any of the parameters
 * are missing, they will be supplied with a default value if
 * possible.  If a cached value may be supplied, it will be.
 * If no DA is present, and convergence is used to gather
 * results, these will be merged into one result.
 *
 * @author Erik Guttman, James Kempf
 */


class UARequester extends Object implements Locator {

    private static SLPConfig config = null;
    private static DATable dat = null;

    private Locale locale;

    UARequester(Locale nlocale) {

	Assert.nonNullParameter(nlocale, "locale");

	if (config == null) {
	    config = SLPConfig.getSLPConfig();
	}

	if (dat == null) {
	    dat = DATable.getDATable();
	}

	locale = nlocale;
    }

    /**
     * Return the Locator's locale object. All requests are made in
     * this locale.
     *
     * @return The Locale object.
     */

    public Locale getLocale() {
	return locale;

    }

    /**
     * Return an enumeration of known service types for this scope and naming
     * authority.  Unless a proprietary or experimental service is being
     * discovered, the namingAuthority parameter should be the empty
     * string, "".
     *
     * @param NA	The naming authority, "" for default,
     *           '*' for any naming authority.
     * @param scopes	The SLP scopes of the types.
     * @return ServiceLocationEnumeration of ServiceType objects for
     *	      the service type names.
     * @exception IllegalArgumentException If any of the parameters are
     *					  null or syntactically incorrect.
     * @exception ServiceLocationException An exception is thrown if the
     *					  operation fails.
     */

    public synchronized ServiceLocationEnumeration
	findServiceTypes(String NA, Vector scopes)
	throws ServiceLocationException {

	Assert.nonNullParameter(NA, " NA");
	Assert.nonNullParameter(scopes, "scopes");

	// Formulate and send off messages.

	Vector msgs = createMessages(SrvLocHeader.SrvTypeRqst,
				     NA,
				     null,
				     null,
				     scopes);

	// Collate results.

	Vector ret = new Vector();
	int i, n = msgs.size();
	int max = config.getMaximumResults();

	for (i = 0; i < n; i++) {
	    CSrvTypeMsg msg = (CSrvTypeMsg)msgs.elementAt(i);

	    // Check for errors.

	    checkForError(msg, msgs);

	    Vector serviceTypes = msg.serviceTypes;

	    addUnique(serviceTypes, ret, max);

	}

	// Return.

	return new ServiceLocationEnumerator(ret);
    }

    /**
     * Return an enumeration of ServiceURL objects for services matching
     * the query. The services are returned from the locale of the
     * locator.
     *
     * @param type	The type of the service (e.g. printer, etc.).
     * @param scopes	The SLP scopes of the service types.
     * @param query		A string with the SLP query.
     * @return ServiceLocationEnumeration of ServiceURL objects for
     *	      services matching the
     *         attributes.
     * @exception ServiceLocationException An exception is returned if the
     *					  operation fails.
     * @see ServiceURL
     */

    public synchronized ServiceLocationEnumeration
	findServices(ServiceType type, Vector scopes, String query)
	throws ServiceLocationException {

	Assert.nonNullParameter(type, "type");
	Assert.nonNullParameter(scopes, "scopes");
	Assert.nonNullParameter(query, "query");

	// Formulate and send off messages.

	Vector msgs = createMessages(SrvLocHeader.SrvReq,
				     type,
				     query,
				     type,
				     scopes);

	// Collate results.

	Vector ret = new Vector();
	int i, n = msgs.size();
	int max = config.getMaximumResults();

	for (i = 0; i < n; i++) {
	    SrvLocMsg msg = (SrvLocMsg)msgs.elementAt(i);

	    // Check for errors.

	    checkForError(msg, msgs);

	    // Be sure to account for DAAdverts and SAAdverts.

	    Vector serviceURLs = null;

	    if (msg instanceof CSrvMsg) {
		serviceURLs = ((CSrvMsg)msg).serviceURLs;

	    } else if (msg instanceof CSAAdvert) {
		serviceURLs = new Vector();
		serviceURLs.addElement(((CSAAdvert)msg).URL);

	    } else if (msg instanceof CDAAdvert) {
		serviceURLs = new Vector();
		serviceURLs.addElement(((CDAAdvert)msg).URL);

	    }

	    addUnique(serviceURLs, ret, max);

	}

	// Return.

	return new ServiceLocationEnumerator(ret);
    }

    /**
     * Return the attributes for the service URL, using the locale
     * of the locator.
     *
     * @param URL	The service URL.
     * @param scopes	The SLP scopes of the service.
     * @param attributeIds A vector of strings identifying the desired
     *			  attributes. A null value means return all
     *			  the attributes.  <b>Partial id strings</b> may
     *                     begin with '*' to match all ids which end with
     *                     the given suffix, or end with '*' to match all
     *                     ids which begin with a given prefix, or begin
     *                     and end with '*' to do substring matching for
     *                     ids containing the given partial id.
     * @return ServiceLocationEnumeration of ServiceLocationAttribute
     *         objects matching the ids.
     * @exception ServiceLocationException An exception is returned if the
     *					  operation fails.
     * @exception IllegalArgumentException If any of the parameters are
     *					  null or syntactically incorrect.
     * @see ServiceLocationAttribute
     *
     */

    public synchronized ServiceLocationEnumeration
	findAttributes(ServiceURL URL, Vector scopes, Vector attributeIds)
	throws ServiceLocationException {

	Assert.nonNullParameter(URL, "URL");
	Assert.nonNullParameter(scopes, "scopes");
	Assert.nonNullParameter(attributeIds, "attributeIds");

	Vector msgs = createMessages(SrvLocHeader.AttrRqst,
				     URL,
				     attributeIds,
				     URL.getServiceType(),
				     scopes);

	// Check results.

	Vector ret = new Vector();
	int i, n = msgs.size();
	int max = config.getMaximumResults();

	// We only take the first message that came back and is OK.

	for (i = 0; i < n; i++) {
	    SrvLocMsg msg = (SrvLocMsg)msgs.elementAt(i);

	    // Check for errors.

	    checkForError(msg, msgs);

	    // Select out attribute list.

	    if (msg instanceof CAttrMsg) {
		ret = ((CAttrMsg)msg).attrList;

	    } else if (msg instanceof CSAAdvert) {

		// Need to check that URL matches.

		CSAAdvert smsg = (CSAAdvert)msg;

		if (!URL.equals(smsg.URL)) {
		    continue;

		}

		ret = smsg.attrs;

	    } else if (msg instanceof CDAAdvert) {

		// Need to check that URL matches.

		CDAAdvert smsg = (CDAAdvert)msg;

		if (!URL.equals(smsg.URL)) {
		    continue;

		}

		ret = smsg.attrs;
	    }

	    // Truncate, if return is larger than maximum.

	    if (ret.size() > max) {
		ret.setSize(max);

	    }

	    // Break out, we only need one.

	    break;

	}

	// Return.

	return new ServiceLocationEnumerator(ret);
    }

    /**
     * Return all attributes for all service URL's having this
     * service type in the locale of the Locator.
     *
     * @param type The service type.
     * @param scopes	The SLP scopes of the service type.
     * @param attributeIds A vector of strings identifying the desired
     *			  attributes. A null value means return all
     *			  the attributes.  <b>Partial id strings</b> may
     *                     begin with '*' to match all ids which end with
     *                     the given suffix, or end with '*' to match all
     *                     ids which begin with a given prefix, or begin
     *                     and end with '*' to do substring matching for
     *                     ids containing the given partial id.
     * @return ServiceLocationEnumeration of ServiceLocationAttribute
     *         objects matching the ids.
     * @exception ServiceLocationException An exception is returned if the
     *					  operation fails.
     * @exception IllegalArgumentException If any of the parameters are
     *					  null or syntactically incorrect.
     * @see ServiceLocationAttribute
     *
     */

    public synchronized ServiceLocationEnumeration
	findAttributes(ServiceType type, Vector scopes, Vector attributeIds)
	throws ServiceLocationException {

	Assert.nonNullParameter(type, "URL");
	Assert.nonNullParameter(scopes, "scopes");
	Assert.nonNullParameter(attributeIds, "attributeIds");

	// Formulate and send off messages.

	Vector msgs = createMessages(SrvLocHeader.AttrRqst,
				     type,
				     attributeIds,
				     type,
				     scopes);
	// Collate results.

	Vector ret = new Vector();
	int i, n = msgs.size();
	int max = config.getMaximumResults();
	Hashtable ht = new Hashtable();

	for (i = 0; i < n && ret.size() < max; i++) {
	    SrvLocMsg msg = (SrvLocMsg)msgs.elementAt(i);

	    // Check for errors.

	    checkForError(msg, msgs);

	    Vector attrList = null;

	    // Get the instance variable.

	    if (msg instanceof CAttrMsg) {
		attrList = ((CAttrMsg)msg).attrList;

	    } else if (msg instanceof CSAAdvert) {
		attrList = ((CSAAdvert)msg).attrs;

	    } else if (msg instanceof CDAAdvert) {
		attrList = ((CDAAdvert)msg).attrs;

	    }

	    // Merge any duplicates.

	    int j, m = attrList.size();

	    for (j = 0; j < m; j++) {
		ServiceLocationAttribute attr =
		    (ServiceLocationAttribute)attrList.elementAt(j);
	
		ServiceLocationAttribute.mergeDuplicateAttributes(attr,
								  ht,
								  ret,
								  true);

		if (ret.size() >= max) {
		    break;

		}
	    }
	}

	// Return.

	return new ServiceLocationEnumerator(ret);
    }

    // Execute the message request, returning messages.

    private Vector
	createMessages(int msgType,
		       Object t1,
		       Object t2,
		       ServiceType type,
		       Vector scopes)
	throws ServiceLocationException {

	// Validate, lower case scopes.

	DATable.validateScopes(scopes, locale);

	SrvLocMsg multiMsg = null;
	SrvLocMsg uniMsg = null;
	Vector daAddresses = null;
	Vector multiCastScopes = null;

	// Get the hashtable of unicast DA addresses and multicast scopes.

	Hashtable daRecords = dat.findDAScopes(scopes);

	// Get multicast scopes and DA addresses.

	multiCastScopes =
	    (Vector)daRecords.get(DATable.MULTICAST_KEY);

	daAddresses =
	    (Vector)daRecords.get(DATable.UNICAST_KEY);

	// Special case for service request and attribute request
	//  if the user is looking for a special SLP type.

	if (((msgType == SrvLocHeader.SrvReq) ||
	    (msgType == SrvLocHeader.AttrRqst)) &&
	    (type.equals(Defaults.DA_SERVICE_TYPE) ||
	    type.equals(Defaults.SA_SERVICE_TYPE))) {

	    multiCastScopes = scopes;
	    daAddresses = null;

	    // Get query. If an attribute request, then the user
	    //  needs to sort out the attributes.

	    String query = "";

	    if (msgType == SrvLocHeader.SrvReq) {
		query = (String)t2;

	    }

	    multiMsg = new CSrvMsg(locale, type, multiCastScopes, query);

	} else {

	    // Handle a regular message.

	    // Multicast scopes are all scopes not supported by any DA.

	    if (multiCastScopes != null) {

		switch (msgType) {

		case SrvLocHeader.SrvTypeRqst:
		    multiMsg =
			new CSrvTypeMsg(locale, (String)t1, multiCastScopes);
		    break;

		case SrvLocHeader.SrvReq:
		    multiMsg =
			new CSrvMsg(locale, type, multiCastScopes, (String)t2);
		    break;

		case SrvLocHeader.AttrRqst:

		    if (t1 instanceof ServiceURL) {
			multiMsg =
			    new CAttrMsg(locale,
					 (ServiceURL)t1,
					 multiCastScopes,
					 (Vector)t2);

		    } else {
			multiMsg =
			    new CAttrMsg(locale,
					 type,
					 multiCastScopes,
					 (Vector)t2);

		    }
		}
	    }

	    // Unicast only requires a single message because the DAs will
	    //  ignore any scopes they do not support, just as long as
	    //  they support one of them.

	    if (daAddresses != null) {
		switch (msgType) {

		case SrvLocHeader.SrvTypeRqst:
		    uniMsg =
			new CSrvTypeMsg(locale, (String)t1, scopes);
		    break;

		case SrvLocHeader.SrvReq:
		    uniMsg =
			new CSrvMsg(locale, type, scopes, (String)t2);
		    break;

		case SrvLocHeader.AttrRqst:

		    if (t1 instanceof ServiceURL) {
			uniMsg =
			    new CAttrMsg(locale,
					 (ServiceURL)t1,
					 scopes,
					 (Vector)t2);

		    } else {
			uniMsg =
			    new CAttrMsg(locale,
					 type,
					 scopes,
					 (Vector)t2);

		    }

		}
	    }
	}

	// Send off messages, return results.

	return Transact.transactUA(daAddresses,
				   uniMsg,
				   multiMsg,
				   config.getMulticastAddress());
    }

    // Check message for error code.

    private static void
	checkForError(SrvLocMsg msg, Vector v)
	throws ServiceLocationException {
	int err = msg.getErrorCode();

	if (err != ServiceLocationException.OK) {
	    if (v.size() == 1) {
		config.writeLog("single_exception",
				new Object[] {
		    new Integer(err)});
		throw
		    new ServiceLocationException((short)err,
						 "remote_error",
						 new Object[] {});
	    } else {
		config.writeLog("multiple_exception",
				new Object[] {
		    new Integer(err)});
	    }
	}
    }

    // Process the incoming vector, adding any unique returns.

    private static void addUnique(Vector incoming, Vector returns, int max) {

	int i, n = incoming.size();

	for (i = 0; i < n; i++) {
	    Object o = incoming.elementAt(i);

	    if (!returns.contains(o) && returns.size() < max) {
		returns.addElement(o);

	    }
	}
    }

}