KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > naming > NamingFactory


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.filebasedfs.naming;
21
22 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo;
23
24 import java.io.File JavaDoc;
25 import java.lang.ref.Reference JavaDoc;
26 import java.lang.ref.WeakReference JavaDoc;
27 import java.util.*;
28 import org.netbeans.modules.masterfs.providers.ProvidedExtensions;
29
30 /**
31  * @author Radek Matous
32  */

33 public final class NamingFactory {
34     private static final Map nameMap = new WeakHashMap();
35
36     public static synchronized FileNaming fromFile(final File JavaDoc file) {
37         final LinkedList list = new LinkedList();
38         File JavaDoc current = file;
39         while (current != null) {
40             list.addFirst(current);
41             current = current.getParentFile();
42         }
43
44         FileNaming fileName = null;
45         for (int i = 0; i < list.size(); i++) {
46             fileName = NamingFactory.registerInstanceOfFileNaming(fileName, (File JavaDoc) list.get(i));
47         }
48
49         return fileName;
50     }
51
52     public static synchronized int getSize () {
53         return nameMap.size();
54     }
55     
56     public static synchronized FileNaming fromFile(final FileNaming parentFn, final File JavaDoc file) {
57         return NamingFactory.registerInstanceOfFileNaming(parentFn, file);
58     }
59     
60     public static synchronized void checkCaseSensitivity(final FileNaming childName, final File JavaDoc f) {
61         if (!childName.getFile().getName().equals(f.getName())) {
62             boolean isCaseSensitive = !new File JavaDoc(f,"a").equals(new File JavaDoc(f,"A"));//NOI18N
63
if (!isCaseSensitive) {
64                     NamingFactory.rename(childName,f.getName());
65             }
66         }
67     }
68
69     private static synchronized FileNaming[] rename (FileNaming fNaming, String JavaDoc newName) {
70         return rename(fNaming, newName, null);
71     }
72     
73     public static synchronized FileNaming[] rename (FileNaming fNaming, String JavaDoc newName, ProvidedExtensions.IOHandler handler) {
74         final ArrayList all = new ArrayList();
75         boolean retVal = false;
76         remove(fNaming, null);
77         retVal = fNaming.rename(newName, handler);
78         all.add(fNaming);
79         NamingFactory.registerInstanceOfFileNaming(fNaming.getParent(), fNaming.getFile(), fNaming);
80         renameChildren(all);
81         return (retVal) ? ((FileNaming[])all.toArray(new FileNaming[all.size()])) : null;
82     }
83
84     private static void renameChildren(final ArrayList all) {
85         HashMap toRename = new HashMap ();
86         for (Iterator iterator = nameMap.entrySet().iterator(); iterator.hasNext();) {
87             Map.Entry entry = (Map.Entry) iterator.next();
88             Integer JavaDoc id = (Integer JavaDoc)entry.getKey();
89             
90             List list = new ArrayList();
91             
92             //handle possible List
93
Object JavaDoc value = entry.getValue();
94             if (value instanceof Reference JavaDoc) {
95                 list.add(value);
96             } else if (value instanceof List) {
97                 list.addAll((List) value);
98             }
99             
100             for (int i = 0; i < list.size(); i++) {
101                 FileNaming fN = (FileNaming)((Reference JavaDoc) list.get(i)).get();
102                 if (fN == null) continue;
103                 Integer JavaDoc computedId = NamingFactory.createID(fN.getFile());
104                 
105                 boolean isRenamed = (!computedId.equals(id));
106                 if (isRenamed) {
107                     toRename.put(id, fN);
108                 }
109             }
110         }
111         
112         for (Iterator iterator = toRename.entrySet().iterator(); iterator.hasNext();) {
113             Map.Entry entry = (Map.Entry) iterator.next();
114             Integer JavaDoc id = (Integer JavaDoc)entry.getKey();
115             FileNaming fN = (FileNaming)entry.getValue();
116             all.add(fN);
117             remove(fN, id);
118             fN.getId(true);
119             NamingFactory.registerInstanceOfFileNaming(fN.getParent(), fN.getFile(), fN);
120         }
121     }
122
123     private static void remove(final FileNaming fNaming, Integer JavaDoc id) {
124         id = (id != null) ? id : fNaming.getId();
125         Object JavaDoc value = NamingFactory.nameMap.get(id);
126         if (value instanceof List) {
127             Reference JavaDoc ref = NamingFactory.getReference((List) value, fNaming.getFile());
128             if (ref != null) {
129                 ((List) value).remove(ref);
130             }
131         } else {
132             NamingFactory.nameMap.remove(id);
133         }
134     }
135
136     public static Integer JavaDoc createID(final File JavaDoc file) {
137         return new Integer JavaDoc(file.hashCode());
138     }
139     private static FileNaming registerInstanceOfFileNaming(final FileNaming parentName, final File JavaDoc file) {
140         return NamingFactory.registerInstanceOfFileNaming(parentName, file, null);
141     }
142
143     private static FileNaming registerInstanceOfFileNaming(final FileNaming parentName, final File JavaDoc file, final FileNaming newValue) {
144         FileNaming retVal;
145         
146         final Object JavaDoc value = NamingFactory.nameMap.get(new Integer JavaDoc(file.hashCode()));
147         Reference JavaDoc ref = (Reference JavaDoc) (value instanceof Reference JavaDoc ? value : null);
148         ref = (ref == null && value instanceof List ? NamingFactory.getReference((List) value, file) : ref);
149
150         final FileNaming cachedElement = (ref != null) ? (FileNaming) ref.get() : null;
151
152         if (cachedElement != null && cachedElement.getFile().compareTo(file) == 0) {
153             retVal = cachedElement;
154         } else {
155             retVal = (newValue == null) ? NamingFactory.createFileNaming(file, parentName) : newValue;
156             final WeakReference JavaDoc refRetVal = new WeakReference JavaDoc(retVal);
157
158             final boolean isList = (value instanceof List);
159             if (cachedElement != null || isList) {
160                 // List impl.
161
if (isList) {
162                     ((List) value).add(refRetVal);
163                 } else {
164                     final List l = new ArrayList();
165                     l.add(refRetVal);
166                     NamingFactory.nameMap.put(retVal.getId(), l);
167                 }
168             } else {
169                 // Reference impl.
170
Reference JavaDoc r = (Reference JavaDoc)NamingFactory.nameMap.put(retVal.getId(), refRetVal);
171                 assert r == null || r.get() == null;
172             }
173         }
174
175         assert retVal != null;
176
177         return retVal;
178     }
179
180     private static Reference JavaDoc getReference(final List list, final File JavaDoc f) {
181         Reference JavaDoc retVal = null;
182         for (int i = 0; retVal == null && i < list.size(); i++) {
183             final Reference JavaDoc ref = (Reference JavaDoc) list.get(i);
184             final FileNaming cachedElement = (ref != null) ? (FileNaming) ref.get() : null;
185             if (cachedElement != null && cachedElement.getFile().compareTo(f) == 0) {
186                 retVal = ref;
187             }
188         }
189         return retVal;
190     }
191
192     private static FileNaming createFileNaming(final File JavaDoc f, final FileNaming parentName) {
193         FileName retVal = null;
194         //TODO: check all tests for isFile & isDirectory
195
final FileInfo fInfo = new FileInfo(f);
196
197         if (f.isFile()) {
198             retVal = new FileName(parentName, f);
199         } else {
200             if (f.isDirectory()) {
201                 retVal = new FolderName(parentName, f);
202             } else {
203                 if (fInfo.isUNCFolder()) {
204                     retVal = new UNCName(parentName, f);
205                 }
206             }
207         }
208
209         if (retVal == null /*&& new FileInfo(f).isUnixSpecialFile()*/) {
210             // broken symlinks and other for me unknown files (sockets or whatever it is)
211
retVal = new FileName(parentName, f) {
212                 public boolean isDirectory() {
213                     return false;
214                 }
215
216                 public boolean isFile() {
217                     return false;
218                 }
219             };
220
221         }
222
223         assert retVal != null /*|| !fInfo.isConvertibleToFileObject()*/ : f.getAbsolutePath() + " isDirectory: " + f.isDirectory() + " isFile: " + f.isFile() + " exists: " + f.exists();//NOI18N
224
return retVal;
225     }
226
227 }
228
Popular Tags