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: import org.jasig.portal.spring.PortalApplicationContextFacade;
011: import org.springframework.beans.BeansException;
012: import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
013: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
014:
015: /**
016: * Check that a particular named Spring bean is defined.
017: *
018: * @version $Revision: 36662 $ $Date: 2006-07-25 21:59:41 -0700 (Tue, 25 Jul 2006) $
019: * @since uPortal 2.5
020: */
021: public class SpringBeanCheck implements ICheck {
022:
023: protected final Log log = LogFactory.getLog(getClass());
024:
025: private final String beanName;
026:
027: private final String requiredBeanTypeClassName;
028:
029: private CheckResult nullBeanNameResult = CheckResult
030: .createFailure(
031: "This check is fatally misconfigured -- it hasn't been told the name of the Spring bean for which it should be checking.",
032: "Fix the check configuration (probably a List named 'checks' declared in applicationContext.xml).");
033:
034: public SpringBeanCheck(String beanName,
035: String requiredBeanTypeClassName) {
036: this .beanName = beanName;
037: this .requiredBeanTypeClassName = requiredBeanTypeClassName;
038: }
039:
040: public SpringBeanCheck(String beanName) {
041: this .beanName = beanName;
042: this .requiredBeanTypeClassName = null;
043: }
044:
045: public CheckResult doCheck() {
046:
047: if (this .beanName == null)
048: return this .nullBeanNameResult;
049:
050: // either verify that the bean exists (and is of the desired type if specified)
051: // or translate the resulting RuntimeException into an explanatory
052: // CheckResult.
053: try {
054: if (this .requiredBeanTypeClassName == null) {
055: PortalApplicationContextFacade
056: .getPortalApplicationContext().getBean(
057: this .beanName);
058: return CheckResult.createSuccess("Bean with name ["
059: + this .beanName + "] was present.");
060: }
061:
062: PortalApplicationContextFacade
063: .getPortalApplicationContext()
064: .getBean(
065: this .beanName,
066: Class
067: .forName(this .requiredBeanTypeClassName));
068: return CheckResult.createSuccess("Bean with name ["
069: + this .beanName + "] was present and of class ["
070: + this .requiredBeanTypeClassName + "]");
071:
072: } catch (NoSuchBeanDefinitionException nsbde) {
073: String remediationAdvice;
074: if (this .requiredBeanTypeClassName == null) {
075: remediationAdvice = "Declare a singleton bean of name ["
076: + this .beanName
077: + "] in a Spring bean definition file mapped in /properties/beanRef.xml.";
078: } else {
079: remediationAdvice = "Declare a singleton bean of name ["
080: + this .beanName
081: + "] and of type ["
082: + this .requiredBeanTypeClassName
083: + "] in a Spring bean definition file mapped in /properties/beanRefFactory.xml.";
084: }
085: return CheckResult
086: .createFailure(
087: "There is no bean named ["
088: + this .beanName
089: + "] in uPortal's Spring bean definition file(s) (e.g., applicationContext.xml).",
090: remediationAdvice);
091: } catch (BeanNotOfRequiredTypeException bnfe) {
092: return CheckResult
093: .createFailure(
094: "The bean named ["
095: + this .beanName
096: + "] defined in uPotal's Spring bean definitions (likely, applicationContext.xml), is not of type ["
097: + this .requiredBeanTypeClassName
098: + "]",
099: "Fix the declaration of bean ["
100: + this .beanName
101: + "] to be of the required type.");
102:
103: } catch (BeansException be) {
104: log.error("Error instantiating " + this .beanName, be);
105: return CheckResult
106: .createFailure(
107: "There was an error instantiating the bean ["
108: + this .beanName
109: + "] required for configuration of configuration checking.",
110: be.getMessage());
111: } catch (ClassNotFoundException cnfe) {
112: return CheckResult
113: .createFailure(
114: "We can't check for the presence of the bean ["
115: + this .beanName
116: + "] because the desired class ["
117: + this .requiredBeanTypeClassName
118: + "] itself could not be found.",
119: "Either fix the configuration of this check so that it's checking for the right class name, or place the required class on the classpath.");
120: }
121:
122: }
123:
124: public String getDescription() {
125: if (this .beanName == null)
126: return "Fatally misconfigured check doesn't do anything because it hasn't been given a bean name to check.";
127: if (this .requiredBeanTypeClassName == null)
128: return "Checks for the presence of the Spring bean named ["
129: + this .beanName + "].";
130: return "Checks for the presence of the Spring bean named ["
131: + this .beanName
132: + "] and that it is an instance of class ["
133: + this .requiredBeanTypeClassName + "]";
134: }
135:
136: }
|