📄 napletserverimpl.java
字号:
/* * @<#>NapletServerImpl.java version 0.0.1, 1/1/2000 * * THIS PROGRAM IS FREE SOFTWARE; YOU CAN DISTRIBUTE IT AND/OR * MODIFY IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION. * * THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, * BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE * GNU GENERAL PUBLIC LICENSE FOR MORE DETAILS. * * Copyright (c) 2000 Wayne State University. All Rights Reserved. */package naplet.server;import java.io.*;import java.util.*;import java.net.*;import java.rmi.*;import java.rmi.registry.*;import java.rmi.server.UnicastRemoteObject;import java.rmi.activation.*;// import java.rmi.server.RMIClassLoader;// import java.security.AccessController;// import javax.security.auth.Subject;// import java.security.Principal;// import java.security.PrivilegedAction;import naplet.*;import naplet.directory.*;import naplet.message.*;import naplet.tracing.*;import naplet.resource.*;import naplet.nsocket.*;// import naplet.server.security.*;/** * The <code>NapletServerImpl</code> class * * @version 0.0.1, 1/1/2000 * @author C. Xu */public class NapletServerImpl extends UnicastRemoteObject implements NapletServer, ServerProperty, Runnable{ private URN serverURN; public final URN getServerURN() { return serverURN; } // private Subject administrator = null; // protected getServerSubject() { return administrator; } private NapletServerLog log; public final NapletServerLog getNapletServerLog() { return log; } /** * Return a handler of naplet directory that contains information about * network-wide naplets */ private NapletDirectory directory; public final NapletDirectory getNapletDirectory() { return directory; } private NapletManagerImpl manager; public final NapletManager getNapletManager() { return manager; } private NavigatorImpl navigator; public final Navigator getNavigator() { return navigator; } // cxu private MessengerImpl messenger; private Messenger messenger; public final Messenger getMessenger() { return messenger; } private LocatorImpl locator; public final Locator getLocator() { return locator; } private NapletMonitorImpl monitor; public final NapletMonitor getNapletMonitor() { return monitor; } private ResourceManagerImpl resManager; public final ResourceManager getResourceManager() { return resManager; } private SocketController socketController = null; public final SocketController getSocketController() { return socketController; } private int socketControllerPort = -1; public final int getControlPort() { return socketControllerPort; } private int controlChannelPort = -1; public final int getControlChannelPort() { return controlChannelPort; } /** * Port and Base specify where public classes are stored. */ private int codePort; private String codebase; /** * Local reference (stub) for remote services */ private Registry reg = null; public NapletServerImpl( Map config ) throws ServerConfigException, ServiceInstallException, RemoteException { initialize( config ); log = new NapletServerLog(); resManager = ResourceManagerImpl.getInstance( config ); try { locator = LocatorImpl.getInstance( this ); navigator = NavigatorImpl.getInstance( this ); messenger = MessengerImpl.getInstance( this ); monitor = NapletMonitorImpl.getInstance( this ); // NapletManager must be created after Navigator // and Messenger manager = NapletManagerImpl.getInstance( this ); if ( ( socketControllerPort != -1 ) && ( controlChannelPort != -1 ) ) { try { // Create socketController and controlChannel socketController = SocketController.getInstance( this ); } catch ( IOException ioe ) { ioe.printStackTrace(); } } } catch ( java.rmi.RemoteException re ) { re.printStackTrace(); throw new NapletInternalError( "Naplet Server Creation Error" ); } } public void run() { Thread thr = new Thread( resManager, "ResourceManager" ); thr.setDaemon( true ); thr.start(); if ( System.getSecurityManager() == null ) { System.setSecurityManager( new NapletSecurityManager() ); } int regPort = serverURN.getPort(); boolean isRegistered = true; try { reg = LocateRegistry.createRegistry( regPort ); } catch ( RemoteException re ) { isRegistered = false; } try { if ( !isRegistered ) { reg = LocateRegistry.getRegistry( regPort ); } else { reg.rebind( NapletManager.DefaultNapletManagerName, manager ); } reg.rebind( serverURN.getServerName(), this ); System.out.println( serverURN.toString() + " is installed." ); } catch ( RemoteException re ) { System.out.println( "Registry cann't be created:" + re.getMessage() ); System.exit( 1 ); } // Install a mini remote file access server try { thr = new Thread( ClassFileServer.getInstance( codePort, codebase ), "ClassFileServer" ); thr.setDaemon( true ); thr.start(); } catch ( java.io.IOException ioe ) {} thr = new Thread( navigator, "Navigator" ); thr.setDaemon( true ); thr.start(); thr = new Thread( ( MessengerImpl ) messenger, "Messenger" ); thr.setDaemon( true ); thr.start(); thr = new Thread( manager, "NapletManager" ); thr.setDaemon( true ); thr.start(); // start socket controller if ( socketController != null ) { thr = new Thread( socketController, "SocketController" ); thr.setDaemon( true ); thr.start(); } NapletServerShutdown sh = new NapletServerShutdown(); Runtime.getRuntime().addShutdownHook( sh ); // Periodically dump or purify NapletServerLog } /** * Called by remote naplet server or client, to dispatch a naplet * * @param desc description of the naplet to be dispatched. */ public void dispatch( NapletDesc desc ) throws RemoteException { ClassLoader cl = new NapletClassLoader(); Thread.currentThread().setContextClassLoader( cl ); ClassLoader newLoader = Thread.currentThread().getContextClassLoader(); System.out.println( "Class loade is " + newLoader.toString() ); Naplet nap = null; try { nap = desc.getNaplet(); } catch ( ClassNotFoundException cnfe ) { throw new RemoteException( "Class not found exception " + cnfe.getMessage() ); } dispatch( nap ); } /** * Called by remote naplet server or client, to dispatch a naplet * * @param naplet naplet to be dispatched. */ public void dispatch( Naplet nap ) throws RemoteException { // Detect where the naplet comes from InetAddress addr = null; try { addr = getSenderInetAddress(); } catch ( Exception e ) { throw new NapletInternalError( "Unable to detect the source of incoming naplet" ); } log.arrive( nap, addr ); navigator.land( nap, addr ); } /** * Called by a remote naplet server for communication. * The message is inserted into mailbox of target naplet if * it is a USER message. Otherwise, the message will be passed * to the napletimmediately via Interrupt. The target naplet * must be interruptable. */ public void post( Message msg ) throws RemoteException { InetAddress addr = null; try { addr = getSenderInetAddress(); } catch ( Exception e ) { throw new NapletInternalError( "Unable to detect the message source " ); } messenger.receive( msg, addr ); } private InetAddress getSenderInetAddress() throws Exception { String sender = UnicastRemoteObject.getClientHost(); InetAddress addr = java.net.InetAddress.getByName( sender ); return addr; } /** * Called by NapletServerManager for remote server management. */ public void control( int cmd, Object[] param ) throws RemoteException { switch ( cmd ) { case Command.RESTART: case Command.SHUTDOWN: } } /** * Initialize serverURN, directoryURN, codeBase, codePort, pvgServiceList, * appServiceHandlerList, pvgServiceChannelList * @param config Naplet server configuration */ private void initialize( Map config ) throws ServerConfigException, RemoteException { int port; URN dirURN = null; String serverName = null; if ( config.containsKey( "ServerPort" ) ) { port = Integer.parseInt( ( String ) config.get( "ServerPort" ) ); } else { port = DefaultServerPort; } if ( config.containsKey( "ServerName" ) ) { serverName = ( String ) config.get( "ServerName" ); } else { serverName = new String( NapletServer.DefaultServerName ); } try { serverURN = new URN( port, serverName ); } catch ( InvalidURNException ire ) { throw new ServerConfigException( ire.getMessage() ); } // Codebase for network-centric mobile code if ( config.containsKey( "CodeBase" ) ) { codebase = ( String ) config.get( "CodeBase" ); } else { throw new ServerConfigException( "Missing CodeBase" ); } if ( config.containsKey( "CodeBasePort" ) ) { codePort = Integer.parseInt( ( String ) config.get( "CodeBasePort" ) ); } else { codePort = DefaultCodeBasePort; // Get a handler of naplet directory for future references } if ( config.containsKey( "SocketControllerPort" ) ) { socketControllerPort = Integer.parseInt( ( String ) config.get( "SocketControllerPort" ) ); } if ( config.containsKey( "ControlChannelPort" ) ) { controlChannelPort = Integer.parseInt( ( String ) config.get( "ControlChannelPort" ) ); } if ( config.containsKey( "DirHost" ) ) { String dirHost = ( String ) config.get( "DirHost" ); int dirPort = NapletDirectory.DefaultDirPort; if ( config.containsKey( "DirPort" ) ) { dirPort = Integer.parseInt( ( String ) config.get( "DirPort" ) ); } try { dirURN = new URN( dirHost, dirPort, "NapletDirectory" ); } catch ( InvalidURNException iue ) { throw new ServerConfigException( iue.getMessage() ); } } if ( dirURN == null ) { directory = null; } else { try { directory = ( NapletDirectory ) Naming.lookup( "rmi://" + dirURN.toString() ); } catch ( Exception e ) { System.out.println( "Unable to access directory in " + dirURN.getHostName() + ":" + dirURN.getPort() ); System.exit( 1 ); } } } /** * Simply display the running status */ public void status( NapletID nid, URN server, String msg ) throws RemoteException { System.out.println( "home: " + serverURN + " received exception msg" ); System.out.println( " ++ Naplet <<" + nid.toString() + ">>" ); System.out.println( " ++ reporting from " + server.toString() + "---" ); System.out.println( msg ); } /** * Terminate naming registry and daemon threads */ private class NapletServerShutdown extends Thread { public void run() { // Clean up naplet server installation System.out.println( "NapletServer is shutted down" ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -