1 19 package org.java.plugin.registry.xml; 20 21 import java.net.URL ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.Locale ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.java.plugin.PathResolver; 31 import org.java.plugin.registry.Identity; 32 import org.java.plugin.registry.IntegrityCheckReport; 33 import org.java.plugin.util.IoUtil; 34 import org.java.plugin.util.ResourceManager; 35 36 37 40 class IntegrityChecker implements IntegrityCheckReport { 41 private static Log log = LogFactory.getLog(IntegrityChecker.class); 42 43 private final PluginRegistryImpl registry; 44 private List items = new LinkedList (); 45 private int errorsCount; 46 private int warningsCount; 47 48 IntegrityChecker(final PluginRegistryImpl aRegistry, 49 final Collection anItems) { 50 this.items = new LinkedList (); 51 this.registry = aRegistry; 52 for (Iterator it = anItems.iterator(); it.hasNext();) { 53 ReportItem item = (ReportItem) it.next(); 54 if (item.getSeverity() == ReportItem.SEVERITY_ERROR) { 55 errorsCount++; 56 } else if (item.getSeverity() == ReportItem.SEVERITY_WARNING) { 57 warningsCount++; 58 } 59 this.items.add(item); 60 } 61 } 62 63 void doCheck(final PathResolver pathResolver) { 64 int count = 0; 65 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, null, 66 ReportItem.ERROR_NO_ERROR, "pluginsCheckStart", null)); try { 68 for (Iterator it = registry.getPluginDescriptors().iterator(); 69 it.hasNext();) { 70 PluginDescriptorImpl descr = (PluginDescriptorImpl) it.next(); 71 count++; 72 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 73 ReportItem.ERROR_NO_ERROR, "pluginCheckStart", descr.getUniqueId())); 75 checkPlugin(descr, pathResolver); 76 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 77 ReportItem.ERROR_NO_ERROR, "pluginCheckFinish", descr.getUniqueId())); 79 } 80 } catch (Exception e) { 81 log.error("integrity check failed for registry " + registry, e); errorsCount++; 83 items.add(new ReportItemImpl(ReportItem.SEVERITY_ERROR, null, 84 ReportItem.ERROR_CHECKER_FAULT, "pluginsCheckError", e)); } 86 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, null, 87 ReportItem.ERROR_NO_ERROR, "pluginsCheckFinish", new Integer (count))); 89 } 90 91 private void checkPlugin(final PluginDescriptorImpl descr, 92 final PathResolver pathResolver) { 93 int count = 0; 95 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 96 ReportItem.ERROR_NO_ERROR, "prerequisitesCheckStart", descr.getUniqueId())); 98 for (Iterator it = descr.getPrerequisites().iterator(); it.hasNext();) { 99 PluginPrerequisiteImpl pre = (PluginPrerequisiteImpl) it.next(); 100 count++; 101 if (!pre.isOptional() && !pre.matches()) { 102 errorsCount++; 103 items.add(new ReportItemImpl(ReportItem.SEVERITY_ERROR, descr, 104 ReportItem.ERROR_UNSATISFIED_PREREQUISITE, 105 "unsatisfiedPrerequisite", new Object [] { pre.getPluginId(), descr.getUniqueId()})); 107 } 108 } 109 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 110 ReportItem.ERROR_NO_ERROR, "prerequisitesCheckFinish", new Object [] {new Integer (count), descr.getUniqueId()})); 112 if (pathResolver != null) { 114 count = 0; 115 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 116 ReportItem.ERROR_NO_ERROR, "librariesCheckStart", descr.getUniqueId())); 118 for (Iterator it = descr.getLibraries().iterator(); it.hasNext();) { 119 LibraryImpl lib = (LibraryImpl) it.next(); 120 count++; 121 URL url = pathResolver.resolvePath(lib, lib.getPath()); 122 if (!IoUtil.isResourceExists(url)) { 123 errorsCount++; 124 items.add(new ReportItemImpl(ReportItem.SEVERITY_ERROR, lib, 125 ReportItem.ERROR_BAD_LIBRARY, 126 "accesToResourceFailed", new Object [] { lib.getUniqueId(), descr.getUniqueId(), url})); 128 } 129 } 130 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 131 ReportItem.ERROR_NO_ERROR, "librariesCheckFinish", new Object [] {new Integer (count), descr.getUniqueId()})); 133 } else { 134 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 135 ReportItem.ERROR_NO_ERROR, "librariesCheckSkip", descr.getUniqueId())); 137 } 138 count = 0; 140 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 141 ReportItem.ERROR_NO_ERROR, "extPointsCheckStart", null)); for (Iterator it = descr.getExtensionPoints().iterator(); 143 it.hasNext();) { 144 count++; 145 ExtensionPointImpl extPoint = (ExtensionPointImpl) it.next(); 146 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, extPoint, 147 ReportItem.ERROR_NO_ERROR, "extPointCheckStart", extPoint.getUniqueId())); 149 Collection extPointItems = extPoint.validate(); 150 for (Iterator it2 = extPointItems.iterator(); it2.hasNext();) { 151 ReportItem item = (ReportItem) it2.next(); 152 if (item.getSeverity() == ReportItem.SEVERITY_ERROR) { 153 errorsCount++; 154 } else if (item.getSeverity() == ReportItem.SEVERITY_WARNING) { 155 warningsCount++; 156 } 157 items.add(item); 158 } 159 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, extPoint, 160 ReportItem.ERROR_NO_ERROR, "extPointCheckFinish", extPoint.getUniqueId())); 162 } 163 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 164 ReportItem.ERROR_NO_ERROR, "extPointsCheckFinish", new Object [] {new Integer (count), descr.getUniqueId()})); 166 count = 0; 168 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 169 ReportItem.ERROR_NO_ERROR, "extsCheckStart", null)); for (Iterator it = descr.getExtensions().iterator(); it.hasNext();) { 171 count++; 172 ExtensionImpl ext = (ExtensionImpl) it.next(); 173 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, ext, 174 ReportItem.ERROR_NO_ERROR, "extCheckStart", ext.getUniqueId())); 176 Collection extItems = ext.validate(); 177 for (Iterator it2 = extItems.iterator(); it2.hasNext();) { 178 ReportItem item = (ReportItem) it2.next(); 179 if (item.getSeverity() == ReportItem.SEVERITY_ERROR) { 180 errorsCount++; 181 } else if (item.getSeverity() == ReportItem.SEVERITY_WARNING) { 182 warningsCount++; 183 } 184 items.add(item); 185 } 186 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, ext, 187 ReportItem.ERROR_NO_ERROR, "extCheckFinish", ext.getUniqueId())); 189 } 190 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr, 191 ReportItem.ERROR_NO_ERROR, "extsCheckFinish", new Object [] {new Integer (count), descr.getUniqueId()})); 193 } 194 195 198 public int countErrors() { 199 return errorsCount; 200 } 201 202 205 public int countWarnings() { 206 return warningsCount; 207 } 208 209 212 public Collection getItems() { 213 return items; 214 } 215 216 static class ReportItemImpl implements ReportItem { 217 private final byte severity; 218 private final Identity source; 219 private final int code; 220 private final String msg; 221 private final Object data; 222 223 ReportItemImpl(final byte aSeverity, final Identity aSource, 224 final int aCode, final String aMsg, final Object aData) { 225 severity = aSeverity; 226 source = aSource; 227 code = aCode; 228 msg = aMsg; 229 data = aData; 230 } 231 232 235 public int getCode() { 236 return code; 237 } 238 239 242 public String getMessage() { 243 return ResourceManager.getMessage(PluginRegistryImpl.PACKAGE_NAME, 244 msg, data); 245 } 246 247 251 public String getMessage(Locale locale) { 252 return ResourceManager.getMessage(PluginRegistryImpl.PACKAGE_NAME, 253 msg, locale, data); 254 } 255 256 259 public byte getSeverity() { 260 return severity; 261 } 262 263 266 public Identity getSource() { 267 return source; 268 } 269 } 270 } 271 | Popular Tags |