File: IntegrityChecker.java

package info (click to toggle)
libjpf-java 1.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,280 kB
  • sloc: java: 13,449; xml: 337; makefile: 17
file content (280 lines) | stat: -rw-r--r-- 11,511 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2004-2007 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin.registry.xml;

import java.net.URL;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.java.plugin.PathResolver;
import org.java.plugin.registry.Extension;
import org.java.plugin.registry.ExtensionPoint;
import org.java.plugin.registry.Identity;
import org.java.plugin.registry.IntegrityCheckReport;
import org.java.plugin.registry.Library;
import org.java.plugin.registry.PluginDescriptor;
import org.java.plugin.registry.PluginPrerequisite;
import org.java.plugin.util.IoUtil;
import org.java.plugin.util.ResourceManager;

/**
 * @version $Id$
 */
class IntegrityChecker implements IntegrityCheckReport {
    private static Log log = LogFactory.getLog(IntegrityChecker.class);

    private final PluginRegistryImpl registry;
    private List<ReportItem> items = new LinkedList<ReportItem>();
    private int errorsCount;
    private int warningsCount;

    IntegrityChecker(final PluginRegistryImpl aRegistry,
            final Collection<ReportItem> anItems) {
        this.items = new LinkedList<ReportItem>();
        this.registry = aRegistry;
        for (ReportItem item : anItems) {
            switch (item.getSeverity()) {
            case ERROR:
                break;
            case WARNING:
                warningsCount++;
                break;
            case INFO:
                // no-op
                break;
            }
            this.items.add(item);
        }
    }
    
    void doCheck(final PathResolver pathResolver) {
        int count = 0;
        items.add(new ReportItemImpl(Severity.INFO, null, Error.NO_ERROR,
                "pluginsCheckStart", null)); //$NON-NLS-1$
        try {
            for (PluginDescriptor descriptor : registry.getPluginDescriptors()) {
                PluginDescriptorImpl descr = (PluginDescriptorImpl) descriptor;
                count++;
                items.add(new ReportItemImpl(Severity.INFO, descr,
                        Error.NO_ERROR, "pluginCheckStart", //$NON-NLS-1$
                        descr.getUniqueId()));
                checkPlugin(descr, pathResolver);
                items.add(new ReportItemImpl(Severity.INFO, descr,
                        Error.NO_ERROR, "pluginCheckFinish", //$NON-NLS-1$
                        descr.getUniqueId()));
            }
        } catch (Exception e) {
            log.error("integrity check failed for registry " + registry, e); //$NON-NLS-1$
            errorsCount++;
            items.add(new ReportItemImpl(Severity.ERROR, null,
                    Error.CHECKER_FAULT, "pluginsCheckError", e)); //$NON-NLS-1$
        }
        items.add(new ReportItemImpl(Severity.INFO, null, Error.NO_ERROR,
                "pluginsCheckFinish", Integer.valueOf(count))); //$NON-NLS-1$
    }
    
