📄 localagletref.java
字号:
package com.ibm.aglets;/* * @(#)LocalAgletRef.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.AgentProfile;import com.ibm.maf.MAFAgentSystem;import com.ibm.maf.ClassName;import com.ibm.maf.Name;import com.ibm.maf.MAFUtil;import com.ibm.maf.MAFExtendedException;import com.ibm.maf.DeserializationFailed;import com.ibm.maf.ClassUnknown;import com.ibm.aglet.Aglet;import com.ibm.aglet.AgletProxy;import com.ibm.aglet.AgletStub;import com.ibm.aglet.AgletInfo;import com.ibm.aglet.AgletContext;import com.ibm.aglet.FutureReply;import com.ibm.aglet.Message;import com.ibm.aglet.MessageManager;import com.ibm.aglet.Ticket;import com.ibm.aglet.AgletException;import com.ibm.aglet.AgletNotFoundException;import com.ibm.aglet.InvalidAgletException;import com.ibm.aglet.MessageException;import com.ibm.aglet.NotHandledException;import com.ibm.aglet.RequestRefusedException;import com.ibm.aglet.ServerNotFoundException;import com.ibm.aglet.event.AgletEvent;import com.ibm.aglet.event.CloneEvent;import com.ibm.aglet.event.MobilityEvent;import com.ibm.aglet.event.PersistencyEvent;import com.ibm.aglet.system.ContextEvent;import java.util.Date;import java.util.Enumeration;import java.util.Properties;import java.util.Hashtable;import java.net.URL;import java.net.MalformedURLException;import java.net.UnknownHostException;import java.net.SocketException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.IOException;import com.ibm.awb.misc.Resource;import com.ibm.awb.misc.Debug;import java.security.AccessController;import java.security.ProtectionDomain;import java.security.Permission;import java.security.PermissionCollection;import java.security.PrivilegedAction;import java.security.cert.Certificate;import java.security.cert.X509Certificate;import com.ibm.aglets.security.AgletPermission;import com.ibm.aglets.security.MessagePermission;import com.ibm.aglets.security.ContextPermission;import com.ibm.aglets.security.Lifetime;import com.ibm.aglets.security.PolicyImpl;import com.ibm.aglet.security.Protections;import com.ibm.aglet.security.Protection;import com.ibm.aglet.security.AgletProtection;import com.ibm.aglet.security.MessageProtection;import com.ibm.awb.weakref.Ref;import com.ibm.awb.weakref.VirtualRef;import org.aglets.log.LogInitializer;import org.aglets.log.LogCategory;/** * Class LocalAgletRef is the implementation of AgletStub. The purpose of * this class is to provide a mechanism to control the aglet. * * @version $Revision: 1.4 $ $Date: 2002/01/21 21:20:05 $ $Author: kbd4hire $ * @author Danny B. Lange * @author Mitsuru Oshima * @author ONO Kouichi */final public class LocalAgletRef extends AgletStub implements AgletRef { static final int NOT_INITIALIZED = 0; static final int ACTIVE = Aglet.ACTIVE; static final int INACTIVE = Aglet.INACTIVE; static final int INVALID = 0x1 << 2; static final String CLASS_AGLET_PERMISSION = "com.ibm.aglets.security.AgletPermission"; static final String CLASS_MESSAGE_PERMISSION = "com.ibm.aglets.security.MessagePermission"; static final String CLASS_AGLET_PROTECTION = "com.ibm.aglet.security.AgletProtection"; static final String CLASS_MESSAGE_PROTECTION = "com.ibm.aglet.security.MessageProtection"; private static final String ACTION_CLONE = "clone"; private static final String ACTION_DISPOSE = "dispose"; private static final String ACTION_DISPATCH = "dispatch"; private static final String ACTION_DEACTIVATE = "deactivate"; private static final String ACTION_ACTIVATE = "activate"; private static final String ACTION_RETRACT = "retract"; private static LogCategory logCategory = LogInitializer.getCategory("com.ibm.aglets.LocalAgletRef"); /* package */ private static AgentProfile _agent_profile = null; static { _agent_profile = new AgentProfile((short)1, /* java */ (short)1, /* Aglets */ "Aglets", (short)0, /* Major */ (short)2, /* minor */ (short)1, /* serialization */ null); } /* package */ Aglet aglet = null; AgletInfo info = null; ResourceManager resourceManager = null; MessageManagerImpl messageManager = null; AgletProxyImpl proxy = null; // # /** // # * The allowance: availability of the aglet's resources; cloning and hops // # */ // # Allowance allowance = null; /** * The protections: permission collection about who can send what kind of * messages to the aglet */ Protections protections = null; /* private */ private Name _name = null; // MAF name private int _state = NOT_INITIALIZED; private boolean _hasSnapshot = false; private AgletContextImpl _context = null; private String _text = null; // legacy private boolean _secure = true; private Certificate _owner = null; private int _mode = -1; private Object lock = new Object(); // locker to synchronized private int num_of_trial_to_dispose = 0; /* * Reference Table. */ static Hashtable local_ref_table = new Hashtable(); static class RefKey { Name name; int hash = 0; RefKey(Name n) { name = n; for (int i = 0; i < n.identity.length; i++) { hash += (hash * 37) + (int)n.identity[i]; } } public int hashCode() { return hash; } public boolean equals(Object obj) { if (obj instanceof RefKey) { return equals(((RefKey)obj).name, name); } return false; } static public boolean equals(Name n1, Name n2) { if ( /* n1.authority.length == n2.authority.length && */ n1.identity.length == n2.identity.length && n1.agent_system_type == n2.agent_system_type) { // int l = n1.authority.length; // for(int i=0; i<l; i++) { // if (n1.authority[i] != n2.authority[i]) { // return false; // } // } int l = n1.identity.length; for (int i = 0; i < l; i++) { if (n1.identity[i] != n2.identity[i]) { return false; } } return true; } return false; } } /** * Creates an aglet reference */ /* package */ LocalAgletRef(AgletContextImpl cxt) { this(cxt, cxt.getSecurity()); } LocalAgletRef(AgletContextImpl cxt, boolean secure) { _context = cxt; _secure = secure; } /* package */ Object _clone() throws CloneNotSupportedException { try { _context.startCreation(); } catch (ShuttingDownException ex) { throw new CloneNotSupportedException("Shutting down"); } synchronized (lock) { boolean success = false; try { checkValidation(); checkActive(); try { dispatchEvent(new CloneEvent(CloneEvent.CLONING, proxy)); } catch (SecurityException ex) { throw ex; } catch (Exception ex) { ex.printStackTrace(); } // # // consume a room of cloning. // # // When the aglet has no available rooms of cloning, // # // raise an ExhaustedException. // # allowance.consumeRoomCloning(); suspendMessageManager(); LocalAgletRef clone_ref = new LocalAgletRef(_context, this._secure); // Set the owner //Certificate owner = AgletRuntime.getCurrentCertificate(); Certificate owner = _owner; Name new_name = AgletRuntime.newName(owner); clone_ref.setName(new_name); // Set AgletInfo clone_ref.info = new AgletInfo(MAFUtil.toAgletID(new_name), info.getAgletClassName(), info.getCodeBase(), _context.getHostingURL().toString(), System.currentTimeMillis(), info.getAPIMajorVersion(), info.getAPIMinorVersion(), owner); AgletWriter writer = new AgletWriter(); writer.writeAglet(this); clone_ref.createResourceManager(writer.getClassNames()); AgletReader reader = new AgletReader(writer.getBytes()); reader.readAglet(clone_ref); Aglet clone = clone_ref.aglet; // # Allowance clone_allowance = null; // # final int roomHops = allowance.getRoomHops(); // # final Lifetime lifetime = allowance.getLifeTime(); // # if(allowance.isFiniteCloning()) { // # // transfer half of rooms // # final int roomCloning = allowance.getRoomCloning()/2; // # clone_allowance = new Allowance(0, roomHops, lifetime); // # allowance.transferRoomCloning(clone_allowance, roomCloning); // # } else { // # clone_allowance = new Allowance(Room.INFINITE, roomHops, lifetime); // # } // # // # clone_ref.allowance = clone_allowance; // because Protections is not Cloneable clone_ref.protections = cloneProtections(protections); // start clone.setStub(clone_ref); clone_ref.proxy = new AgletProxyImpl(clone_ref); clone_ref.startClonedAglet(_context, proxy); success = true; return clone_ref.proxy; } catch (ClassNotFoundException ex) { throw new CloneNotSupportedException("Class Not Found :" + ex.getMessage()); // } catch (ExhaustedException ex) { // throw new CloneNotSupportedException("Available room of cloning was exhausted : " + ex.getMessage()); } catch (IOException ex) { ex.printStackTrace(); throw new CloneNotSupportedException("IO Exception :" + ex.getMessage()); } catch (AgletException ex) { throw new CloneNotSupportedException("Aglet Exception :" + ex.getMessage()); } catch (RuntimeException ex) { throw ex; } finally { resumeMessageManager(); if (success) { _context.log("Clone", info.getAgletClassName()); } else { _context.log("Clone", "Failed to clone the aglet [" + info.getAgletClassName() + "]"); } dispatchEvent(new CloneEvent(CloneEvent.CLONED, proxy)); _context.endCreation(); } } } /** * * */ public void activate() throws IOException, InvalidAgletException, AgletNotFoundException, ShuttingDownException { _context.startCreation(); synchronized (lock) { checkValidation(); if (isActive()) { _context.endCreation(); return; } // if (_mode == DeactivationInfo.SUSPENDED) { // throw new AgletNotFoundException("Cannot activate the suspended aglet"); // } String key = getPersistenceKey(); ObjectInputStream oin = null; Persistence persistence = _context.getPersistence(); try { if (_mode == DeactivationInfo.SUSPENDED) { messageManager.state = MessageManagerImpl.UNINITIALIZED; } else { // - checkAgletPermissionAndProtection(ACTION_ACTIVATE); PersistentEntry entry = persistence.getEntry(key); oin = new ObjectInputStream(entry.getInputStream()); DeactivationInfo dinfo = (DeactivationInfo)oin.readObject(); setMessageManager((MessageManagerImpl)oin.readObject()); _hasSnapshot = dinfo.isSnapshot(); ClassName[] classnames = (ClassName[])oin.readObject(); byte[] agent = new byte[oin.readInt()]; oin.readFully(agent); AgletReader reader = new AgletReader(agent); reader.readInfo(this); createResourceManager(classnames); reader.readAglet(this); aglet.setStub(this); // this should already have proxy. } if (_mode == DeactivationInfo.SUSPENDED) { startResumedAglet(); } else { startActivatedAglet(); } _context.log("Activated", info.getAgletClassName()); return; } catch (IOException ex) { throw new AgletNotFoundException(key); } catch (ClassNotFoundException ex) { // REMIND throw new AgletNotFoundException(key); } catch (InvalidAgletException ex) { ex.printStackTrace(); throw new AgletNotFoundException(key); } finally { try { try { if (oin != null) { oin.close(); } } catch (IOException e) {} _context._timer.removeInfo(key); if (_hasSnapshot == false) { persistence.removeEntry(key); } } finally { _context.endCreation(); } } } } /* package protected */ private static void addAgletRef(Name name, LocalAgletRef ref) { local_ref_table.put(new RefKey(name), ref); } /** * Returns that the protections can be set or not * @param protections collection of protections about who can send * what kind of messages to the aglet */ private boolean canSetProtections(PermissionCollection newprotections) { if (newprotections == null) { return false; } // if(_protection==null) { // return true; // } // // // needed ? // Enumeration prots = newprotections.elements(); // while(prots.hasMoreElements()) { // Permission protection = (Permission)prots.nextElement(); // if(!protections.implies(new_protection)) { // return false; // } // } return true; } void checkActive() { if (isActive() == false) { throw new AgletsSecurityException(""); } } private void checkAgletPermission(String actions) { // System.out.println("checkPermission(new AgletPermission("+_identity.getName()+", "+actions+"))"); checkPermission(new AgletPermission(AgletRuntime .getCertificateAlias(_owner), actions)); } private void checkAgletPermissionAndProtection(String actions) { checkAgletPermission(actions); checkAgletProtection(actions); } private void checkAgletProtection(String actions) { Certificate cert = AgletRuntime.getCurrentCertificate(); // ?????????? What if cert is null????? (HT) if (cert != null) { // System.out.println("checkProtection(new AgletProtection("+id.getName()+", "+actions+"))"); checkProtection(new AgletProtection(AgletRuntime .getCertificateAlias(cert), actions)); } } private void checkMessagePermission(MessageImpl msg) { // System.out.println("checkPermission(msg.getPermission("+_identity.getName()+"))"); Permission p = msg.getPermission(AgletRuntime.getCertificateAlias(_owner)); // System.out.println("checkPermission("+p.toString()+")"); checkPermission(p); } private void checkMessagePermission(String actions) { // System.out.println("checkPermission(new MessagePermission("+_identity.getName()+", "+actions+"))"); checkPermission(new MessagePermission(AgletRuntime .getCertificateAlias(_owner), actions)); } private void checkMessagePermissionAndProtection(MessageImpl msg) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -