KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > PluginIndex


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.help.internal.search;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 import org.eclipse.core.runtime.FileLocator;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.help.internal.base.HelpBasePlugin;
26 import org.eclipse.help.internal.util.ResourceLocator;
27 import org.osgi.framework.Bundle;
28
29 public class PluginIndex {
30
31     private static final String JavaDoc COMPLETE_FILENAME = "indexed_complete"; //$NON-NLS-1$
32

33     private String JavaDoc pluginId;
34
35     /**
36      * index path as defined in plugin.xml, e.g. "index"
37      */

38     private String JavaDoc path;
39
40     private SearchIndex targetIndex;
41
42     /**
43      * path prefixes where index is found e.g. "", "nl/en/US", "ws/gtk"
44      */

45     private List JavaDoc indexIDs;
46
47     /**
48      * resolved directory paths (Strings) corresponding to indexes at given
49      * prefixes, e.g. //d/eclipse/...../os/linux/index,
50      */

51     private List JavaDoc resolvedPaths;
52
53     public PluginIndex(String JavaDoc pluginId, String JavaDoc path, SearchIndex targetIndex) {
54         super();
55         this.pluginId = pluginId;
56         this.path = path;
57         this.targetIndex = targetIndex;
58     }
59
60     private void resolve() {
61         if (indexIDs != null) {
62             // resolved
63
return;
64         }
65         indexIDs = new ArrayList JavaDoc();
66         resolvedPaths = new ArrayList JavaDoc();
67         Bundle bundle = Platform.getBundle(pluginId);
68         if (bundle == null) {
69             return;
70         }
71         boolean found = false;
72         ArrayList JavaDoc availablePrefixes = ResourceLocator.getPathPrefix(targetIndex
73                 .getLocale());
74         for (int i = 0; i < availablePrefixes.size(); i++) {
75             String JavaDoc prefix = (String JavaDoc) availablePrefixes.get(i);
76             IPath prefixedPath = new Path(prefix + path);
77             // find index at this directory in plugin or fragments
78
URL JavaDoc url = FileLocator.find(bundle, prefixedPath, null);
79             if (url == null) {
80                 continue;
81             }
82             found = true;
83             if (!isCompatible(bundle, prefixedPath)) {
84                 continue;
85             }
86             URL JavaDoc resolved;
87             try {
88                 resolved = FileLocator.resolve(url);
89             } catch (IOException JavaDoc ioe) {
90                 HelpBasePlugin.logError("Help index directory at " //$NON-NLS-1$
91
+ prefixedPath + " for plugin " //$NON-NLS-1$
92
+ bundle.getSymbolicName() + " cannot be resolved.", //$NON-NLS-1$
93
ioe);
94                 continue;
95             }
96             if ("file".equals(resolved.getProtocol())) { //$NON-NLS-1$
97
indexIDs.add(getIndexId(prefix));
98                 resolvedPaths.add(resolved.getFile());
99                 if (isComplete(bundle, prefixedPath)) {
100                     // don't process default language index
101
break;
102                 }
103             } else {
104                 try {
105                     // extract index from jarred bundles
106
URL JavaDoc localURL = FileLocator.toFileURL(url);
107                     if ("file".equals(localURL.getProtocol())) { //$NON-NLS-1$
108
indexIDs.add(getIndexId(prefix));
109                         resolvedPaths.add(localURL.getFile());
110                         if (isComplete(bundle, prefixedPath)) {
111                             // don't process default language index
112
break;
113                         }
114                     }
115                 } catch (IOException JavaDoc ioe) {
116                     HelpBasePlugin.logError(
117                             "Help index directory at " + prefixedPath //$NON-NLS-1$
118
+ " for plugin " + bundle.getSymbolicName() //$NON-NLS-1$
119
+ " cannot be resolved.", ioe); //$NON-NLS-1$
120
continue;
121                 }
122             }
123         }
124         if (!found) {
125             HelpBasePlugin.logError(
126                     "Help index declared, but missing for plugin " //$NON-NLS-1$
127
+ getPluginId() + ".", null); //$NON-NLS-1$
128

129         }
130     }
131
132     private boolean isCompatible(Bundle bundle, IPath prefixedPath) {
133         URL JavaDoc url = FileLocator.find(bundle, prefixedPath
134                 .append(SearchIndex.DEPENDENCIES_VERSION_FILENAME), null);
135         if (url == null) {
136             HelpBasePlugin.logError(prefixedPath
137                     .append(SearchIndex.DEPENDENCIES_VERSION_FILENAME)
138                     + " file missing from help index \"" //$NON-NLS-1$
139
+ path + "\" of plugin " + getPluginId(), null); //$NON-NLS-1$
140

141             return false;
142         }
143         InputStream JavaDoc in = null;
144         try {
145             in = url.openStream();
146             Properties JavaDoc prop = new Properties JavaDoc();
147             prop.load(in);
148             String JavaDoc lucene = prop
149                     .getProperty(SearchIndex.DEPENDENCIES_KEY_LUCENE);
150             String JavaDoc analyzer = prop
151                     .getProperty(SearchIndex.DEPENDENCIES_KEY_ANALYZER);
152             if (!targetIndex.isLuceneCompatible(lucene)
153                     || !targetIndex.isAnalyzerCompatible(analyzer)) {
154                 return false;
155             }
156         } catch (MalformedURLException JavaDoc mue) {
157             return false;
158         } catch (IOException JavaDoc ioe) {
159             HelpBasePlugin.logError(
160                     "IOException accessing prebuilt index.", ioe); //$NON-NLS-1$
161
} finally {
162             if (in != null) {
163                 try {
164                     in.close();
165                 } catch (IOException JavaDoc e) {
166                 }
167             }
168         }
169         return true;
170     }
171
172     private boolean isComplete(Bundle bundle, IPath prefixedPath) {
173         URL JavaDoc url = FileLocator.find(bundle, prefixedPath.append(COMPLETE_FILENAME), null);
174         return url != null;
175     }
176
177     /**
178      * Creates id of prebuilt index
179      *
180      * @param prefix
181      * index directory prefix, e.g. "", "ws/gtk"
182      * @return indexId string, e.g. "/", "/ws/gtk"
183      */

184     private String JavaDoc getIndexId(String JavaDoc prefix) {
185         if (prefix.length() == 0) {
186             // root
187
return "/"; //$NON-NLS-1$
188
}
189         return "/" + prefix.substring(0, prefix.length() - 1); //$NON-NLS-1$
190
}
191
192     public boolean equals(Object JavaDoc obj) {
193         return pluginId.equals(obj);
194     }
195
196     public int hashCode() {
197         return pluginId.hashCode();
198     }
199
200     public String JavaDoc toString() {
201         StringBuffer JavaDoc ret = new StringBuffer JavaDoc(pluginId);
202         ret.append(":"); //$NON-NLS-1$
203
ret.append(path);
204         ret.append("="); //$NON-NLS-1$
205
if (indexIDs == null) {
206             ret.append("unresolved"); //$NON-NLS-1$
207
} else {
208             for (int i = 0; i < indexIDs.size(); i++) {
209                 ret.append(indexIDs.get(i));
210                 ret.append("@"); //$NON-NLS-1$
211
ret.append(resolvedPaths.get(i));
212             }
213         }
214         return ret.toString();
215     }
216
217     public List JavaDoc getIDs() {
218         resolve();
219         return indexIDs;
220     }
221
222     /**
223      * @return list of paths (string) to an index directory. Paths are ordered
224      * from
225      */

226     public List JavaDoc getPaths() {
227         resolve();
228         return resolvedPaths;
229     }
230
231     public String JavaDoc getPluginId() {
232         return pluginId;
233     }
234
235 }
236
Popular Tags