summaryrefslogtreecommitdiff
path: root/usr/src/lib/libslp/javalib/com/sun/slp/SLPTemplateRegistry.java
blob: 56f3692eb212059bafe325565b9df55600a2171a (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
/*
 * 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.
 *
 */

//  SLPTemplateRegistry.java: Service object for registering a new service
//			  template.
//  Author:           James Kempf
//  Created On:       Tue May 27 15:04:35 1997
//  Last Modified By: James Kempf
//  Last Modified On: Thu Jan  7 14:25:20 1999
//  Update Count:     134
//

package com.sun.slp;

import java.util.*;

/**
 * The SLPTemplateRegistry class registers and unregisters service templates,
 * looks up the template based on the service type name, and returns an
 * attribute verifier for the service.It subclasses the TemplateRegistry
 * abstract class.
 *
 * An slp-template URL has the following format:
 *
 *   service:slp-template:<document URL>;type=<service type>;
 *				          version=<version no.>;
 *					  language=<language locale>
 *
 * @author James Kempf
 *
 */

class SLPTemplateRegistry extends TemplateRegistry {

    /**
     * Attribute id for attribute describing service type name.
     * String, single valued attribute.
     */

    static final String SERVICE_ATTR_ID = "template-type";

    /**
     * Attribute id for attribute describing help text.
     * String, single valued, required attribute, .
     */

    static final String DESCRIPTION_ATTR_ID = "template-description";

    /**
     * Attribute id for attribute describing service version. The
     * version number is of the form ``n.m'', where n and m are integers.
     * String, single valued, required attribute.
     */

    static final String VERSION_ATTR_ID = "template-version";

    /**
     * Attribute id for attribute describing service URL url part grammer.
     * String, single valued, required attribute.
     */

    static final String SERVICE_URL_ATTR_ID = "template-url-syntax";

    /**
     * The service type name for the template type.
     */

    static final String TEMPLATE_SERVICE_TYPE = "service:slp-template";

    // The distinguished template registry object.

    private static TemplateRegistry registry = null;

    // Package private constructor for singleton pattern maintained
    // by the ServiceLocationManager.

    SLPTemplateRegistry() throws ServiceLocationException {

    }

    //
    // Public implementation.
    //

    /**
     * Register the new service.
     *
     * @param serviceType	Name of the service.
     * @param documentURL	URL of the template document.
     * @param languageLocale	Locale of the template langugae.
     * @param version		Version number of template document.
     * @exception ServiceLocationException Error code is
     *				    INVALID_REGISTRATION
     *					   if the service already exists or
     *					   the registration fails.
     *					   Throws
     *				    SYSTEM_ERROR
     *					   if the scope vector is null or
     *					   empty.
     *					   Throws
     *				    PARSE_ERROR
     *					   if an attribute is bad.
     * @exception IllegalArgumentException Thrown if any parameters are null.
     *
     */

    public void registerServiceTemplate(ServiceType serviceType,
					String documentURL,
					Locale languageLocale,
					String version)
	throws ServiceLocationException {

	// Check for illegal parameters.

	Assert.nonNullParameter(serviceType, "serviceType");

	Assert.nonNullParameter(documentURL, "documentURL");

	Assert.nonNullParameter(languageLocale, "language");

	Assert.nonNullParameter(version, "version");

	String language = languageLocale.getLanguage();

	if (language == null || language.length() <= 0) {
	    throw
		new IllegalArgumentException(
		SLPConfig.getSLPConfig().formatMessage("template_lang_null",
						       new Object[] {
		    documentURL}));
	}

	String turl = null;

	try {

	    turl = findTemplateURL(serviceType,
				   languageLocale,
				   version);

	} catch (ServiceLocationException ex) {

	    // Ignore if language not supported, it just means there
	    //  isn't any.

	    if (ex.getErrorCode() !=
		ServiceLocationException.LANGUAGE_NOT_SUPPORTED) {
		throw ex;

	    }
	}

	// Throw an exception if it exists.

	if (turl != null) {
	    throw
		new ServiceLocationException(
				ServiceLocationException.INVALID_REGISTRATION,
				"template_already_registered",
				new Object[] {
		    documentURL,
			version,
			languageLocale});
	}

	// Construct attributes for the registration.

	Vector attributes = new Vector();

	// Add the service type name.

	Vector values = new Vector();
	values.addElement(serviceType.toString());
	ServiceLocationAttribute attr =
	    new ServiceLocationAttribute(SERVICE_ATTR_ID, values);

	attributes.addElement(attr);

	// Add the version.

	values = new Vector();
	values.addElement(version);
	attr =
	    new ServiceLocationAttribute(VERSION_ATTR_ID, values);

	attributes.addElement(attr);

	// Construct a service URL for the template.

	ServiceURL surl =
	    new ServiceURL(TEMPLATE_SERVICE_TYPE +
			   ":"+
			   documentURL+
			   ";"+
			   SERVICE_ATTR_ID+
			   "="+
			   serviceType+
			   ";"+
			   VERSION_ATTR_ID+
			   "="+
			   version,
			   ServiceURL.LIFETIME_MAXIMUM);


	// Do the registration.

	Advertiser serviceAgent =
	    ServiceLocationManager.getAdvertiser(languageLocale);

	if (serviceAgent == null) {
	    throw
		new ServiceLocationException(
				ServiceLocationException.NOT_IMPLEMENTED,
				"no_advertiser",
				new Object[0]);
	}

	serviceAgent.register(surl, attributes);

	// Note that the assumption here is that the URL containing the
	//  path to the template document is written "somehow".
	//  It is up to the client to make sure that the template document
	//  has been written.

    }

    /**
     * Deregister the template for service type.
     *
     * @param serviceType	Name of service.
     * @param <i>languageLocale</i> Language locale of template.
     * @param <i>version</i> Version of the template, null for latest.
     * @exception ServiceLocationException Thrown if the deregistration
     *					  fails.
     * @exception IllegalArgumentException Thrown if the parameter is null.
     *
     */

    public void deregisterServiceTemplate(ServiceType serviceType,
					  Locale languageLocale,
					  String version)
	throws ServiceLocationException {

	// Check the parameter.

	Assert.nonNullParameter(serviceType, "serviceType");

	Assert.nonNullParameter(languageLocale, "languageLocale");

	// Get the template document URL for the service.

	ServiceURL turl = findVersionedURL(serviceType,
					   languageLocale,
					   version);

	// If there's no template, then throw an exception.

	if (turl == null) {
	    throw
		new ServiceLocationException(
				ServiceLocationException.INVALID_REGISTRATION,
				"template_not_registered",
				new Object[] {
		    serviceType,
			version,
			languageLocale});
	}

	// Deregister in all scopes.

	Advertiser serviceAgent =
	    ServiceLocationManager.getAdvertiser(languageLocale);

	if (serviceAgent == null) {
	    throw
		new ServiceLocationException(
				ServiceLocationException.NOT_IMPLEMENTED,
				"no_advertiser",
				new Object[0]);
	}

	// Deregister the service URL.

	serviceAgent.deregister(turl);

    }



    /**
     * Find the service URL for the service.
     *
     * @param serviceType		Name of service.
     * @param <i>languageLocale</i> Language locale of template.
     * @param <i>version</i> Version of the template, null for latest.
     * @return ServiceURL for the service template. If the service doesn't
     *		exist, returns null.
     * @exception ServiceLocationException Error code is
     *				    SYSTEM_ERROR
     *					   if the scope vector is null or
     *					   empty or if more than one
     *					   template URL is returned.
     * @exception IllegalArgumentException Thrown if any parameters are null.
     *
     */

    public String findTemplateURL(ServiceType serviceType,
				  Locale languageLocale,
				  String version)
	throws ServiceLocationException {

	// Check the parameter.

	Assert.nonNullParameter(serviceType, "serviceType");

	Assert.nonNullParameter(languageLocale, "languageLocale");

	ServiceURL turl = findVersionedURL(serviceType,
					   languageLocale,
					   version);

	// If nothing returned, then simply return.

	if (turl == null) {
	    return null;

	}

	// Form the document URL.

	ServiceType type = turl.getServiceType();
	String url = turl.toString();
	String abstractType = type.getAbstractTypeName();

	if (!abstractType.equals(TEMPLATE_SERVICE_TYPE)) {
	    throw
		new ServiceLocationException(
				ServiceLocationException.PARSE_ERROR,
				"template_url_malformed",
				new Object[] {turl});

	}

	// Parse off the URL path.

	int idx = url.indexOf(";"+SERVICE_ATTR_ID+"=");

	if (idx == -1) {
	    throw
		new ServiceLocationException(
				ServiceLocationException.PARSE_ERROR,
				"template_url_malformed",
				new Object[] {turl});

	}

	int jdx = TEMPLATE_SERVICE_TYPE.length() + 1; // don't forget :!!!

	// Return the document URL.

	return url.substring(jdx, idx);
    }

    // Return a URL given a version and language locale.

    private ServiceURL findVersionedURL(ServiceType serviceType,
					Locale languageLocale,
					String version)
	throws ServiceLocationException {

	// Templates should be registered in all scopes. Look for them
	//  in all.

	Vector scopes = ServiceLocationManager.findScopes();

	// Set up query.

	ServiceLocationEnumeration results = null;
	String query = "(" + SERVICE_ATTR_ID + "=" + serviceType + ")";

	if (version != null) {
	    query = query + "(" + VERSION_ATTR_ID + "=" + version + ")";

	}

	query = "(&" + query + ")";

	// Get user agent for query.

	Locator userAgent =
	    ServiceLocationManager.getLocator(languageLocale);

	if (userAgent == null) {
	    throw
		new ServiceLocationException(
				ServiceLocationException.NOT_IMPLEMENTED,
				"no_locator",
				new Object[0]);
	}

	try {
	    ServiceType type = new ServiceType(TEMPLATE_SERVICE_TYPE);

	    results =
		userAgent.findServices(type,	
				       scopes,	
				       query);

	} catch (ServiceLocationException ex) {

	    // If language not supported, it just means none there.

	    if (ex.getErrorCode() !=
		ServiceLocationException.LANGUAGE_NOT_SUPPORTED) {
		throw ex;

	    }

	}

	// If nothing came back, then return null.

	if (!results.hasMoreElements()) {
	    return null;

	}


	ServiceURL turl = null;
	float highest = (float)-1.0;

	// If there's more than one service of this type registered, then
	//  take highest version if version number was null.

	while (results.hasMoreElements()) {
	    ServiceURL surl = (ServiceURL)results.nextElement();
	    String urlPath = surl.getURLPath();

	    if (version == null) {

		// Get the version attribute from the URL.
	
		String token = ";"+VERSION_ATTR_ID+"=";

		int idx = urlPath.indexOf(token);

		if (idx == -1) { // ignore, there may be more...
		    continue;

		}

		urlPath =
		    urlPath.substring(idx+token.length(), urlPath.length());

		idx = urlPath.indexOf(";");

		if (idx == -1) { // ignore, there may be more...
		    continue;

		}

		String temversion = urlPath.substring(0, idx);
		float current = (float)0.0;

		// Convert to float.

		try {

		    current = Float.valueOf(temversion).floatValue();

		} catch (NumberFormatException ex) {

		    continue;  // ignore, there may be more...

		}

		// Identify if this is the highest version number so far.

		if (current > highest) {
		    turl = surl;
		}

	    } else {

		// If we found more than one, may be a problem.

		if (turl != null) {

		    throw
			new ServiceLocationException(
				ServiceLocationException.INTERNAL_SYSTEM_ERROR,
				"template_multiple",
				new Object[] {
			    serviceType,
				version,
				languageLocale});
		}

		turl = surl;
	    }
	}

	return turl;
    }

    /**
     * Create an attribute verifier for the template document URL.
     *
     * @param documentURL A URL for the template document URL.
     * @return An attribute verifier for the service
     * @exception ServiceLocationException Throws
     *					  PARSE_ERROR
     *					  if any syntax errors
     *					  are encountered during parsing
     *					  of service's template definition.
     *					  Throws
     *					SYSTEM_ERROR
     *					  if URL parsing error occurs.
     *					  Throws ServiceLocationException
     *					  if any other errors occur.
     * @exception IllegalArgumentException Thrown if any parameters are null.
     *
     */

    public ServiceLocationAttributeVerifier attributeVerifier(
							String documentURL)
	throws ServiceLocationException {

	// Check the parameter.

	Assert.nonNullParameter(documentURL, "documentURL");

	// Create a URL attribute parser to parse the document.

	return new URLAttributeVerifier(documentURL);
    }

}