KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > source > usages > BinaryAnalyser


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.retouche.source.usages;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
31 import org.netbeans.api.progress.ProgressHandle;
32 import org.netbeans.modules.retouche.source.util.LowMemoryEvent;
33 import org.netbeans.modules.retouche.source.util.LowMemoryListener;
34
35
36 /**
37  * This file is originally from Retouche, the Java Support
38  * infrastructure in NetBeans. I have modified the file as little
39  * as possible to make merging Retouche fixes back as simple as
40  * possible.
41  *
42  *
43  * @author Petr Hrebejk, Tomas Zezula
44  */

45 public class BinaryAnalyser implements LowMemoryListener {
46     
47     static final String JavaDoc OBJECT = Object JavaDoc.class.getName();
48     
49     private final Index index;
50     private final Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> refs = new HashMap JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>>();
51     private final Set JavaDoc<String JavaDoc> toDelete = new HashSet JavaDoc<String JavaDoc> ();
52     private final AtomicBoolean JavaDoc lowMemory;
53     private boolean cacheCleared;
54
55     public BinaryAnalyser (Index index) {
56        assert index != null;
57        this.index = index;
58        this.lowMemory = new AtomicBoolean JavaDoc (false);
59     }
60     
61         /** Analyses a classpath root.
62      * @param URL the classpath root, either a folder or an archive file.
63      *
64      */

65     public final void analyse (final File JavaDoc root, final ProgressHandle handle) throws IOException JavaDoc, IllegalArgumentException JavaDoc {
66 // assert root != null;
67
// ClassIndexManager.getDefault().writeLock(new ClassIndexManager.ExceptionAction<Void> () {
68
// public Void run () throws IOException {
69
// LowMemoryNotifier.getDefault().addLowMemoryListener (BinaryAnalyser.this);
70
// try {
71
// if (root.isDirectory()) { //NOI18N
72
// String path = root.getAbsolutePath ();
73
// if (path.charAt(path.length()-1) != File.separatorChar) {
74
// path = path + File.separatorChar;
75
// }
76
// cacheCleared = false;
77
// analyseFolder(root, path);
78
// }
79
// else {
80
// if (root.exists() && root.canRead()) {
81
// if (!isUpToDate(null,root.lastModified())) {
82
// index.clear();
83
// if (handle != null) { //Tests don't provide handle
84
// handle.setDisplayName (String.format(NbBundle.getMessage(RepositoryUpdater.class,"MSG_Analyzing"),root.getAbsolutePath()));
85
// }
86
// final ZipFile zipFile = new ZipFile(root);
87
// try {
88
// analyseArchive( zipFile );
89
// }
90
// finally {
91
// zipFile.close();
92
// }
93
// }
94
// }
95
// }
96
// } finally {
97
// LowMemoryNotifier.getDefault().removeLowMemoryListener(BinaryAnalyser.this);
98
// }
99
// store();
100
// return null;
101
// }});
102
}
103     
104         /** Analyses a folder
105      * @param folder to analyze
106      * @param rootURL the url of root, it would be nicer to pass an URL type,
107      * but the {@link URL#toExternalForm} from some strange reason does not cache result,
108      * the String type is faster.
109      */

110     private void analyseFolder (final File JavaDoc folder, final String JavaDoc rootPath) throws IOException JavaDoc {
111 // if (folder.exists() && folder.canRead()) {
112
// File[] children = folder.listFiles();
113
// for (File file : children) {
114
// if (file.isDirectory()) {
115
// analyseFolder(file, rootPath);
116
// }
117
// else if (this.accepts(file.getName())) {
118
// String filePath = file.getAbsolutePath();
119
// long fileMTime = file.lastModified();
120
// int dotIndex = filePath.lastIndexOf('.');
121
// int slashIndex = filePath.lastIndexOf('/');
122
// int endPos;
123
// if (dotIndex>slashIndex) {
124
// endPos = dotIndex;
125
// }
126
// else {
127
// endPos = filePath.length();
128
// }
129
// String relativePath = FileObjects.convertFolder2Package (filePath.substring(rootPath.length(), endPos));
130
// if (!isUpToDate (relativePath, fileMTime)) {
131
// if (!cacheCleared) {
132
// this.index.clear();
133
// cacheCleared = true;
134
// }
135
// InputStream in = new BufferedInputStream (new FileInputStream (file));
136
// analyse (in);
137
// if (this.lowMemory.getAndSet(false)) {
138
// this.store();
139
// }
140
// }
141
// }
142
// }
143
// }
144
}
145     
146     public void lowMemory (final LowMemoryEvent event) {
147         this.lowMemory.set(true);
148     }
149
150     // Implementation of StreamAnalyser ----------------------------------------
151

152     private boolean accepts(String JavaDoc name) {
153         int index = name.lastIndexOf('.');
154         if (index == -1 || (index+1) == name.length()) {
155             return false;
156         }
157         return "CLASS".equalsIgnoreCase(name.substring(index+1)); // NOI18N
158
}
159
160     //Cleans up usages of deleted class
161
private final void delete (final String JavaDoc className) throws IOException JavaDoc {
162         assert className != null;
163         if (!this.index.isValid(false)) {
164             return;
165         }
166         this.toDelete.add(className);
167     }
168     
169     private void analyse (InputStream JavaDoc inputStream ) throws IOException JavaDoc {
170 // throw new RuntimeException("Not yet implemented");
171
}
172     
173     
174     private final boolean isUpToDate(String JavaDoc resourceName, long resourceMTime) throws IOException JavaDoc {
175         return this.index.isUpToDate(resourceName, resourceMTime);
176     }
177 }
178
Popular Tags