SLF4J on Netbeans RCP Applications

This article explains how to configure SLF4J logging framework for your Netbeans RCP modules.

This article was originally published on techtavern.wordpress.com

Netbeans RCP favors Java standards for a small memory footprint and to spare us from learning new APIs. For example, Netbeans uses java.util.logging instead of creating its own logging API. However, most developers prefer leaner logger APIs like SLF4J.

Context

It happens that Netbeans kept only the API and replaced most of the java.util.logging original implementation. The startup module replaces the log manager. A custom handler writes messages into the platform messages file using a custom pattern. Another handler repeats messages on the platform messages TopComponent.

A third handler identifies error and warning messages with exceptions and presents them as a error dialogs. The Dialog API deprecated error dialogs in order to “encourage” java.util.logging.

Even worse, the platform classloader prevents the slf4j bridges from replacing the java.util.logging framework on platform and third party modules. Module importing SLF4J themselves will behave inconsistently to other modules and to platform defaults.

Solution

Instead of trying to bypass the Platform logging features, we opted for SLF4J redirecting messages to java.util.logging, which is then handled by the Platform on its custom way.

We created a library wrapper module containing slf4j-api-x.y.z.jar and slf4j-sdk14-x.y.z.jar. No coding is required. Only the org.slf4j packages needs to be set ‘public’.

Netbeans RCP module wrapping SLF4J libraries

Netbeans RCP module wrapping SLF4J libraries

Netbeans RCP module exporting SLF4J public packages

Netbeans RCP module exporting SLF4J public packages

This combination preserves the platform logging features and conventions, while enabling our modules with the SLF4J better API.

Netbeans RCP Toys

The solution described above is provided by the Netbeans RCP Toys project at Github. Look for the Logger-Toys suite, that contains the Logger-SLF4J-Libs module.

References

Netbeans RCP: external layer.xml

A Netbeans RCP application might benefit from an additional layer.xml file stored outside of application bundles. A network directory or a http server may provide some layer.xml with centralized or corporated configuration or user profiles, maintained the network administrator.

This article was first published at techtavern.wordpress.com

There are two solutions to add an additional layer.xml file to your application. Both solutions are provided by Netbeans RCP Toys at github.

LayerProvider

Register a LayerProvider using the @ServiceProvider(service = LayerProvider.class) annotation. The platform calls all LayerProvider.registerLayers(Collection<? super URL>) to populate the collection with URLs to additional layer files. This URL may refer a local file or a remote over HTTP. As this method is called early, your application might not yet know the URL. For example, it does not work for command line argument as they have not been parsed then LayerProvider.registerLayers() is called. This approach is file as long as your URLs are declared as static constants.

@ServiceProvider(service = LayerProvider.class)
public class SystemPropertyUrlLayer extends Repository.LayerProvider {
    @Override
    protected void registerLayers(Collection<? super URL> context) {
       try {
          context.add(new URL(...));
        } catch (MalformedURLException ex) {
            throw new IllegalArgumentException("Invalid URL.", ex);
        }
    }
}

This solution is demonstrated by the Platform-Layer-Property example at Netbeans RCP Toys, which extracts the additional layer.xml URL from a system property.

Dynamic FileSystem

Register your own FileSystem instance using the @ServiceProvider(service = FileSystem.class) annotation. It will be instantiated early, but a FileSystem instance may change it content later when the additional layer.xml path is known. A custom and changeable FileSystem implementation is quite complex. Fortunately, the platform provides the DynamicLayer that may be extended to emulate the custom and changeable behavior.


/**
 * @see http://wiki.netbeans.org/DevFaqDynamicSystemFilesystem
 * @author Daniel Felix Ferber - X7WS
 */
@ServiceProvider(service = FileSystem.class)
public class PostLoadingFileSystem extends MultiFileSystem {

    private static PostLoadingFileSystem INSTANCE;

    public PostLoadingFileSystem() {
        // will be created on startup, exactly once
        INSTANCE = this;
        setPropagateMasks(true); // permit *_hidden masks to be used
    }

    public static PostLoadingFileSystem getInstance() {
        return INSTANCE;
    }

    public void setFileSystem(FileSystem fs) {
        FileSystem[] newDelegates = new FileSystem[] { fs };
        setDelegates(newDelegates);
    }

    public void setFileSystem(URL url) {
       try {
            setFileSystem(new XMLFileSystem(url));
        } catch (SAXException ex) {
            throw new IllegalArgumentException("URL references an invalid XML document.", ex);
        }
    }
}

This solution is demonstrated by the Platform-Layer-Argument example at Netbeans RCP Toys, which extracts the additional layer.xml URL from a command line argument.