1 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 ; 45 import java.util.Iterator ; 46 import java.util.List ; 47 import java.util.SortedMap ; 48 import java.util.TreeMap ; 49 50 import org.apache.commons.logging.Log; 51 52 63 public class CmsContentCheck { 64 65 66 public static final String VFS_PATH_PLUGIN_FOLDER = CmsWorkplace.VFS_PATH_WORKPLACE 67 + "admin/contenttools/check/plugin/"; 68 69 70 private static final Log LOG = CmsLog.getLog(CmsContentCheck.class); 71 72 73 private CmsObject m_cms; 74 75 76 private List m_paths; 77 78 79 private List m_plugins; 80 81 82 private I_CmsReport m_report; 83 84 85 private SortedMap m_resources; 86 87 88 private CmsContentCheckResult m_result; 89 90 95 public CmsContentCheck(CmsObject cms) { 96 97 m_cms = cms; 98 m_plugins = new ArrayList (); 99 m_paths = new ArrayList (); 100 m_result = new CmsContentCheckResult(); 101 102 try { 103 init(); 104 } catch (CmsException e) { 105 } 107 } 108 109 114 public List getPaths() { 115 116 return m_paths; 117 } 118 119 124 public List getPlugins() { 125 126 return m_plugins; 127 } 128 129 134 public int getPluginsCount() { 135 136 return m_plugins.size(); 137 } 138 139 144 public CmsContentCheckResult getResults() { 145 146 return m_result; 147 } 148 149 153 public void setPaths(List paths) { 154 155 m_paths = paths; 156 } 157 158 165 public void startContentCheck(CmsObject cms, I_CmsReport report) throws Exception { 166 167 m_report = report; 168 m_report.println(Messages.get().container(Messages.RPT_CONTENT_CHECK_BEGIN_0), I_CmsReport.FORMAT_HEADLINE); 169 170 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 (m_resources.size())), 181 I_CmsReport.FORMAT_HEADLINE); 182 183 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 i = m_resources.keySet().iterator(); 190 while (i.hasNext()) { 191 String resourceName = (String )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 (count++), 197 new Integer (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 Iterator j = getPlugins().iterator(); 207 while (j.hasNext()) { 208 I_CmsContentCheck plugin = (I_CmsContentCheck)j.next(); 209 if (plugin.isActive()) { 210 try { 211 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 List 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 k = errors.iterator(); 234 while (k.hasNext()) { 235 String error = (String )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 List 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 k = warnings.iterator(); 252 while (k.hasNext()) { 253 String warning = (String )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 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 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 286 public String toString() { 287 288 StringBuffer buf = new StringBuffer (); 289 buf.append("CmsContentCheck Paths=["); 290 for (int i = 0; i < m_paths.size(); i++) { 291 String path = (String )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 322 private SortedMap collectResources(CmsObject cms) { 323 324 SortedMap collectedResources = new TreeMap (); 325 326 Iterator i = CmsFileUtil.removeRedundancies(m_paths).iterator(); 328 while (i.hasNext()) { 329 String path = (String )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 resources = cms.readResources(path, CmsResourceFilter.IGNORE_EXPIRATION, true); 338 Iterator j = resources.iterator(); 340 while (j.hasNext()) { 341 CmsResource res = (CmsResource)j.next(); 342 CmsContentCheckResource contentCheckRes = new CmsContentCheckResource(res); 350 collectedResources.put(contentCheckRes.getResourceName(), contentCheckRes); 351 } 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 377 private void init() throws CmsException { 378 379 List resources = m_cms.readResourcesWithProperty(VFS_PATH_PLUGIN_FOLDER, CmsToolManager.HANDLERCLASS_PROPERTY); 383 Iterator i = resources.iterator(); 384 while (i.hasNext()) { 385 CmsResource res = (CmsResource)i.next(); 386 if (res.isFolder()) { 388 String classname = m_cms.readPropertyObject( 389 res.getRootPath(), 390 CmsToolManager.HANDLERCLASS_PROPERTY, 391 false).getValue(); 392 try { 393 Object objectInstance = Class.forName(classname).newInstance(); 394 if (objectInstance instanceof I_CmsContentCheck) { 395 I_CmsContentCheck plugin = (I_CmsContentCheck)objectInstance; 396 plugin.init(m_cms); 397 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 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 |