2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2001,2003 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A *
2N/A */
2N/A
2N/A// SunDATable.java: A DATable implementation that uses the IPC connection.
2N/A// Author: James Kempf
2N/A// Created On: Mon May 11 15:00:23 1998
2N/A// Last Modified By: James Kempf
2N/A// Last Modified On: Thu Mar 11 15:00:58 1999
2N/A// Update Count: 71
2N/A//
2N/A
2N/Apackage com.sun.slp;
2N/A
2N/Aimport java.util.*;
2N/Aimport java.net.*;
2N/Aimport java.io.*;
2N/A
2N/A/**
2N/A * The SunDATable class uses the IPC connection to obtain DA information
2N/A * from the SA server. By convention, the SA server answers service
2N/A * requests for the service type "directory-agent.sun" and including
2N/A * a filter for the scopes formatted as:
2N/A *
2N/A * "(&(|(scopes=<scope1>)(scopes=<scope2>)(scopes=<scope3>)...)(version=2))"
2N/A *
2N/A * with a collection of URLs that fit the request. The scope of the
2N/A * request is the hostname of the local machine, which will not be
2N/A * forwarded to any DAs. The URLs contain the
2N/A * DA IP address in the host field and a list of scopes in the URL
2N/A * part in the form of an attribute value assignment, i.e.:
2N/A *
2N/A * service:directory-agent.sun:// 199.200.200.5/scopes=eng, corp, freeb
2N/A *
2N/A * The DA/scope table is initially obtained for all scopes in the
2N/A * useScopes list, then refreshed periodically when the
2N/A * time stamp runs out. The time stamp is determined as the minimum
2N/A * expiration time of the service URLs.
2N/A *
2N/A * @author James Kempf
2N/A */
2N/A
2N/Aclass SunDATable extends DATable {
2N/A
2N/A // The scopes identifier.
2N/A
2N/A final static String SCOPES_ID = "424242SUN-TABLE-SCOPES424242";
2N/A
2N/A // DA version number.
2N/A
2N/A final static String VERSION_ID = "424242SUN-TABLE-VERSION424242";
2N/A
2N/A // The scopes which reside on the SA only.
2N/A
2N/A private Vector saOnlyScopes = new Vector();
2N/A
2N/A // The cached vector of DA equivalence classes.
2N/A
2N/A private Vector cache = null;
2N/A
2N/A // The time when the cache should be refreshed.
2N/A
2N/A private long timeStamp = -1;
2N/A
2N/A /**
2N/A * Construct a DATable. We get a cached table of accessable
2N/A * DAs and scopes and the SA scope names to use for querying.
2N/A */
2N/A
2N/A SunDATable() throws ServiceLocationException {
2N/A
2N/A // Remove the common scopes from the SA scopes. This will leave
2N/A // the private, SA only scopes.
2N/A
2N/A saOnlyScopes = conf.getSAOnlyScopes();
2N/A
2N/A Assert.slpassert(saOnlyScopes.size() > 0,
2N/A "no_sa_scopes",
2N/A new Object[0]);
2N/A
2N/A // Initialize the cache. We want the scopes that can be dynamically
2N/A // discovered. If we have been configured with scopes, then
2N/A // it will be those. If not, then we want whatever we can discovery.
2N/A // we only want the default version for client side.
2N/A
2N/A cache = getWireTable(conf.getConfiguredScopes(), Defaults.version);
2N/A
2N/A }
2N/A
2N/A /**
2N/A * Return a hashtable of DA equivalence classes and multicast
2N/A * scopes. Multicast scopes are stored in the special hashtable
2N/A * key MULTICAST_KEY. Unicast DA equivalence classes are stored
2N/A * under the key UNICAST_KEY.
2N/A *
2N/A * @param scopes Scope list for DAs needed.
2N/A * @return Hashtable with DA addresses as keys and scopes to contact
2N/A * them with as values. Any scopes not associated with a
2N/A * DA come back stored under the key MULTICAST_KEY.
2N/A * Unicast DA equivalence classes are stored
2N/A * under the key UNICAST_KEY.
2N/A */
2N/A
2N/A synchronized Hashtable findDAScopes(Vector scopes)
2N/A throws ServiceLocationException {
2N/A
2N/A Hashtable ret = new Hashtable();
2N/A
2N/A Vector equivClasses = null;
2N/A
2N/A // Refresh the local cache if necessary.
2N/A
2N/A if (timeStamp <= System.currentTimeMillis()) {
2N/A Vector useScopes = conf.getConfiguredScopes();
2N/A
2N/A cache = getWireTable(useScopes, Defaults.version);
2N/A
2N/A }
2N/A
2N/A equivClasses = (Vector)cache.clone();
2N/A int i;
2N/A
2N/A // Sort through the local cache, matching against the input parameter.
2N/A // Collect multicast scopes.
2N/A
2N/A Vector multicastScopes = (Vector)scopes.clone();
2N/A
2N/A for (i = 0; i < equivClasses.size(); i++) {
2N/A DARecord rec = (DARecord)equivClasses.elementAt(i);
2N/A Vector daScopes = (Vector)rec.scopes.clone();
2N/A
2N/A // Filter multicast scopes first. Remove any from the multicast
2N/A // scope list that are in daScopes.
2N/A
2N/A filterScopes(multicastScopes, daScopes, true);
2N/A
2N/A // Now filter daScopes. Remove any from the daScopes that are
2N/A // not in the input scopes.
2N/A
2N/A filterScopes(daScopes, scopes, false);
2N/A
2N/A // Remove this record if there are none left.
2N/A
2N/A if (daScopes.size() <= 0) {
2N/A /*
2N/A * Must decrement the index 'i' otherwise the next iteration
2N/A * around the loop will miss the element immediately after
2N/A * the element removed.
2N/A *
2N/A * WARNING: Do not use 'i' again until the loop has
2N/A * iterated as it may, after decrementing,
2N/A * be negative.
2N/A */
2N/A equivClasses.removeElementAt(i);
2N/A i--;
2N/A continue;
2N/A }
2N/A }
2N/A
2N/A // Install the unicast and multicast scopes if any.
2N/A
2N/A if (multicastScopes.size() > 0) {
2N/A ret.put(MULTICAST_KEY, multicastScopes);
2N/A
2N/A }
2N/A
2N/A if (equivClasses.size() > 0) {
2N/A ret.put(UNICAST_KEY, equivClasses);
2N/A
2N/A }
2N/A
2N/A return ret;
2N/A
2N/A }
2N/A
2N/A /**
2N/A * Remove a DA by address. We only remove it from the wire table
2N/A * so if it's down temporarily, we'll get it back again.
2N/A *
2N/A * @param address The host address of the DA.
2N/A * @param scopes The scopes.
2N/A * @return True if removed, false if not.
2N/A */
2N/A
2N/A boolean removeDA(InetAddress address, Vector scopes) {
2N/A
2N/A // Sort through the table of equivalence classes in cache.
2N/A
2N/A boolean foundit = false;
2N/A int i;
2N/A
2N/A for (i = 0; i < cache.size(); i++) {
2N/A DARecord rec = (DARecord)cache.elementAt(i);
2N/A Vector daAddresses = rec.daAddresses;
2N/A
2N/A // Ignore scopes, delete if there. Scopes will always be the
2N/A // ones for which this DA is to be removed.
2N/A
2N/A int j, m = daAddresses.size();
2N/A
2N/A for (j = 0; j < m; j++) {
2N/A InetAddress daaddr = (InetAddress)daAddresses.elementAt(j);
2N/A
2N/A // If they are equal, remove it, exit loop.
2N/A
2N/A if (address.equals(daaddr)) {
2N/A foundit = true;
2N/A daAddresses.removeElementAt(j);
2N/A
2N/A // If the cache entry is empty, remove it.
2N/A
2N/A if (daAddresses.size() <= 0) {
2N/A cache.removeElementAt(i);
2N/A
2N/A }
2N/A
2N/A break;
2N/A
2N/A }
2N/A }
2N/A }
2N/A
2N/A return foundit;
2N/A
2N/A }
2N/A
2N/A // Return a vector of DARecord equivalence classes by going out to the
2N/A // wire for them. Merge any that are in the current process'
2N/A // DAAddresses property.
2N/A
2N/A private Vector getWireTable(Vector scopes, int version)
2N/A throws ServiceLocationException {
2N/A
2N/A Vector ret = new Vector();
2N/A
2N/A // Get replies from the SA server. These will be CSrvMsg replies.
2N/A
2N/A CSrvMsg msg = getSrvReply(scopes, version);
2N/A
2N/A // Process reply into the vector of equivalence classes by adding to
2N/A // to those from the preconfigured DAs.
2N/A
2N/A processReply(msg, ret);
2N/A
2N/A // Return vector.
2N/A
2N/A return ret;
2N/A }
2N/A
2N/A private CSrvMsg getSrvReply(Vector scopes, int version)
2N/A throws ServiceLocationException {
2N/A
2N/A // Form the query.
2N/A
2N/A StringBuffer buf = new StringBuffer();
2N/A int i, n = scopes.size();
2N/A
2N/A for (i = 0; i < n; i++) {
2N/A buf.append("(");
2N/A buf.append(SCOPES_ID);
2N/A buf.append("=");
2N/A buf.append((String)scopes.elementAt(i));
2N/A buf.append(")");
2N/A }
2N/A
2N/A // Add logical disjunction if there is more than one scope.
2N/A
2N/A if (i > 1) {
2N/A buf.insert(0, "(|");
2N/A buf.append(")");
2N/A
2N/A }
2N/A
2N/A // Add version number restriction.
2N/A
2N/A if (i > 0) {
2N/A buf.insert(0, "(&");
2N/A
2N/A }
2N/A
2N/A buf.append("(");
2N/A buf.append(VERSION_ID);
2N/A buf.append("=");
2N/A buf.append((new Integer(version)).toString());
2N/A buf.append(")");
2N/A
2N/A // Add closing paren if there were any scopes.
2N/A
2N/A if (i > 0) {
2N/A buf.append(")");
2N/A
2N/A }
2N/A
2N/A // Create the message object. Note that if scope vector is
2N/A // empty, the query is the null string, and so all DAs
2N/A // will be returned.
2N/A
2N/A CSrvMsg msg = new CSrvMsg(Defaults.locale,
2N/A Defaults.SUN_DA_SERVICE_TYPE,
2N/A saOnlyScopes,
2N/A buf.toString());
2N/A
2N/A // Send it down the pipe to the IPC process. It's a bad bug
2N/A // if the reply comes back as not a CSrvMsg.
2N/A
2N/A SrvLocMsg rply =
2N/A Transact.transactTCPMsg(conf.getLoopback(), msg, true);
2N/A
2N/A // Check error code.
2N/A
2N/A if (rply == null ||
2N/A rply.getErrorCode() != ServiceLocationException.OK) {
2N/A short errCode =
2N/A (rply == null ?
2N/A ServiceLocationException.INTERNAL_SYSTEM_ERROR :
2N/A rply.getErrorCode());
2N/A throw
2N/A new ServiceLocationException(errCode,
2N/A "loopback_error",
2N/A new Object[] {
2N/A new Short(errCode)});
2N/A
2N/A }
2N/A
2N/A return (CSrvMsg)rply;
2N/A }
2N/A
2N/A // Process CSrvMsg reply into DA equivalence class vector
2N/A
2N/A private void processReply(CSrvMsg msg, Vector ret)
2N/A throws ServiceLocationException {
2N/A
2N/A int shortTimer = Integer.MAX_VALUE;
2N/A
2N/A // Get the URLs.
2N/A
2N/A Vector serviceURLs = msg.serviceURLs;
2N/A
2N/A // Process each service URL.
2N/A
2N/A int i, n = serviceURLs.size();
2N/A
2N/A for (i = 0; i < n; i++) {
2N/A ServiceURL url = (ServiceURL)serviceURLs.elementAt(i);
2N/A
2N/A // If the time to live is less than the current minimum,
2N/A // save it.
2N/A
2N/A int lifetime = url.getLifetime();
2N/A
2N/A if (lifetime < shortTimer) {
2N/A shortTimer = lifetime;
2N/A
2N/A }
2N/A
2N/A // Get the host name and URL part.
2N/A
2N/A String daaddr = url.getHost();
2N/A String urlpart = url.getURLPath();
2N/A
2N/A // Parse URL part into scope list. Be
2N/A // sure not to include the initial `/' in the parse.
2N/A
2N/A StringTokenizer tk =
2N/A new StringTokenizer(urlpart.substring(1,
2N/A urlpart.length()),
2N/A ";");
2N/A Vector daScopes = null;
2N/A
2N/A while (tk.hasMoreElements()) {
2N/A String attrExp = tk.nextToken();
2N/A
2N/A // Convert to an SLP attribute.
2N/A
2N/A ServiceLocationAttribute attr =
2N/A new ServiceLocationAttribute("(" + attrExp + ")", false);
2N/A
2N/A // Depending on the attribute id, do something.
2N/A
2N/A String id = attr.getId();
2N/A Vector vals = attr.getValues();
2N/A
2N/A if (id.equals(SCOPES_ID)) {
2N/A daScopes = vals;
2N/A
2N/A } else {
2N/A throw
2N/A new ServiceLocationException(
2N/A ServiceLocationException.PARSE_ERROR,
2N/A "loopback_parse_error",
2N/A new Object[] {url});
2N/A }
2N/A }
2N/A
2N/A // Add it to the equivalence class.
2N/A
2N/A addToEquivClass(daaddr, daScopes, ret);
2N/A
2N/A }
2N/A
2N/A // Reset the timestamp.
2N/A
2N/A timeStamp = System.currentTimeMillis() + (long)(shortTimer * 1000);
2N/A
2N/A }
2N/A
2N/A}