📄 agletclassloader.java
字号:
* @since */ public String toString() { String cb = null; if (_codeBase == null) { cb = "NOWHERE"; } else { cb = _codeBase.toString(); } String owner = null; if (_ownerCert == null) { owner = "NOBODY"; } else { owner = _ownerCert.toString(); } return "[AgletClassLoader codebase = " + cb + " owner = " + owner + "]"; } /** * Description of the Method * * @since */ public void unsetResourceManagerContext() { } /** * Gets the resourceAsByteArray attribute of the AgletClassLoader object * * @param filename Description of Parameter * @return The resourceAsByteArray value * @since */ protected synchronized byte[] getResourceAsByteArray(String filename) { long digest = _digest_table.getDigest(filename); byte data[] = null; if (digest != 0) { data = _cache.getData(filename, digest); log.debug("get '" + filename + "' from cache by getData(" + filename + "," + digest + ")"); } if (data == null) { try { final String fn = filename; data = (byte[]) AccessController.doPrivileged( new PrivilegedAction() { public Object run() { log.debug("get '" + fn + "' from codebase"); byte[] res = loadResourceFromCodeBase(fn); return res; } }); } catch (Throwable t) { log.error("Error getting resource: "+t.getMessage() ); } } // -No needed? (HT, MO) // - if (data == null) { // - // get any data that matches the filename // - // code base info can be used here to specify which. // - data = _cache.getData(filename); // - } if (data != null) { putResource(filename, data); } return data; } /* * Digest is only good for the classes other than system classes. * private byte[] digest(Class c) { * return digest(c.getName()); * } */ /* * Digest is only good for the classes other than system classes. * synchronized private byte[] digest(String filename) { * * byte[] digest = _digest_table.getDigest(filename); * byte[] digest = _digest_table.getDigest(filename); * if (digest == null) { * byte[] d = findByteCodeInCache(filename); * if (d == null) { * return null; * } * digest = _digest_table.setData(filename, d); * } * return digest; * } */ /** * Description of the Method * * @param filename Description of Parameter * @return Description of the Returned Value * @since */ protected byte[] findByteCodeInCache(String filename) { // byte[] b = _digest_table.getDigest(filename); long d = _digest_table.getDigest(filename); return _cache.getData(filename, d); } /** * Loads a class specified by the param name. If a bytecode of the class * has not been loaded, an AgletClassLoader object will load the bytecode * form the codebase and define the class. The loaded bytecode and class * will be stored into the class data cache and the class cache * respectively. * * @param name the name of the desired class. * @param resolve true if the class must be resolved. * @return a loaded class * @exception ClassNotFoundException if the class is not found. * @since */ protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException { log.debug("loadClass()++ ["+resolve+"]"); try { Class cl = findResolvedClass(name); if (cl != null) { log.debug("Using class " + name + " in resolved cache"); return cl; } // REMIND: THIS IS NOT CORRECT (M.Oshima) // Needs to check in the context of loading aglet if // necessary. // AccessController.checkPermission(new RuntimePermission("package.access." + name)); cl = findClassInternal(name); if (cl == null) { throw new ClassNotFoundException(name); } String realName = cl.getName(); if (!realName.equals(name)) { throw new ClassNotFoundException(name); } if (resolve) { if (_resolvedClassCache.contains(cl)) { log.debug(name + " was resolved before."); return cl; } else { log.debug("resolving.. " + name); } boolean success = false; try { resolveClass(cl); success = true; } catch (Exception ex) { // _archive.removeResource(name); throw new ClassNotFoundException("Resolve class: " + ex.toString()); } finally { if (success) { cacheResolvedClass(cl); } else { // _archive.removeResource(name); } } } return cl; } catch (SecurityException e) { e.printStackTrace(); Thread.dumpStack(); throw e; } } /** * Description of the Method * * @param classname Description of Parameter * @return Description of the Returned Value * @since */ private byte[] findByteCode(String classname) { return getResourceAsByteArray(classname.replace('.', '/') + ".class"); } /** * Loads a class * * @param name Description of Parameter * @return Description of the Returned Value * @exception ClassNotFoundException Description of Exception * @since * @see AgletClassLoader#loadClass * @see AgletClassLoader#instantiageAglet */ private Class findClassInternal(String name) throws ClassNotFoundException { Class clazz = null; try { clazz = findSystemClass(name); if (clazz != null) { log.debug("Loading " + name + " from System"); return clazz; } } catch (ClassNotFoundException ex) { } clazz = findLoadedClass(name); if (clazz != null) { log.debug("Using class " + name + " in cache"); return clazz; } clazz = loadClassFromCodeBase(name); if (clazz != null) { log.debug("Loading class " + name + " from CodeBase"); } return clazz; } /** * Gets the class specified by the name from resolved cache. * * @param classname Description of Parameter * @return the class of the name, null if the class is not in the * cache. * @since */ private Class findResolvedClass(String classname) { return (Class) _resolvedClassCache.get(classname); } /** * Description of the Method * * @param classname Description of Parameter * @return Description of the Returned Value * @exception ClassNotFoundException Description of Exception * @since */ private Class loadClassFromCodeBase(String classname) throws ClassNotFoundException { log.debug("loadClassFromCodeBase(" + classname + ")"); byte[] bytecode = findByteCode(classname); if (bytecode == null) { throw new ClassNotFoundException(classname); } log.debug("findByteCode(" + classname + ") returns bytecode (" + bytecode.length + "bytes)"); // if(AgletRuntime.isVerbose()) { // dumpBytes(bytecode); // } try { log.debug("define class " + classname); Object[] signers = null; Certificate[] certs = null; if (_ownerCert != null) { certs = new Certificate[1]; certs[0] = _ownerCert; } final CodeSource cs = new CodeSource(_codeBase, certs); // Permissions perms = new Permissions(); // perms.add(new AllPermission()); // ???????(To be implemented) ProtectionDomain pd = (ProtectionDomain) AccessController.doPrivileged( new PrivilegedAction() { public Object run() { Policy policy = Policy.getPolicy(); PermissionCollection perms = policy.getPermissions(cs); return new ProtectionDomain(cs, perms); } }); Class clazz = defineClass(classname, bytecode, 0, bytecode.length, pd); if (clazz.getName().equals(classname) == false) { // if the name is not same as the name of the loaded class /* * _archive.removeResource(name); */ throw new ClassNotFoundException(classname); } // <RAB> 02142002 cacheResolvedClass(clazz); // </rab> return clazz; } catch (ClassFormatError e) { e.printStackTrace(); System.err.println("When loading " + classname + " from " + _codeBase + " : " + e.getClass().getName() + e.getMessage()); throw new ClassNotFoundException("When loading " + classname + " from " + _codeBase + " : " + e.getClass().getName() + e.getMessage()); } } /** * The method for loading class data. This loads the bytecode from codebase * of this loader. * * @param name the class name. * @return the bytecode for the class. * @since */ private byte[] loadResourceFromCodeBase(String name) { /* * try { * Ticket t = new Ticket(_codeBase); * MAFAgentSystem maf = MAFAgentSystem.getMAFAgentSystem(t); * * ClassName[] list = new ClassName[1]; * list[0] = new ClassName(name, null); * * byte[][] classes = maf.fetch_class(list, _codeBase.toString(), * _agent_profile); * return classes[0]; * } catch (UnknownHostException ex) { * ex.printStackTrace(); * return null; * * } catch (ClassUnknown ex) { * ex.printStackTrace(); * return null; * * } catch (MAFExtendedException ex) { * ex.printStackTrace(); * return null; * * } */ byte[] bytecode; InputStream is = null; log.debug("LoadResourceFromCodeBase()++"); try { URL url = new URL(_codeBase, name); int content_length = -1; // fetch URLConnection connection = url.openConnection(); connection.setRequestProperty("user-agent", "Aglets/1.1"); connection.setRequestProperty("agent-system", "aglets"); connection.setRequestProperty("agent-language", "java"); connection.setDoInput(true); connection.setUseCaches(false); connection.connect(); // connection.sendRequest(); is = connection.getInputStream(); content_length = connection.getContentLength(); if (content_length < 0) { content_length = is.available(); } if (content_length == 0) { return null; } bytecode = new byte[content_length]; int offset = 0; while (content_length > 0) { int read = is.read(bytecode, offset, content_length); offset += read; content_length -= read; } is.close(); } catch (IOException ex) { log.error("Error loading ["+name+"] resource from ["+_codeBase+"]", ex); bytecode = null; } finally { if (is != null) { try { is.close(); } catch (Exception ex) { log.error("Error closing.",ex); } } } log.debug("LoadResourceFromCodeBase()--"); return bytecode; } /** * Description of the Method * * @param name Description of Parameter * @param data Description of Parameter * @since */ private void putResource(String name, byte[] data) { // byte[] digest = _digest_table.setData(name, value); long digest = _digest_table.getDigest(name); if (digest == 0) { digest = _digest_table.setData(name, data); log.debug("digest of " + name + " = " + digest); _cache.putData(name, digest, data, true); } else { _cache.putData(name, digest, data, false); } } /** * Description of the Method * * @since */ private void releaseCacheEntries() { synchronized (_digest_table) { for (int i = 0; i < _digest_table.size(); i++) { String name = _digest_table.getName(i); long digest = _digest_table.getDigest(i); _cache.releaseData(name, digest); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -