001: /* Copyright 2005 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.tools.checks;
007:
008: import java.util.ArrayList;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.jasig.portal.spring.PortalApplicationContextFacade;
015:
016: /**
017: * SafeDelegatingCheckRunner safely attempts to delegate to a Spring-configured
018: * ICheckRunner. Even if Spring isn't present or that ICheckRunner is misconfigured
019: * or broken, instances of this class will still implement the ICheckRunner API - that is,
020: * they will express this failure as a return value from the interface method.
021: *
022: * @version $Revision: 35833 $ $Date: 2005-05-19 18:51:40 -0700 (Thu, 19 May 2005) $
023: * @since uPortal 2.5
024: */
025: public class SafeDelegatingCheckRunner implements ICheckRunner {
026:
027: private Log log = LogFactory.getLog(getClass());
028:
029: /**
030: * The name of the Spring bean we expect will be an instance of ICheckRunner
031: * to which we will be delegating.
032: */
033: public static final String CHECKS_KEY = "checkRunner";
034:
035: public List doChecks() {
036:
037: // check our own dependencies
038:
039: // List of checks for our own dependencies
040: List localChecks = new ArrayList();
041: localChecks.add(new SpringPresenceCheck());
042: localChecks.add(new SpringBeanCheck(CHECKS_KEY,
043: ICheckRunner.class.getName()));
044:
045: BasicCheckRunner localCheckRunner = new BasicCheckRunner();
046: localCheckRunner.setChecks(localChecks);
047:
048: List results = localCheckRunner.doChecks();
049:
050: if (!containsFailedCheck(results)) {
051:
052: try {
053: // great, local dependencies look good, let's try to run the
054: // Spring configured ICheckRunner.
055:
056: ICheckRunner configuredDelegate = (ICheckRunner) PortalApplicationContextFacade
057: .getPortalApplicationContext().getBean(
058: SafeDelegatingCheckRunner.CHECKS_KEY,
059: ICheckRunner.class);
060: results.addAll(configuredDelegate.doChecks());
061:
062: } catch (Throwable t) {
063: log.error(
064: "Failed to obtain Spring-configured ICheckRunner named ["
065: + CHECKS_KEY + "].", t);
066: CheckResult delegateFailedResult = CheckResult
067: .createFailure(
068: "The Spring-configured ICheckRunner threw an exception: "
069: + t,
070: "Examine the configuration of the Spring bean named ["
071: + SafeDelegatingCheckRunner.CHECKS_KEY
072: + "]");
073: CheckAndResult delegateFailedCheckAndResult = new CheckAndResult(
074: "Check that the configured ICheckRunner delegate did not throw any Throwable.",
075: delegateFailedResult);
076: results.add(delegateFailedCheckAndResult);
077: }
078:
079: }
080:
081: return results;
082: }
083:
084: /**
085: * Return true if any CheckAndResult in the list of results is not a success.
086: * @param results a List of CheckAndResult instances.
087: * @return true if any failures, false otherwise
088: */
089: private boolean containsFailedCheck(List results) {
090: for (Iterator iter = results.iterator(); iter.hasNext();) {
091: CheckAndResult checkAndResult = (CheckAndResult) iter
092: .next();
093: if (!checkAndResult.isSuccess()) {
094: return true;
095: }
096: }
097: return false;
098: }
099:
100: }
|