001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.registry.xml;
019:
020: import java.net.URL;
021: import java.util.Collection;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Locale;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.java.plugin.PathResolver;
029: import org.java.plugin.registry.Extension;
030: import org.java.plugin.registry.ExtensionPoint;
031: import org.java.plugin.registry.Identity;
032: import org.java.plugin.registry.IntegrityCheckReport;
033: import org.java.plugin.registry.Library;
034: import org.java.plugin.registry.PluginDescriptor;
035: import org.java.plugin.registry.PluginPrerequisite;
036: import org.java.plugin.util.IoUtil;
037: import org.java.plugin.util.ResourceManager;
038:
039: /**
040: * @version $Id$
041: */
042: class IntegrityChecker implements IntegrityCheckReport {
043: private static Log log = LogFactory.getLog(IntegrityChecker.class);
044:
045: private final PluginRegistryImpl registry;
046: private List<ReportItem> items = new LinkedList<ReportItem>();
047: private int errorsCount;
048: private int warningsCount;
049:
050: IntegrityChecker(final PluginRegistryImpl aRegistry,
051: final Collection<ReportItem> anItems) {
052: this .items = new LinkedList<ReportItem>();
053: this .registry = aRegistry;
054: for (ReportItem item : anItems) {
055: switch (item.getSeverity()) {
056: case ERROR:
057: break;
058: case WARNING:
059: warningsCount++;
060: break;
061: case INFO:
062: // no-op
063: break;
064: }
065: this .items.add(item);
066: }
067: }
068:
069: void doCheck(final PathResolver pathResolver) {
070: int count = 0;
071: items.add(new ReportItemImpl(Severity.INFO, null,
072: Error.NO_ERROR, "pluginsCheckStart", null)); //$NON-NLS-1$
073: try {
074: for (PluginDescriptor descriptor : registry
075: .getPluginDescriptors()) {
076: PluginDescriptorImpl descr = (PluginDescriptorImpl) descriptor;
077: count++;
078: items.add(new ReportItemImpl(Severity.INFO, descr,
079: Error.NO_ERROR, "pluginCheckStart", //$NON-NLS-1$
080: descr.getUniqueId()));
081: checkPlugin(descr, pathResolver);
082: items.add(new ReportItemImpl(Severity.INFO, descr,
083: Error.NO_ERROR, "pluginCheckFinish", //$NON-NLS-1$
084: descr.getUniqueId()));
085: }
086: } catch (Exception e) {
087: log
088: .error(
089: "integrity check failed for registry " + registry, e); //$NON-NLS-1$
090: errorsCount++;
091: items.add(new ReportItemImpl(Severity.ERROR, null,
092: Error.CHECKER_FAULT, "pluginsCheckError", e)); //$NON-NLS-1$
093: }
094: items.add(new ReportItemImpl(Severity.INFO, null,
095: Error.NO_ERROR,
096: "pluginsCheckFinish", Integer.valueOf(count))); //$NON-NLS-1$
097: }
098:
099: private void checkPlugin(final PluginDescriptorImpl descr,
100: final PathResolver pathResolver) {
101: // checking prerequisites
102: int count = 0;
103: items.add(new ReportItemImpl(Severity.INFO, descr,
104: Error.NO_ERROR,
105: "prerequisitesCheckStart", descr.getUniqueId())); //$NON-NLS-1$
106: for (PluginPrerequisite prerequisite : descr.getPrerequisites()) {
107: PluginPrerequisiteImpl pre = (PluginPrerequisiteImpl) prerequisite;
108: count++;
109: if (!pre.isOptional() && !pre.matches()) {
110: errorsCount++;
111: items.add(new ReportItemImpl(Severity.ERROR, descr,
112: Error.UNSATISFIED_PREREQUISITE,
113: "unsatisfiedPrerequisite", new Object[] { //$NON-NLS-1$
114: pre.getPluginId(), descr.getUniqueId() }));
115: }
116: }
117: items.add(new ReportItemImpl(Severity.INFO, descr,
118: Error.NO_ERROR, "prerequisitesCheckFinish", //$NON-NLS-1$
119: new Object[] { Integer.valueOf(count),
120: descr.getUniqueId() }));
121: // checking libraries
122: if (pathResolver != null) {
123: count = 0;
124: items.add(new ReportItemImpl(Severity.INFO, descr,
125: Error.NO_ERROR,
126: "librariesCheckStart", descr.getUniqueId())); //$NON-NLS-1$
127: for (Library library : descr.getLibraries()) {
128: LibraryImpl lib = (LibraryImpl) library;
129: count++;
130: URL url = pathResolver.resolvePath(lib, lib.getPath());
131: if (!IoUtil.isResourceExists(url)) {
132: errorsCount++;
133: items
134: .add(new ReportItemImpl(
135: Severity.ERROR,
136: lib,
137: Error.BAD_LIBRARY,
138: "accesToResourceFailed", new Object[] { //$NON-NLS-1$
139: lib.getUniqueId(),
140: descr.getUniqueId(), url }));
141: }
142: }
143: items.add(new ReportItemImpl(Severity.INFO, descr,
144: Error.NO_ERROR, "librariesCheckFinish", //$NON-NLS-1$
145: new Object[] { Integer.valueOf(count),
146: descr.getUniqueId() }));
147: } else {
148: items.add(new ReportItemImpl(Severity.INFO, descr,
149: Error.NO_ERROR,
150: "librariesCheckSkip", descr.getUniqueId())); //$NON-NLS-1$
151: }
152: // checking extension points
153: count = 0;
154: items.add(new ReportItemImpl(Severity.INFO, descr,
155: Error.NO_ERROR, "extPointsCheckStart", null)); //$NON-NLS-1$
156: for (ExtensionPoint extensionPoint : descr.getExtensionPoints()) {
157: count++;
158: ExtensionPointImpl extPoint = (ExtensionPointImpl) extensionPoint;
159: items.add(new ReportItemImpl(Severity.INFO, extPoint,
160: Error.NO_ERROR, "extPointCheckStart", //$NON-NLS-1$
161: extPoint.getUniqueId()));
162: Collection<ReportItem> extPointItems = extPoint.validate();
163: for (ReportItem item : extPointItems) {
164: switch (item.getSeverity()) {
165: case ERROR:
166: errorsCount++;
167: break;
168: case WARNING:
169: warningsCount++;
170: break;
171: case INFO:
172: // no-op
173: break;
174: }
175: items.add(item);
176: }
177: items.add(new ReportItemImpl(Severity.INFO, extPoint,
178: Error.NO_ERROR, "extPointCheckFinish", //$NON-NLS-1$
179: extPoint.getUniqueId()));
180: }
181: items.add(new ReportItemImpl(Severity.INFO, descr,
182: Error.NO_ERROR, "extPointsCheckFinish", //$NON-NLS-1$
183: new Object[] { Integer.valueOf(count),
184: descr.getUniqueId() }));
185: // checking extensions
186: count = 0;
187: items.add(new ReportItemImpl(Severity.INFO, descr,
188: Error.NO_ERROR, "extsCheckStart", null)); //$NON-NLS-1$
189: for (Extension extension : descr.getExtensions()) {
190: count++;
191: ExtensionImpl ext = (ExtensionImpl) extension;
192: items
193: .add(new ReportItemImpl(Severity.INFO, ext,
194: Error.NO_ERROR,
195: "extCheckStart", ext.getUniqueId())); //$NON-NLS-1$
196: Collection<ReportItem> extItems = ext.validate();
197: for (ReportItem item : extItems) {
198: switch (item.getSeverity()) {
199: case ERROR:
200: errorsCount++;
201: break;
202: case WARNING:
203: warningsCount++;
204: break;
205: case INFO:
206: // no-op
207: break;
208: }
209: items.add(item);
210: }
211: items
212: .add(new ReportItemImpl(Severity.INFO, ext,
213: Error.NO_ERROR,
214: "extCheckFinish", ext.getUniqueId())); //$NON-NLS-1$
215: }
216: items.add(new ReportItemImpl(Severity.INFO, descr,
217: Error.NO_ERROR, "extsCheckFinish", //$NON-NLS-1$
218: new Object[] { Integer.valueOf(count),
219: descr.getUniqueId() }));
220: }
221:
222: /**
223: * @see org.java.plugin.registry.IntegrityCheckReport#countErrors()
224: */
225: public int countErrors() {
226: return errorsCount;
227: }
228:
229: /**
230: * @see org.java.plugin.registry.IntegrityCheckReport#countWarnings()
231: */
232: public int countWarnings() {
233: return warningsCount;
234: }
235:
236: /**
237: * @see org.java.plugin.registry.IntegrityCheckReport#getItems()
238: */
239: public Collection<ReportItem> getItems() {
240: return items;
241: }
242:
243: static class ReportItemImpl implements ReportItem {
244: private final Severity severity;
245: private final Identity source;
246: private final Error code;
247: private final String msg;
248: private final Object data;
249:
250: ReportItemImpl(final Severity aSeverity,
251: final Identity aSource, final Error aCode,
252: final String aMsg, final Object aData) {
253: severity = aSeverity;
254: source = aSource;
255: code = aCode;
256: msg = aMsg;
257: data = aData;
258: }
259:
260: /**
261: * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getCode()
262: */
263: public Error getCode() {
264: return code;
265: }
266:
267: /**
268: * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getMessage()
269: */
270: public String getMessage() {
271: return ResourceManager.getMessage(
272: PluginRegistryImpl.PACKAGE_NAME, msg, data);
273: }
274:
275: /**
276: * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getMessage(
277: * java.util.Locale)
278: */
279: public String getMessage(Locale locale) {
280: return ResourceManager.getMessage(
281: PluginRegistryImpl.PACKAGE_NAME, msg, locale, data);
282: }
283:
284: /**
285: * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getSeverity()
286: */
287: public Severity getSeverity() {
288: return severity;
289: }
290:
291: /**
292: * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getSource()
293: */
294: public Identity getSource() {
295: return source;
296: }
297: }
298: }
|