KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > registry > xml > IntegrityChecker


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2005 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.registry.xml;
20
21 import java.net.URL JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Locale JavaDoc;
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 /**
38  * @version $Id: IntegrityChecker.java,v 1.3 2005/12/03 14:38:29 ddimon Exp $
39  */

40 class IntegrityChecker implements IntegrityCheckReport {
41     private static Log log = LogFactory.getLog(IntegrityChecker.class);
42
43     private final PluginRegistryImpl registry;
44     private List JavaDoc items = new LinkedList JavaDoc();
45     private int errorsCount;
46     private int warningsCount;
47
48     IntegrityChecker(final PluginRegistryImpl aRegistry,
49             final Collection JavaDoc anItems) {
50         this.items = new LinkedList JavaDoc();
51         this.registry = aRegistry;
52         for (Iterator JavaDoc 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)); //$NON-NLS-1$
67
try {
68             for (Iterator JavaDoc 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", //$NON-NLS-1$
74
descr.getUniqueId()));
75                 checkPlugin(descr, pathResolver);
76                 items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
77                         ReportItem.ERROR_NO_ERROR, "pluginCheckFinish", //$NON-NLS-1$
78
descr.getUniqueId()));
79             }
80         } catch (Exception JavaDoc e) {
81             log.error("integrity check failed for registry " + registry, e); //$NON-NLS-1$
82
errorsCount++;
83             items.add(new ReportItemImpl(ReportItem.SEVERITY_ERROR, null,
84                     ReportItem.ERROR_CHECKER_FAULT, "pluginsCheckError", e)); //$NON-NLS-1$
85
}
86         items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, null,
87                 ReportItem.ERROR_NO_ERROR, "pluginsCheckFinish", //$NON-NLS-1$
88
new Integer JavaDoc(count)));
89     }
90     
91     private void checkPlugin(final PluginDescriptorImpl descr,
92             final PathResolver pathResolver) {
93         // checking prerequisites
94
int count = 0;
95         items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
96                 ReportItem.ERROR_NO_ERROR, "prerequisitesCheckStart", //$NON-NLS-1$
97
descr.getUniqueId()));
98         for (Iterator JavaDoc 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 JavaDoc[] { //$NON-NLS-1$
106
pre.getPluginId(), descr.getUniqueId()}));
107             }
108         }
109         items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
110                 ReportItem.ERROR_NO_ERROR, "prerequisitesCheckFinish", //$NON-NLS-1$
111
new Object JavaDoc[] {new Integer JavaDoc(count), descr.getUniqueId()}));
112         // checking libraries
113
if (pathResolver != null) {
114             count = 0;
115             items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
116                     ReportItem.ERROR_NO_ERROR, "librariesCheckStart", //$NON-NLS-1$
117
descr.getUniqueId()));
118             for (Iterator JavaDoc it = descr.getLibraries().iterator(); it.hasNext();) {
119                 LibraryImpl lib = (LibraryImpl) it.next();
120                 count++;
121                 URL JavaDoc 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 JavaDoc[] { //$NON-NLS-1$
127
lib.getUniqueId(), descr.getUniqueId(), url}));
128                 }
129             }
130             items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
131                     ReportItem.ERROR_NO_ERROR, "librariesCheckFinish", //$NON-NLS-1$
132
new Object JavaDoc[] {new Integer JavaDoc(count), descr.getUniqueId()}));
133         } else {
134             items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
135                     ReportItem.ERROR_NO_ERROR, "librariesCheckSkip", //$NON-NLS-1$
136
descr.getUniqueId()));
137         }
138         // checking extension points
139
count = 0;
140         items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
141                 ReportItem.ERROR_NO_ERROR, "extPointsCheckStart", null)); //$NON-NLS-1$
142
for (Iterator JavaDoc 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", //$NON-NLS-1$
148
extPoint.getUniqueId()));
149             Collection JavaDoc extPointItems = extPoint.validate();
150             for (Iterator JavaDoc 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", //$NON-NLS-1$
161
extPoint.getUniqueId()));
162         }
163         items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
164                 ReportItem.ERROR_NO_ERROR, "extPointsCheckFinish", //$NON-NLS-1$
165
new Object JavaDoc[] {new Integer JavaDoc(count), descr.getUniqueId()}));
166         // checking extensions
167
count = 0;
168         items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
169                 ReportItem.ERROR_NO_ERROR, "extsCheckStart", null)); //$NON-NLS-1$
170
for (Iterator JavaDoc 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", //$NON-NLS-1$
175
ext.getUniqueId()));
176             Collection JavaDoc extItems = ext.validate();
177             for (Iterator JavaDoc 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", //$NON-NLS-1$
188
ext.getUniqueId()));
189         }
190         items.add(new ReportItemImpl(ReportItem.SEVERITY_INFO, descr,
191                 ReportItem.ERROR_NO_ERROR, "extsCheckFinish", //$NON-NLS-1$
192
new Object JavaDoc[] {new Integer JavaDoc(count), descr.getUniqueId()}));
193     }
194     
195     /**
196      * @see org.java.plugin.registry.IntegrityCheckReport#countErrors()
197      */

198     public int countErrors() {
199         return errorsCount;
200     }
201
202     /**
203      * @see org.java.plugin.registry.IntegrityCheckReport#countWarnings()
204      */

205     public int countWarnings() {
206         return warningsCount;
207     }
208
209     /**
210      * @see org.java.plugin.registry.IntegrityCheckReport#getItems()
211      */

212     public Collection JavaDoc 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 JavaDoc msg;
221         private final Object JavaDoc data;
222         
223         ReportItemImpl(final byte aSeverity, final Identity aSource,
224                 final int aCode, final String JavaDoc aMsg, final Object JavaDoc aData) {
225             severity = aSeverity;
226             source = aSource;
227             code = aCode;
228             msg = aMsg;
229             data = aData;
230         }
231         
232         /**
233          * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getCode()
234          */

235         public int getCode() {
236             return code;
237         }
238         
239         /**
240          * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getMessage()
241          */

242         public String JavaDoc getMessage() {
243             return ResourceManager.getMessage(PluginRegistryImpl.PACKAGE_NAME,
244                     msg, data);
245         }
246
247         /**
248          * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getMessage(
249          * java.util.Locale)
250          */

251         public String JavaDoc getMessage(Locale JavaDoc locale) {
252             return ResourceManager.getMessage(PluginRegistryImpl.PACKAGE_NAME,
253                     msg, locale, data);
254         }
255         
256         /**
257          * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getSeverity()
258          */

259         public byte getSeverity() {
260             return severity;
261         }
262         
263         /**
264          * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getSource()
265          */

266         public Identity getSource() {
267             return source;
268         }
269     }
270 }
271
Popular Tags