1 11 package org.eclipse.help.internal.search; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.ArrayList ; 18 import java.util.List ; 19 import java.util.Properties ; 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 COMPLETE_FILENAME = "indexed_complete"; 33 private String pluginId; 34 35 38 private String path; 39 40 private SearchIndex targetIndex; 41 42 45 private List indexIDs; 46 47 51 private List resolvedPaths; 52 53 public PluginIndex(String pluginId, String 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 return; 64 } 65 indexIDs = new ArrayList (); 66 resolvedPaths = new ArrayList (); 67 Bundle bundle = Platform.getBundle(pluginId); 68 if (bundle == null) { 69 return; 70 } 71 boolean found = false; 72 ArrayList availablePrefixes = ResourceLocator.getPathPrefix(targetIndex 73 .getLocale()); 74 for (int i = 0; i < availablePrefixes.size(); i++) { 75 String prefix = (String ) availablePrefixes.get(i); 76 IPath prefixedPath = new Path(prefix + path); 77 URL 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 resolved; 87 try { 88 resolved = FileLocator.resolve(url); 89 } catch (IOException ioe) { 90 HelpBasePlugin.logError("Help index directory at " + prefixedPath + " for plugin " + bundle.getSymbolicName() + " cannot be resolved.", ioe); 94 continue; 95 } 96 if ("file".equals(resolved.getProtocol())) { indexIDs.add(getIndexId(prefix)); 98 resolvedPaths.add(resolved.getFile()); 99 if (isComplete(bundle, prefixedPath)) { 100 break; 102 } 103 } else { 104 try { 105 URL localURL = FileLocator.toFileURL(url); 107 if ("file".equals(localURL.getProtocol())) { indexIDs.add(getIndexId(prefix)); 109 resolvedPaths.add(localURL.getFile()); 110 if (isComplete(bundle, prefixedPath)) { 111 break; 113 } 114 } 115 } catch (IOException ioe) { 116 HelpBasePlugin.logError( 117 "Help index directory at " + prefixedPath + " for plugin " + bundle.getSymbolicName() + " cannot be resolved.", ioe); continue; 121 } 122 } 123 } 124 if (!found) { 125 HelpBasePlugin.logError( 126 "Help index declared, but missing for plugin " + getPluginId() + ".", null); 129 } 130 } 131 132 private boolean isCompatible(Bundle bundle, IPath prefixedPath) { 133 URL 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 \"" + path + "\" of plugin " + getPluginId(), null); 141 return false; 142 } 143 InputStream in = null; 144 try { 145 in = url.openStream(); 146 Properties prop = new Properties (); 147 prop.load(in); 148 String lucene = prop 149 .getProperty(SearchIndex.DEPENDENCIES_KEY_LUCENE); 150 String analyzer = prop 151 .getProperty(SearchIndex.DEPENDENCIES_KEY_ANALYZER); 152 if (!targetIndex.isLuceneCompatible(lucene) 153 || !targetIndex.isAnalyzerCompatible(analyzer)) { 154 return false; 155 } 156 } catch (MalformedURLException mue) { 157 return false; 158 } catch (IOException ioe) { 159 HelpBasePlugin.logError( 160 "IOException accessing prebuilt index.", ioe); } finally { 162 if (in != null) { 163 try { 164 in.close(); 165 } catch (IOException e) { 166 } 167 } 168 } 169 return true; 170 } 171 172 private boolean isComplete(Bundle bundle, IPath prefixedPath) { 173 URL url = FileLocator.find(bundle, prefixedPath.append(COMPLETE_FILENAME), null); 174 return url != null; 175 } 176 177 184 private String getIndexId(String prefix) { 185 if (prefix.length() == 0) { 186 return "/"; } 189 return "/" + prefix.substring(0, prefix.length() - 1); } 191 192 public boolean equals(Object obj) { 193 return pluginId.equals(obj); 194 } 195 196 public int hashCode() { 197 return pluginId.hashCode(); 198 } 199 200 public String toString() { 201 StringBuffer ret = new StringBuffer (pluginId); 202 ret.append(":"); ret.append(path); 204 ret.append("="); if (indexIDs == null) { 206 ret.append("unresolved"); } else { 208 for (int i = 0; i < indexIDs.size(); i++) { 209 ret.append(indexIDs.get(i)); 210 ret.append("@"); ret.append(resolvedPaths.get(i)); 212 } 213 } 214 return ret.toString(); 215 } 216 217 public List getIDs() { 218 resolve(); 219 return indexIDs; 220 } 221 222 226 public List getPaths() { 227 resolve(); 228 return resolvedPaths; 229 } 230 231 public String getPluginId() { 232 return pluginId; 233 } 234 235 } 236 | Popular Tags |