17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9a70fc3bSMark J. Nelson  * Common Development and Distribution License (the "License").
6*9a70fc3bSMark J. Nelson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
237c478bd9Sstevel@tonic-gate  * All rights reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate //  SARequester.java: Requester operations for SA.
287c478bd9Sstevel@tonic-gate //  Author:           James Kempf
297c478bd9Sstevel@tonic-gate //  Created On:       Thu Jan  8 14:59:41 1998
307c478bd9Sstevel@tonic-gate //  Last Modified By: James Kempf
317c478bd9Sstevel@tonic-gate //  Last Modified On: Thu Jan 28 15:33:33 1999
327c478bd9Sstevel@tonic-gate //  Update Count:     58
337c478bd9Sstevel@tonic-gate //
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate package com.sun.slp;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate import java.io.*;
387c478bd9Sstevel@tonic-gate import java.util.*;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /**
417c478bd9Sstevel@tonic-gate  * The SARequester class implements the Advertiser interface.
427c478bd9Sstevel@tonic-gate  * It handles the request for the API. Registration is done
437c478bd9Sstevel@tonic-gate  * by calling into the loopback I/O so the SA server does
447c478bd9Sstevel@tonic-gate  * the registration.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * @author Erik Guttman, James Kempf
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate class SARequester extends Object implements Advertiser {
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate     // For maintaining registrations that are LIFETIME_PERMANENT.
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate     private static PermSARegTable pregtable = null;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate     private static SLPConfig config = null;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate     private Locale locale;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
SARequester(Locale nlocale)617c478bd9Sstevel@tonic-gate     SARequester(Locale nlocale) {
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	Assert.nonNullParameter(nlocale, "locale");
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	// Initialize...
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	getPermSARegTable();
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	locale = nlocale;
707c478bd9Sstevel@tonic-gate     }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate     // Initialize config, PermSARegTable.
737c478bd9Sstevel@tonic-gate 
getPermSARegTable()747c478bd9Sstevel@tonic-gate     static PermSARegTable getPermSARegTable() {
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (config == null) {
777c478bd9Sstevel@tonic-gate 	    config = SLPConfig.getSLPConfig();
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (pregtable == null) {
827c478bd9Sstevel@tonic-gate 	    pregtable = new PermSARegTable(config);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	return pregtable;
877c478bd9Sstevel@tonic-gate     }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate     //
907c478bd9Sstevel@tonic-gate     // Advertiser Interface implementation.
917c478bd9Sstevel@tonic-gate     //
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate     /**
947c478bd9Sstevel@tonic-gate      * Return the Locator's locale object. All requests are made in
957c478bd9Sstevel@tonic-gate      * this locale.
967c478bd9Sstevel@tonic-gate      *
977c478bd9Sstevel@tonic-gate      * @return The Locale object.
987c478bd9Sstevel@tonic-gate      */
997c478bd9Sstevel@tonic-gate 
getLocale()1007c478bd9Sstevel@tonic-gate     public Locale getLocale() {
1017c478bd9Sstevel@tonic-gate 	return locale;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate     }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate     /**
1067c478bd9Sstevel@tonic-gate      * Register a new service with the service location protocol in
1077c478bd9Sstevel@tonic-gate      * the Advertiser's locale.
1087c478bd9Sstevel@tonic-gate      *
1097c478bd9Sstevel@tonic-gate      * @param URL	The service URL for the service.
1107c478bd9Sstevel@tonic-gate      * @param serviceLocationAttributes A vector of ServiceLocationAttribute
1117c478bd9Sstevel@tonic-gate      *				       objects describing the service.
1127c478bd9Sstevel@tonic-gate      * @exception ServiceLocationException An exception is thrown if the
1137c478bd9Sstevel@tonic-gate      *					  registration fails.
1147c478bd9Sstevel@tonic-gate      * @exception IllegalArgumentException A  parameter is null or
1157c478bd9Sstevel@tonic-gate      *					  otherwise invalid.
1167c478bd9Sstevel@tonic-gate      *
1177c478bd9Sstevel@tonic-gate      */
1187c478bd9Sstevel@tonic-gate 
register(ServiceURL URL, Vector serviceLocationAttributes)1197c478bd9Sstevel@tonic-gate     public void register(ServiceURL URL,
1207c478bd9Sstevel@tonic-gate 			 Vector serviceLocationAttributes)
1217c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	registerInternal(URL, serviceLocationAttributes, true);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate     }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate     /**
1287c478bd9Sstevel@tonic-gate      * Deregister a service with the service location protocol.
1297c478bd9Sstevel@tonic-gate      * This has the effect of deregistering the service from <b>every</b>
1307c478bd9Sstevel@tonic-gate      * Locale and scope under which it was registered.
1317c478bd9Sstevel@tonic-gate      *
1327c478bd9Sstevel@tonic-gate      * @param URL	The service URL for the service.
1337c478bd9Sstevel@tonic-gate      * @exception ServiceLocationException An exception is thrown if the
1347c478bd9Sstevel@tonic-gate      *					  deregistration fails.
1357c478bd9Sstevel@tonic-gate      */
1367c478bd9Sstevel@tonic-gate 
deregister(ServiceURL URL)1377c478bd9Sstevel@tonic-gate     public void deregister(ServiceURL URL)
1387c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	deregisterInternal(URL, null);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate     }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate     /**
1457c478bd9Sstevel@tonic-gate      * Add attributes to a service URL in the locale of the Advertiser.
1467c478bd9Sstevel@tonic-gate      *
1477c478bd9Sstevel@tonic-gate      * Note that due to SLP v1 update semantics, the URL will be registered
1487c478bd9Sstevel@tonic-gate      * if it is not already.
1497c478bd9Sstevel@tonic-gate      *
1507c478bd9Sstevel@tonic-gate      *
1517c478bd9Sstevel@tonic-gate      * @param URL	The service URL for the service.
1527c478bd9Sstevel@tonic-gate      * @param serviceLocationAttributes A vector of ServiceLocationAttribute
1537c478bd9Sstevel@tonic-gate      *				       objects to add.
1547c478bd9Sstevel@tonic-gate      * @exception ServiceLocationException An exception is thrown if the
1557c478bd9Sstevel@tonic-gate      *					  operation fails.
1567c478bd9Sstevel@tonic-gate      */
1577c478bd9Sstevel@tonic-gate 
addAttributes(ServiceURL URL, Vector serviceLocationAttributes)1587c478bd9Sstevel@tonic-gate     public void addAttributes(ServiceURL URL,
1597c478bd9Sstevel@tonic-gate 			      Vector serviceLocationAttributes)
1607c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	registerInternal(URL, serviceLocationAttributes, false);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate     }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate     /**
1677c478bd9Sstevel@tonic-gate      * Delete the attributes from a service URL in the locale of
1687c478bd9Sstevel@tonic-gate      * the Advertiser. The deletions are made for all scopes in
1697c478bd9Sstevel@tonic-gate      * which the URL is registered.
1707c478bd9Sstevel@tonic-gate      *
1717c478bd9Sstevel@tonic-gate      *
1727c478bd9Sstevel@tonic-gate      * @param URL	The service URL for the service.
1737c478bd9Sstevel@tonic-gate      * @param attributeIds A vector of Strings indicating
1747c478bd9Sstevel@tonic-gate      *			  the attributes to remove.
1757c478bd9Sstevel@tonic-gate      * @exception ServiceLocationException An exception is thrown if the
1767c478bd9Sstevel@tonic-gate      *					  operation fails.
1777c478bd9Sstevel@tonic-gate      */
1787c478bd9Sstevel@tonic-gate 
deleteAttributes(ServiceURL URL, Vector attributeIds)1797c478bd9Sstevel@tonic-gate     public void deleteAttributes(ServiceURL URL,
1807c478bd9Sstevel@tonic-gate 				 Vector attributeIds)
1817c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (attributeIds == null || attributeIds.size() <= 0) {
1847c478bd9Sstevel@tonic-gate 	    throw
1857c478bd9Sstevel@tonic-gate 		new IllegalArgumentException(
1867c478bd9Sstevel@tonic-gate 				config.formatMessage("null_or_empty_vector",
1877c478bd9Sstevel@tonic-gate 						     new Object[] {
1887c478bd9Sstevel@tonic-gate 				    "attributeIds"}));
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	deregisterInternal(URL, attributeIds);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate     }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate     //
1977c478bd9Sstevel@tonic-gate     // Internal methods to do actual work.
1987c478bd9Sstevel@tonic-gate     //
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate     /**
2017c478bd9Sstevel@tonic-gate      * Takes care of registering a service.
2027c478bd9Sstevel@tonic-gate      * @param URL    The service URL to register.
2037c478bd9Sstevel@tonic-gate      * @param vAttrs A vector of ServiceLocationAttributes.
2047c478bd9Sstevel@tonic-gate      * @param bFresh Informs whether this is to be a fresh registration or
2057c478bd9Sstevel@tonic-gate      *               a reregistration.
2067c478bd9Sstevel@tonic-gate      * @exception ServiceLocationException<br> If any errors occur during
2077c478bd9Sstevel@tonic-gate      *		parsing out or on the remote agent.
2087c478bd9Sstevel@tonic-gate      */
2097c478bd9Sstevel@tonic-gate 
registerInternal(ServiceURL URL, Vector vAttrs, boolean bFresh)2107c478bd9Sstevel@tonic-gate     private void registerInternal(ServiceURL URL,
2117c478bd9Sstevel@tonic-gate 				  Vector     vAttrs,
2127c478bd9Sstevel@tonic-gate 				  boolean    bFresh)
2137c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	// Check parameters.
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	Assert.nonNullParameter(URL, "URL");
2187c478bd9Sstevel@tonic-gate 	Assert.nonNullParameter(vAttrs,
2197c478bd9Sstevel@tonic-gate 				"serviceLocationAttributes");
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	// Service agents are required to register in all the
2227c478bd9Sstevel@tonic-gate 	//  scopes they know.
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	Vector vScopes = config.getSAConfiguredScopes();
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	// Create registration message.
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	CSrvReg srvreg =
2297c478bd9Sstevel@tonic-gate 	    new CSrvReg(bFresh,
2307c478bd9Sstevel@tonic-gate 			locale,
2317c478bd9Sstevel@tonic-gate 			URL,
2327c478bd9Sstevel@tonic-gate 			vScopes,
2337c478bd9Sstevel@tonic-gate 			vAttrs,
2347c478bd9Sstevel@tonic-gate 			null,
2357c478bd9Sstevel@tonic-gate 			null);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	// Register down the loopback.
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	SrvLocMsg reply =
2407c478bd9Sstevel@tonic-gate 	    Transact.transactTCPMsg(config.getLoopback(), srvreg, true);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	// Handle any errors.
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	handleError(reply);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	// Add registration for updating.
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	// Create a reg to use for refreshing if the URL was permanently
2497c478bd9Sstevel@tonic-gate 	//  registered.
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (URL.getIsPermanent()) {
2527c478bd9Sstevel@tonic-gate 	    CSrvReg srvReg =
2537c478bd9Sstevel@tonic-gate 		new CSrvReg(false,
2547c478bd9Sstevel@tonic-gate 			    locale,
2557c478bd9Sstevel@tonic-gate 			    URL,
2567c478bd9Sstevel@tonic-gate 			    vScopes,
2577c478bd9Sstevel@tonic-gate 			    new Vector(),
2587c478bd9Sstevel@tonic-gate 			    null,
2597c478bd9Sstevel@tonic-gate 			    null);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	    pregtable.reg(URL, srvReg);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	} else {
2647c478bd9Sstevel@tonic-gate 	    pregtable.dereg(URL);  // in case somebody registered permanent...
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate     }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate     /**
2727c478bd9Sstevel@tonic-gate      * Takes care of deregistering a service or service attributes.
2737c478bd9Sstevel@tonic-gate      *
2747c478bd9Sstevel@tonic-gate      * @param URL The URL to deregister.
2757c478bd9Sstevel@tonic-gate      * @param vAttrs The attribute tags, if any, to deregister.
2767c478bd9Sstevel@tonic-gate      */
2777c478bd9Sstevel@tonic-gate 
deregisterInternal(ServiceURL URL, Vector vAttrs)2787c478bd9Sstevel@tonic-gate     private void deregisterInternal(ServiceURL URL,
2797c478bd9Sstevel@tonic-gate 				    Vector vAttrs)
2807c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	Assert.nonNullParameter(URL, "URL");
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	// Service agents are required to register in all the
2857c478bd9Sstevel@tonic-gate 	//  scopes they know.
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	Vector vScopes = config.getSAConfiguredScopes();
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	// If there are no attributes listed in the dereg, it removes the
2907c478bd9Sstevel@tonic-gate 	//  whole service.  In this case, purge it from the Permanent SA
2917c478bd9Sstevel@tonic-gate 	//  registration table.
2927c478bd9Sstevel@tonic-gate 	//
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (vAttrs == null) {
2957c478bd9Sstevel@tonic-gate 	    pregtable.dereg(URL);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	// Create deregistration message.
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	CSrvDereg sdr =
3027c478bd9Sstevel@tonic-gate 	    new CSrvDereg(locale,
3037c478bd9Sstevel@tonic-gate 			  URL,
3047c478bd9Sstevel@tonic-gate 			  vScopes,
3057c478bd9Sstevel@tonic-gate 			  vAttrs,
3067c478bd9Sstevel@tonic-gate 			  null);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	// Register down the loopback.
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	SrvLocMsg reply =
3117c478bd9Sstevel@tonic-gate 	    Transact.transactTCPMsg(config.getLoopback(), sdr, true);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	// Handle any errors.
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	handleError(reply);
3167c478bd9Sstevel@tonic-gate     }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate     // Handle error returns.
3197c478bd9Sstevel@tonic-gate 
handleError(SrvLocMsg msg)3207c478bd9Sstevel@tonic-gate     private void handleError(SrvLocMsg msg) throws ServiceLocationException {
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (msg == null ||
3237c478bd9Sstevel@tonic-gate 	    ((msg.getHeader().functionCode == SrvLocHeader.SrvAck) == false)) {
3247c478bd9Sstevel@tonic-gate 	    throw new ServiceLocationException(
3257c478bd9Sstevel@tonic-gate 				ServiceLocationException.NETWORK_ERROR,
3267c478bd9Sstevel@tonic-gate 				"unexpected_ipc",
3277c478bd9Sstevel@tonic-gate 				new Object[0]);
3287c478bd9Sstevel@tonic-gate 	} else {
3297c478bd9Sstevel@tonic-gate 	    short ex =
3307c478bd9Sstevel@tonic-gate 		msg.getErrorCode();
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	    if (ex != ServiceLocationException.OK) {
3337c478bd9Sstevel@tonic-gate 		throw new ServiceLocationException(ex,
3347c478bd9Sstevel@tonic-gate 						   "remote_error",
3357c478bd9Sstevel@tonic-gate 						   new Object[0]);
3367c478bd9Sstevel@tonic-gate 	    }
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate     }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate }
341