001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.jci;
019:
020: import java.io.InputStream;
021: import java.net.URL;
022:
023: import org.apache.commons.jci.listeners.ReloadNotificationListener;
024: import org.apache.commons.jci.stores.ResourceStore;
025: import org.apache.commons.jci.stores.ResourceStoreClassLoader;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: * The ReloadingClassLoader uses a delegation mechansim to allow
031: * classes to be reloaded. That means that loadClass calls may
032: * return different results if the class was change in the underlying
033: * ResoruceStore.
034: *
035: * @author tcurdt
036: */
037: public class ReloadingClassLoader extends ClassLoader implements
038: ReloadNotificationListener {
039:
040: private final Log log = LogFactory
041: .getLog(ReloadingClassLoader.class);
042:
043: private final ClassLoader parent;
044: private ResourceStore[] stores = new ResourceStore[0];
045: private ClassLoader delegate;
046:
047: public ReloadingClassLoader(final ClassLoader pParent) {
048: super (pParent);
049: parent = pParent;
050:
051: delegate = new ResourceStoreClassLoader(parent, stores);
052: }
053:
054: public boolean addResourceStore(final ResourceStore pStore) {
055: try {
056: final int n = stores.length;
057: final ResourceStore[] newStores = new ResourceStore[n + 1];
058: System.arraycopy(stores, 0, newStores, 1, n);
059: newStores[0] = pStore;
060: stores = newStores;
061: delegate = new ResourceStoreClassLoader(parent, stores);
062: return true;
063: } catch (final Exception e) {
064: log.error("could not add resource store " + pStore);
065: }
066: return false;
067: }
068:
069: public boolean removeResourceStore(final ResourceStore pStore) {
070:
071: final int n = stores.length;
072: int i = 0;
073:
074: // FIXME: this should be improved with a Map
075: // find the pStore and index position with var i
076: while ((i < n) && (stores[i] != pStore)) {
077: i++;
078: }
079:
080: // pStore was not found
081: if (i == n) {
082: return false;
083: }
084:
085: // if stores length > 1 then array copy old values, else create new empty store
086: final ResourceStore[] newStores = new ResourceStore[n - 1];
087: if (i > 0) {
088: System.arraycopy(stores, 0, newStores, 0, i);
089: }
090: if (i < n - 1) {
091: System.arraycopy(stores, i + 1, newStores, i, (n - i - 1));
092: }
093:
094: stores = newStores;
095: delegate = new ResourceStoreClassLoader(parent, stores);
096: return true;
097: }
098:
099: public void handleNotification() {
100: log.debug("reloading");
101: delegate = new ResourceStoreClassLoader(parent, stores);
102: }
103:
104: public void clearAssertionStatus() {
105: delegate.clearAssertionStatus();
106: }
107:
108: public URL getResource(String name) {
109: return delegate.getResource(name);
110: }
111:
112: public InputStream getResourceAsStream(String name) {
113: return delegate.getResourceAsStream(name);
114: }
115:
116: public Class loadClass(String name) throws ClassNotFoundException {
117: return delegate.loadClass(name);
118: }
119:
120: public void setClassAssertionStatus(String className,
121: boolean enabled) {
122: delegate.setClassAssertionStatus(className, enabled);
123: }
124:
125: public void setDefaultAssertionStatus(boolean enabled) {
126: delegate.setDefaultAssertionStatus(enabled);
127: }
128:
129: public void setPackageAssertionStatus(String packageName,
130: boolean enabled) {
131: delegate.setPackageAssertionStatus(packageName, enabled);
132: }
133: }
|