use of org.apache.sling.commons.classloader.ClassLoaderWriterListener in project sling by apache.
the class FSClassLoaderProvider method activate.
/**
* Activate this component. Create the root directory.
*
* @param componentContext
* @throws MalformedURLException
* @throws InvalidSyntaxException
* @throws MalformedObjectNameException
*/
@Activate
protected void activate(final ComponentContext componentContext) throws MalformedURLException, InvalidSyntaxException, MalformedObjectNameException {
// get the file root
this.root = new File(componentContext.getBundleContext().getDataFile(""), "classes");
this.root.mkdirs();
this.rootURL = this.root.toURI().toURL();
this.callerBundle = componentContext.getUsingBundle();
classLoaderWriterListeners.clear();
if (classLoaderWriterServiceListener != null) {
componentContext.getBundleContext().removeServiceListener(classLoaderWriterServiceListener);
classLoaderWriterServiceListener = null;
}
classLoaderWriterServiceListener = new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
ServiceReference<ClassLoaderWriterListener> reference = (ServiceReference<ClassLoaderWriterListener>) event.getServiceReference();
if (event.getType() == ServiceEvent.MODIFIED || event.getType() == ServiceEvent.REGISTERED) {
classLoaderWriterListeners.put(getId(reference), reference);
} else {
classLoaderWriterListeners.remove(getId(reference));
}
}
private Long getId(ServiceReference<ClassLoaderWriterListener> reference) {
return (Long) reference.getProperty(Constants.SERVICE_ID);
}
};
componentContext.getBundleContext().addServiceListener(classLoaderWriterServiceListener, LISTENER_FILTER);
// handle the MBean Installation
if (mbeanRegistration != null) {
mbeanRegistration.unregister();
mbeanRegistration = null;
}
Hashtable<String, String> jmxProps = new Hashtable<String, String>();
jmxProps.put("type", "ClassLoader");
jmxProps.put("name", "FSClassLoader");
final Hashtable<String, Object> mbeanProps = new Hashtable<String, Object>();
mbeanProps.put(Constants.SERVICE_DESCRIPTION, "Apache Sling FSClassLoader Controller Service");
mbeanProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
mbeanProps.put("jmx.objectname", new ObjectName("org.apache.sling.classloader", jmxProps));
mbeanRegistration = componentContext.getBundleContext().registerService(FSClassLoaderMBean.class.getName(), new FSClassLoaderMBeanImpl(this, componentContext.getBundleContext()), mbeanProps);
}
use of org.apache.sling.commons.classloader.ClassLoaderWriterListener in project sling by apache.
the class FSClassLoaderProvider method delete.
/**
* @see org.apache.sling.commons.classloader.ClassLoaderWriter#delete(java.lang.String)
*/
@Override
public boolean delete(final String name) {
final String path = cleanPath(name);
final File file = new File(path);
if (file.exists()) {
final List<String> names = new ArrayList<String>();
final boolean result = deleteRecursive(file, names);
logger.debug("Deleted {} : {}", name, result);
if (result) {
for (final String n : names) {
this.checkClassLoader(n);
}
for (ServiceReference<ClassLoaderWriterListener> reference : classLoaderWriterListeners.values()) {
if (reference != null) {
ClassLoaderWriterListener listener = callerBundle.getBundleContext().getService(reference);
if (listener != null) {
listener.onClassLoaderClear(name);
} else {
logger.warn("Found ClassLoaderWriterListener Service reference with no service bound");
}
}
}
}
return result;
}
// file does not exist so we return false
return false;
}
Aggregations