private Class fastFindClass(final String name) { if (stores != null) { String fileName = name.replace('.', '/') + ".class"; for (final ResourceStore store : stores) { final byte[] clazzBytes = store.read(fileName); if (clazzBytes != null) { definePackage(name); return defineClass(name, clazzBytes, 0, clazzBytes.length); } } } return null; }
protected Class findClass(final String name) throws ClassNotFoundException { final Class clazz = fastFindClass(name); if (clazz == null) { throw new ClassNotFoundException(name); } return clazz; }
public Class loadClass(String name) throws ClassNotFoundException { return isAccepted(name) ? delegate.loadClass(name) : parent.loadClass(name); }
public void setupReloading(String[] watchList, String acceptClasses, ServletContext servletContext, boolean reloadConfig) { this.reloadConfig = reloadConfig; classLoader = new ReloadingClassLoader(ClassReloadingXMLWebApplicationContext.class.getClassLoader()); patterns.add(Pattern.compile(pattern)); classLoader.setAccepClasses(patterns); classLoader.addResourceStore(new JarResourceStore(file)); getAllPaths(file, dirs); classLoader.addResourceStore(new FileResourceStore(file));
protected void initReloadClassLoader() { //when the configuration is reloaded, a new classloader will be setup if (isReloadEnabled() && reloadingClassLoader == null) reloadingClassLoader = new ReloadingClassLoader(getClassLoader()); }
protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException { Class clazz = findLoadedClass(name); if (clazz == null) { clazz = fastFindClass(name); if (clazz == null) { final ClassLoader parent = getParent(); if (parent != null) { clazz = parent.loadClass(name); } else { throw new ClassNotFoundException(name); } } } if (resolve) { resolveClass(clazz); } return clazz; }
public ReloadingClassLoader(final ClassLoader pParent) { super(pParent); parent = pParent; URL parentRoot = pParent.getResource(""); FileManager fileManager = ActionContext.getContext().getInstance(FileManagerFactory.class).getFileManager(); URL root = fileManager.normalizeToFileProtocol(parentRoot); root = ObjectUtils.defaultIfNull(root, parentRoot); try { if (root != null) { stores = new ResourceStore[]{new FileResourceStore(new File(root.toURI()))}; } else { throw new XWorkException("Unable to start the reloadable class loader, consider setting 'struts.convention.classes.reload' to false"); } } catch (URISyntaxException e) { throw new XWorkException("Unable to start the reloadable class loader, consider setting 'struts.convention.classes.reload' to false", e); } catch (RuntimeException e) { // see WW-3121 // TODO: Fix this for a reloading mechanism to be marked as stable if (root != null) LOG.error("Exception while trying to build the ResourceStore for URL [#0]", e, root.toString()); else LOG.error("Exception while trying to get root resource from class loader", e); LOG.error("Consider setting struts.convention.classes.reload=false"); throw e; } delegate = new ResourceStoreClassLoader(parent, stores); }
/** * Define the package information associated with a class. * * @param className the class name of for which the package information * is to be determined. */ protected void definePackage(String className){ int classIndex = className.lastIndexOf('.'); if (classIndex == -1) { return; } String packageName = className.substring(0, classIndex); if (getPackage(packageName) != null) { return; } definePackage(packageName, null, null, null, null, null, null, null); } }
public byte[] read(final String pResourceName) { FileInputStream fis = null; try { File file = getFile(pResourceName); byte[] data = new byte[(int) file.length()]; fis = new FileInputStream(file); fis.read(data); return data; } catch (Exception e) { if (LOG.isDebugEnabled()) LOG.debug("Unable to read file [#0]", e, pResourceName); return null; } finally { closeQuietly(fis); } }
public byte[] read(String pResourceName) { InputStream in = null; try { ZipFile jarFile = new ZipFile(file); ZipEntry entry = jarFile.getEntry(pResourceName); //read into byte array ByteArrayOutputStream out = new ByteArrayOutputStream(); in = jarFile.getInputStream(entry); copy(in, out); return out.toByteArray(); } catch (Exception e) { if (LOG.isDebugEnabled()) LOG.debug("Unable to read file [#0] from [#1]", e, pResourceName, file.getName()); return null; } finally { closeQuietly(in); } }
public boolean removeResourceStore(final ResourceStore pStore) { final int n = stores.length; int i = 0; // FIXME: this should be improved with a Map // find the pStore and index position with var i while ((i < n) && (stores[i] != pStore)) { i++; } // pStore was not found if (i == n) { return false; } // if stores length > 1 then array copy old values, else create new empty store final ResourceStore[] newStores = new ResourceStore[n - 1]; if (i > 0) { System.arraycopy(stores, 0, newStores, 0, i); } if (i < n - 1) { System.arraycopy(stores, i + 1, newStores, i, (n - i - 1)); } stores = newStores; delegate = new ResourceStoreClassLoader(parent, stores); return true; }
public void refresh() throws BeansException, IllegalStateException { if (classLoader != null) { classLoader.reload(); } super.refresh(); }
protected void initReloadClassLoader() { // when the configuration is reloaded, a new classloader will be setup if (isReloadEnabled() && reloadingClassLoader == null) reloadingClassLoader = new ReloadingClassLoader(getClassLoader()); }
public boolean addResourceStore(final ResourceStore pStore) { try { final int n = stores.length; final ResourceStore[] newStores = new ResourceStore[n + 1]; System.arraycopy(stores, 0, newStores, 1, n); newStores[0] = pStore; stores = newStores; delegate = new ResourceStoreClassLoader(parent, stores); return true; } catch (final RuntimeException e) { LOG.error("Could not add resource store", e); } return false; }
private void reload(File file) { if (classLoader != null) { LOG.debug("Change detected in file [{}], reloading class loader", file.getAbsolutePath()); classLoader.reload(); if (reloadConfig && Dispatcher.getInstance() != null) { LOG.debug("Change detected in file [{}], reloading configuration", file.getAbsolutePath()); Dispatcher.getInstance().getConfigurationManager().reload(); } } }
ReloadingClassLoader classloader = new ReloadingClassLoader(this.getClass().getClassLoader()); ReloadingListener listener = new ReloadingListener(); listener.addReloadNotificationListener(classloader); FilesystemAlterationMonitor fam = new FilesystemAlterationMonitor(); fam.addListener(directory, listener); fam.start();
public void reload() { if (LOG.isTraceEnabled()) LOG.trace("Reloading class loader"); delegate = new ResourceStoreClassLoader(parent, stores); }