Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members  

NamingServiceImpl.java

Go to the documentation of this file.
00001 /**************************************************************************
00002  Copyright (c) 2000-2001, Tony Garnock-Jones
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without
00006  modification, are permitted provided that the following conditions are
00007  met:
00008 
00009      * Redistributions of source code must retain the above copyright
00010        notice, this list of conditions and the following disclaimer.
00011 
00012      * Redistributions in binary form must reproduce the above
00013        copyright notice, this list of conditions and the following
00014        disclaimer in the documentation and/or other materials provided
00015        with the distribution.
00016 
00017      * Neither the names of the copyright holders nor the names of this
00018        software's contributors may be used to endorse or promote
00019        products derived from this software without specific prior
00020        written permission.
00021 
00022  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
00026  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 **************************************************************************/
00034 
00035 package org.hebe.mps;
00036 
00037 import org.hebe.mps.naming.Binding;
00038 import org.hebe.mps.naming.NamingServiceServer;
00039 import java.util.Hashtable;
00040 import java.util.Enumeration;
00041 import java.net.*;
00042 
00043 /**
00044  * Implementation class for the MPS Server Interface defined in
00045  * ../namingservice.mps. MPS uses an MPS server to locate its own
00046  * server names!
00047  *
00048  * <p>
00049  * Note that this class has a <code>main</code> method - it is its
00050  * own application as well: a standalone MPS name server.
00051  *
00052  * @author Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
00053  */
00054 public class NamingServiceImpl implements org.hebe.mps.naming.NamingService {
00055     /**
00056      * Main method for the standalone naming server.
00057      */
00058     public static void main(String [] args) {
00059         if (args.length > 2 || (args.length > 0 && args[0].equals("-h"))) {
00060             usage();
00061             return;
00062         }
00063 
00064         int portNumber = 9091;
00065         InetAddress hostname = null;
00066 
00067         if (args.length > 0) {
00068             try {
00069                 portNumber = Integer.parseInt(args[0]);
00070             } catch (NumberFormatException nfe) {
00071                 System.out.println("Invalid port number.");
00072                 usage();
00073                 return;
00074             }
00075         }
00076 
00077         if (args.length > 1) {
00078             try {
00079                 hostname = InetAddress.getByName(args[1]);
00080             } catch (UnknownHostException uhe) {
00081                 System.out.println("Unknown host \"" + args[1] + "\"");
00082                 return;
00083             }
00084         }
00085 
00086         try {
00087             NamingServiceImpl impl = new NamingServiceImpl();
00088             NamingServiceServer svr =
00089                 new NamingServiceServer("NamingService", hostname, portNumber, impl);
00090             impl.bind("NamingService", svr.getBoundName(), true);
00091         } catch (Exception e) {
00092             e.printStackTrace();
00093         }
00094     }
00095 
00096     /**
00097      * Display a friendly application-usage message to stdout.
00098      */
00099     public static void usage() {
00100         System.out.println("Usage: org.hebe.mps.NamingServiceImpl [<portnumber>] [<hostname>]");
00101         System.out.println("    <portnumber> defaults to 9091");
00102         System.out.println("    <hostname> defaults to INADDR_ANY");
00103     }
00104 
00105     ///////////////////////////////////////////////////////////////////////////
00106 
00107     /**
00108      * This class uses a naive hashtable-lookup to map objectNames
00109      * onto resolvedNames (aka canonical names).
00110      */
00111     private Hashtable names = new Hashtable();
00112 
00113     public NamingServiceImpl() {
00114     }
00115 
00116     /**
00117      * Implement the naming-service resolution function.
00118      *
00119      * @param name the objectName to look up
00120      * @return the results of the lookup - an address, or the empty-string
00121      */
00122     public String resolve(String name) {
00123         if (name.startsWith("mps:"))
00124             return name;
00125 
00126         if (names.containsKey(name)) {
00127             String result = (String) names.get(name);
00128             if (result != null)
00129                 return result;
00130         }
00131 
00132         return "";
00133     }
00134 
00135     /**
00136      * Implement the naming-service name binding function.
00137      *
00138      * @param name the object name to use
00139      * @param cname the canonical name to use
00140      * @param replace whether to replace any potential existing bindings with the same name or not
00141      * @return true if the bind succeeded; false if an existing binding was not replaced
00142      */
00143     public boolean bind(String name, String cname, boolean replace) {
00144         if (name.startsWith("mps:"))
00145             return true;
00146 
00147         if (names.containsKey(name) && !replace)
00148             return false;
00149 
00150         System.out.println("Binding \"" + name + "\" to \"" + cname + "\"");
00151         names.put(name, cname);
00152         return true;
00153     }
00154 
00155     /**
00156      * Implement the naming-service name unbinding function.
00157      *
00158      * @param name the name to remove from this nameserver
00159      * @return true if the name was removed; false if it was not bound in the first place
00160      */
00161     public boolean unbind(String name) {
00162         if (names.containsKey(name) &&
00163             names.get(name) != null) {
00164             names.put(name, null);
00165             return true;
00166         }
00167 
00168         return false;
00169     }
00170 
00171     /**
00172      * Get a list of all the name-value pairs in the current running
00173      * database.
00174      *
00175      * @return an array of Result structures containing every map in the hashtable
00176      */
00177     public Binding[] enumerate() {
00178         Binding[] results = new Binding[names.size()];
00179         int i = 0;
00180 
00181         for (Enumeration e = names.keys();
00182              e.hasMoreElements();
00183              i++) {
00184             String name = (String) e.nextElement();
00185             String cname = (String) names.get(name);
00186             results[i] = new Binding(name, cname);
00187         }
00188         return results;
00189     }
00190 }

Generated at Wed Aug 15 01:05:34 2001 for mps-java by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001