📄 agletruntime.java
字号:
* @param username an certificate alias in the keystore. * @return certificate of the user */ public static Certificate getCertificate(String username) { Certificate cert = null; try { cert = _keyStore.getCertificate(username); if (cert != null) { return cert; } else { System.out .println("AgletRuntime is requested to get unknown user's certificate: " + username); return getAnonymousUserCertificate(); } } catch (KeyStoreException ex) { ex.printStackTrace(); return null; } } /** * @return java.lang.String * @param cert java.security.cert.Certificate */ public static String getCertificateAlias(Certificate cert) { try { return _keyStore.getCertificateAlias(cert); } catch (KeyStoreException ex) { ex.printStackTrace(); return null; } } /** * This was getCurrentIdentity */ static Certificate getCurrentCertificate() { SecurityManager sm = System.getSecurityManager(); if (sm != null && sm instanceof AgletsSecurityManager) { AgletsSecurityManager asm = (AgletsSecurityManager)sm; Certificate cert = asm.getCurrentCertificate(); if (cert == null) { com.ibm.aglet.system.AgletRuntime runtime = getAgletRuntime(); if (runtime != null) { cert = runtime.getOwnerCertificate(); } } return cert; } // ??? Should return anonymous user's cert? (HT) return getAnonymousUserCertificate(); } /* * Returns a default ResourceManagerFactory */ static public ResourceManagerFactory getDefaultResourceManagerFactory() { check(); return defaultResourceManagerFactory; } /** * Returns certificate of the user who owns the runtime. * @return Certificate of the user who owns the runtime */ public Certificate getOwnerCertificate() { return _ownerCertificate; } /** * Returns name of the user who owns the runtime. * @return name of the user who owns the runtime */ public String getOwnerName() { return _ownerAlias; } /** * @return java.security.PrivateKey * @param cert java.security.cert.Certificate * @param passwd byte[] */ public static java.security.PrivateKey getPrivateKey(Certificate cert, char[] passwd) { try { return (java.security .PrivateKey)_keyStore .getKey(getCertificateAlias(cert), passwd); } catch (Exception ex) { ex.printStackTrace(); return null; } } /** * Returns property of the user who owns the runtime. * It needs PropertyPermission for the key of specified property, * and FilePermission for the property file. * @param prop name of properties * @param key key of property * @return property of the user who owns the runtime. * If the property for the key does not exist, return null. * @exception SecurityException if PropertyPermission for the key is not given. */ public String getProperty(String prop, String key) { return getProperty(prop, key, null); } /** * Returns property of the user who owns the runtime. * It needs PropertyPermission for the key of specified property, * and FilePermission for the property file. * @param prop name of properties * @param key key of property * @param def default value of property * @return property of the user who owns the runtime. * If the property for the key does not exist, return def. * @exception SecurityException if PropertyPermission for the key is not given. */ public String getProperty(String prop, String key, String def) { try { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPropertyAccess(key); } } catch (SecurityException ex) { return def; } Resource res = Resource.getResourceFor(prop); if (res == null) { // needed ? String username = getOwnerName(); if (username == null) { log.error("No user."); return def; } String propfile = null; final String uname = username; final String prp = prop; propfile = (String)AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return FileUtils.getPropertyFilenameForUser(uname, prp); } }); try { res = Resource.createResource(prop, propfile, null); } catch (MalformedURLException ex) { System.err.println(ex.toString()); } log.debug("getProperty: reading " + prop + " property from "+ propfile); } String ret = null; if (res != null) { ret = res.getString(key); } return ret == null ? def : ret; } /** * Returns certificate of a registered user. If the user's certificate is not found, * returns null. * Certificate is read from the keystore located by "aglets.keystore.file" * property. * @param username an certificate alias in the keystore. * @return certificate of the user, null if the user was not found. */ public static Certificate getRegisteredCertificate(String username) { Certificate cert = null; try { cert = _keyStore.getCertificate(username); if (cert != null) { return cert; } else { return null; } } catch (KeyStoreException ex) { ex.printStackTrace(); return null; } } public String getServerAddress() { return MAFAgentSystem.getLocalMAFAgentSystem().getAddress(); } synchronized protected void initialize(String args[]) { if (initialized) { throw new IllegalAccessError("AgletRuntime already initialized"); } // Interprets the command line option. Opt.setopt(MAFAgentSystem.option_defs); Opt.setopt(option_defs); Opt.getopt(args); if (Opt.checkopt(args) == false) { System.exit(1); } // Check if it's verbose. Resource res = Resource.getResourceFor("system"); if (res != null) { verbose = res.getBoolean("verbose", false); } // Load the keystore. String keyStoreFile = System.getProperty("aglets.keystore.file", null); if (keyStoreFile == null) { String userHome = System.getProperty("user.home"); if (!userHome.endsWith(File.separator)) { userHome += File.separator; } keyStoreFile = userHome + ".keystore"; } String keyStorePwd = System.getProperty("aglets.keystore.password", null); try { FileInputStream in = new FileInputStream(keyStoreFile); if (_keyStore == null) { _keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); } char[] pwd = null; if (keyStorePwd != null) { pwd = keyStorePwd.toCharArray(); } _keyStore.load(in, pwd); // Read the certificate of anonymous user String anonName = System.getProperty("aglets.keystore.anonymousAlias", "anonymous"); ANONYMOUS_USER = _keyStore.getCertificate(anonName); } catch (Exception ex) { ex.printStackTrace(); // ILog.emerg(); System.exit(-1); } // Read policy file com.ibm.aglets.security.PolicyImpl policyImpl = new com.ibm.aglets.security.PolicyImpl(); Policy.setPolicy(policyImpl); // Anyway, load shared secrets file (or create it if it doesn't exist) SharedSecrets secrets = SharedSecrets.getSharedSecrets(); // Mark that initialization is done. initialized = true; // For users information. //System.out.println(bundle.getString("aglets.license")); //System.out.println(bundle.getString("aglets.version")); } /** * Verbose message */ public static final boolean isVerbose() { return verbose; } /** * Kill the specified aglet. */ public void killAglet(AgletProxy proxy) throws InvalidAgletException { LocalAgletRef ref = LocalAgletRef.getAgletRef(MAFUtil.toName(proxy.getAgletID(), null)); if (ref != null) { ref.kill(); } else { throw new InvalidAgletException("kill: local aglet not found"); } } static Name newName(Certificate authority) { byte[] b = new byte[8]; _randomGenerator.nextBytes(b); try { return new Name(authority.getEncoded(), b, MAFUtil.AGENT_SYSTEM_TYPE_AGLETS); } catch (java.security.cert.CertificateEncodingException ex) { ex.printStackTrace(); return null; } } public void removeAgletContext(AgletContext cxt) { synchronized (contexts) { String name = cxt.getName(); contexts.remove(name); // exported_contexts.remove(name); } } /** * Sets aglets property of the user who owns the runtime. * It needs PropertyPermission for the key of aglets property, * and FilePermission for the aglets property file. * @param key key of aglets property * @param value value of specified aglets property * @exception SecurityException if permissions for the key are not given. */ public void setAgletsProperty(String key, String value) { setProperty("aglets", key, value); } /* * Set a default ResourceManagerFactory */ static public void setDefaultResourceManagerFactory(ResourceManagerFactory factory) { check(); defaultResourceManagerFactory = factory; } /* * Set a default ResourceManagerFactory */ static public void setPersistenceFactory(PersistenceFactory p_factory) { check(); persistenceFactory = p_factory; } /** * Sets property of the user who owns the runtime. * It needs PropertyPermission for the key of property, * and FilePermission for the property file. * @param prop name of properties * @param key key of property * @param value value of specified property * @exception SecurityException if permissions for the key are not given. */ public void setProperty(String prop, String key, String value) { checkPermission(new PropertyPermission(key, "write")); Resource res = Resource.getResourceFor(prop); if (res == null) { String username = getOwnerName(); if (username == null) { System.err.println("No user."); return; } String propfile = null; final String uname = username; final String prp = prop; propfile = (String)AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return FileUtils.getPropertyFilenameForUser(uname, prp); } }); try { res = Resource.createResource(prop, propfile, null); } catch (MalformedURLException ex) { System.err.println(ex.toString()); } log.debug("setProperty: reading " + prop + " property from "+ propfile); } if (res == null) { log.error("No resource."); return; } res.setResource(key, value); res.save(prop); return; } /** * Shutdown */ public void shutdown() { shutdown(new Message("shutdown")); } public void shutdown(Message msg) { synchronized (contexts) { Enumeration e = contexts.elements(); while (e.hasMoreElements()) { ((AgletContextImpl)e.nextElement()).shutdown(msg); } } try { MAFAgentSystem as = MAFAgentSystem.getLocalMAFAgentSystem(); MAFFinder finder = as.get_MAFFinder(); if (finder != null) { try { AgentSystemInfo asi = as.get_agent_system_info(); finder.unregister_agent_system(asi.agent_system_name); } catch (Exception ex) { ex.printStackTrace(); } } } catch (FinderNotFound ex) {} } private void startup() { String username = getOwnerName(); if (username == null) { System.err.println("No user."); return; } Resource res = Resource.getResourceFor("aglets"); if (res == null) { try { String propfile = FileUtils.getPropertyFilenameForUser(username, "aglets"); res = Resource.createResource("aglets", propfile, null); log.debug("startup: reading aglets property from "+ propfile); } catch (Exception ex) { ex.printStackTrace(); } } res.importOptionProperties("aglets"); // // This should be moved to "resource bundle". // res.setResource("aglets.version", VERSION.toString()); res.setResource("aglets.stream.version", String.valueOf(AgletRuntime.AGLET_STREAM_VERSION)); res.setOptionResource("aglets.copyright", "Licenced Materials - Property of IBM \n" + "(c) Copyright IBM Corp. 1996,1997 All Rights Reserved"); // // // String export_path[] = res.getStringArray("aglets.export.path", File.pathSeparator + ", "); if (export_path.length > 0 && export_path[0] != null && export_path[0].length() > 0) { res.setOptionResource("aglets.public.root", export_path[0]); } if (export_path.length > 1) { System.out .println(bundle .getString("aglets.error.export_morethan_one")); } String aglets_home = res.getString("aglets.home", null); // // Default Resources // String default_resources[][] = { { "aglets.public.root", aglets_home + File.separator + "public" }, { "aglets.viewer", DEFAULT_VIEWER }, { "aglets.addressbook", "" }, // {"aglets.box.userid", mailaddress}, { "aglets.agletsList", "examples.simple.DisplayAglet " + "examples.hello.HelloAglet " + "examples.itinerary.CirculateAglet " + "examples.mdispatcher.HelloAglet " + "examples.http.WebServerAglet " + "examples.talk.TalkMaster" }, }; res.setDefaultResources(default_resources); setSecure(res.getBoolean("aglets.secure", true)); try { String spool_dir = FileUtils.getSpoolDirectory(); res.setDefaultResource("aglets.spool", spool_dir); spool_dir = res.getString("aglets.spool"); String cache_dir = FileUtils.getCacheDirectory(); res.setDefaultResource("aglets.cache", cache_dir); cache_dir = res.getString("aglets.cache"); FileUtils.ensureDirectory(spool_dir + File.separator); FileUtils.ensureDirectory(cache_dir + File.separator); } catch (SecurityException ex) { ex.printStackTrace(); // } } public static final void verboseOut(String msg) { if (verbose) { log.debug(msg); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -