📄 localagletref.java
字号:
checkValidation(); FutureReplyImpl future = new FutureReplyImpl(); MessageImpl clone = cloneMessageAndCheck(msg, Message.ONEWAY); clone.future = future; mng.pushMessage(clone); return; } /* * @see com.ibm.aglet.AgletProxy#setStub */ protected void setAglet(Aglet a) { if (a != null) { new IllegalAccessError("Aglet canont be set twice"); } aglet = a; final Class cls = aglet.getClass(); ProtectionDomain domain = (ProtectionDomain) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return cls.getProtectionDomain(); } } ); // = ProtectionDomain domain = ProtectionDomain.getDomain(cls); // System.out.println("Class="+aglet.getClass()); // System.out.println("ClaasLoader="+cls.getClassLoader()); // System.out.println("ProtectionDomain="+String.valueOf(domain)); if (domain != null && protections == null) { PermissionCollection ps = domain.getPermissions(); if (ps != null) { Enumeration perms = ps.elements(); while (perms.hasMoreElements()) { Permission perm = (Permission)perms.nextElement(); if (perm instanceof Protection) { if (protections == null) { protections = new Protections(); } protections.add(perm); } } } // System.out.println("protections="+String.valueOf(protections)); } } /* * */ /* package synchronized */ void setMessageManager(MessageManagerImpl impl) { messageManager = impl; messageManager.setAgletRef(this); } /* package */ void setName(Name n) { _name = n; _owner = AgletRuntime.getCertificate(_name.authority); // String authorityName = new String(_name.authority); // _identity = AgletRuntime.getIdentity(authorityName); // String name = _identity.getName(); // if (!authorityName.equals(name)) { // System.err.println("Unknown authority '" + authorityName + "'. Regard as '" + name + "'"); // } } /** * Sets the protections: permission collection about * who can send what kind of messages to the aglet * @param protections collection of protections about who can send * what kind of messages to the aglet */ protected void setProtections(PermissionCollection newprotections) { if (canSetProtections(newprotections)) { // only restriction can be done Protections ps = new Protections(); Enumeration prots = newprotections.elements(); while (prots.hasMoreElements()) { Permission protection = (Permission)prots.nextElement(); ps.add(protection); } protections = ps; } else { // cannot moderate restriction throw new IllegalArgumentException("cannot moderate protection"); } } public void setRef(VirtualRef vref, ObjectInputStream s) throws IOException, ClassNotFoundException { // never called. throw new RuntimeException("Should Not Called"); } void setSecurity(boolean secure) { _secure = secure; } /** * Sets/Shows a text. * @param text */ protected void setText(String text) { checkActive(); _text = text; _context .postEvent(new ContextEvent(ContextEvent .STATE_CHANGED, _context, proxy, text), true); } /** * Checkpointing the snapshot of the aglet. * @exception IOException */ protected void snapshot() throws IOException { synchronized (lock) { checkActive(); ObjectOutputStream out = null; Persistence persistence = _context.getPersistence(); suspendMessageManager(); String key = getPersistenceKey(); boolean success = false; try { PersistentEntry entry = persistence.createEntryWith(key); out = new ObjectOutputStream(entry.getOutputStream()); writeDeactivatedAglet(out, new DeactivationInfo(_name, -1, key, DeactivationInfo .DEACTIVATED)); _hasSnapshot = true; success = true; } catch (IOException ex) { try { persistence.removeEntry(key); } catch (Exception ee) {} throw ex; } catch (RuntimeException ex) { try { persistence.removeEntry(key); } catch (Exception ee) {} throw ex; } finally { resumeMessageManager(); if (success) { _context.log("Snapshot", key); } else { _context.log("Snapshot", "Fail to save snapshot for aglet [" + key + "]"); } if (out != null) { try { out.close(); } catch (IOException ex) {} } } } } /** * Send events to the activated aglet. * @param cxt the aglet context in which the aglet activated * @exception AgletException if the activation fails. * @see Aglet#onActivation */ void startActivatedAglet() throws InvalidAgletException { _state = ACTIVE; messageManager .postMessage(new EventMessage(new PersistencyEvent(PersistencyEvent .ACTIVATION, proxy, 0))); messageManager.postMessage(new SystemMessage(SystemMessage.RUN, null)); _context .postEvent(new ContextEvent(ContextEvent .ACTIVATED, _context, proxy), true); resumeMessageManager(); } /** * Activates the arrived aglet. * @param cxt the aglet context in which the aglet activated * @param sender url of the departure * @exception AgletException if the activation fails. * @see Aglet#onArrival */ void startArrivedAglet(AgletContextImpl cxt, String sender) throws InvalidAgletException { validate(cxt, ACTIVE); messageManager .postMessage(new EventMessage(new MobilityEvent(MobilityEvent .ARRIVAL, proxy, _context.getHostingURL()))); messageManager.postMessage(new SystemMessage(SystemMessage.RUN, null)); _context .postEvent(new ContextEvent(ContextEvent .ARRIVED, cxt, proxy, sender), true); resumeMessageManager(); } /** * Activates the cloned aglet. * @param cxt the aglet context in which the aglet activated * @param parent proxy to the original aglet * @exception AgletException if the activation fails. * @see Aglet#onClone */ void startClonedAglet(AgletContextImpl cxt, AgletProxyImpl parent) throws InvalidAgletException { validate(cxt, ACTIVE); messageManager .postMessage(new EventMessage(new CloneEvent(CloneEvent.CLONE, proxy))); messageManager.postMessage(new SystemMessage(SystemMessage.RUN, null)); _context .postEvent(new ContextEvent(ContextEvent .CLONED, cxt, proxy, parent), true); resumeMessageManager(); } /** * Initializes the aglet. * @param cxt the aglet context in which the aglet activated * @param init argumetns to be used in onCreation method. * @exception InvalidAgletException if the aglet is invalid. * @see Aglet#onCreation */ void startCreatedAglet(AgletContextImpl cxt, Object init) throws InvalidAgletException { validate(cxt, ACTIVE); messageManager = new MessageManagerImpl(this); messageManager.postMessage(new SystemMessage(SystemMessage.CREATE, init)); messageManager.postMessage(new SystemMessage(SystemMessage.RUN, null)); _context.postEvent(new ContextEvent(ContextEvent.CREATED, cxt, proxy), true); startMessageManager(); } /* * Start the aglet threads */ void startMessageManager() { messageManager.start(); } /** * Send events to the resumed aglet. * @param cxt the aglet context in which the aglet activated * @exception AgletException if the activation fails. * @see Aglet#onActivation */ void startResumedAglet() throws InvalidAgletException { _state = ACTIVE; // //messageManager.postMessage(new EventMessage(new PersistencyEvent(PersistencyEvent.RESUME, proxy, 0))); messageManager.postMessage(new SystemMessage(SystemMessage.RUN, null)); _context .postEvent(new ContextEvent(ContextEvent .RESUMED, _context, proxy), true); resumeMessageManager(); } // -- subscribe to a specific whiteboard. // protected void subscribeMessage(String kind) { synchronized (lock) { checkActive(); checkPermission(new ContextPermission(kind, "subscribe")); _context._subscriberManager.subscribe(this, kind); } } /** * Suspend aglet for the specified amount of time. The suspended aglet will * remain in the memory. * @param duration the duration to sleep in milliseconds. * @exception InvalidAgletEception if can not suspend the aglet. */ protected void suspend(long duration) throws InvalidAgletException { try { checkActive(); /* * TO AVOID SELF CKECKING : M.O * checkPermission(new AgletPermission("this", ACTION_DEACTIVATE)); * checkProtection(new AgletProtection("this", ACTION_DEACTIVATE)); */ checkAgletPermissionAndProtection(ACTION_DEACTIVATE); suspend(AgletThread.getCurrentMessage(), duration); } catch (InvalidAgletException excpt) { throw new AgletsSecurityException(ACTION_DEACTIVATE + " : " + excpt); } catch (RequestRefusedException excpt) { throw new AgletsSecurityException(ACTION_DEACTIVATE + " : " + excpt); } } void suspend(MessageImpl msg, long duaration) throws InvalidAgletException, RequestRefusedException { synchronized (lock) { checkValidation(); if (duaration < 0) { throw new IllegalArgumentException("minutes must be positive"); } // // Suspend all threads except for the current thread // suspendMessageManager(); // start String key = getPersistenceKey(); long wakeupTime = duaration == 0 ? 0 : System.currentTimeMillis() + duaration; DeactivationInfo dinfo = new DeactivationInfo(_name, wakeupTime, key, DeactivationInfo.SUSPENDED); _context._timer.add(dinfo); _state = INACTIVE; _mode = DeactivationInfo.SUSPENDED; if (msg != null && msg.future != null) { msg.future.sendReplyIfNeeded(null); } // message manager is persistent and will be restored later messageManager.deactivate(); terminateThreads(); try { _context.log("Suspend", key); _context .postEvent(new ContextEvent(ContextEvent .SUSPENDED, _context, proxy), true); } finally { // resourceManager.disposeAllResources(); // resourceManager.stopThreadGroup(); } } } /* * Retraction */ void suspendForRetraction(Ticket ticket) throws InvalidAgletException { synchronized (lock) { checkValidation(); try { dispatchEvent(new MobilityEvent(MobilityEvent.REVERTING, proxy, ticket)); } catch (SecurityException ex) { throw ex; } catch (Exception ex) { ex.printStackTrace(); } suspendMessageManager(); } } /* * Suspends all threads and accepting new message */ void suspendMessageManager() { messageManager.suspend(); } void terminateThreads() { // Debug.check(); resourceManager.stopAllThreads(); } static String toMessage(Exception ex) { return ex.getClass().getName() + ':' + ex.getMessage(); } public String toString() { if (!isValid()) { return "Aglet [ invalid ]"; } StringBuffer buff = new StringBuffer(); if (_state == ACTIVE) { buff.append("Aglet [active]\n"); } else if (_state == INACTIVE) { buff.append("Aglet [inactive]\n"); } String ownerName; if(_owner == null) ownerName = "ANONYMOUS"; else ownerName = ((X509Certificate)_owner).getSubjectDN().getName(); buff.append(" ClassName [" + info.getAgletClassName() + "]\n"); buff.append(" Identifier[" + info.getAgletID() + "]\n"); buff.append(" Owner[" + ownerName + "]\n"); buff.append(" CodeBase[" + info.getCodeBase() + "]\n"); buff.append(resourceManager.toString()); /* * if (threadGroup == null) { * return buff.toString(); * } else { * Thread t[] = new Thread[threadGroup.activeCount()]; * threadGroup.enumerate(t); * String head = " Threads "; * for(int i=0; i<t.length; i++) { * buff.append(head + t[i].toString() + "\n"); * head = " "; * } * // buff.append(" MessageManager\n"); * // buff.append(messageManager.toString()); * return buff.toString(); * } */ return buff.toString(); } public void unreferenced() {} // -- unsubscribe from all whiteboards. // protected void unsubscribeAllMessages() { synchronized (lock) { checkActive(); _context._subscriberManager.unsubscribeAll(this); } } // -- unsubscribe from a specific whiteboard. // protected boolean unsubscribeMessage(String kind) { synchronized (lock) { checkActive(); return _context._subscriberManager.unsubscribe(this, kind); } } /* * This method is supposed to be called only once. * */ /* synchronized */ void validate(AgletContextImpl context, int state) throws InvalidAgletException { if (isValid()) { throw new IllegalAccessError("Aglet is already validated"); } _state = state; _context = context; _context.addAgletProxy(info.getAgletID(), proxy); // see AgletReader addAgletRef(_name, this); } private void writeDeactivatedAglet(ObjectOutputStream out, DeactivationInfo dinfo) throws IOException { out.writeObject(dinfo); out.writeObject(messageManager); AgletWriter writer = new AgletWriter(); writer.writeInfo(this); writer.writeAglet(this); // Class Table out.writeObject(writer.getClassNames()); byte[] b = writer.getBytes(); // Aglet out.writeInt(b.length); out.write(b); } /* * @see com.ibm.aglets.RemoteAgletRef#findRef */ public void writeInfo(ObjectOutputStream s) throws IOException { s.writeObject(_name); s.writeObject(_context.getHostingURL().toString()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -