📄 localagletref.java
字号:
/* * dispatches */ protected void dispatch(URL url) throws IOException, RequestRefusedException { dispatch(new Ticket(url)); } /* * Event */ public void dispatchEvent(AgletEvent ev) { aglet.dispatchEvent(ev); } /** * Disposes the aglet. * @exception InvalidAgletException if the aglet is invalid. */ protected void dispose() { try { checkActive(); /* * TO AVOID SELF CKECKING : M.O * checkPermission(new AgletPermission("this", ACTION_DISPOSE)); * checkProtection(new AgletProtection("this", ACTION_DISPOSE)); */ checkAgletPermissionAndProtection(ACTION_DISPOSE); dispose(AgletThread.getCurrentMessage()); } catch (InvalidAgletException excpt) { throw new AgletsSecurityException(ACTION_DISPOSE + " : " + excpt); } catch (RequestRefusedException excpt) { throw new AgletsSecurityException(ACTION_DISPOSE + " : " + excpt); } } void dispose(MessageImpl msg) throws InvalidAgletException, RequestRefusedException { // // this is ad hoc // if (num_of_trial_to_dispose > 2 && isValid()) { disposeAnyway(msg); return; } num_of_trial_to_dispose++; synchronized (lock) { checkValidation(); try { aglet.onDisposing(); } finally { disposeAnyway(msg); } } } private void disposeAnyway(MessageImpl msg) throws RequestRefusedException { suspendMessageManager(); invalidateReference(); if (msg != null && msg.future != null) { msg.future.sendReplyIfNeeded(null); } removeSnapshot(); terminateThreads(); destroyMessageManager(); try { _context.log("Dispose", info.getAgletClassName()); _context .postEvent(new ContextEvent(ContextEvent .DISPOSED, _context, proxy), true); } finally { // Debug.check(); releaseResource(); } } // # /** // # * Gets the allowance: availability of the aglet's resources. // # * @return an Allowance object // # */ // # public Allowance getAllowance() { // # // checkActive(); // # return allowance; // # } /** * Gets the address. * * @return the current context address */ public String getAddress() throws InvalidAgletException { AgletContext c = _context; checkValidation(); return c.getHostingURL().toString(); } /** * Gets the aglet. If the aglet is access protected it will require * the right key to get access. * @return the aglet * @exception SecurityException if the current execution is not allowed. */ public Aglet getAglet() throws InvalidAgletException { checkValidation(); checkMessagePermissionAndProtection("access"); return aglet; } /* * Gets the context */ protected AgletContext getAgletContext() { checkActive(); return _context; } /** * Gets the information of the aglet * @return an AgletInfo object */ public AgletInfo getAgletInfo() { return info; } /* pakcage protected */ static LocalAgletRef getAgletRef(Name name) { return (LocalAgletRef)local_ref_table.get(new RefKey(name)); } /** * Gets the Certificate of the aglet's class. * @return a Certificate */ public Certificate getCertificate() throws InvalidAgletException { checkValidation(); return _owner; } /* * */ protected MessageManager getMessageManager() { checkActive(); return messageManager; } /** * */ /* package */ public Name getName() { return _name; } /* * */ private String getPersistenceKey() { return info.getAgletID().toString(); } /** * Gets the protections: permission collection about * who can send what kind of messages to the aglet * @return collection of protections about who can send * what kind of messages to the aglet */ protected PermissionCollection getProtections() { return protections; } public Ref getRef(VirtualRef vref) { // if (forwarding..) return this; } public String getRefClassName() { return "com.ibm.aglets.RemoteAgletRef"; } boolean getSecurity() { return _secure; } /* * Helpers */ String getStateAsString() { switch (_state) { case INVALID: return "INVALID"; case ACTIVE: return "ACTIVE"; case INACTIVE: return "INACTIVE"; default: return "DEFAULT"; } } /** * Gets the current content of the Aglet's message line. * @return the message line. */ public String getText() { checkActive(); return _text == null ? "" : _text; } void invalidateReference() { unsubscribeAllMessages(); _state = INVALID; _context.removeAgletProxy(info.getAgletID(), proxy); removeAgletRef(_name, this); } /** * Checks if it's valid or not. */ public boolean isActive() { return _state == ACTIVE; } /** * Checks if it's remote or not. */ public boolean isRemote() { return false; } /** * Check the state */ public boolean isState(int s) { return (_state & s) != 0; } /** * Checks if it's valid or not. */ public boolean isValid() { return _state == ACTIVE || _state == INACTIVE; } protected void kill() { suspendMessageManager(); switch (_state) { case ACTIVE: aglet = null; break; case INACTIVE: String key = getPersistenceKey(); _context._timer.removeInfo(key); try { _context.getPersistence().removeEntry(key); } catch (Exception ex) {} break; default: } invalidateReference(); removeSnapshot(); terminateThreads(); destroyMessageManager(); try { _context.log("Dispose", info.getAgletClassName()); _context .postEvent(new ContextEvent(ContextEvent .DISPOSED, _context, proxy), true); } finally { releaseResource(); } } /* * =============================== * * See com.ibm.awb.weakref.Ref * */ public void referenced() {} /* * This method is supposed to be called only from synchronized block */ void releaseResource() { _context = null; aglet = null; messageManager = null; resourceManager.disposeAllResources(); resourceManager.stopThreadGroup(); } /* package protected */ private static void removeAgletRef(Name name, LocalAgletRef ref) { if (local_ref_table.contains(ref)) { local_ref_table.remove(new RefKey(name)); // exported_aglets.remove(id); } } /* */ void removeSnapshot() { if (_hasSnapshot) { _hasSnapshot = false; try { _context.getPersistence().removeEntry(getPersistenceKey()); } catch (Exception ex) { ex.printStackTrace(); } } } public void resume() throws AgletNotFoundException, InvalidAgletException, ShuttingDownException { _context.startCreation(); synchronized (lock) { checkValidation(); if (isActive()) { _context.endCreation(); return; } if (_mode != DeactivationInfo.SUSPENDED) { throw new AgletNotFoundException("Cannot resume the deactivated aglet"); } String key = getPersistenceKey(); try { messageManager.state = MessageManagerImpl.UNINITIALIZED; // this should already have proxy. startResumedAglet(); _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 { _context._timer.removeInfo(key); } finally { _context.endCreation(); } } } } /* * Resumes all threads and resume accepting incoming messages. */ void resumeMessageManager() { messageManager.resume(); } /* package */ byte[] retract() throws MAFExtendedException { boolean success = false; String classname = info.getAgletClassName(); try { checkAgletPermissionAndProtection(ACTION_RETRACT); Message m = new SystemMessage(Message.REVERT, null, SystemMessage.RETRACT_REQUEST); FutureReply f = sendFutureMessage(m); f.waitForReply(50000); if (f.isAvailable()) { try { f.getReply(); } catch (MessageException ex) { if (ex.getException() instanceof SecurityException) { throw (SecurityException)ex.getException(); } else { // why not successfull ? should not happen ex.printStackTrace(); } } catch (NotHandledException ex) { // invalid..? check with proxy.checkValidation()... } } checkValidation(); AgletWriter writer = new AgletWriter(); writer.writeInfo(this); writer.writeAglet(this); byte[] agent = writer.getBytes(); invalidateReference(); removeSnapshot(); terminateThreads(); destroyMessageManager(); success = true; _context .postEvent(new ContextEvent(ContextEvent .REVERTED, this, proxy, null), true); return agent; } catch (SecurityException ex) { throw new MAFExtendedException(toMessage(ex)); } catch (IOException ex) { throw new MAFExtendedException(toMessage(ex)); } catch (InvalidAgletException ex) { throw new MAFExtendedException(toMessage(ex)); } finally { if (success) { _context.log("Reverted", classname); releaseResource(); } else { _context.log("Reverted", "Failed to revert " + classname); resumeMessageManager(); } } } /* * Sends a message. This posts a message into the message manager. * MessageManager controls concurrency and no synchronized * modifier is needed. * * REMIND: The way to handle system messages have to be imporved. */ public FutureReply sendFutureMessage(Message msg) throws InvalidAgletException { // // Just for thread safety to avoid a message being posted // into destroyed messageManager. this must be improved. // FutureReplyImpl future = new FutureReplyImpl(); sendFutureMessage(msg, future); return future; } /* protected */ void sendFutureMessage(Message msg, FutureReplyImpl future) throws InvalidAgletException { // // Just for thread safety to avoid a message being posted // into destroyed messageManager. this must be improved. // MessageManagerImpl mng = messageManager; checkValidation(); MessageImpl clone = cloneMessageAndCheck(msg, Message.FUTURE); clone.future = future; mng.postMessage(clone); } /* * Sends a message in synchronous way. */ public Object sendMessage(Message msg) throws MessageException, InvalidAgletException, NotHandledException { // // Just for thread safety to avoid a message being posted // into destroyed messageManager. this must be improved. // MessageManagerImpl mng = messageManager; checkValidation(); FutureReplyImpl future = new FutureReplyImpl(); MessageImpl clone = cloneMessageAndCheck(msg, Message.SYNCHRONOUS); clone.future = future; mng.postMessage(clone); return future.getReply(); } /* * Sends an oneway message. * REMIND: IMPLEMENT! */ public void sendOnewayMessage(Message msg) throws InvalidAgletException { // // Just for thread safety to avoid a message being posted // into destroyed messageManager. this must be improved. // MessageManagerImpl mng = messageManager;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -