KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > ProviderCall


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs;
21
22 import org.openide.filesystems.FileSystem;
23 import org.openide.util.Lookup;
24 import org.openide.util.LookupListener;
25 import org.openide.util.LookupEvent;
26 import org.netbeans.modules.masterfs.providers.AutoMountProvider;
27 import org.netbeans.modules.masterfs.providers.FileSystemProvider;
28
29 import java.io.IOException JavaDoc;
30 import java.beans.PropertyVetoException JavaDoc;
31 import java.util.*;
32 import java.awt.*;
33
34 public class ProviderCall {
35     static private Map providerMap;
36     static private Lookup.Result preventsLookupResultFromGC;
37
38     static void init () {
39         getProviderMap();
40     }
41     
42     static FileSystem createFileSystem(String JavaDoc resName) {
43         FileSystem retVal = null;
44
45         AutoMountProvider ap = getMountAble(resName);
46
47         if (ap != null) {
48             try {
49                 retVal = ap.createFileSystem(resName);
50             } catch (IOException JavaDoc e) {
51                 e.printStackTrace();
52                 return null;
53             } catch (PropertyVetoException JavaDoc e) {
54                 e.printStackTrace();
55                 return null;
56             }
57         }
58
59         return retVal;
60     }
61
62
63     static boolean isMountAble(String JavaDoc resName) {
64         return getMountAble(resName) == null ? false : true;
65     }
66
67     static Image getIcon(String JavaDoc resName, int iconType) {
68         Image icon = null;
69         AutoMountProvider aProvider = ProviderCall.getMountAble(resName);
70         if (aProvider != null) {
71             icon = aProvider.getIcon(resName, iconType);
72         }
73         return icon;
74     }
75
76
77     private static AutoMountProvider getMountAble(String JavaDoc resName) {
78         Map providerMapCopy;
79         synchronized (getProviderMap()) {
80             providerMapCopy = new HashMap(getProviderMap());
81         }
82
83         Iterator it = providerMapCopy.values().iterator();
84         while (it.hasNext()) {
85             AutoMountProvider aProvider = (AutoMountProvider) it.next();
86             if (aProvider == null) continue;
87             boolean retVal = aProvider.isRootOfFileSystem(resName);
88             if (retVal) return aProvider;
89         }
90
91         return null;
92     }
93
94 // }
95

96     static private Map getProviderMap() {
97         synchronized (MasterFileSystem.class) {
98             if (providerMap == null) {
99                 providerMap = Collections.synchronizedMap(new HashMap());
100                 final Lookup.Result providerResult =
101                         Lookup.getDefault().lookup(new Lookup.Template(FileSystemProvider.class));
102                 preventsLookupResultFromGC = providerResult;
103                 initFileSystemProviders(providerResult);
104                 providerResult.addLookupListener(new LookupListener() {
105                     public void resultChanged(LookupEvent ev) {
106                         initFileSystemProviders(providerResult);
107                     }
108                 });
109             }
110         }
111         return providerMap;
112     }
113
114     static private void initFileSystemProviders(Lookup.Result result) {
115         Set newProviders = new HashSet(result.allInstances());
116         
117         Map upToDateMap = new HashMap(newProviders.size());
118         Iterator it = newProviders.iterator();
119         while (it.hasNext()) {
120             FileSystemProvider provider = (FileSystemProvider) it.next();
121             AutoMountProvider aProvider = (AutoMountProvider) getProviderMap().get(provider);
122             if (aProvider == null) {
123                 if (!getProviderMap().keySet().contains(provider)) {
124                     aProvider = provider.initialize(MountTable.getDefault().mountSupport);
125                 }
126             }
127             upToDateMap.put(provider, aProvider);
128         }
129         getProviderMap().clear();
130         getProviderMap().putAll(upToDateMap);
131     }
132
133 }
134
Popular Tags