Revision as of 06:08, 13 February 2010 by Tboudreau (Talk | contribs)

Can I dynamically change the contents of the System Filesystem at runtime?

Yes. This technique is commonly used in platform applications which require the user to log in, to keep menu contents/toolbar actions/keyboard shortcuts disabled until the user has is authenticated.

There are two ways to do it. The most obvious way is to programmatically write files to disk at runtime (call FileUtil.getConfigRoot().createFolder(), etc.). This technique is appropriate if you are creating files which should be preserved across restarts (for example, adding folders to the Favorites window). It is completely inappropriate in the case of authentication, or any other case where you do not want the added files to be present after restart. You cannot depend on the application always being shut down normally and having a chance to clean such files up - since they are actually written to disk.

Using Dynamically Added Layers

The alternative is quite simple: Write a subclass of MultiFileSystem. Put it in the default lookup.

At runtime, when you want to add contents to the system filesystem, simply add additional filesystems to your MultiFileSystem. To remove the contents, simply remove those filesystems.

A convenient way to do this is to use XMLFileSystem - this is a filesystem created with exactly the same syntax as normal module XML layer files. The following code loads an XML filesystem, which is in an XML file in the same package as the class, called dynamicContent.xml:

@ServiceProvider (service=FileSystem.class)
public class DynamicLayerContent extends MultiFileSystem {
    private static DynamicLayerContent INSTANCE;
    public DynamicLayerContent() {
        //will be created on startup, exactly once
        INSTANCE = this;

    static boolean hasContent() {
        return INSTANCE.getDelegates().length > 0;

    static void enable() {
        if (!hasContent()) {
            try {
                INSTANCE.setDelegates(new XMLFileSystem(
            } catch (SAXException ex) {

    static void disable() {
        INSTANCE.setDelegates (new FileSystem[0]);

If finer grained control of what is added is needed at runtime, there are two possibilities, using this general approach:

  • If the new layer contents are fixed and known, but perhaps correspond to user roles which may be overlaid together, split up the functionality for each role into a separate XML file (hint: define an enum of roles, where each roll can point to an XML file URL, use EnumSet.of() and process that to decide what to enable)
  • Write contents programmatically, but write folders/files to an in-memory filesystem created using FileUtil.createMemoryFileSystem() so the contents disappear on VM exit

Note that as of NetBeans 6.8, you can use this technique to add contents to the system filesystem, but not to remove or mask content files or folders (using _hidden in dynamic filesystem content will not work)

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo