📄 agletcontextimpl.java
字号:
String msg = "Receive : " + ref.info.getAgletClassName() + " from " + sender; postEvent(new ContextEvent(ContextEvent.MESSAGE, this, null, msg), false); } catch (java.io.NotSerializableException ex) { ex.printStackTrace(); throw new AgletException("Incoming aglet is not serializable in this system " + ex.getMessage()); } catch (IOException ex) { ex.printStackTrace(); throw new AgletException("Failed to receive.. " + ex.getMessage()); } catch (Exception ex) { ex.printStackTrace(); throw new AgletException("Failed to receive.. " + ex.getMessage()); } finally { // com.ibm.awb.misc.Debug.check(); endCreation(); // com.ibm.awb.misc.Debug.check(); } } /* * Removes an aglet from the current context. */ void removeAgletProxy(AgletID aid, AgletProxyImpl proxy) { // CRITICAL SESSION synchronized (_agletProxies) { // // This is a hack to enable dispatching from one machine // to itself. This must be improved. // if (proxy == _agletProxies.get(aid)) { _agletProxies.remove(aid); } } } synchronized public void removeContextListener(ContextListener o) { if (REMOVE_LISTENER_PERMISSION == null) { REMOVE_LISTENER_PERMISSION = new ContextPermission("listener", "remove"); } checkPermission(REMOVE_LISTENER_PERMISSION); if (listeners == null) { return; } synchronized (listeners) { if (listeners == o) { listeners = null; } else if (listeners != null && listeners instanceof ListenerList) { ((ListenerList)listeners).removeElement(o); } } } public AgletProxy retractAglet(Ticket ticket, AgletID aid) throws IOException, AgletException { String destination = ticket.getDestination().toString(); // - checkPermission(new ContextPermission("", "retract")); checkPermission(new ContextPermission(destination, "retract")); boolean success = false; try { MAFAgentSystem _maf = MAFAgentSystem.getMAFAgentSystem(ticket); if (_maf == null) { throw new ServerNotFoundException(ticket.toString()); } Name name = MAFUtil.toName(aid, null); byte[] agent = _maf.retract_agent(name); AgletReader reader = new AgletReader(agent); LocalAgletRef ref = new LocalAgletRef(this, _secure); // ref.setName(name); // reader.readInfo(ref); // ref.createResourceManager(null);// for the time being. reader.readInfo(ref); ref.setName(MAFUtil.toName(ref.info.getAgletID(), ref.info.getAuthorityCertificate())); ref.createResourceManager(null); reader.readAglet(ref); ref.aglet.setStub(ref); ref.proxy = new AgletProxyImpl(ref); ref.startArrivedAglet(this, destination); // / if (_finder != null) { // / try { // / _finder.register_agent(ref.getName(), // / _hostingURL.toString(), // / MAF.toAgentProfile(ref.info)); // / } catch (NameInvalid ex) { // / ex.printStackTrace(); // / } // / } success = true; return ref.proxy; } catch (ClassNotFoundException ex) { // REMIND: throw new AgletException("Fail to retract : " + ex.getMessage()); } catch (UnknownHostException ex) { throw new ServerNotFoundException(ticket.toString()); } catch (IOException ex) { throw new AgletException(ticket.toString()); /* * MAF Exceptions */ } catch (AgentNotFound ex) { throw new InvalidAgletException(ex.getMessage()); } catch (MAFExtendedException ex) { throw new AgletException(ex.getMessage()); } finally { if (success) { log("Retract", aid + " from " + ticket); } else { log("Retract", "Fail to retract " + ticket); } } } /** * Retracts the Aglet specified by its url: * scheme://host-domain-name/[user-name]#aglet-identity. * @param url the location and aglet identity of the aglet to be retracted. * @return the aglet proxy for the retracted aglet. * @exception AgletException when the method failed to retract the aglet. * @deprecated */ public AgletProxy retractAglet(URL url) throws IOException, AgletException { return retractAglet(new Ticket(url), new AgletID(url.getRef())); } /** * Retracts the Aglet specified by its url: * scheme://host-domain-name/[user-name]#aglet-identity. * @param url the location and aglet identity of the aglet to be retracted. * @param aid the aglet identity of the aglet to be retracted. * @return the aglet proxy for the retracted aglet. * @exception AgletException when the method failed to retract the aglet. */ public AgletProxy retractAglet(URL url, AgletID aid) throws IOException, AgletException { return retractAglet(new Ticket(url), aid); } public void setPersistence(Persistence p) throws AgletException { if (_persistence != null) { throw new AgletsSecurityException("Persistence already set"); } _persistence = p; } /** * Sets the context property */ public void setProperty(String key, Object value) { checkPermission(new ContextPermission("property." + key, "write")); if (value == null) { _contextProperties.remove(key); } else { _contextProperties.put(key, value); } } /** * */ public void setResourceManagerFactory(ResourceManagerFactory rmf) { if (_rm_factory != null) { throw new AgletsSecurityException("Factory already set"); } _rm_factory = rmf; } void setSecurity(boolean secure) { _secure = secure; } /** * Shows a new document. This may be ignored by the aglet context. * ContextPermission("showDocument", url) is required. * @param url an url to be shown */ public void showDocument(URL url) { String urlstr = null; if (url != null) { urlstr = url.toString(); } checkPermission(new ContextPermission("showDocument", urlstr)); postEvent(new ContextEvent(ContextEvent .SHOW_DOCUMENT, this, null, url), false); } public void shutdown() { shutdown(new Message("shutdown")); } /* * */ public void shutdown(Message msg) { if (SHUTDOWN_PERMISSION == null) { SHUTDOWN_PERMISSION = new ContextPermission("context", "shutdown"); } checkPermission(SHUTDOWN_PERMISSION); shutting_down = true; _timer.destroy(); logCategory.info("shutting down."); synchronized (creationLock) { while (creating > 0) { try { creationLock.wait(); } catch (InterruptedException ex) {} } } Enumeration e = _agletProxies.elements(); ReplySet set = new ReplySet(); while (e.hasMoreElements()) { AgletProxy proxy = (AgletProxy)e.nextElement(); try { FutureReply f = proxy.sendAsyncMessage(msg); set.addFutureReply(f); } catch (InvalidAgletException ex) {} } logCategory.debug("[waiting for response..]"); while (set.hasMoreFutureReplies()) { set.waitForNextFutureReply(5000); if (set.isAnyAvailable()) { set.getNextFutureReply(); } else { System.err.println("[some of the aglets didn't respond...]"); break; } } logCategory.info("[terminating aglets.]"); MAFFinder finder = null; try { finder = MAFAgentSystem.getLocalMAFAgentSystem().get_MAFFinder(); } catch (FinderNotFound ex) { finder = null; } if (finder != null) { try { finder.unregister_place(_hostingURL.toString()); } catch (Exception ex) { ex.printStackTrace(); } } e = _agletProxies.elements(); while (e.hasMoreElements()) { AgletProxyImpl ref = (AgletProxyImpl)e.nextElement(); try { if (ref.isActive()) { ref.dispose(); if (finder != null) { LocalAgletRef r = (LocalAgletRef)ref.getAgletRef(); if (finder != null) { finder.unregister_agent(r.getName()); } } } } catch (InvalidAgletException ex) {} catch (EntryNotFound ex) {} catch (Exception ex) { ex.printStackTrace(); } } Resource aglets_res = Resource.getResourceFor("aglets"); aglets_res.save("Aglets"); // save context property if (_persistence != null) { try { Properties p = (Properties)_contextProperties.clone(); e = p.keys(); while (e.hasMoreElements()) { String k = (String)e.nextElement(); if ((_contextProperties.get(k) instanceof String) == false) { AgletRuntime.verboseOut("removing property :" + k); _contextProperties.remove(k); } } PersistentEntry entry = _persistence.getEntry("properties-" + _name); if (entry == null) { entry = _persistence.createEntryWith("properties-" + _name); } OutputStream out = entry.getOutputStream(); _contextProperties.store(out, "ContextProperty/" + _name); } catch (IOException ex) { ex.printStackTrace(); } } postEvent(new ContextEvent(ContextEvent.SHUTDOWN, this, null), true); } /** * Starts */ synchronized public void start() { start(true); } synchronized public void start(boolean reactivate) { if (START_PERMISSION == null) { START_PERMISSION = new ContextPermission("context", "start"); } checkPermission(START_PERMISSION); if (shutting_down == false) { return; } shutting_down = false; String addr = MAFAgentSystem.getLocalMAFAgentSystem().getAddress(); // URL url = AgletRuntime.getAgletRuntime().getServerURL(); try { URL url = new URL(addr); _hostingURL = new URL(url.getProtocol(), url.getHost(), url.getPort(), '/' + _name); } catch (MalformedURLException ex) { logCategory.error(ex); } // // ResourceManagerFacotry is a mandatory and set the default object // if its empty // if (_rm_factory == null) { _rm_factory = AgletRuntime.getDefaultResourceManagerFactory(); } if (_persistence == null) { _persistence = AgletRuntime.createPersistenceFor(this); } // // Persistence is not a mandatory // if (_persistence != null) { PersistentEntry entry = _persistence.getEntry("properties-" + _name); if (reactivate) { if (entry != null) { try { InputStream in = entry.getInputStream(); try { _contextProperties.load(in); } finally { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } try { _timer.recoverTimer(_persistence); } catch (AgletException ex) { ex.printStackTrace(); } } else { logCategory.info("removing deactivated aglets in the context(" + _name + ")"); for (Enumeration e = _persistence.entryKeys(); e.hasMoreElements(); ) { String key = (String)e.nextElement(); if (!key.equals("properties-" + _name)) { logCategory.debug("\t" + key); _persistence.removeEntry(key); } } } } // // REMIND: here, context can start receiving aglets... // currently not.... // postEvent(new ContextEvent(ContextEvent.STARTED, this, null), true); // // Timer // _timer.start(); // // Register this context to MAFFinder server // try { MAFAgentSystem local = MAFAgentSystem.getLocalMAFAgentSystem(); MAFFinder finder = local.get_MAFFinder(); if (finder != null) { try { String place_name = _hostingURL.toString(); // place_name should be a canonical form? H.T. finder.register_place(place_name, _hostingURL.toString()); } catch (Exception ex) { ex.printStackTrace(); } } } catch (FinderNotFound ex) {} } void startCreation() throws ShuttingDownException { synchronized (creationLock) { if (shutting_down) { throw new ShuttingDownException(); } creating++; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -