1 5 package net.nutch.plugin; 6 import java.io.File ; 7 import java.io.IOException ; 8 import java.net.MalformedURLException ; 9 import java.net.URL ; 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.Locale ; 13 import java.util.MissingResourceException ; 14 import java.util.ResourceBundle ; 15 import java.util.logging.Logger ; 16 import net.nutch.util.LogFormatter; 17 27 public class PluginDescriptor { 28 private String fPluginPath; 29 private String fPluginClass = Plugin.class.getName(); 30 private String fPluginId; 31 private String fVersion; 32 private String fName; 33 private String fProviderName; 34 private HashMap fMessages = new HashMap (); 35 private ArrayList fExtensionPoints = new ArrayList (); 36 private ArrayList fDependencies = new ArrayList (); 37 private ArrayList fExportedLibs = new ArrayList (); 38 private ArrayList fNotExportedLibs = new ArrayList (); 39 private ArrayList fExtensions = new ArrayList (); 40 private PluginClassLoader fClassLoader; 41 public static final Logger LOG = LogFormatter 42 .getLogger(PluginDescriptor.class.getName()); 43 53 public PluginDescriptor(String pId, String pVersion, String pName, 54 String pProviderName, String pPluginclazz, String pPath) { 55 setPath(pPath); 56 setPluginId(pId); 57 setVersion(pVersion); 58 setName(pName); 59 setProvidername(pProviderName); 60 61 if (pPluginclazz != null) 62 setPluginClass(pPluginclazz); 63 64 } 65 68 private void setPath(String pPath) { 69 fPluginPath = pPath; 70 } 71 76 public String getName() { 77 return fName; 78 } 79 82 private void setProvidername(String providerName) { 83 fProviderName = providerName; 84 } 85 88 private void setName(String name) { 89 fName = name; 90 } 91 94 private void setVersion(String version) { 95 fVersion = version; 96 } 97 103 public String getPluginClass() { 104 return fPluginClass; 105 } 106 111 public String getPluginId() { 112 return fPluginId; 113 } 114 119 public Extension[] getExtensions() { 120 return (Extension[]) fExtensions.toArray(new Extension[fExtensions 121 .size()]); 122 } 123 128 public void addExtension(Extension pExtension) { 129 fExtensions.add(pExtension); 130 } 131 137 private void setPluginClass(String pluginClass) { 138 fPluginClass = pluginClass; 139 } 140 146 private void setPluginId(String pluginId) { 147 fPluginId = pluginId; 148 } 149 154 public void addExtensionPoint(ExtensionPoint extensionPoint) { 155 fExtensionPoints.add(extensionPoint); 156 } 157 162 public ExtensionPoint[] getExtenstionPoints() { 163 return (ExtensionPoint[]) fExtensionPoints 164 .toArray(new ExtensionPoint[fExtensionPoints.size()]); 165 } 166 171 public String [] getDependencies() { 172 return (String []) fDependencies 173 .toArray(new String [fDependencies.size()]); 174 } 175 181 public void addDependency(String pId) { 182 fDependencies.add(pId); 183 } 184 189 public void addExportedLibRelative(String pLibPath) 190 throws MalformedURLException { 191 URL url = new File (getPluginPath() + File.separator + pLibPath).toURL(); 192 fExportedLibs.add(url); 193 } 194 199 public String getPluginPath() { 200 return fPluginPath; 201 } 202 207 public URL [] getExportedLibUrls() { 208 return (URL []) fExportedLibs.toArray(new URL [0]); 209 } 210 215 public void addNotExportedLibRelative(String pLibPath) 216 throws MalformedURLException { 217 URL url = new File (getPluginPath() + File.separator + pLibPath).toURL(); 218 fNotExportedLibs.add(url); 219 } 220 225 public URL [] getNotExportedLibUrls() { 226 return (URL []) fNotExportedLibs 227 .toArray(new URL [fNotExportedLibs.size()]); 228 } 229 236 public PluginClassLoader getClassLoader() { 237 if (fClassLoader != null) 238 return fClassLoader; 239 ArrayList arrayList = new ArrayList (); 240 arrayList.addAll(fExportedLibs); 241 arrayList.addAll(fNotExportedLibs); 242 arrayList.addAll(getDependencyLibs()); 243 File file = new File (getPluginPath()); 244 File [] files = file.listFiles(); 245 try { 246 for (int i = 0; i < files.length; i++) { 247 File file2 = files[i]; 248 String path = file2.getAbsolutePath(); 249 if (file2.getAbsolutePath().endsWith("properties")) 250 arrayList.add(file2.getParentFile().toURL()); 251 } 252 } catch (MalformedURLException e) { 253 LOG.fine(getPluginId() + " " + e.toString()); 254 } 255 URL [] urls = (URL []) arrayList.toArray(new URL [arrayList.size()]); 256 fClassLoader = new PluginClassLoader(urls, PluginDescriptor.class 257 .getClassLoader()); 258 return fClassLoader; 259 } 260 263 private ArrayList getDependencyLibs() { 264 ArrayList list = new ArrayList (); 265 collectLibs(list, this); 266 return list; 267 } 268 271 private void collectLibs(ArrayList pLibs, PluginDescriptor pDescriptor) { 272 String [] pPluginIds = pDescriptor.getDependencies(); 273 for (int i = 0; i < pPluginIds.length; i++) { 274 String id = pPluginIds[i]; 275 PluginDescriptor descriptor = PluginRepository.getInstance() 276 .getPluginDescriptor(id); 277 URL [] libs = descriptor.getExportedLibUrls(); 278 for (int j = 0; j < libs.length; j++) { 279 URL url = libs[j]; 280 pLibs.add(url); 281 } 282 collectLibs(pLibs, descriptor); 283 } 284 } 285 295 public String getResourceString(String pKey, Locale pLocale) 296 throws IOException { 297 if (fMessages.containsKey(pLocale.toString())) { 298 ResourceBundle bundle = (ResourceBundle ) fMessages.get(pLocale 299 .toString()); 300 try { 301 return bundle.getString(pKey); 302 } catch (MissingResourceException e) { 303 return '!' + pKey + '!'; 304 } 305 } 306 try { 307 ResourceBundle res = ResourceBundle.getBundle("messages", pLocale, 308 getClassLoader()); 309 return res.getString(pKey); 310 } catch (MissingResourceException x) { 311 return '!' + pKey + '!'; 312 } 313 } 314 } 315 | Popular Tags |