📄 agletcontextimpl.java
字号:
v.addElement(p); } } return v.elements(); } } /** * Gets the proxy for an aglet specified by its identity. * @param aid the identity of the aglet. * @return the aglet proxy. */ public AgletProxy getAgletProxy(AgletID aid) { AgletProxy p = (AgletProxy)_agletProxies.get(aid); if (p != null) { return p; } try { MAFFinder finder = MAFAgentSystem.getLocalMAFAgentSystem().get_MAFFinder(); if (finder != null) { String[] locations = finder.lookup_agent(MAFUtil.toName(aid, null), null); p = getAgletProxy(new URL(locations[0]), aid); } } catch (EntryNotFound ex) { p = null; } catch (MalformedURLException ex) { ex.printStackTrace(); p = null; } catch (FinderNotFound ex) { ex.printStackTrace(); p = null; } catch (Exception ex) { ex.printStackTrace(); p = null; } return p; } /** * Gets the proxy for a remote aglet specified by url * @param aid the identity of the aglet. * @return the aglet proxy * @deprecated */ public AgletProxy getAgletProxy(URL host, AgletID aid) { try { // REMIND: toString is not acculate // Ticket ticket = new Ticket(host); AgletRef ref = RemoteAgletRef.getAgletRef(ticket, MAFUtil.toName(aid, null)); return new AgletProxyImpl(ref); } catch (Exception ex) { return null; } } /** * */ public AudioClip getAudioClip(URL url) { /* NEW SECURITY */ if ("file".equalsIgnoreCase(url.getProtocol())) { checkPermission(new FilePermission(url.getFile(), "read")); } else { String hostport = url.getHost() + ':' + url.getPort(); checkPermission(new SocketPermission(hostport, "connect")); } AudioClip c = (AudioClip)clips.get(url); if (c == null) { InputStream in = null; try { /* * Fetch the url. */ URLConnection conn = url.openConnection(); conn.setRequestProperty("user-agent", "Tahiti/Alpha5x"); conn.setRequestProperty("agent-system", "aglets"); conn.setAllowUserInteraction(true); conn.connect(); in = conn.getInputStream(); AudioData data = new AudioStream(in).getData(); c = new AgletAudioClip(url, data); } catch (IOException ex) { ex.printStackTrace(); return null; } finally { if (in != null) { try { in.close(); } catch (IOException ex) {} } } clips.put(url, c); } return c; } /** * Returns the URL of the daemon serving all current execution contexts. * @exception AgletException if the hosting URL cannot be determined. */ public URL getHostingURL() { return _hostingURL; } /** * */ public Image getImage(ImageData d) { ImageData data = d; Image img = (Image)images.get(data); if (img == null) { img = Toolkit.getDefaultToolkit() .createImage(data.getImageProducer()); images.put(data, img); } return img; } /** * */ public Image getImage(URL url) { Image img = (Image)images.get(url); if (img == null) { img = Toolkit.getDefaultToolkit().getImage(url); images.put(url, img); } return img; } /* * Multi Media support. */ public ImageData getImageData(URL url) { InputStream in = null; try { URLConnection conn = url.openConnection(); conn.setRequestProperty("user-agent", "Tahiti/Alpha5x"); conn.setRequestProperty("agent-system", "aglets"); conn.setAllowUserInteraction(true); conn.connect(); in = conn.getInputStream(); String type = conn.getContentType(); int len = conn.getContentLength(); if (len < 0) { len = in.available(); } byte[] b = new byte[len]; int off = 0; int n = 0; while (n < len) { int count = in.read(b, off + n, len - n); if (count < 0) { throw new java.io.EOFException(); } n += count; } in.close(); return new AgletImageData(url, b, type); } catch (Exception ex) { ex.printStackTrace(); return null; } } /** * Gets the name of the context * @return the name of the context */ public String getName() { return _name; } public Persistence getPersistence() throws IOException { if (_persistence == null) { new IOException("Persistency Service is not supported."); } return _persistence; } /** * Gets the context property indicated by the key. * @param key the name of the context property. * @return the value of the specified key. */ public Object getProperty(String key) { return getProperty(key, null); } /** * Gets the context property indicated by the key and default value. * @param key the name of the context property. * @param def the value to use if this property is not set. * @return the value of the specified key. */ public Object getProperty(String key, Object def) { checkPermission(new ContextPermission("property." + key, "read")); Object r = _contextProperties.get(key); return r == null ? def : r; } public ResourceManagerFactory getResourceManagerFactory() { return _rm_factory; } boolean getSecurity() { return _secure; } Object handleMessage(Message msg) throws NotHandledException, MessageException { if (msg.sameKind("createAglet")) { Object codebase = msg.getArg("codebase"); Object classname = msg.getArg("classname"); // remote creator is anonymous user Certificate owner = AgletRuntime.getAnonymousUserCertificate(); Object init = msg.getArg("init"); if ((codebase == null || codebase instanceof String) && classname instanceof String) { try { return createAglet(codebase == null ? null : new URL((String)codebase), (String)classname, owner, init); } catch (Exception ex) { throw new MessageException(ex, "createAglet failed due to: "); } } throw new MessageException(new IllegalArgumentException("createAglet"), "createAglet failed due to: "); } else if (msg.sameKind("getAgletProxies")) { synchronized (_agletProxies) { Vector tmp = new Vector(); Enumeration e = _agletProxies.elements(); while (e.hasMoreElements()) { AgletProxy p = (AgletProxy)e.nextElement(); tmp.addElement(p); } return tmp; } } throw new NotHandledException("Message not handled: " + msg); } /* * Prints a message */ void log(String kind, String msg) { postEvent(new ContextEvent(ContextEvent.MESSAGE, this, null, kind + " : " + msg), false); } /** * */ public ReplySet multicastMessage(Message msg) { checkPermission(new ContextPermission(msg.getKind(), "multicast")); return _subscriberManager.multicastMessage(msg); } /* * To handle the aglet which doesn't respond to any request by the * system. Viewer may pops up the dialog to confirm that the system * can dispose by force. */ boolean noResponseAglet(AgletProxy proxy) { postEvent(new ContextEvent(ContextEvent.NO_RESPONSE, this, proxy), true); return true; } public void postEvent(ContextEvent event, boolean sync) { if (sync) { if (erunner != null) { erunner.sync(); } postEvent0(event); } else { if (erunner == null) { synchronized (this) { if (erunner == null) { erunner = new EventRunner(); erunner.start(); } } } erunner.postEvent(event); } } public void postEvent0(ContextEvent event) { // Update registration to Finder try { MAFFinder finder = MAFAgentSystem.getLocalMAFAgentSystem().get_MAFFinder(); if (finder != null) { AgletProxyImpl p = (AgletProxyImpl)event.getAgletProxy(); AgletRef ref0 = p.getAgletRef(); if (ref0 instanceof LocalAgletRef) { LocalAgletRef ref = (LocalAgletRef)p.getAgletRef(); switch (event.getID()) { case ContextEvent.CREATED: case ContextEvent.CLONED: case ContextEvent.ARRIVED: try { finder .register_agent(ref.getName(), _hostingURL.toString(), MAF.toAgentProfile(ref.info)); break; } catch (Exception ex) { ex.printStackTrace(); } case ContextEvent.DISPOSED: case ContextEvent.REVERTED: try { finder.unregister_agent(ref.getName()); } catch (Exception ex) { ex.printStackTrace(); } break; case ContextEvent.DISPATCHED: try { finder .register_agent(ref.getName(), event.arg.toString(), MAF.toAgentProfile(ref.info)); } catch (Exception ex) { ex.printStackTrace(); } break; case ContextEvent.DEACTIVATED: case ContextEvent.ACTIVATED: case ContextEvent.SHUTDOWN: } } } } catch (NullPointerException ex) {} catch (FinderNotFound ex) { // ex.printStackTrace(); } if (listeners == null) { return; } // synchronized(listeners) { switch (event.getID()) { case ContextEvent.STARTED: listeners.contextStarted(event); break; case ContextEvent.SHUTDOWN: listeners.contextShutdown(event); break; case ContextEvent.CREATED: listeners.agletCreated(event); break; case ContextEvent.CLONED: listeners.agletCloned(event); break; case ContextEvent.DISPOSED: listeners.agletDisposed(event); break; case ContextEvent.DISPATCHED: listeners.agletDispatched(event); break; case ContextEvent.REVERTED: listeners.agletReverted(event); break; case ContextEvent.ARRIVED: listeners.agletArrived(event); break; case ContextEvent.DEACTIVATED: listeners.agletDeactivated(event); break; case ContextEvent.ACTIVATED: listeners.agletActivated(event); break; case ContextEvent.STATE_CHANGED: listeners.agletStateChanged(event); break; case ContextEvent.SHOW_DOCUMENT: listeners.showDocument(event); break; case ContextEvent.MESSAGE: listeners.showMessage(event); } // } } /** * Receives an aglet. Will start the aglet and return its proxy. * @param aglet the aglet to be received by the context. * @exception AgletException if it is not received. */ public void receiveAglet(Name agent_name, ClassName[] classnames, String codebase, byte[] agent, String sender) throws AgletException, ClassNotFoundException { startCreation(); try { String authorityName = new String(agent_name.authority); // this permission should be checked with context's privileges checkPermission(new ContextPermission(authorityName, "receive")); LocalAgletRef ref = new LocalAgletRef(this, _secure); ref.setName(agent_name); AgletReader reader = new AgletReader(agent); reader.readInfo(ref); ref.createResourceManager(classnames); reader.readAglet(ref); ref.aglet.setStub(ref); ref.proxy = new AgletProxyImpl(ref); ref.startArrivedAglet(this, sender); // com.ibm.awb.misc.Debug.check(); // / if (_finder != null) { // / _finder.register_agent(ref.getName(), // / _hostingURL.toString(), // / MAF.toAgentProfile(ref.info)); // / }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -