FileDocCategorySizeDatePackage
FileSystemRegistry.javaAPI DocphoneME MR2 API (J2ME)6306Wed May 02 18:00:28 BST 2007javax.microedition.io.file

FileSystemRegistry

public class FileSystemRegistry extends Object
This class is defined by the JSR-75 specification PDA Optional Packages for the J2ME™ Platform

Fields Summary
private static Vector
fileSystemListeners
Currently registered listeners.
private static boolean
isListenerRegistered
Determines whether internal filesystem events listener is created and registered.
Constructors Summary
FileSystemRegistry()
Constructor.


      
     
    
Methods Summary
public static booleanaddFileSystemListener(FileSystemListener listener)

        if (listener == null) {
            throw new NullPointerException();
        }

        checkReadPermission();

        // Create and register file system events listener in MIDP event system
        // (if there is no registered yet)
        if (!isListenerRegistered) {
            // Create root cache object and fill it's internal cache with
            // currently mounted roots.
            // Cache is used to determine which roots were mounted/unmounted
            // if EventTypes.FC_DISKS_CHANGED_EVENT event arrives.
            RootCache.initialize();

            FileSystemEventHandler.setListener(new FileSystemEventHandler());
            isListenerRegistered = true;
        }

        fileSystemListeners.addElement(listener);

        return true;
    
static synchronized voidaddRoot(java.lang.String root)
Adds a root to the cache.

param
root path to add to roots

        RootCache cache = RootCache.getInstance();
        if (!cache.isRoot(root)) {
            cache.addRoot(root);
            notifyListeners(FileSystemListener.ROOT_ADDED, root);
        }
    
private static voidcheckReadPermission()
Checks the read permission.

throws
SecurityException if read is not allowed

        MIDletSuite suite = Scheduler.getScheduler().getMIDletSuite();

        try {
            suite.checkForPermission
                (Permissions.FILE_CONNECTION_READ, null);
        } catch (InterruptedException ie) {
            throw new SecurityException(
                "Interrupted while trying to ask the user permission");
        }
    
static java.util.EnumerationlistCachedRoots()
Gets a list of cached file system roots without checking permissions.

return
Enumeration of roots

        /** List of file system roots. */
        return new Enumeration() {
            /** Array of root pathnames. */
            String[] roots = RootCache.getInstance().getRoots();
            /** Current index int the enumeration. */
            int index = 0;
            /**
              * Checks if more data available.
              * @return <code>true</code> if more
              * elements available.
              */
            public boolean hasMoreElements() {
                return index < roots.length;
            }
            /**
              * Gets the next element.
              * @return next object in list
              */
            public Object nextElement() {
                try {
                    return roots[index++];
                } catch (ArrayIndexOutOfBoundsException e) {
                    throw new NoSuchElementException();
                }
            }
        };
    
public static java.util.EnumerationlistRoots()

        checkReadPermission();
        // retrieve up-to-date list of mounted roots
        return Protocol.listRoots().elements();
    
private static voidnotifyListeners(int event, java.lang.String root)
Notify registered listeners about mount/unmount event.

param
event root added or removed event
param
root pathname of the root file system

        for (int i = 0; i < fileSystemListeners.size(); i++) {
            try {
                ((FileSystemListener)fileSystemListeners.elementAt(i)).
                    rootChanged(event, root);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

    
public static booleanremoveFileSystemListener(FileSystemListener listener)

        if (listener == null) {
            throw new NullPointerException();
        }

        return fileSystemListeners.removeElement(listener);
    
static synchronized voidremoveRoot(java.lang.String root)
Removes a root from the cache.

param
root path to be removed

        RootCache cache = RootCache.getInstance();
        if (cache.isRoot(root)) {
            cache.removeRoot(root);
            notifyListeners(FileSystemListener.ROOT_REMOVED, root);
        }