    private void checkPlugin(final PluginDescriptorImpl descr,
            final PathResolver pathResolver) {
        // checking prerequisites
        int count = 0;
        items.add(new ReportItemImpl(Severity.INFO, descr, Error.NO_ERROR,
                "prerequisitesCheckStart", descr.getUniqueId())); //$NON-NLS-1$
        for (PluginPrerequisite prerequisite : descr.getPrerequisites()) {
            PluginPrerequisiteImpl pre = (PluginPrerequisiteImpl) prerequisite;
            count++;
            if (!pre.isOptional() && !pre.matches()) {
                errorsCount++;
                items.add(new ReportItemImpl(Severity.ERROR, descr,
                        Error.UNSATISFIED_PREREQUISITE,
                        "unsatisfiedPrerequisite", new Object[] { //$NON-NLS-1$
                        pre.getPluginId(), descr.getUniqueId()}));
            }
        }
        items.add(new ReportItemImpl(Severity.INFO, descr, Error.NO_ERROR,
                "prerequisitesCheckFinish", //$NON-NLS-1$
                new Object[] {Integer.valueOf(count), descr.getUniqueId()}));
        // checking libraries
        if (pathResolver != null) {
            count = 0;
            items.add(new ReportItemImpl(Severity.INFO, descr, Error.NO_ERROR,
                    "librariesCheckStart", descr.getUniqueId())); //$NON-NLS-1$
            for (Library library : descr.getLibraries()) {
                LibraryImpl lib = (LibraryImpl) library;
                count++;
                URL url = pathResolver.resolvePath(lib, lib.getPath());
                if (!IoUtil.isResourceExists(url)) {
                    errorsCount++;
                    items.add(new ReportItemImpl(Severity.ERROR, lib,
                            Error.BAD_LIBRARY,
                            "accesToResourceFailed", new Object[] { //$NON-NLS-1$
                            lib.getUniqueId(), descr.getUniqueId(), url}));
                }
            }
            items.add(new ReportItemImpl(Severity.INFO, descr, Error.NO_ERROR,
                    "librariesCheckFinish", //$NON-NLS-1$
                    new Object[] {Integer.valueOf(count),
                    descr.getUniqueId()}));
        } else {
            items.add(new ReportItemImpl(Severity.INFO, descr, Error.NO_ERROR,
                    "librariesCheckSkip", descr.getUniqueId())); //$NON-NLS-1$
        }
        // checking extension points
        count = 0;
        items.add(new ReportItemImpl(Severity.INFO, descr, Error.NO_ERROR,
                "extPointsCheckStart", null)); //$NON-NLS-1$
        for (ExtensionPoint extensionPoint : descr.getExtensionPoints()) {
            count++;
            ExtensionPointImpl extPoint = (ExtensionPointImpl) extensionPoint;
            items.add(new ReportItemImpl(Severity.INFO, extPoint,
                    Error.NO_ERROR, "extPointCheckStart", //$NON-NLS-1$
                    extPoint.getUniqueId()));
            Collection<ReportItem> extPointItems = extPoint.validate();
            for (ReportItem item : extPointItems) {
                switch (item.getSeverity()) {
                case ERROR:
                    errorsCount++;
                    break;
                case WARNING:
                    warningsCount++;
                    break;
                case INFO:
                    // no-op
                    break;
                }
                items.add(item);
            }
            items.add(new ReportItemImpl(Severity.INFO, extPoint,
                    Error.NO_ERROR, "extPointCheckFinish", //$NON-NLS-1$
                    extPoint.getUniqueId()));
        }
        items.add(new ReportItemImpl(Severity.INFO, descr, Error.NO_ERROR,
                "extPointsCheckFinish", //$NON-NLS-1$
                new Object[] {Integer.valueOf(count), descr.getUniqueId()}));
        // checking extensions
        count = 0;
        items.add(new ReportItemImpl(Severity.INFO, descr, Error.NO_ERROR,
                "extsCheckStart", null)); //$NON-NLS-1$
        for (Extension extension : descr.getExtensions()) {
            count++;
            ExtensionImpl ext = (ExtensionImpl) extension;
            items.add(new ReportItemImpl(Severity.INFO, ext, Error.NO_ERROR,
                    "extCheckStart", ext.getUniqueId())); //$NON-NLS-1$
            Collection<ReportItem> extItems = ext.validate();
            for (ReportItem item : extItems) {
                switch (item.getSeverity()) {
                case ERROR:
                    errorsCount++;
                    break;
                case WARNING:
                    warningsCount++;
                    break;
                case INFO:
                    // no-op
                    break;
                }
                items.add(item);
            }
            items.add(new ReportItemImpl(Severity.INFO, ext, Error.NO_ERROR,
                    "extCheckFinish", ext.getUniqueId())); //$NON-NLS-1$
        }
        items.add(new ReportItemImpl(Severity.INFO, descr, Error.NO_ERROR,
                "extsCheckFinish", //$NON-NLS-1$
                new Object[] {Integer.valueOf(count), descr.getUniqueId()}));
    }
    
    /**
     * @see org.java.plugin.registry.IntegrityCheckReport#countErrors()
     */
    public int countErrors() {
        return errorsCount;
    }

    /**
     * @see org.java.plugin.registry.IntegrityCheckReport#countWarnings()
     */
    public int countWarnings() {
        return warningsCount;
    }

    /**
     * @see org.java.plugin.registry.IntegrityCheckReport#getItems()
     */
    public Collection<ReportItem> getItems() {
        return items;
    }

    static class ReportItemImpl implements ReportItem {
        private final Severity severity;
        private final Identity source;
        private final Error code;
        private final String msg;
        private final Object data;
        
        ReportItemImpl(final Severity aSeverity, final Identity aSource,
                final Error aCode, final String aMsg, final Object aData) {
            severity = aSeverity;
            source = aSource;
            code = aCode;
            msg = aMsg;
            data = aData;
        }
        
        /**
         * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getCode()
         */
        public Error getCode() {
            return code;
        }
        
        /**
         * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getMessage()
         */
        public String getMessage() {
            return ResourceManager.getMessage(PluginRegistryImpl.PACKAGE_NAME,
                    msg, data);
        }

        /**
         * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getMessage(
         *      java.util.Locale)
         */
        public String getMessage(Locale locale) {
            return ResourceManager.getMessage(PluginRegistryImpl.PACKAGE_NAME,
                    msg, locale, data);
        }
        
        /**
         * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getSeverity()
         */
        public Severity getSeverity() {
            return severity;
        }
        
        /**
         * @see org.java.plugin.registry.IntegrityCheckReport.ReportItem#getSource()
         */
        public Identity getSource() {
            return source;
        }
    }
}