001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/check/CmsContentCheck.java,v $
003: * Date : $Date: 2008-02-27 12:05:42 $
004: * Version: $Revision: 1.5 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.tools.content.check;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.file.CmsResource;
036: import org.opencms.file.CmsResourceFilter;
037: import org.opencms.main.CmsException;
038: import org.opencms.main.CmsLog;
039: import org.opencms.report.I_CmsReport;
040: import org.opencms.util.CmsFileUtil;
041: import org.opencms.workplace.CmsWorkplace;
042: import org.opencms.workplace.tools.CmsToolManager;
043:
044: import java.util.ArrayList;
045: import java.util.Iterator;
046: import java.util.List;
047: import java.util.SortedMap;
048: import java.util.TreeMap;
049:
050: import org.apache.commons.logging.Log;
051:
052: /**
053: * Main implementation of the content check. It maintains all configured content check
054: * plugins and handles the check process.<p>
055: *
056: *
057: * @author Michael Emmerich
058: *
059: * @version $Revision: 1.5 $
060: *
061: * @since 6.1.2
062: */
063: public class CmsContentCheck {
064:
065: /** Path to the plugin folder. */
066: public static final String VFS_PATH_PLUGIN_FOLDER = CmsWorkplace.VFS_PATH_WORKPLACE
067: + "admin/contenttools/check/plugin/";
068:
069: /** The log object for this class. */
070: private static final Log LOG = CmsLog.getLog(CmsContentCheck.class);
071:
072: /** The CmsObject. */
073: private CmsObject m_cms;
074:
075: /** The list of paths to be processed. */
076: private List m_paths;
077:
078: /** The list of all instantiated plugins. */
079: private List m_plugins;
080:
081: /** The report for the output. */
082: private I_CmsReport m_report;
083:
084: /** The map of resources to check. */
085: private SortedMap m_resources;
086:
087: /** The content check result. */
088: private CmsContentCheckResult m_result;
089:
090: /**
091: * Constructor, creates a new CmsContentCheck. <p>
092: *
093: * @param cms the CmsObject
094: */
095: public CmsContentCheck(CmsObject cms) {
096:
097: m_cms = cms;
098: m_plugins = new ArrayList();
099: m_paths = new ArrayList();
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 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 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 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)
166: throws Exception {
167:
168: m_report = report;
169: m_report.println(Messages.get().container(
170: Messages.RPT_CONTENT_CHECK_BEGIN_0),
171: I_CmsReport.FORMAT_HEADLINE);
172:
173: // collect all resources
174: m_report.print(Messages.get().container(
175: Messages.RPT_CONTENT_COLLECT_BEGIN_0),
176: I_CmsReport.FORMAT_HEADLINE);
177: m_report.println(org.opencms.report.Messages.get().container(
178: org.opencms.report.Messages.RPT_DOTS_0),
179: I_CmsReport.FORMAT_HEADLINE);
180: m_resources = collectResources(cms);
181: m_report.print(org.opencms.report.Messages.get().container(
182: org.opencms.report.Messages.RPT_DOTS_0),
183: I_CmsReport.FORMAT_HEADLINE);
184: m_report.println(Messages.get().container(
185: Messages.RPT_CONTENT_COLLECT_END_1,
186: new Integer(m_resources.size())),
187: I_CmsReport.FORMAT_HEADLINE);
188:
189: // now process all resources
190: m_report.print(Messages.get().container(
191: Messages.RPT_CONTENT_PROCESS_BEGIN_0),
192: I_CmsReport.FORMAT_HEADLINE);
193: m_report.println(org.opencms.report.Messages.get().container(
194: org.opencms.report.Messages.RPT_DOTS_0),
195: I_CmsReport.FORMAT_HEADLINE);
196: int count = 1;
197: Iterator i = m_resources.keySet().iterator();
198: while (i.hasNext()) {
199: String resourceName = (String) i.next();
200: CmsContentCheckResource res = (CmsContentCheckResource) m_resources
201: .get(resourceName);
202: boolean errorWarning = false;
203: m_report.print(Messages.get().container(
204: Messages.RPT_CONTENT_PROCESS_2,
205: new Integer(count++),
206: new Integer(m_resources.size())),
207: I_CmsReport.FORMAT_NOTE);
208: m_report.print(Messages.get().container(
209: Messages.RPT_CONTENT_PROCESS_RESOURCE_1,
210: res.getResourceName()), I_CmsReport.FORMAT_DEFAULT);
211: m_report.print(org.opencms.report.Messages.get().container(
212: org.opencms.report.Messages.RPT_DOTS_0),
213: I_CmsReport.FORMAT_NOTE);
214:
215: // loop through all plugins and perform each content check
216: Iterator j = getPlugins().iterator();
217: while (j.hasNext()) {
218: I_CmsContentCheck plugin = (I_CmsContentCheck) j.next();
219: if (plugin.isActive()) {
220: try {
221: // process the content check
222: res = plugin.executeContentCheck(m_cms, res);
223:
224: } catch (CmsException e) {
225: errorWarning = true;
226: m_report.println(Messages.get().container(
227: Messages.RPT_EMPTY_0),
228: I_CmsReport.FORMAT_DEFAULT);
229: m_report.print(Messages.get().container(
230: Messages.RPT_CONTENT_PROCESS_ERROR_2,
231: plugin.getName(), e),
232: I_CmsReport.FORMAT_ERROR);
233: }
234: }
235: }
236:
237: // check if there are some errors
238: List errors = res.getErrors();
239: if (errors != null && errors.size() > 0) {
240: errorWarning = true;
241: m_report.println(Messages.get().container(
242: Messages.RPT_EMPTY_0),
243: I_CmsReport.FORMAT_DEFAULT);
244: m_report.println(Messages.get().container(
245: Messages.RPT_CONTENT_PROCESS_ERROR_0),
246: I_CmsReport.FORMAT_ERROR);
247: Iterator k = errors.iterator();
248: while (k.hasNext()) {
249: String error = (String) k.next();
250: m_report
251: .println(
252: Messages
253: .get()
254: .container(
255: Messages.RPT_CONTENT_PROCESS_ERROR_1,
256: error),
257: I_CmsReport.FORMAT_ERROR);
258:
259: }
260: }
261:
262: // check if there are some warnings
263: List warnings = res.getWarnings();
264: if (warnings != null && warnings.size() > 0) {
265: errorWarning = true;
266: m_report.println(Messages.get().container(
267: Messages.RPT_EMPTY_0),
268: I_CmsReport.FORMAT_DEFAULT);
269: m_report.println(Messages.get().container(
270: Messages.RPT_CONTENT_PROCESS_WARNING_0),
271: I_CmsReport.FORMAT_WARNING);
272: Iterator k = warnings.iterator();
273: while (k.hasNext()) {
274: String warning = (String) k.next();
275: m_report.println(Messages.get().container(
276: Messages.RPT_CONTENT_PROCESS_WARNING_1,
277: warning), I_CmsReport.FORMAT_WARNING);
278:
279: }
280: }
281:
282: // store the updated CmsContentCheckResource
283: m_resources.put(resourceName, res);
284:
285: if (!errorWarning) {
286: m_report.println(
287: org.opencms.report.Messages.get().container(
288: org.opencms.report.Messages.RPT_OK_0),
289: I_CmsReport.FORMAT_OK);
290: } else {
291: // there was an error or warning, so store the CmsContentCheckResource
292: // in the results
293: m_result.addResult(res);
294: }
295: }
296: m_report.print(org.opencms.report.Messages.get().container(
297: org.opencms.report.Messages.RPT_DOTS_0),
298: I_CmsReport.FORMAT_HEADLINE);
299: m_report.println(Messages.get().container(
300: Messages.RPT_CONTENT_PROCESS_END_0),
301: I_CmsReport.FORMAT_HEADLINE);
302:
303: m_report.println(Messages.get().container(
304: Messages.RPT_CONTENT_CHECK_END_0),
305: I_CmsReport.FORMAT_HEADLINE);
306: }
307:
308: /**
309: *
310: * @see java.lang.Object#toString()
311: */
312: public String toString() {
313:
314: StringBuffer buf = new StringBuffer();
315: buf.append("CmsContentCheck Paths=[");
316: for (int i = 0; i < m_paths.size(); i++) {
317: String path = (String) m_paths.get(i);
318: buf.append(path);
319: if (i < m_paths.size() - 1) {
320: buf.append(",");
321: }
322: }
323: buf.append("] Plugins=[");
324: for (int i = 0; i < m_plugins.size(); i++) {
325: I_CmsContentCheck plugin = (I_CmsContentCheck) m_plugins
326: .get(i);
327: buf.append(plugin.getName());
328: buf.append(" (");
329: buf.append(plugin.isActive());
330: buf.append(")");
331: if (i < m_plugins.size() - 1) {
332: buf.append(",");
333: }
334: }
335: buf.append("]");
336: return buf.toString();
337: }
338:
339: /**
340: * Collects all resources required for the content checks and stores them in a Set.<p>
341: *
342: * The collection of resources is build based on the vfs paths stored in this object.
343: * To prevent that resources are collected multiple times (in case of overlapping vfs paths),
344: * the results will be stored as a map.
345: *
346: * @param cms the CmsObject
347: * @return map of CmsContentCheckResources
348: */
349: private SortedMap collectResources(CmsObject cms) {
350:
351: SortedMap collectedResources = new TreeMap();
352:
353: // get all vfs paths and extract the resources from there
354: Iterator i = CmsFileUtil.removeRedundancies(m_paths).iterator();
355: while (i.hasNext()) {
356: String path = (String) i.next();
357: m_report.print(Messages.get().container(
358: Messages.RPT_EXTRACT_FROM_PATH_BEGIN_1, path),
359: I_CmsReport.FORMAT_HEADLINE);
360: m_report.println(org.opencms.report.Messages.get()
361: .container(org.opencms.report.Messages.RPT_DOTS_0),
362: I_CmsReport.FORMAT_HEADLINE);
363: try {
364: List resources = cms.readResources(path,
365: CmsResourceFilter.IGNORE_EXPIRATION, true);
366: // get the single resources and store them
367: Iterator j = resources.iterator();
368: while (j.hasNext()) {
369: CmsResource res = (CmsResource) j.next();
370: // m_report.print(
371: // Messages.get().container(Messages.RPT_EXTRACT_FROM_PATH_1, cms.getSitePath(res)),
372: // I_CmsReport.FORMAT_DEFAULT);
373: // m_report.print(
374: // org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0),
375: // I_CmsReport.FORMAT_DEFAULT);
376: // create a CmsContentCheckResource for each resource
377: CmsContentCheckResource contentCheckRes = new CmsContentCheckResource(
378: res);
379: collectedResources.put(contentCheckRes
380: .getResourceName(), contentCheckRes);
381: // m_report.println(
382: // org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
383: // I_CmsReport.FORMAT_OK);
384: }
385:
386: } catch (CmsException e) {
387: m_report.println(Messages.get()
388: .container(
389: Messages.RPT_EXTRACT_FROM_PATH_ERROR_2,
390: path, e), I_CmsReport.FORMAT_ERROR);
391: }
392:
393: m_report.print(org.opencms.report.Messages.get().container(
394: org.opencms.report.Messages.RPT_DOTS_0),
395: I_CmsReport.FORMAT_HEADLINE);
396: m_report.println(Messages.get().container(
397: Messages.RPT_EXTRACT_FROM_PATH_END_0),
398: I_CmsReport.FORMAT_HEADLINE);
399:
400: }
401:
402: return collectedResources;
403: }
404:
405: /**
406: * Initializes the CmsContent check and reads all available plugins.<p>
407: *
408: * @throws CmsException if somehting goes wrong
409: */
410: private void init() throws CmsException {
411:
412: // first get all installed plugins
413: // plugins are subfolders of the "/plugin/" folder with a template
414: // property holdding the name of the plugin class
415: List resources = m_cms.readResourcesWithProperty(
416: VFS_PATH_PLUGIN_FOLDER,
417: CmsToolManager.HANDLERCLASS_PROPERTY);
418: Iterator i = resources.iterator();
419: while (i.hasNext()) {
420: CmsResource res = (CmsResource) i.next();
421: // ony check folders
422: if (res.isFolder()) {
423: String classname = m_cms.readPropertyObject(
424: res.getRootPath(),
425: CmsToolManager.HANDLERCLASS_PROPERTY, false)
426: .getValue();
427: try {
428: Object objectInstance = Class.forName(classname)
429: .newInstance();
430: if (objectInstance instanceof I_CmsContentCheck) {
431: I_CmsContentCheck plugin = (I_CmsContentCheck) objectInstance;
432: plugin.init(m_cms);
433: // store the plugin instance
434: m_plugins.add(plugin);
435: LOG.info(Messages.get().getBundle()
436: .key(Messages.LOG_CREATE_PLUGIN_1,
437: classname));
438: } else {
439: LOG.warn(Messages.get().getBundle().key(
440: Messages.LOG_CANNOT_CREATE_PLUGIN_1,
441: classname));
442: }
443: } catch (Throwable t) {
444: LOG.error(Messages.get().getBundle().key(
445: Messages.LOG_CANNOT_CREATE_PLUGIN_2,
446: classname, t));
447: }
448: }
449: }
450: }
451:
452: }
|