📄 agletcontextimpl.java
字号:
package com.ibm.aglets;/* * @(#)AgletContextImpl.java * * IBM Confidential-Restricted * * OCO Source Materials * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The source code for this program is not published or otherwise * divested of its trade secrets, irrespective of what has been * deposited with the U.S. Copyright Office. */import com.ibm.maf.*;import com.ibm.aglet.*;import com.ibm.aglet.system.ContextEvent;import com.ibm.aglet.system.ContextListener;import com.ibm.aglet.util.ImageData;// import com.ibm.awb.misc.DigestTable;import com.ibm.awb.misc.Archive;import com.ibm.awb.misc.Resource;import java.security.AccessController;import java.security.PrivilegedAction;import java.security.Permission;import java.security.cert.Certificate;import java.security.cert.X509Certificate;import java.net.SocketPermission;import java.io.FilePermission;import com.ibm.aglets.security.AgletPermission;import com.ibm.aglets.security.MessagePermission;import com.ibm.aglets.security.ContextPermission;import java.util.Hashtable;import java.util.Properties;import java.util.Enumeration;import java.util.Vector;import java.security.Identity;import java.net.URL;import java.net.URLConnection;import java.io.PrintWriter;import java.io.IOException;import java.io.OutputStream;import java.io.InputStream;import java.io.DataInput;import java.io.DataInputStream;import java.io.ObjectOutput;import java.io.ByteArrayOutputStream;import java.lang.ClassNotFoundException;import java.net.MalformedURLException;import java.net.UnknownHostException;import java.net.SocketException;import java.rmi.RemoteException;import com.ibm.maf.NameInvalid;import org.aglets.log.*;/* * MM */import java.awt.Image;import java.awt.Toolkit;import sun.audio.*;import java.applet.AudioClip;/** * The <tt>AgletContextImpl</tt> class is the execution context for running aglets. * It provides means for maintaining and managing running aglets in an * environment where the aglets are protected from each other and the host * system is secured against malicious aglets. * * @version 1.20 $Date: 2002/01/19 22:10:43 $ * @author Danny B. Lange * @author Mitsuru Oshima * @author ONO Kouichi */final public class AgletContextImpl implements AgletContext { private static LogCategory logCategory = LogInitializer.getCategory("com.ibm.aglets.AgletContextImpl"); /* * secure/unsecure */ private boolean _secure = true; /* * permissions */ private static ContextPermission START_PERMISSION = null; private static ContextPermission SHUTDOWN_PERMISSION = null; private static ContextPermission ADD_LISTENER_PERMISSION = null; private static ContextPermission REMOVE_LISTENER_PERMISSION = null; /* * Hosting information. */ private URL _hostingURL = null; /* * Context name */ private String _name = ""; /* * Persistence */ private Persistence _persistence; /* * Table of Agletproxies */ private Hashtable _agletProxies = new Hashtable(); /* * Context properties. */ Properties _contextProperties = new Properties(); /* * Subscriber Manager */ SubscriberManager _subscriberManager = new SubscriberManager(); /* * ResourceManagerFactroy for this context */ private ResourceManagerFactory _rm_factory = null; /* * Timer Management */ AgletTimer _timer = null; /* * The object to synchronise creational operation */ private Object creationLock = new Object(); private int creating = 0; private boolean shutting_down = true; /* * Checks to see if it is possible to retrieve the aglet. * public void revertAglet(AgletID aid, URL remoteURL, OutputStream out) throws IOException, InvalidAgletException { * // * // REMIND: here, we have to lock the proxy * // * return; * } */ AgletID context_aid = new AgletID("00"); /* * */ ContextListener listeners = null; EventRunner erunner = null; /* protected */ class EventRunner extends Thread { Vector events = new Vector(); boolean sync = false; EventRunner() { setPriority(6); } public void postEvent(ContextEvent event) { events.addElement(event); } public void sync() { sync = true; synchronized (this) { ContextEvent event; while (events.size() > 0) { event = (ContextEvent)events.firstElement(); events.removeElementAt(0); postEvent0(event); } sync = false; } } public void run() { ContextEvent event; while (true) { synchronized (this) { while (sync || events.size() == 0) { try { wait(1500); } catch (Exception ex) { return; } } event = (ContextEvent)events.firstElement(); events.removeElementAt(0); } try { postEvent0(event); } catch (Exception t) { t.printStackTrace(); } } } } private Hashtable images = new Hashtable(); private Hashtable clips = new Hashtable(); /** * Creates an execution context for aglets. * @param prop property list. */ /* package protected */ AgletContextImpl(String name) { this(name, AgletRuntime.getAgletRuntime().isSecure()); } AgletContextImpl(String name, boolean secure) { _name = name; _timer = new AgletTimer(this); setSecurity(secure); } /* * Adds an aglet to the current context. */ void addAgletProxy(AgletID aid, AgletProxyImpl proxy) throws InvalidAgletException { // proxy.checkValidation(); // REMIND: critical session _agletProxies.put(aid, proxy); } synchronized public void addContextListener(ContextListener o) { if (ADD_LISTENER_PERMISSION == null) { ADD_LISTENER_PERMISSION = new ContextPermission("listener", "add"); } checkPermission(ADD_LISTENER_PERMISSION); if (listeners == null) { listeners = o; return; } synchronized (listeners) { if (listeners instanceof ListenerList) { ListenerList tmp = (ListenerList)listeners; if (!tmp.contains(o)) { tmp.addElement(o); } } else { ListenerList tmp = new ListenerList(); tmp.addElement(listeners); tmp.addElement(o); listeners = tmp; } } } /* * check permission */ // accessable in package void checkPermission(Permission p) { if (!_secure) { return; } // ---- I don't make sense why doPricileged() // - final Permission perm = p; // - AccessController.doPrivileged(new PrivilegedAction() { // - public Object run() { // - AccessController.checkPermission(perm); // - return null; // - } // - }); // System.out.println("permission="+String.valueOf(p)); AccessController.checkPermission(p); } /** * Clear the cache */ public void clearCache(URL codebase) { _rm_factory.clearCache(codebase, AgletRuntime.getCurrentCertificate()); } /** * Creates an instance of the specified aglet located at the specified URL. * @param url the URL to load the aglet class from. * @param name the aglet's class name. * @return a newly instantiated and initialized Aglet. * @exception ClassNotFoundException if the class was not found * @exception InstantiationException if failed to instantiate the Aglet. */ public AgletProxy createAglet(URL url, String classname, Object init) throws IOException, AgletException, ClassNotFoundException, InstantiationException { Certificate owner = AgletRuntime.getCurrentCertificate(); return createAglet(url, classname, owner, init); } /** * Creates an instance of the specified aglet located at the specified URL. * @param url the URL to load the aglet class from. * @param name the aglet's class name. * @return a newly instantiated and initialized Aglet. * @exception ClassNotFoundException if the class was not found * @exception InstantiationException if failed to instantiate the Aglet. */ private AgletProxy createAglet(URL url, String classname, Certificate owner, Object init) throws IOException, AgletException, ClassNotFoundException, InstantiationException { // System.out.println("createAglet("+url+","+classname+","+owner.getName()+","+init+")"); startCreation(); try { // // Converts URL to the destination url. // if (url != null && url.getRef() != null) { log("Create", "Fail to create an aglet \"" + classname + "\" from " + (url == null ? "Local" : url.toString())); throw new MalformedURLException("MalformedURL in createAglet:" + url); } if (url == null) { url = _rm_factory.lookupCodeBaseFor(classname); // System.out.println("lookupCodeBaseFor("+classname+")="+String.valueOf(url)); if (url == null) { throw new ClassNotFoundException(classname); } } String agletLocation = String.valueOf(url) + "@" + classname; checkPermission(new ContextPermission(agletLocation, "create")); Aglet aglet = null; LocalAgletRef ref = new LocalAgletRef(this, _secure); ref.setName(AgletRuntime.newName(owner)); ref.info = new AgletInfo(MAFUtil.toAgletID(ref.getName()), classname, url, getHostingURL().toString(), System.currentTimeMillis(), Aglet.MAJOR_VERSION, Aglet.MINOR_VERSION, owner); ResourceManager rm = ref.createResourceManager(null); rm.setResourceManagerContext(); try { aglet = (Aglet)rm.loadClass(classname).newInstance(); } catch (ClassCastException ex) { log("Create", "Fail to create an aglet \"" + classname + "\" from " + url); throw new InstantiationException("ClassCastException:" + classname + ":" + ex.getMessage()); } catch (ClassNotFoundException ex) { log("Create", "Fail to create an aglet \"" + classname + "\" from " + url); throw ex; } catch (InstantiationException ex) { log("Create", "Fail to create an aglet \"" + classname + "\" from " + url); throw ex; } catch (IllegalAccessException ex) { log("Create", "Fail to create an aglet \"" + classname + "\" from " + url); throw new InstantiationException("IllegalAccessException:" + classname + ":" + ex.getMessage()); } finally { rm.unsetResourceManagerContext(); } // # // get an allowance for the codebase at the url from policy DB file // # ref.resetAllowance(); // start aglet.setStub(ref); ref.proxy = new AgletProxyImpl(ref); ref.startCreatedAglet(this, init); log("Create", classname + " from " + url); // / if (_finder != null) { // / try { // / _finder.register_agent(ref.getName(), // / _hostingURL.toString(), // / MAF.toAgentProfile(ref.info)); // / } catch (NameInvalid ex) { // / ex.printStackTrace(); // / } // / } return ref.proxy; } finally { endCreation(); } } /** * */ synchronized ResourceManager createResourceManager(URL codebase, Certificate owner, ClassName[] table) { return _rm_factory.createResourceManager(codebase, owner, table); } void endCreation() { synchronized (creationLock) { creating--; if (shutting_down) { creationLock.notify(); } } } /** * Gets the aglet proxies in the current execution context. * @return an enumeration of aglet proxies. */ public Enumeration getAgletProxies() { return _agletProxies.elements(); } /** * Gets the aglet proxies in the current execution context. * @return an enumeration of aglet proxies. */ public Enumeration getAgletProxies(int type) { synchronized (_agletProxies) { Vector v = new Vector(); Enumeration e = _agletProxies.elements(); while (e.hasMoreElements()) { AgletProxy p = (AgletProxy)e.nextElement(); if (p.isState(type)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -