📄 napletmonitorimpl.java
字号:
/* * @<#> NapletMonitorImpl.java version 0.1, 7/1.2001 * * 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 naplet.*;import naplet.message.*;import java.io.*;import java.net.*;import java.rmi.*;import java.util.*;import java.rmi.server.UnicastRemoteObject;/** * The <code>NapletMonitorImpl</code> provides an implementation * of the <code>NapletMonitor</code> interface. It relies on a * <code>NapletThreadTable</code> to maintain the run-time information * of the residing alien naplets. * * @see NapletMonitor * @version 0.1, 7/1/2001 * @author C. Xu, czxu@wayne.edu */public class NapletMonitorImpl implements NapletMonitor, Runnable{ private static int MAX_NAPLETTHREAD_PRI = 8; private static NapletMonitorImpl instance = null; private ServerProperty property; private NapletThreadTable threadTable; /** table for server socket */ private Hashtable nssTable = new Hashtable(); protected NapletMonitorImpl( ServerProperty property ) { this.property = property; threadTable = new NapletThreadTable(); } public static NapletMonitorImpl getInstance( ServerProperty property ) { if ( instance == null ) { instance = new NapletMonitorImpl( property ); } return instance; } public void join( Naplet nap ) { NapletThreadGroup group = new NapletThreadGroup( nap ); group.setMaxPriority( MAX_NAPLETTHREAD_PRI ); NapletContext context = new NapletContext( property.getServerURN(), property.getNavigator(), property.getMessenger(), property.getResourceManager().getServiceProxy( nap.getNapletID() ) ); /** record server here */ String svr = nap.getServer(); String sid = nap.getServerID(); if ( ( svr != null ) && ( sid != null ) ) { nssTable.put( sid, svr ); } Thread thr = new Thread( group, new NapletThread( nap, context ) ); threadTable.put( nap.getNapletID(), thr ); thr.start(); } /** * locate a server according to its naplet id. * @param nid * @return */ public String locate( NapletID nid ) { return ( String ) nssTable.get( nid.toString() ); } public void leave( NapletID nid ) {} // NapletMonitor communicates to a running naplet asynchronously public void interrupt( NapletID nid ) { Thread thr = threadTable.get( nid ); if ( thr != null ) { thr.interrupt(); } } public void run() {} private class NapletThreadGroup extends ThreadGroup { Naplet nap; NapletThreadGroup( Naplet nap ) { super( nap.getNapletID().toString() ); this.nap = nap; } public void uncaughtException( Thread thr, Throwable e ) { String home = new String( nap.getNapletHome().toString() ); int index = home.indexOf( "/" ); // get the NapletManager URL in the naplet's home server String homeNapletManagerURL = "rmi://" + home.substring( 0, index + 1 ) + "NapletManager"; try { NapletManager homeNM = ( NapletManager ) Naming.lookup( homeNapletManagerURL ); StringWriter strOut = new StringWriter(); PrintWriter out = new PrintWriter( strOut ); e.printStackTrace( out ); homeNM.status( nap.getNapletID(), property.getServerURN(), strOut.toString() ); } catch ( NotBoundException ube ) { System.out.println( "Naplet home NapletManager: " + home + " is unbounded" ); ube.printStackTrace(); } catch ( MalformedURLException mfue ) { System.out.println( "The home NapletManager URL: " + home + " of " + nap.getNapletID() + " is malformed." ); mfue.printStackTrace(); } catch ( RemoteException re ) { System.out.println( "Failed to report the exception back to naplet home" ); re.printStackTrace(); } catch ( Exception ex ) { System.out.println( "Failed to locate the naplet home" ); ex.printStackTrace(); } } } /** * Create a naplet thread */ private class NapletThread implements Runnable { Naplet naplet; NapletContext context; NapletThread( Naplet naplet, NapletContext context ) { this.naplet = naplet; this.context = context; } public void run() { naplet.init0( context ); naplet.init(); try { naplet.onStart(); } catch ( InterruptedException ie ) { naplet.onInterrupt(); } } } /** * The <code>NapletThreadTable</code> object contains * a list of threads (thread group) associated with each * alien naplet. NapletServer manages the threads based on * this information. * * @version 0.0.1, 1/1/2001 * @author C. Xu * * @see NapletServer */ private class NapletThreadTable { private HashMap threadTable; protected NapletThreadTable() { threadTable = new HashMap(); } protected void put( NapletID nid, Thread thr ) { String key = nid.toString(); threadTable.put( key, thr ); } protected void remove( NapletID nid ) { String key = nid.toString(); threadTable.remove( key ); } protected Thread get( NapletID nid ) { String key = nid.toString(); return ( Thread ) threadTable.get( key ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -