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