📄 pluginmanager.java
字号:
{ if (!checkSelfNamed(Class.forName(iname))) log.error("The class \""+iname+"\" is NOT a subclass of SelfNamedPlugin but it should be!"); } catch (ClassNotFoundException ce) { log.error("No class definition found for self-named class interface: \""+iname+"\""); } return false; } // recursively climb superclass stack until we find SelfNamedPlugin private static boolean checkSelfNamed(Class cls) { Class sup = cls.getSuperclass(); if (sup == null) return false; else if (sup.equals(SelfNamedPlugin.class)) return true; else return checkSelfNamed(sup); } // check named-plugin names by interface -- call the usual // configuration and let it find missing or duplicate names. private static void checkNames(String iname) { try { configureNamedPlugin(iname); } catch (ClassNotFoundException ce) { // bogus classname should be old news by now. } } /** * Validate the entries in the DSpace Configuration relevant to * PluginManager. Look for inconsistencies, illegal syntax, etc. * Announce violations with "log.error" so they appear in the log * or in the standard error stream if this is run interactively. * <ul> * <li>Look for duplicate keys (by parsing the config file) * <li>Interface in plugin.single, plugin.sequence, plugin.named, plugin.selfnamed is valid. * <li>Classname in plugin.reusable exists and matches a plugin config. * <li>Classnames in config values exist. * <li>Classnames in plugin.selfnamed loads and is subclass of <code>SelfNamedPlugin</code> * <li>Implementations of named plugin have no name collisions. * <li>Named plugin entries lacking names. * </ul> */ public static void checkConfiguration() throws IOException { /* XXX TODO: (maybe) test that implementation class is really a * subclass or impl of the plugin "interface" */ // tables of config keys for each type of config line: Map singleKey = new HashMap(); Map sequenceKey = new HashMap(); Map namedKey = new HashMap(); Map selfnamedKey = new HashMap(); Map reusableKey = new HashMap(); // 1. First pass -- grovel the actual config file to check for // duplicate keys, since Properties class hides them from us. // Also build lists of each type of key, check for misspellings. File config = ConfigurationManager.getConfigurationFile(); BufferedReader cr = new BufferedReader(new FileReader(config)); String line = null; boolean continued = false; HashMap keyMap = new HashMap(); Pattern keyPattern = Pattern.compile("([^\\s\\=\\:]+)"); while ((line = cr.readLine()) != null) { line = line.trim(); if (line.startsWith("!") || line.startsWith("#")) continued = false; else { if (!continued && line.startsWith("plugin.")) { Matcher km = keyPattern.matcher(line); if (km.find()) { String key = line.substring(0, km.end(1)); if (keyMap.containsKey(key)) log.error("Duplicate key \""+key+"\" in DSpace configuration file="+config.toString()); else keyMap.put(key, key); if (key.startsWith(SINGLE_PREFIX)) singleKey.put(key.substring(SINGLE_PREFIX.length()), key); else if (key.startsWith(SEQUENCE_PREFIX)) sequenceKey.put(key.substring(SEQUENCE_PREFIX.length()), key); else if (key.startsWith(NAMED_PREFIX)) namedKey.put(key.substring(NAMED_PREFIX.length()), key); else if (key.startsWith(SELFNAMED_PREFIX)) selfnamedKey.put(key.substring(SELFNAMED_PREFIX.length()), key); else if (key.startsWith(REUSABLE_PREFIX)) reusableKey.put(key.substring(REUSABLE_PREFIX.length()), key); else log.error("Key with unknown prefix \""+key+"\" in DSpace configuration file="+config.toString()); } } continued = line.length() > 0 && line.charAt(line.length()-1) == '\\'; } } // 1.1 Sanity check, make sure keyMap == set of keys from Configuration Enumeration pne = ConfigurationManager.propertyNames(); HashSet pn = new HashSet(); while (pne.hasMoreElements()) { String nk = (String)pne.nextElement(); if (nk.startsWith("plugin.")) { pn.add(nk); if (!keyMap.containsKey(nk)) log.error("Key is in ConfigurationManager.propertyNames() but NOT text crawl: \""+nk+"\""); } } Iterator pi = keyMap.keySet().iterator(); while (pi.hasNext()) { String key = (String)pi.next(); if (!pn.contains(key)) log.error("Key is in text crawl but NOT ConfigurationManager.propertyNames(): \""+key+"\""); } // 2. Build up list of all interfaces and test that they are loadable. // don't bother testing that they are "interface" rather than "class" // since either one will work for the Plugin Manager. ArrayList allInterfaces = new ArrayList(); allInterfaces.addAll(singleKey.keySet()); allInterfaces.addAll(sequenceKey .keySet()); allInterfaces.addAll(namedKey.keySet()); allInterfaces.addAll(selfnamedKey.keySet()); allInterfaces.addAll(reusableKey.keySet()); Iterator ii = allInterfaces.iterator(); while (ii.hasNext()) checkClassname((String)ii.next(), "key interface or class"); // Check implementation classes: // - each class is loadable. // - plugin.selfnamed values are each subclass of SelfNamedPlugin // - save classname in allImpls Map allImpls = new HashMap(); // single plugins - just check that it has a valid impl. class ii = singleKey.keySet().iterator(); while (ii.hasNext()) { String key = (String)ii.next(); String val = ConfigurationManager.getProperty(SINGLE_PREFIX+key); if (val == null) log.error("Single plugin config not found for: "+SINGLE_PREFIX+key); else { val = val.trim(); if (checkClassname(val, "implementation class")) allImpls.put(val, val); } } // sequence plugins - all values must be classes ii = sequenceKey.keySet().iterator(); while (ii.hasNext()) { String key = (String)ii.next(); String val = ConfigurationManager.getProperty(SEQUENCE_PREFIX+key); if (val == null) log.error("Sequence plugin config not found for: "+SEQUENCE_PREFIX+key); else { val = val.trim(); String classname[] = val.split("\\s*,\\s*"); for (int i = 0; i < classname.length; ++i) if (checkClassname(classname[i], "implementation class")) allImpls.put(classname[i], classname[i]); } } // 3. self-named plugins - grab and check all values // then make sure it is a subclass of SelfNamedPlugin ii = selfnamedKey.keySet().iterator(); while (ii.hasNext()) { String key = (String)ii.next(); String val = ConfigurationManager.getProperty(SELFNAMED_PREFIX+key); if (val == null) log.error("Selfnamed plugin config not found for: "+SELFNAMED_PREFIX+key); else { val = val.trim(); String classname[] = val.split("\\s*,\\s*"); for (int i = 0; i < classname.length; ++i) if (checkClassname(classname[i], "selfnamed implementation class")) { allImpls.put(classname[i], classname[i]); checkSelfNamed(classname[i]); } checkNames(key); } } // 4. named plugins - extract the classnames and treat same as sequence. // use named plugin config mechanism to test for duplicates, unnamed. ii = namedKey.keySet().iterator(); Pattern classnameEqual = Pattern.compile("([\\w\\p{Sc}\\.]+)\\s*\\="); while (ii.hasNext()) { String key = (String)ii.next(); String val = ConfigurationManager.getProperty(NAMED_PREFIX+key); if (val == null) log.error("Named plugin config not found for: "+NAMED_PREFIX+key); else { checkNames(key); val = val.trim(); Matcher classMatcher = classnameEqual.matcher(val); while (classMatcher.find()) { String classname = classMatcher.group(1); if (checkClassname(classname, "implementation class")) allImpls.put(classname, classname); } } } // 5. all classes named in Reusable config lines must be other classes. Iterator ri = reusableKey.keySet().iterator(); while (ri.hasNext()) { String rk = (String)ri.next(); if (!(allImpls.containsKey(rk))) log.error("In plugin.reusable configuration, class \""+rk+"\" is NOT a plugin implementation class."); } } /** * Invoking this class from the command line just runs * <code>checkConfiguration</code> and shows the results. * There are no command-line options. */ public static void main(String[] argv) throws Exception { checkConfiguration(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -