bootstrapserver.java

来自「java jdk 1.4的源码」· Java 代码 · 共 517 行 · 第 1/2 页

JAVA
517
字号
/* * @(#)BootstrapServer.java	1.36 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.corba.se.internal.CosNaming;import java.lang.Thread;import java.util.Enumeration;import java.util.Properties;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.omg.CORBA.*;import com.sun.corba.se.internal.iiop.*;import com.sun.corba.se.internal.core.*;import com.sun.corba.se.internal.orbutil.CorbaResourceUtil;/** * Class BootstrapServer is the main loop for the bootstrap server * implementation, listening for new connections and spawning threads * to handle requests on each new connection. * It uses BootstrapRequestHandler objects to actually * serve requests on the connections. The supported services is a * special properties object that ensures all accesses are synchronized; * if a file is supplied then the properties are loaded from and stored to * that file. The properties object is shared among all threads. * @see BootstrapServiceProperties * @see BootstrapRequestHandler */public class BootstrapServer{    /**     * Constructs a new BootstrapServer object and the     * BootstrapServiceProperties object shared among all threads.     * @param port the port on which the server will listen.     * @parma file a file containing the supported properties     * @param svcs a Properties defining the set of supported services.     */    public BootstrapServer(com.sun.corba.se.internal.iiop.ORB orb, int port,			   File file, Properties svcs)    {	listenerPort = port;	this.orb = orb;	// Create a properties object with the file and properties	supportedServices =  new BootstrapServiceProperties(file,svcs);	// Create the server SC	serverSC = new BootstrapRequestHandler(orb,supportedServices);	// Register it with the orb for big and little indians.. eh, endians.	orb.getSubcontractRegistry().registerBootstrapServer(serverSC);    }    /**     * Start the BootstrapServer. It creates a listener thread on     * the specified listenerport which will serve requests.     */    public void start() {	// Create a listener thread on the port - this will process requests	orb.getServerGIOP().getBootstrapEndpoint(listenerPort);    }    /**     * Adds a service to the supported set of services. Delegates to     * the shared BootstrapServiceProperties object.     * @param key the key under which the service is known.     * @param val the stringified object reference for the service.     * @param save a boolean which is true if the key,value pair should be saved.     * @return any previously bound value under the key.     */    public String addService(String key, String val, boolean save) {	// Don't hesitate - delegate.	return supportedServices.put(key,val,save);    }    /**     * Accessor method to get the Initial Service based on the Key.     * @param key the service name.     *     * @return stringified IOR previously registered or null if none registered     */    public String getService( String key ) {        return supportedServices.get( key );    }    /**     * Main startup routine in case the bootstrap server is run standalone.     * It first determines the port on which to listen, checks that the     * specified file is available, and then creates the BootstrapServer object     * that will service the requests.     * @param args the command-line arguments to the main program.     */    public static final void main(String[] args)    {	String propertiesFilename = null;	// Get an orb object. Narrow to specific ORB so we can use Wait().	Properties props = new Properties() ;	props.put( "org.omg.CORBA.ORBClass", "com.sun.corba.se.internal.iiop.ORB" ) ;	com.sun.corba.se.internal.iiop.ORB orb = (com.sun.corba.se.internal.iiop.ORB)	    org.omg.CORBA.ORB.init(args,props);	// Determine the initial bootstrap port to use	int initialPort = 900; // by convention	try {	    // Try environment	    String ips = System.getProperty("org.omg.CORBA.ORBInitialPort");	    if (ips != null && ips.length() > 0)		initialPort = java.lang.Integer.parseInt(ips);	} catch (java.lang.NumberFormatException e) {	    // do nothing	}	// Process arguments	for (int i=0;i<args.length;i++) {	    // Look for the filename	    if (args[i].equals("-InitialServicesFile") && i < args.length -1) {		propertiesFilename = args[i+1];	    }	    // Was the initial port specified? If so, override	    if (args[i].equals("-ORBInitialPort") && i < args.length-1) {		initialPort = java.lang.Integer.parseInt(args[i+1]);	    }	}	if (propertiesFilename == null) {	    System.out.println(CorbaResourceUtil.getText("bootstrap.usage", "BootstrapServer"));	    return;	}	// Create a file	File file = new File(propertiesFilename);	// Verify that if it exists, it is readable	if (file.exists() == true && file.canRead() == false) {	    System.err.println(CorbaResourceUtil.getText("bootstrap.filenotreadable", file.getAbsolutePath()));	    return;	}	// Success: start up	System.out.println(CorbaResourceUtil.getText("bootstrap.success", Integer.toString(initialPort), file.getAbsolutePath()));	// We are ready: start the server with the computed file and	// with an empty set of properties: it will be read from the file.	BootstrapServer server = new BootstrapServer(orb,						     initialPort,						     file,						     new Properties());	// Start it	server.start();	// Stick around	java.lang.Object sync = new java.lang.Object();	try {	    synchronized (sync) { sync.wait();}	} catch (Exception ex) {}    }    public final static boolean debug = false;    private int listenerPort;    private Thread listenerThread;    private BootstrapServiceProperties supportedServices;    private BootstrapRequestHandler serverSC;    private com.sun.corba.se.internal.iiop.ORB orb;}/** * Class BootstrapServiceProperties implements a synchronized properties set * that automatically gets loaded from the supplied file if the modification * timestamp changes, or stored to the file when updated. Not providing a * file implies running in memory with no persistent storage. * It only implements get(), put() and keys(); all of * these methods are synchronized to ensure correct updates. */final class BootstrapServiceProperties{    /**     * Constructs a BootstrapServiceProperties object.     * The file and properties object reference are stored; loading is     * done on demand.     * @param pFile a file object which is the file containing the Properties.     */    public BootstrapServiceProperties(File pFile, Properties props) {	// Do not bother loading now; wait for first request	propFile = pFile;	savedProps = props;	allProps = new Properties(savedProps);    }    /**   * Returns the value associated with the key, if any.   * First the check() method is called to make sure we have an up to   * date copy, and then the getProperty() method is called on the   * Properties object. This method is synchronized so access gets   * serialized.   * @param key a string which is the key to look up.   * @return a string which is the value associated with the key, or null   * if no such key exists.   */    public synchronized String get(String key) {	// Check first, then call properties	this.check();	return allProps.getProperty(key);    }    /**   * Stores a value associated with a key, and optionally writes the   * key,value pair to a file.   * This method is synchronized so access gets   * serialized.   * @param key a string which is the key to store the value under.   * @param val a string which is the value to store.   * @param save a boolean which is true if the key,value pair should be saved.   * @return any previously bound value.   */    public synchronized String put(String key,String val, boolean save) {	// If we're not supposed to save it, just put it in and be done	if (!save)	    return (String)allProps.put(key,val);	// Check first, then call properties	String s = (String)savedProps.put(key,val);	if (propFile != null) {	    try {		// Store file		FileOutputStream fileOS = new FileOutputStream(propFile);		// Clear and reload		savedProps.save(fileOS,null);		fileOS.close();		// Update timestamp		fileModified = propFile.lastModified();	    } catch (java.io.FileNotFoundException e) {		System.err.println(CorbaResourceUtil.getText("bootstrap.filenotfound", propFile.getAbsolutePath()));	    } catch (java.io.IOException e) {		System.err.println(CorbaResourceUtil.getText("bootstrap.exception",propFile.getAbsolutePath(), e.toString()));	    }	}	return s;    }    /**   * Returns an array of strings containing the list of available keys.   * First the check() method is called to make sure we have an up to   * date copy, and then an enumeration is created to iterate through   * the properties object, collecting all keys in a string array.   * @return an array of strings containing the available keys in the   * Properties object.   */    public synchronized String[] keys() {	// Ensure up to date	this.check();	// Compute list of keys

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?