Search in sources :

Example 1 with ClassLoaderWriterListener

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);
}
Also used : ClassLoaderWriterListener(org.apache.sling.commons.classloader.ClassLoaderWriterListener) ServiceListener(org.osgi.framework.ServiceListener) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) ObjectName(javax.management.ObjectName) ServiceEvent(org.osgi.framework.ServiceEvent) File(java.io.File) Activate(org.osgi.service.component.annotations.Activate)

Example 2 with ClassLoaderWriterListener

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;
}
Also used : ClassLoaderWriterListener(org.apache.sling.commons.classloader.ClassLoaderWriterListener) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

File (java.io.File)2 ClassLoaderWriterListener (org.apache.sling.commons.classloader.ClassLoaderWriterListener)2 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 ObjectName (javax.management.ObjectName)1 ServiceEvent (org.osgi.framework.ServiceEvent)1 ServiceListener (org.osgi.framework.ServiceListener)1 ServiceReference (org.osgi.framework.ServiceReference)1 Activate (org.osgi.service.component.annotations.Activate)1