xref: /onnv-gate/usr/src/lib/libslp/javalib/com/sun/slp/SARequester.java (revision 7298:b69e27387f74)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7298SMark.J.Nelson@Sun.COM  * Common Development and Distribution License (the "License").
6*7298SMark.J.Nelson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
230Sstevel@tonic-gate  * All rights reserved.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate //  SARequester.java: Requester operations for SA.
280Sstevel@tonic-gate //  Author:           James Kempf
290Sstevel@tonic-gate //  Created On:       Thu Jan  8 14:59:41 1998
300Sstevel@tonic-gate //  Last Modified By: James Kempf
310Sstevel@tonic-gate //  Last Modified On: Thu Jan 28 15:33:33 1999
320Sstevel@tonic-gate //  Update Count:     58
330Sstevel@tonic-gate //
340Sstevel@tonic-gate 
350Sstevel@tonic-gate package com.sun.slp;
360Sstevel@tonic-gate 
370Sstevel@tonic-gate import java.io.*;
380Sstevel@tonic-gate import java.util.*;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /**
410Sstevel@tonic-gate  * The SARequester class implements the Advertiser interface.
420Sstevel@tonic-gate  * It handles the request for the API. Registration is done
430Sstevel@tonic-gate  * by calling into the loopback I/O so the SA server does
440Sstevel@tonic-gate  * the registration.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * @author Erik Guttman, James Kempf
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 
500Sstevel@tonic-gate class SARequester extends Object implements Advertiser {
510Sstevel@tonic-gate 
520Sstevel@tonic-gate     // For maintaining registrations that are LIFETIME_PERMANENT.
530Sstevel@tonic-gate 
540Sstevel@tonic-gate     private static PermSARegTable pregtable = null;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate     private static SLPConfig config = null;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate     private Locale locale;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 
SARequester(Locale nlocale)610Sstevel@tonic-gate     SARequester(Locale nlocale) {
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	Assert.nonNullParameter(nlocale, "locale");
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	// Initialize...
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	getPermSARegTable();
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	locale = nlocale;
700Sstevel@tonic-gate     }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate     // Initialize config, PermSARegTable.
730Sstevel@tonic-gate 
getPermSARegTable()740Sstevel@tonic-gate     static PermSARegTable getPermSARegTable() {
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (config == null) {
770Sstevel@tonic-gate 	    config = SLPConfig.getSLPConfig();
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (pregtable == null) {
820Sstevel@tonic-gate 	    pregtable = new PermSARegTable(config);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	return pregtable;
870Sstevel@tonic-gate     }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate     //
900Sstevel@tonic-gate     // Advertiser Interface implementation.
910Sstevel@tonic-gate     //
920Sstevel@tonic-gate 
930Sstevel@tonic-gate     /**
940Sstevel@tonic-gate      * Return the Locator's locale object. All requests are made in
950Sstevel@tonic-gate      * this locale.
960Sstevel@tonic-gate      *
970Sstevel@tonic-gate      * @return The Locale object.
980Sstevel@tonic-gate      */
990Sstevel@tonic-gate 
getLocale()1000Sstevel@tonic-gate     public Locale getLocale() {
1010Sstevel@tonic-gate 	return locale;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate     }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate     /**
1060Sstevel@tonic-gate      * Register a new service with the service location protocol in
1070Sstevel@tonic-gate      * the Advertiser's locale.
1080Sstevel@tonic-gate      *
1090Sstevel@tonic-gate      * @param URL	The service URL for the service.
1100Sstevel@tonic-gate      * @param serviceLocationAttributes A vector of ServiceLocationAttribute
1110Sstevel@tonic-gate      *				       objects describing the service.
1120Sstevel@tonic-gate      * @exception ServiceLocationException An exception is thrown if the
1130Sstevel@tonic-gate      *					  registration fails.
1140Sstevel@tonic-gate      * @exception IllegalArgumentException A  parameter is null or
1150Sstevel@tonic-gate      *					  otherwise invalid.
1160Sstevel@tonic-gate      *
1170Sstevel@tonic-gate      */
1180Sstevel@tonic-gate 
register(ServiceURL URL, Vector serviceLocationAttributes)1190Sstevel@tonic-gate     public void register(ServiceURL URL,
1200Sstevel@tonic-gate 			 Vector serviceLocationAttributes)
1210Sstevel@tonic-gate 	throws ServiceLocationException {
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	registerInternal(URL, serviceLocationAttributes, true);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate     }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate     /**
1280Sstevel@tonic-gate      * Deregister a service with the service location protocol.
1290Sstevel@tonic-gate      * This has the effect of deregistering the service from <b>every</b>
1300Sstevel@tonic-gate      * Locale and scope under which it was registered.
1310Sstevel@tonic-gate      *
1320Sstevel@tonic-gate      * @param URL	The service URL for the service.
1330Sstevel@tonic-gate      * @exception ServiceLocationException An exception is thrown if the
1340Sstevel@tonic-gate      *					  deregistration fails.
1350Sstevel@tonic-gate      */
1360Sstevel@tonic-gate 
deregister(ServiceURL URL)1370Sstevel@tonic-gate     public void deregister(ServiceURL URL)
1380Sstevel@tonic-gate 	throws ServiceLocationException {
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	deregisterInternal(URL, null);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate     }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate     /**
1450Sstevel@tonic-gate      * Add attributes to a service URL in the locale of the Advertiser.
1460Sstevel@tonic-gate      *
1470Sstevel@tonic-gate      * Note that due to SLP v1 update semantics, the URL will be registered
1480Sstevel@tonic-gate      * if it is not already.
1490Sstevel@tonic-gate      *
1500Sstevel@tonic-gate      *
1510Sstevel@tonic-gate      * @param URL	The service URL for the service.
1520Sstevel@tonic-gate      * @param serviceLocationAttributes A vector of ServiceLocationAttribute
1530Sstevel@tonic-gate      *				       objects to add.
1540Sstevel@tonic-gate      * @exception ServiceLocationException An exception is thrown if the
1550Sstevel@tonic-gate      *					  operation fails.
1560Sstevel@tonic-gate      */
1570Sstevel@tonic-gate 
addAttributes(ServiceURL URL, Vector serviceLocationAttributes)1580Sstevel@tonic-gate     public void addAttributes(ServiceURL URL,
1590Sstevel@tonic-gate 			      Vector serviceLocationAttributes)
1600Sstevel@tonic-gate 	throws ServiceLocationException {
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	registerInternal(URL, serviceLocationAttributes, false);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate     }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate     /**
1670Sstevel@tonic-gate      * Delete the attributes from a service URL in the locale of
1680Sstevel@tonic-gate      * the Advertiser. The deletions are made for all scopes in
1690Sstevel@tonic-gate      * which the URL is registered.
1700Sstevel@tonic-gate      *
1710Sstevel@tonic-gate      *
1720Sstevel@tonic-gate      * @param URL	The service URL for the service.
1730Sstevel@tonic-gate      * @param attributeIds A vector of Strings indicating
1740Sstevel@tonic-gate      *			  the attributes to remove.
1750Sstevel@tonic-gate      * @exception ServiceLocationException An exception is thrown if the
1760Sstevel@tonic-gate      *					  operation fails.
1770Sstevel@tonic-gate      */
1780Sstevel@tonic-gate 
deleteAttributes(ServiceURL URL, Vector attributeIds)1790Sstevel@tonic-gate     public void deleteAttributes(ServiceURL URL,
1800Sstevel@tonic-gate 				 Vector attributeIds)
1810Sstevel@tonic-gate 	throws ServiceLocationException {
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (attributeIds == null || attributeIds.size() <= 0) {
1840Sstevel@tonic-gate 	    throw
1850Sstevel@tonic-gate 		new IllegalArgumentException(
1860Sstevel@tonic-gate 				config.formatMessage("null_or_empty_vector",
1870Sstevel@tonic-gate 						     new Object[] {
1880Sstevel@tonic-gate 				    "attributeIds"}));
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	deregisterInternal(URL, attributeIds);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate     }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate     //
1970Sstevel@tonic-gate     // Internal methods to do actual work.
1980Sstevel@tonic-gate     //
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate     /**
2010Sstevel@tonic-gate      * Takes care of registering a service.
2020Sstevel@tonic-gate      * @param URL    The service URL to register.
2030Sstevel@tonic-gate      * @param vAttrs A vector of ServiceLocationAttributes.
2040Sstevel@tonic-gate      * @param bFresh Informs whether this is to be a fresh registration or
2050Sstevel@tonic-gate      *               a reregistration.
2060Sstevel@tonic-gate      * @exception ServiceLocationException<br> If any errors occur during
2070Sstevel@tonic-gate      *		parsing out or on the remote agent.
2080Sstevel@tonic-gate      */
2090Sstevel@tonic-gate 
registerInternal(ServiceURL URL, Vector vAttrs, boolean bFresh)2100Sstevel@tonic-gate     private void registerInternal(ServiceURL URL,
2110Sstevel@tonic-gate 				  Vector     vAttrs,
2120Sstevel@tonic-gate 				  boolean    bFresh)
2130Sstevel@tonic-gate 	throws ServiceLocationException {
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	// Check parameters.
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	Assert.nonNullParameter(URL, "URL");
2180Sstevel@tonic-gate 	Assert.nonNullParameter(vAttrs,
2190Sstevel@tonic-gate 				"serviceLocationAttributes");
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	// Service agents are required to register in all the
2220Sstevel@tonic-gate 	//  scopes they know.
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	Vector vScopes = config.getSAConfiguredScopes();
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	// Create registration message.
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	CSrvReg srvreg =
2290Sstevel@tonic-gate 	    new CSrvReg(bFresh,
2300Sstevel@tonic-gate 			locale,
2310Sstevel@tonic-gate 			URL,
2320Sstevel@tonic-gate 			vScopes,
2330Sstevel@tonic-gate 			vAttrs,
2340Sstevel@tonic-gate 			null,
2350Sstevel@tonic-gate 			null);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	// Register down the loopback.
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	SrvLocMsg reply =
2400Sstevel@tonic-gate 	    Transact.transactTCPMsg(config.getLoopback(), srvreg, true);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	// Handle any errors.
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	handleError(reply);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	// Add registration for updating.
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	// Create a reg to use for refreshing if the URL was permanently
2490Sstevel@tonic-gate 	//  registered.
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (URL.getIsPermanent()) {
2520Sstevel@tonic-gate 	    CSrvReg srvReg =
2530Sstevel@tonic-gate 		new CSrvReg(false,
2540Sstevel@tonic-gate 			    locale,
2550Sstevel@tonic-gate 			    URL,
2560Sstevel@tonic-gate 			    vScopes,
2570Sstevel@tonic-gate 			    new Vector(),
2580Sstevel@tonic-gate 			    null,
2590Sstevel@tonic-gate 			    null);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	    pregtable.reg(URL, srvReg);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	} else {
2640Sstevel@tonic-gate 	    pregtable.dereg(URL);  // in case somebody registered permanent...
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate     }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate     /**
2720Sstevel@tonic-gate      * Takes care of deregistering a service or service attributes.
2730Sstevel@tonic-gate      *
2740Sstevel@tonic-gate      * @param URL The URL to deregister.
2750Sstevel@tonic-gate      * @param vAttrs The attribute tags, if any, to deregister.
2760Sstevel@tonic-gate      */
2770Sstevel@tonic-gate 
deregisterInternal(ServiceURL URL, Vector vAttrs)2780Sstevel@tonic-gate     private void deregisterInternal(ServiceURL URL,
2790Sstevel@tonic-gate 				    Vector vAttrs)
2800Sstevel@tonic-gate 	throws ServiceLocationException {
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	Assert.nonNullParameter(URL, "URL");
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	// Service agents are required to register in all the
2850Sstevel@tonic-gate 	//  scopes they know.
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	Vector vScopes = config.getSAConfiguredScopes();
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	// If there are no attributes listed in the dereg, it removes the
2900Sstevel@tonic-gate 	//  whole service.  In this case, purge it from the Permanent SA
2910Sstevel@tonic-gate 	//  registration table.
2920Sstevel@tonic-gate 	//
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if (vAttrs == null) {
2950Sstevel@tonic-gate 	    pregtable.dereg(URL);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	// Create deregistration message.
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	CSrvDereg sdr =
3020Sstevel@tonic-gate 	    new CSrvDereg(locale,
3030Sstevel@tonic-gate 			  URL,
3040Sstevel@tonic-gate 			  vScopes,
3050Sstevel@tonic-gate 			  vAttrs,
3060Sstevel@tonic-gate 			  null);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	// Register down the loopback.
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	SrvLocMsg reply =
3110Sstevel@tonic-gate 	    Transact.transactTCPMsg(config.getLoopback(), sdr, true);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	// Handle any errors.
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	handleError(reply);
3160Sstevel@tonic-gate     }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate     // Handle error returns.
3190Sstevel@tonic-gate 
handleError(SrvLocMsg msg)3200Sstevel@tonic-gate     private void handleError(SrvLocMsg msg) throws ServiceLocationException {
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (msg == null ||
3230Sstevel@tonic-gate 	    ((msg.getHeader().functionCode == SrvLocHeader.SrvAck) == false)) {
3240Sstevel@tonic-gate 	    throw new ServiceLocationException(
3250Sstevel@tonic-gate 				ServiceLocationException.NETWORK_ERROR,
3260Sstevel@tonic-gate 				"unexpected_ipc",
3270Sstevel@tonic-gate 				new Object[0]);
3280Sstevel@tonic-gate 	} else {
3290Sstevel@tonic-gate 	    short ex =
3300Sstevel@tonic-gate 		msg.getErrorCode();
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	    if (ex != ServiceLocationException.OK) {
3330Sstevel@tonic-gate 		throw new ServiceLocationException(ex,
3340Sstevel@tonic-gate 						   "remote_error",
3350Sstevel@tonic-gate 						   new Object[0]);
3360Sstevel@tonic-gate 	    }
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate     }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate }
341