xref: /illumos-gate/usr/src/lib/libslp/javalib/com/sun/slp/SARequester.java (revision a6e6969cf9cfe2070eae4cd6071f76b0fa4f539f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 1999 by Sun Microsystems, Inc.
26  * All rights reserved.
27  *
28  */
29 
30 //  SCCS Status:      %W%	%G%
31 //  SARequester.java: Requester operations for SA.
32 //  Author:           James Kempf
33 //  Created On:       Thu Jan  8 14:59:41 1998
34 //  Last Modified By: James Kempf
35 //  Last Modified On: Thu Jan 28 15:33:33 1999
36 //  Update Count:     58
37 //
38 
39 package com.sun.slp;
40 
41 import java.io.*;
42 import java.util.*;
43 
44 /**
45  * The SARequester class implements the Advertiser interface.
46  * It handles the request for the API. Registration is done
47  * by calling into the loopback I/O so the SA server does
48  * the registration.
49  *
50  * @version %R%.%L% %D%
51  * @author Erik Guttman, James Kempf
52  */
53 
54 
55 class SARequester extends Object implements Advertiser {
56 
57     // For maintaining registrations that are LIFETIME_PERMANENT.
58 
59     private static PermSARegTable pregtable = null;
60 
61     private static SLPConfig config = null;
62 
63     private Locale locale;
64 
65 
66     SARequester(Locale nlocale) {
67 
68 	Assert.nonNullParameter(nlocale, "locale");
69 
70 	// Initialize...
71 
72 	getPermSARegTable();
73 
74 	locale = nlocale;
75     }
76 
77     // Initialize config, PermSARegTable.
78 
79     static PermSARegTable getPermSARegTable() {
80 
81 	if (config == null) {
82 	    config = SLPConfig.getSLPConfig();
83 	}
84 
85 
86 	if (pregtable == null) {
87 	    pregtable = new PermSARegTable(config);
88 
89 	}
90 
91 	return pregtable;
92     }
93 
94     //
95     // Advertiser Interface implementation.
96     //
97 
98     /**
99      * Return the Locator's locale object. All requests are made in
100      * this locale.
101      *
102      * @return The Locale object.
103      */
104 
105     public Locale getLocale() {
106 	return locale;
107 
108     }
109 
110     /**
111      * Register a new service with the service location protocol in
112      * the Advertiser's locale.
113      *
114      * @param URL	The service URL for the service.
115      * @param serviceLocationAttributes A vector of ServiceLocationAttribute
116      *				       objects describing the service.
117      * @exception ServiceLocationException An exception is thrown if the
118      *					  registration fails.
119      * @exception IllegalArgumentException A  parameter is null or
120      *					  otherwise invalid.
121      *
122      */
123 
124     public void register(ServiceURL URL,
125 			 Vector serviceLocationAttributes)
126 	throws ServiceLocationException {
127 
128 	registerInternal(URL, serviceLocationAttributes, true);
129 
130     }
131 
132     /**
133      * Deregister a service with the service location protocol.
134      * This has the effect of deregistering the service from <b>every</b>
135      * Locale and scope under which it was registered.
136      *
137      * @param URL	The service URL for the service.
138      * @exception ServiceLocationException An exception is thrown if the
139      *					  deregistration fails.
140      */
141 
142     public void deregister(ServiceURL URL)
143 	throws ServiceLocationException {
144 
145 	deregisterInternal(URL, null);
146 
147     }
148 
149     /**
150      * Add attributes to a service URL in the locale of the Advertiser.
151      *
152      * Note that due to SLP v1 update semantics, the URL will be registered
153      * if it is not already.
154      *
155      *
156      * @param URL	The service URL for the service.
157      * @param serviceLocationAttributes A vector of ServiceLocationAttribute
158      *				       objects to add.
159      * @exception ServiceLocationException An exception is thrown if the
160      *					  operation fails.
161      */
162 
163     public void addAttributes(ServiceURL URL,
164 			      Vector serviceLocationAttributes)
165 	throws ServiceLocationException {
166 
167 	registerInternal(URL, serviceLocationAttributes, false);
168 
169     }
170 
171     /**
172      * Delete the attributes from a service URL in the locale of
173      * the Advertiser. The deletions are made for all scopes in
174      * which the URL is registered.
175      *
176      *
177      * @param URL	The service URL for the service.
178      * @param attributeIds A vector of Strings indicating
179      *			  the attributes to remove.
180      * @exception ServiceLocationException An exception is thrown if the
181      *					  operation fails.
182      */
183 
184     public void deleteAttributes(ServiceURL URL,
185 				 Vector attributeIds)
186 	throws ServiceLocationException {
187 
188 	if (attributeIds == null || attributeIds.size() <= 0) {
189 	    throw
190 		new IllegalArgumentException(
191 				config.formatMessage("null_or_empty_vector",
192 						     new Object[] {
193 				    "attributeIds"}));
194 
195 	}
196 
197 	deregisterInternal(URL, attributeIds);
198 
199     }
200 
201     //
202     // Internal methods to do actual work.
203     //
204 
205     /**
206      * Takes care of registering a service.
207      * @param URL    The service URL to register.
208      * @param vAttrs A vector of ServiceLocationAttributes.
209      * @param bFresh Informs whether this is to be a fresh registration or
210      *               a reregistration.
211      * @exception ServiceLocationException<br> If any errors occur during
212      *		parsing out or on the remote agent.
213      */
214 
215     private void registerInternal(ServiceURL URL,
216 				  Vector     vAttrs,
217 				  boolean    bFresh)
218 	throws ServiceLocationException {
219 
220 	// Check parameters.
221 
222 	Assert.nonNullParameter(URL, "URL");
223 	Assert.nonNullParameter(vAttrs,
224 				"serviceLocationAttributes");
225 
226 	// Service agents are required to register in all the
227 	//  scopes they know.
228 
229 	Vector vScopes = config.getSAConfiguredScopes();
230 
231 	// Create registration message.
232 
233 	CSrvReg srvreg =
234 	    new CSrvReg(bFresh,
235 			locale,
236 			URL,
237 			vScopes,
238 			vAttrs,
239 			null,
240 			null);
241 
242 	// Register down the loopback.
243 
244 	SrvLocMsg reply =
245 	    Transact.transactTCPMsg(config.getLoopback(), srvreg, true);
246 
247 	// Handle any errors.
248 
249 	handleError(reply);
250 
251 	// Add registration for updating.
252 
253 	// Create a reg to use for refreshing if the URL was permanently
254 	//  registered.
255 
256 	if (URL.getIsPermanent()) {
257 	    CSrvReg srvReg =
258 		new CSrvReg(false,
259 			    locale,
260 			    URL,
261 			    vScopes,
262 			    new Vector(),
263 			    null,
264 			    null);
265 
266 	    pregtable.reg(URL, srvReg);
267 
268 	} else {
269 	    pregtable.dereg(URL);  // in case somebody registered permanent...
270 
271 	}
272     }
273 
274 
275 
276     /**
277      * Takes care of deregistering a service or service attributes.
278      *
279      * @param URL The URL to deregister.
280      * @param vAttrs The attribute tags, if any, to deregister.
281      */
282 
283     private void deregisterInternal(ServiceURL URL,
284 				    Vector vAttrs)
285 	throws ServiceLocationException {
286 
287 	Assert.nonNullParameter(URL, "URL");
288 
289 	// Service agents are required to register in all the
290 	//  scopes they know.
291 
292 	Vector vScopes = config.getSAConfiguredScopes();
293 
294 	// If there are no attributes listed in the dereg, it removes the
295 	//  whole service.  In this case, purge it from the Permanent SA
296 	//  registration table.
297 	//
298 
299 	if (vAttrs == null) {
300 	    pregtable.dereg(URL);
301 
302 	}
303 
304 	// Create deregistration message.
305 
306 	CSrvDereg sdr =
307 	    new CSrvDereg(locale,
308 			  URL,
309 			  vScopes,
310 			  vAttrs,
311 			  null);
312 
313 	// Register down the loopback.
314 
315 	SrvLocMsg reply =
316 	    Transact.transactTCPMsg(config.getLoopback(), sdr, true);
317 
318 	// Handle any errors.
319 
320 	handleError(reply);
321     }
322 
323     // Handle error returns.
324 
325     private void handleError(SrvLocMsg msg) throws ServiceLocationException {
326 
327 	if (msg == null ||
328 	    ((msg.getHeader().functionCode == SrvLocHeader.SrvAck) == false)) {
329 	    throw new ServiceLocationException(
330 				ServiceLocationException.NETWORK_ERROR,
331 				"unexpected_ipc",
332 				new Object[0]);
333 	} else {
334 	    short ex =
335 		msg.getErrorCode();
336 
337 	    if (ex != ServiceLocationException.OK) {
338 		throw new ServiceLocationException(ex,
339 						   "remote_error",
340 						   new Object[0]);
341 	    }
342 	}
343     }
344 
345 }
346