KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > content > check > CmsContentCheck


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/check/CmsContentCheck.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools.content.check;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.file.CmsResourceFilter;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39 import org.opencms.report.I_CmsReport;
40 import org.opencms.util.CmsFileUtil;
41 import org.opencms.workplace.CmsWorkplace;
42 import org.opencms.workplace.tools.CmsToolManager;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.SortedMap JavaDoc;
48 import java.util.TreeMap JavaDoc;
49
50 import org.apache.commons.logging.Log;
51
52 /**
53  * Main implementation of the content check. It maintains all configured content check
54  * plugins and handles the check process.<p>
55  *
56  *
57  * @author Michael Emmerich
58  *
59  * @version $Revision: 1.2 $
60  *
61  * @since 6.1.2
62  */

63 public class CmsContentCheck {
64
65     /** Path to the plugin folder. */
66     public static final String JavaDoc VFS_PATH_PLUGIN_FOLDER = CmsWorkplace.VFS_PATH_WORKPLACE
67         + "admin/contenttools/check/plugin/";
68
69     /** The log object for this class. */
70     private static final Log LOG = CmsLog.getLog(CmsContentCheck.class);
71
72     /** The CmsObject. */
73     private CmsObject m_cms;
74
75     /** The list of paths to be processed. */
76     private List JavaDoc m_paths;
77
78     /** The list of all instantiated plugins. */
79     private List JavaDoc m_plugins;
80
81     /** The report for the output. */
82     private I_CmsReport m_report;
83
84     /** The map of resources to check. */
85     private SortedMap JavaDoc m_resources;
86
87     /** The content check result. */
88     private CmsContentCheckResult m_result;
89
90     /**
91      * Constructor, creates a new CmsContentCheck. <p>
92      *
93      * @param cms the CmsObject
94      */

95     public CmsContentCheck(CmsObject cms) {
96
97         m_cms = cms;
98         m_plugins = new ArrayList JavaDoc();
99         m_paths = new ArrayList JavaDoc();
100         m_result = new CmsContentCheckResult();
101
102         try {
103             init();
104         } catch (CmsException e) {
105             // TODO: do some errorhandling
106
}
107     }
108
109     /**
110      * Gets a list of all paths to be processed by the content check plugins.<p>
111      *
112      * @return list of vfs paths
113      */

114     public List JavaDoc getPaths() {
115
116         return m_paths;
117     }
118
119     /**
120      * Gets a list of instances of all available content check plugins.<p>
121      *
122      * @return list of plugin instances.
123      */

124     public List JavaDoc getPlugins() {
125
126         return m_plugins;
127     }
128
129     /**
130      * Gets the number of installed plugins.<p>
131      *
132      * @return number of installed plugins
133      */

134     public int getPluginsCount() {
135
136         return m_plugins.size();
137     }
138
139     /**
140      * Gets the results of the content check.<p>
141      *
142      * @return CmsContentCheckResult object containing all resources that collected an error or warning
143      */

144     public CmsContentCheckResult getResults() {
145
146         return m_result;
147     }
148
149     /**
150      * Sets the list of all paths to be processed by the content check plugins.<p>
151      * @param paths list of vfs paths
152      */

153     public void setPaths(List JavaDoc paths) {
154
155         m_paths = paths;
156     }
157
158     /**
159      * Starts the content check.<p>
160      *
161      * @param cms the CmsObject
162      * @param report StringBuffer for reporting
163      * @throws Exception if something goes wrong
164      */

165     public void startContentCheck(CmsObject cms, I_CmsReport report) throws Exception JavaDoc {
166
167         m_report = report;
168         m_report.println(Messages.get().container(Messages.RPT_CONTENT_CHECK_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
169
170         // collect all resources
171
m_report.print(Messages.get().container(Messages.RPT_CONTENT_COLLECT_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
172         m_report.println(
173             org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0),
174             I_CmsReport.FORMAT_HEADLINE);
175         m_resources = collectResources(cms);
176         m_report.print(
177             org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0),
178             I_CmsReport.FORMAT_HEADLINE);
179         m_report.println(
180             Messages.get().container(Messages.RPT_CONTENT_COLLECT_END_1, new Integer JavaDoc(m_resources.size())),
181             I_CmsReport.FORMAT_HEADLINE);
182
183         // now process all resources
184
m_report.print(Messages.get().container(Messages.RPT_CONTENT_PROCESS_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
185         m_report.println(
186             org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0),
187             I_CmsReport.FORMAT_HEADLINE);
188         int count = 1;
189         Iterator JavaDoc i = m_resources.keySet().iterator();
190         while (i.hasNext()) {
191             String JavaDoc resourceName = (String JavaDoc)i.next();
192             CmsContentCheckResource res = (CmsContentCheckResource)m_resources.get(resourceName);
193             boolean errorWarning = false;
194             m_report.print(Messages.get().container(
195                 Messages.RPT_CONTENT_PROCESS_2,
196                 new Integer JavaDoc(count++),
197                 new Integer JavaDoc(m_resources.size())), I_CmsReport.FORMAT_NOTE);
198             m_report.print(
199                 Messages.get().container(Messages.RPT_CONTENT_PROCESS_RESOURCE_1, res.getResourceName()),
200                 I_CmsReport.FORMAT_DEFAULT);
201             m_report.print(
202                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0),
203                 I_CmsReport.FORMAT_NOTE);
204
205             // loop through all plugins and perform each content check
206
Iterator JavaDoc j = getPlugins().iterator();
207             while (j.hasNext()) {
208                 I_CmsContentCheck plugin = (I_CmsContentCheck)j.next();
209                 if (plugin.isActive()) {
210                     try {
211                         // process the content check
212
res = plugin.executeContentCheck(m_cms, res);
213
214                     } catch (CmsException e) {
215                         errorWarning = true;
216                         m_report.println(Messages.get().container(Messages.RPT_EMPTY_0), I_CmsReport.FORMAT_DEFAULT);
217                         m_report.print(Messages.get().container(
218                             Messages.RPT_CONTENT_PROCESS_ERROR_2,
219                             plugin.getName(),
220                             e), I_CmsReport.FORMAT_ERROR);
221                     }
222                 }
223             }
224
225             // check if there are some errors
226
List JavaDoc errors = res.getErrors();
227             if (errors != null && errors.size() > 0) {
228                 errorWarning = true;
229                 m_report.println(Messages.get().container(Messages.RPT_EMPTY_0), I_CmsReport.FORMAT_DEFAULT);
230                 m_report.println(
231                     Messages.get().container(Messages.RPT_CONTENT_PROCESS_ERROR_0),
232                     I_CmsReport.FORMAT_ERROR);
233                 Iterator JavaDoc k = errors.iterator();
234                 while (k.hasNext()) {
235                     String JavaDoc error = (String JavaDoc)k.next();
236                     m_report.println(
237                         Messages.get().container(Messages.RPT_CONTENT_PROCESS_ERROR_1, error),
238                         I_CmsReport.FORMAT_ERROR);
239
240                 }
241             }
242
243             // check if there are some warnings
244
List JavaDoc warnings = res.getWarnings();
245             if (warnings != null && warnings.size() > 0) {
246                 errorWarning = true;
247                 m_report.println(Messages.get().container(Messages.RPT_EMPTY_0), I_CmsReport.FORMAT_DEFAULT);
248                 m_report.println(
249                     Messages.get().container(Messages.RPT_CONTENT_PROCESS_WARNING_0),
250                     I_CmsReport.FORMAT_WARNING);
251                 Iterator JavaDoc k = warnings.iterator();
252                 while (k.hasNext()) {
253                     String JavaDoc warning = (String JavaDoc)k.next();
254                     m_report.println(
255                         Messages.get().container(Messages.RPT_CONTENT_PROCESS_WARNING_1, warning),
256                         I_CmsReport.FORMAT_WARNING);
257
258                 }
259             }
260
261             // store the updated CmsContentCheckResource
262
m_resources.put(resourceName, res);
263
264             if (!errorWarning) {
265                 m_report.println(
266                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
267                     I_CmsReport.FORMAT_OK);
268             } else {
269                 // there was an error or warning, so store the CmsContentCheckResource
270
// in the results
271
m_result.addResult(res);
272             }
273         }
274         m_report.print(
275             org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0),
276             I_CmsReport.FORMAT_HEADLINE);
277         m_report.println(Messages.get().container(Messages.RPT_CONTENT_PROCESS_END_0), I_CmsReport.FORMAT_HEADLINE);
278
279         m_report.println(Messages.get().container(Messages.RPT_CONTENT_CHECK_END_0), I_CmsReport.FORMAT_HEADLINE);
280     }
281
282     /**
283      *
284      * @see java.lang.Object#toString()
285      */

286     public String JavaDoc toString() {
287
288         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
289         buf.append("CmsContentCheck Paths=[");
290         for (int i = 0; i < m_paths.size(); i++) {
291             String JavaDoc path = (String JavaDoc)m_paths.get(i);
292             buf.append(path);
293             if (i < m_paths.size() - 1) {
294                 buf.append(",");
295             }
296         }
297         buf.append("] Plugins=[");
298         for (int i = 0; i < m_plugins.size(); i++) {
299             I_CmsContentCheck plugin = (I_CmsContentCheck)m_plugins.get(i);
300             buf.append(plugin.getName());
301             buf.append(" (");
302             buf.append(plugin.isActive());
303             buf.append(")");
304             if (i < m_plugins.size() - 1) {
305                 buf.append(",");
306             }
307         }
308         buf.append("]");
309         return buf.toString();
310     }
311
312     /**
313      * Collects all resources required for the content checks and stores them in a Set.<p>
314      *
315      * The collection of resources is build based on the vfs paths stored in this object.
316      * To prevent that resources are collected multiple times (in case of overlapping vfs paths),
317      * the results will be stored as a map.
318      *
319      * @param cms the CmsObject
320      * @return map of CmsContentCheckResources
321      */

322     private SortedMap JavaDoc collectResources(CmsObject cms) {
323
324         SortedMap JavaDoc collectedResources = new TreeMap JavaDoc();
325
326         // get all vfs paths and extract the resources from there
327
Iterator JavaDoc i = CmsFileUtil.removeRedundancies(m_paths).iterator();
328         while (i.hasNext()) {
329             String JavaDoc path = (String JavaDoc)i.next();
330             m_report.print(
331                 Messages.get().container(Messages.RPT_EXTRACT_FROM_PATH_BEGIN_1, path),
332                 I_CmsReport.FORMAT_HEADLINE);
333             m_report.println(
334                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0),
335                 I_CmsReport.FORMAT_HEADLINE);
336             try {
337                 List JavaDoc resources = cms.readResources(path, CmsResourceFilter.IGNORE_EXPIRATION, true);
338                 // get the single resources and store them
339
Iterator JavaDoc j = resources.iterator();
340                 while (j.hasNext()) {
341                     CmsResource res = (CmsResource)j.next();
342                     // m_report.print(
343
// Messages.get().container(Messages.RPT_EXTRACT_FROM_PATH_1, cms.getSitePath(res)),
344
// I_CmsReport.FORMAT_DEFAULT);
345
// m_report.print(
346
// org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0),
347
// I_CmsReport.FORMAT_DEFAULT);
348
// create a CmsContentCheckResource for each resource
349
CmsContentCheckResource contentCheckRes = new CmsContentCheckResource(res);
350                     collectedResources.put(contentCheckRes.getResourceName(), contentCheckRes);
351                     // m_report.println(
352
// org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
353
// I_CmsReport.FORMAT_OK);
354
}
355
356             } catch (CmsException e) {
357                 m_report.println(
358                     Messages.get().container(Messages.RPT_EXTRACT_FROM_PATH_ERROR_2, path, e),
359                     I_CmsReport.FORMAT_ERROR);
360             }
361
362             m_report.print(
363                 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0),
364                 I_CmsReport.FORMAT_HEADLINE);
365             m_report.println(
366                 Messages.get().container(Messages.RPT_EXTRACT_FROM_PATH_END_0),
367                 I_CmsReport.FORMAT_HEADLINE);
368
369         }
370
371         return collectedResources;
372     }
373
374     /**
375      * Initializes the CmsContent check and reads all available plugins.<p>
376      */

