Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.parsing.impl.indexing; |
43 |
|
44 |
import java.net.URL; |
45 |
import java.util.*; |
46 |
import org.netbeans.api.annotations.common.CheckForNull; |
47 |
import org.netbeans.api.annotations.common.NonNull; |
48 |
import org.netbeans.modules.parsing.spi.indexing.BinaryIndexer; |
49 |
import org.netbeans.modules.parsing.spi.indexing.BinaryIndexerFactory; |
50 |
import org.netbeans.modules.parsing.spi.indexing.ConstrainedBinaryIndexer; |
51 |
import org.netbeans.modules.parsing.spi.indexing.Context; |
52 |
import org.openide.filesystems.FileObject; |
53 |
import org.openide.util.Parameters; |
54 |
|
55 |
/** |
56 |
* |
57 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
58 |
* @author Tomas Zezula |
59 |
*/ |
60 |
public final class ProxyBinaryIndexerFactory extends BinaryIndexerFactory { |
61 |
|
62 |
private static final String ATTR_DELEGATE = "delegate"; //NOI18N |
63 |
private static final String ATTR_INDEXER_NAME = "name"; //NOI18N |
64 |
private static final String ATTR_INDEXER_VERSION = "version"; //NOI18N |
65 |
private static final String ATTR_REQUIRED_RES = "requiredResource"; //NOI18N |
66 |
private static final String ATTR_REQUIRED_MIME = "mimeType"; //NOI18N |
67 |
private static final String PROP_CHECKED = "ProxyBinaryIndexerFactory_checked"; //NOI18N |
68 |
private static final String PROP_MATCHED_FILES = "ProxyBinaryIndexerFactory_matchedFiles"; //NOI18N |
69 |
|
70 |
private final Map<String,Object> params; |
71 |
private final String indexerName; |
72 |
private final int indexerVersion; |
73 |
@org.netbeans.api.annotations.common.SuppressWarnings( |
74 |
value="DMI_COLLECTION_OF_URLS" |
75 |
/*,justification="URLs have never host part"*/) //NOI18N |
76 |
private final Set<URL> activeRoots; |
77 |
|
78 |
public ProxyBinaryIndexerFactory( |
79 |
@NonNull final Map<String,Object> params) { |
80 |
Parameters.notNull("params", params); //NOI18N |
81 |
this.params = params; |
82 |
indexerName = (String)params.get(ATTR_INDEXER_NAME); |
83 |
Parameters.notNull("indexerName", indexerName); //NOI18N |
84 |
Integer iv = (Integer)params.get(ATTR_INDEXER_VERSION); |
85 |
Parameters.notNull("version", iv); //NOI18N |
86 |
indexerVersion = iv; |
87 |
activeRoots = new HashSet<URL>(); |
88 |
} |
89 |
|
90 |
@Override |
91 |
public String getIndexerName() { |
92 |
return indexerName; |
93 |
} |
94 |
|
95 |
@Override |
96 |
public int getIndexVersion() { |
97 |
return indexerVersion; |
98 |
} |
99 |
|
100 |
@Override |
101 |
public BinaryIndexer createIndexer() { |
102 |
return new Indexer(); |
103 |
} |
104 |
|
105 |
@Override |
106 |
public void rootsRemoved(Iterable<? extends URL> removedRoots) { |
107 |
final Set<URL> filtered = new HashSet<URL>(); |
108 |
for (URL removedRoot : removedRoots) { |
109 |
if (activeRoots.remove(removedRoot)) { |
110 |
filtered.add(removedRoot); |
111 |
} |
112 |
} |
113 |
SPIAccessor.getInstance().rootsRemoved( |
114 |
getDelegate(), |
115 |
Collections.unmodifiableSet(filtered)); |
116 |
} |
117 |
|
118 |
@Override |
119 |
public boolean scanStarted (final Context context) { |
120 |
if (supports(context) != null) { |
121 |
return SPIAccessor.getInstance().scanStarted(getDelegate(),context); |
122 |
} |
123 |
return true; |
124 |
} |
125 |
|
126 |
@Override |
127 |
public void scanFinished (final Context context) { |
128 |
if (supports(context) != null) { |
129 |
SPIAccessor.getInstance().scanFinished(getDelegate(),context); |
130 |
} |
131 |
} |
132 |
|
133 |
|
134 |
@CheckForNull |
135 |
private Map<String,? extends Iterable<? extends FileObject>> supports(@NonNull final Context ctx) { |
136 |
final FileObject root = ctx.getRoot(); |
137 |
if (root == null) { |
138 |
return null; |
139 |
} |
140 |
if (Boolean.TRUE == SPIAccessor.getInstance().getProperty(ctx, PROP_CHECKED)) { |
141 |
return (Map<String,? extends Iterable<? extends FileObject>>) SPIAccessor.getInstance().getProperty(ctx, PROP_MATCHED_FILES); |
142 |
} |
143 |
final Map<String,Queue<FileObject>> matchedFiles = new HashMap<String, Queue<FileObject>>(); |
144 |
|
145 |
String s = (String) params.get(ATTR_REQUIRED_RES); |
146 |
final String[] requiredResources = s == null ? null : s.split(","); //NOI18N |
147 |
s = (String) params.get(ATTR_REQUIRED_MIME); |
148 |
final String[] mimeTypes = s == null ? null : s.split(","); //NOI18N |
149 |
try { |
150 |
if (requiredResources != null) { |
151 |
boolean found = false; |
152 |
for (String r : requiredResources) { |
153 |
if (root.getFileObject(r) != null) { |
154 |
found = true; |
155 |
break; |
156 |
} |
157 |
} |
158 |
if (!found) { |
159 |
return null; |
160 |
} |
161 |
} |
162 |
if (mimeTypes != null) { |
163 |
final Enumeration<? extends FileObject> fos = root.getChildren(true); |
164 |
final Set<String> mimeTypesSet = new HashSet<String>(Arrays.asList(mimeTypes)); |
165 |
while (fos.hasMoreElements()) { |
166 |
final FileObject f = fos.nextElement(); |
167 |
final String mt = f.getMIMEType(mimeTypes); |
168 |
if (mimeTypesSet.contains(mt)) { |
169 |
Queue<FileObject> q = matchedFiles.get(mt); |
170 |
if (q == null) { |
171 |
q = new ArrayDeque<FileObject>(); |
172 |
matchedFiles.put(mt, q); |
173 |
} |
174 |
q.offer(f); |
175 |
} |
176 |
} |
177 |
if (matchedFiles.isEmpty()) { |
178 |
return null; |
179 |
} |
180 |
} |
181 |
final Map<String, ? extends Iterable<? extends FileObject>> res = Collections.unmodifiableMap(matchedFiles); |
182 |
SPIAccessor.getInstance().putProperty(ctx, PROP_MATCHED_FILES, res); |
183 |
return res; |
184 |
} finally { |
185 |
SPIAccessor.getInstance().putProperty(ctx, PROP_CHECKED, Boolean.TRUE); |
186 |
} |
187 |
} |
188 |
|
189 |
@NonNull |
190 |
private ConstrainedBinaryIndexer getDelegate() { |
191 |
final Object delegate = params.get(ATTR_DELEGATE); |
192 |
if (!(delegate instanceof ConstrainedBinaryIndexer)) { |
193 |
throw new IllegalArgumentException(); |
194 |
} |
195 |
return (ConstrainedBinaryIndexer) delegate; |
196 |
} |
197 |
|
198 |
private final class Indexer extends BinaryIndexer { |
199 |
|
200 |
@Override |
201 |
protected void index(@NonNull final Context context) { |
202 |
final Map<String,? extends Iterable<? extends FileObject>> matchedFiles = supports(context); |
203 |
if (matchedFiles != null) { |
204 |
activeRoots.add(context.getRootURI()); |
205 |
SPIAccessor.getInstance().index( |
206 |
getDelegate(), |
207 |
matchedFiles, |
208 |
context); |
209 |
} |
210 |
} |
211 |
} |
212 |
} |