KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filesystem > InternalFileSystemCore


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.filesystem;
12
13 import java.net.URI JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import org.eclipse.core.filesystem.*;
16 import org.eclipse.core.filesystem.provider.FileSystem;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19
20 /**
21  * The class manages internal implementation of methods on EFS.
22  * This includes maintaining a list of file system extensions.
23  */

24 public class InternalFileSystemCore implements IRegistryChangeListener {
25     private static final InternalFileSystemCore INSTANCE = new InternalFileSystemCore();
26
27     /**
28      * A map (String -> (IConfigurationElement or IFileSystem)) mapping URI
29      * scheme to the file system for that scheme. If the corresponding file
30      * system has never been accessed, then the map contains the configuration
31      * element for the extension. Once the file system has been created, the
32      * map contains the IFileSystem instance for that scheme.
33      */

34     private HashMap JavaDoc fileSystems;
35
36     /**
37      * Returns the singleton instance of this class.
38      * @return The singleton instance.
39      */

40     public static InternalFileSystemCore getInstance() {
41         return INSTANCE;
42     }
43
44     /**
45      * This class has a singleton instance.
46      */

47     private InternalFileSystemCore() {
48         super();
49         RegistryFactory.getRegistry().addRegistryChangeListener(this);
50     }
51
52     /**
53      * Implements the method EFS#getFileSystem(String)
54      *
55      * @param scheme The URI scheme of the file system
56      * @return The file system
57      * @throws CoreException
58      */

59     public IFileSystem getFileSystem(String JavaDoc scheme) throws CoreException {
60         if (scheme == null)
61             throw new NullPointerException JavaDoc();
62         final HashMap JavaDoc registry = getFileSystemRegistry();
63         Object JavaDoc result = registry.get(scheme);
64         if (result == null)
65             Policy.error(EFS.ERROR_INTERNAL, NLS.bind(Messages.noFileSystem, scheme));
66         if (result instanceof IFileSystem)
67             return (IFileSystem) result;
68         try {
69             IConfigurationElement element = (IConfigurationElement) result;
70             FileSystem fs = (FileSystem) element.createExecutableExtension("run"); //$NON-NLS-1$
71
fs.initialize(scheme);
72             //store the file system instance so we don't have to keep recreating it
73
registry.put(scheme, fs);
74             return fs;
75         } catch (CoreException e) {
76             //remove this invalid file system from the registry
77
registry.remove(scheme);
78             throw e;
79         }
80     }
81
82     /**
83      * Implements the method EFS#getLocalFileSystem()
84      *
85      * @return The local file system
86      */

87     public IFileSystem getLocalFileSystem() {
88         try {
89             return getFileSystem(EFS.SCHEME_FILE);
90         } catch (CoreException e) {
91             //the local file system is always present
92
throw new Error JavaDoc(e);
93         }
94     }
95
96     /**
97      * Implements the method EFS#getStore(URI)
98      *
99      * @param uri The URI of the store to retrieve
100      * @return The file store corresponding to the given URI
101      * @throws CoreException
102      */

103     public IFileStore getStore(URI JavaDoc uri) throws CoreException {
104         final String JavaDoc scheme = uri.getScheme();
105         if (scheme == null)
106             Policy.error(EFS.ERROR_INTERNAL, Messages.noScheme + uri);
107         return getFileSystem(scheme).getStore(uri);
108     }
109
110     /**
111      * Returns the fully initialized file system registry
112      * @return The file system registry
113      */

114     private synchronized HashMap JavaDoc getFileSystemRegistry() {
115         if (fileSystems == null) {
116             fileSystems = new HashMap JavaDoc();
117             IExtensionPoint point = RegistryFactory.getRegistry().getExtensionPoint(EFS.PI_FILE_SYSTEM, EFS.PT_FILE_SYSTEMS);
118             IExtension[] extensions = point.getExtensions();
119             for (int i = 0; i < extensions.length; i++) {
120                 IConfigurationElement[] elements = extensions[i].getConfigurationElements();
121                 for (int j = 0; j < elements.length; j++) {
122                     if ("filesystem".equals(elements[j].getName())) { //$NON-NLS-1$
123
String JavaDoc scheme = elements[j].getAttribute("scheme"); //$NON-NLS-1$
124
if (scheme != null)
125                             fileSystems.put(scheme, elements[j]);
126                     }
127                 }
128             }
129         }
130         return fileSystems;
131     }
132
133     /*
134      * (non-Javadoc)
135      * @see org.eclipse.core.runtime.IRegistryChangeListener#registryChanged(org.eclipse.core.runtime.IRegistryChangeEvent)
136      */

137     public void registryChanged(IRegistryChangeEvent event) {
138         IExtensionDelta[] changes = event.getExtensionDeltas(EFS.PI_FILE_SYSTEM, EFS.PT_FILE_SYSTEMS);
139         if (changes.length == 0)
140             return;
141         synchronized (this) {
142             //let the registry be rebuilt lazily
143
fileSystems = null;
144         }
145     }
146
147     /**
148      * Implements {@link EFS#getNullFileSystem()}.
149      * @return The null file system
150      */

151     public IFileSystem getNullFileSystem() {
152         try {
153             return getFileSystem(EFS.SCHEME_NULL);
154         } catch (CoreException e) {
155             //the local file system is always present
156
throw new Error JavaDoc(e);
157         }
158     }
159 }
Popular Tags