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

//  SARequester.java: Requester operations for SA.
//  Author:           James Kempf
//  Created On:       Thu Jan  8 14:59:41 1998
//  Last Modified By: James Kempf
//  Last Modified On: Thu Jan 28 15:33:33 1999
//  Update Count:     58
//

package com.sun.slp;

import java.io.*;
import java.util.*;

/**
 * The SARequester class implements the Advertiser interface.
 * It handles the request for the API. Registration is done
 * by calling into the loopback I/O so the SA server does
 * the registration.
 *
 * @author Erik Guttman, James Kempf
 */


class SARequester extends Object implements Advertiser {

    // For maintaining registrations that are LIFETIME_PERMANENT.

    private static PermSARegTable pregtable = null;

    private static SLPConfig config = null;

    private Locale locale;


    SARequester(Locale nlocale) {

	Assert.nonNullParameter(nlocale, "locale");

	// Initialize...

	getPermSARegTable();

	locale = nlocale;
    }

    // Initialize config, PermSARegTable.

    static PermSARegTable getPermSARegTable() {

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


	if (pregtable == null) {
	    pregtable = new PermSARegTable(config);

	}

	return pregtable;
    }

    //
    // Advertiser Interface implementation.
    //

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

    public Locale getLocale() {
	return locale;

    }

    /**
     * Register a new service with the service location protocol in
     * the Advertiser's locale.
     *
     * @param URL	The service URL for the service.
     * @param serviceLocationAttributes A vector of ServiceLocationAttribute
     *				       objects describing the service.
     * @exception ServiceLocationException An exception is thrown if the
     *					  registration fails.
     * @exception IllegalArgumentException A  parameter is null or
     *					  otherwise invalid.
     *
     */

    public void register(ServiceURL URL,
			 Vector serviceLocationAttributes)
	throws ServiceLocationException {

	registerInternal(URL, serviceLocationAttributes, true);

    }

    /**
     * Deregister a service with the service location protocol.
     * This has the effect of deregistering the service from <b>every</b>
     * Locale and scope under which it was registered.
     *
     * @param URL	The service URL for the service.
     * @exception ServiceLocationException An exception is thrown if the
     *					  deregistration fails.
     */

    public void deregister(ServiceURL URL)
	throws ServiceLocationException {

	deregisterInternal(URL, null);

    }

    /**
     * Add attributes to a service URL in the locale of the Advertiser.
     *
     * Note that due to SLP v1 update semantics, the URL will be registered
     * if it is not already.
     *
     *
     * @param URL	The service URL for the service.
     * @param serviceLocationAttributes A vector of ServiceLocationAttribute
     *				       objects to add.
     * @exception ServiceLocationException An exception is thrown if the
     *					  operation fails.
     */

    public void addAttributes(ServiceURL URL,
			      Vector serviceLocationAttributes)
	throws ServiceLocationException {

	registerInternal(URL, serviceLocationAttributes, false);

    }

    /**
     * Delete the attributes from a service URL in the locale of
     * the Advertiser. The deletions are made for all scopes in
     * which the URL is registered.
     *
     *
     * @param URL	The service URL for the service.
     * @param attributeIds A vector of Strings indicating
     *			  the attributes to remove.
     * @exception ServiceLocationException An exception is thrown if the
     *					  operation fails.
     */

    public void deleteAttributes(ServiceURL URL,
				 Vector attributeIds)
	throws ServiceLocationException {

	if (attributeIds == null || attributeIds.size() <= 0) {
	    throw
		new IllegalArgumentException(
				config.formatMessage("null_or_empty_vector",
						     new Object[] {
				    "attributeIds"}));

	}

	deregisterInternal(URL, attributeIds);

    }

    //
    // Internal methods to do actual work.
    //

    /**
     * Takes care of registering a service.
     * @param URL    The service URL to register.
     * @param vAttrs A vector of ServiceLocationAttributes.
     * @param bFresh Informs whether this is to be a fresh registration or
     *               a reregistration.
     * @exception ServiceLocationException<br> If any errors occur during
     *		parsing out or on the remote agent.
     */

    private void registerInternal(ServiceURL URL,
				  Vector     vAttrs,
				  boolean    bFresh)
	throws ServiceLocationException {

	// Check parameters.

	Assert.nonNullParameter(URL, "URL");
	Assert.nonNullParameter(vAttrs,
				"serviceLocationAttributes");

	// Service agents are required to register in all the
	//  scopes they know.

	Vector vScopes = config.getSAConfiguredScopes();

	// Create registration message.

	CSrvReg srvreg =
	    new CSrvReg(bFresh,
			locale,
			URL,
			vScopes,
			vAttrs,
			null,
			null);

	// Register down the loopback.

	SrvLocMsg reply =
	    Transact.transactTCPMsg(config.getLoopback(), srvreg, true);

	// Handle any errors.

	handleError(reply);

	// Add registration for updating.

	// Create a reg to use for refreshing if the URL was permanently
	//  registered.

	if (URL.getIsPermanent()) {
	    CSrvReg srvReg =
		new CSrvReg(false,
			    locale,
			    URL,
			    vScopes,
			    new Vector(),
			    null,
			    null);

	    pregtable.reg(URL, srvReg);

	} else {
	    pregtable.dereg(URL);  // in case somebody registered permanent...

	}
    }



    /**
     * Takes care of deregistering a service or service attributes.
     *
     * @param URL The URL to deregister.
     * @param vAttrs The attribute tags, if any, to deregister.
     */

    private void deregisterInternal(ServiceURL URL,
				    Vector vAttrs)
	throws ServiceLocationException {

	Assert.nonNullParameter(URL, "URL");

	// Service agents are required to register in all the
	//  scopes they know.

	Vector vScopes = config.getSAConfiguredScopes();

	// If there are no attributes listed in the dereg, it removes the
	//  whole service.  In this case, purge it from the Permanent SA
	//  registration table.
	//

	if (vAttrs == null) {
	    pregtable.dereg(URL);

	}

	// Create deregistration message.

	CSrvDereg sdr =
	    new CSrvDereg(locale,
			  URL,
			  vScopes,
			  vAttrs,
			  null);

	// Register down the loopback.

	SrvLocMsg reply =
	    Transact.transactTCPMsg(config.getLoopback(), sdr, true);

	// Handle any errors.

	handleError(reply);
    }

    // Handle error returns.

    private void handleError(SrvLocMsg msg) throws ServiceLocationException {

	if (msg == null ||
	    ((msg.getHeader().functionCode == SrvLocHeader.SrvAck) == false)) {
	    throw new ServiceLocationException(
				ServiceLocationException.NETWORK_ERROR,
				"unexpected_ipc",
				new Object[0]);
	} else {
	    short ex =
		msg.getErrorCode();

	    if (ex != ServiceLocationException.OK) {
		throw new ServiceLocationException(ex,
						   "remote_error",
						   new Object[0]);
	    }
	}
    }

}