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 org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: /**
012: * Checks that a class named as a constructor argument is present.
013: *
014: * @version $Revision: 35837 $ $Date: 2005-05-20 12:01:34 -0700 (Fri, 20 May 2005) $
015: * @since uPortal 2.5
016: */
017: public class ClassPresenceCheck implements ICheck {
018:
019: protected final Log log = LogFactory.getLog(getClass());
020:
021: /**
022: * The class we will be looking for.
023: */
024: private final String targetClass;
025:
026: /**
027: * The CheckResult we will return if our target class is present.
028: * We populate this field with a default at construction and allow it to be
029: * overridden via a setter method.
030: */
031: private CheckResult successResult;
032:
033: /**
034: * The CheckResult we will return if our target class is not present.
035: * We populate this field with a default at costruction and allow it to be
036: * overridden via a setter method.
037: */
038: private CheckResult failureResult;
039:
040: /**
041: * A String describing this check.
042: * We populate this field with a default at construction and allow it to be
043: * overridden via a setter method.
044: */
045: private String checkDescription;
046:
047: /**
048: * Each instance of ClassPresenceCheck exists to test for the presence of
049: * exactly one class. That class must be specified as the single required
050: * constructor argument, which must be a non-null String representing the
051: * fully qualified name of the class.
052: *
053: * @param targetClass fully qualified name of the target class
054: * @throws IllegalArgumentException if targetClass is null
055: */
056: public ClassPresenceCheck(String targetClass) {
057: if (targetClass == null) {
058: throw new IllegalArgumentException(
059: "The constructor argument to the "
060: + "ClassPresenceCheck constructor was illegally null.");
061: }
062: this .targetClass = targetClass;
063:
064: // generate default description success and failure results
065:
066: this .checkDescription = "Checks for the presence of class "
067: + targetClass;
068:
069: this .successResult = CheckResult
070: .createSuccess("Successfully touched class "
071: + targetClass);
072:
073: this .failureResult = CheckResult.createFailure(
074: "Failed to touch class " + targetClass,
075: "Is a required .jar missing from the /WEB-INF/lib directory or from "
076: + "the JRE endorsed directory or from "
077: + "the Tomcat endorsed directory?");
078:
079: }
080:
081: public CheckResult doCheck() {
082: try {
083: getClass().getClassLoader().loadClass(this .targetClass);
084: } catch (ClassNotFoundException e) {
085: return this .failureResult;
086: }
087: return this .successResult;
088: }
089:
090: public String getDescription() {
091: return this .checkDescription;
092: }
093:
094: /**
095: * Set the check description which we will return for on calls to
096: * the getDescription() method we're implementing as part of being an
097: * {@link ICheck}.
098: * @param description
099: */
100: public void setDescription(String description) {
101: if (description == null) {
102: throw new IllegalArgumentException(
103: "Cannot set description to null.");
104: }
105: this .checkDescription = description;
106: }
107:
108: /**
109: * Get the CheckResult we will return when we fail to touch our
110: * configured targetClass.
111: * @return CheckResult we will return on failure
112: */
113: CheckResult getFailureResult() {
114: return this .failureResult;
115: }
116:
117: /**
118: * Set the CheckResult we will return when we fail to touch our
119: * configured targetClass.
120: * @param failureResult desired CheckResult on failure
121: */
122: void setFailureResult(CheckResult failureResult) {
123: this .failureResult = failureResult;
124: }
125:
126: /**
127: * Get the CheckResult we will return when we succeed in touching
128: * our configured targetClass.
129: * @return CheckResult we will return on success.
130: */
131: CheckResult getSuccessResult() {
132: return this .successResult;
133: }
134:
135: /**
136: * Set the CheckResult we will return when we succeed in touching
137: * our configured targetClass.
138: * @param successResult desired CheckResult on success.
139: */
140: void setSuccessResult(CheckResult successResult) {
141: this .successResult = successResult;
142: }
143:
144: /**
145: * Get the name of the class for which we check.
146: * @return the fully qualified name of the class for which we check.
147: */
148: String getTargetClass() {
149: return this.targetClass;
150: }
151: }
|