377     private void init() throws CmsException {
378
379         // first get all installed plugins
380
// plugins are subfolders of the "/plugin/" folder with a template
381
// property holdding the name of the plugin class
382
List JavaDoc resources = m_cms.readResourcesWithProperty(VFS_PATH_PLUGIN_FOLDER, CmsToolManager.HANDLERCLASS_PROPERTY);
383         Iterator JavaDoc i = resources.iterator();
384         while (i.hasNext()) {
385             CmsResource res = (CmsResource)i.next();
386             // ony check folders
387
if (res.isFolder()) {
388                 String JavaDoc classname = m_cms.readPropertyObject(
389                     res.getRootPath(),
390                     CmsToolManager.HANDLERCLASS_PROPERTY,
391                     false).getValue();
392                 try {
393                     Object JavaDoc objectInstance = Class.forName(classname).newInstance();
394                     if (objectInstance instanceof I_CmsContentCheck) {
395                         I_CmsContentCheck plugin = (I_CmsContentCheck)objectInstance;
396                         plugin.init(m_cms);
397                         // store the plugin instance
398
m_plugins.add(plugin);
399                         LOG.info(Messages.get().getBundle().key(Messages.LOG_CREATE_PLUGIN_1, classname));
400                     } else {
401                         LOG.warn(Messages.get().getBundle().key(Messages.LOG_CANNOT_CREATE_PLUGIN_1, classname));
402                     }
403                 } catch (Throwable JavaDoc t) {
404                     LOG.error(Messages.get().getBundle().key(Messages.LOG_CANNOT_CREATE_PLUGIN_2, classname, t));
405                 }
406             }
407         }
408     }
409
410 }
411
Popular Tags