001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.config;
017:
018: import static org.apache.openejb.util.Join.join;
019: import org.apache.openejb.OpenEJBException;
020: import org.apache.openejb.loader.SystemInstance;
021: import org.apache.openejb.util.Logger;
022: import org.apache.openejb.util.LogCategory;
023:
024: import java.util.List;
025: import java.util.Arrays;
026:
027: /**
028: * @version $Rev: 636688 $ $Date: 2008-03-13 02:58:53 -0700 $
029: */
030: public class ReportValidationResults implements DynamicDeployer {
031:
032: private static final Logger logger = Logger.getInstance(
033: LogCategory.OPENEJB_STARTUP_VALIDATION,
034: "org.apache.openejb.config.rules");
035:
036: private static final String VALIDATION_LEVEL = "openejb.validation.output.level";
037:
038: private enum Level {
039: TERSE, MEDIUM, VERBOSE
040: }
041:
042: public AppModule deploy(AppModule appModule)
043: throws OpenEJBException {
044: String levelString = SystemInstance.get().getProperty(
045: VALIDATION_LEVEL, Level.MEDIUM.toString());
046:
047: Level level;
048: try {
049: level = Level.valueOf(levelString.toUpperCase());
050: } catch (IllegalArgumentException noSuchEnumConstant) {
051: try {
052: int i = Integer.parseInt(levelString) - 1;
053: level = Level.values()[i];
054: } catch (Exception e) {
055: level = Level.MEDIUM;
056: }
057: }
058:
059: boolean hasErrors = appModule.hasErrors();
060: boolean hasFailures = appModule.hasFailures();
061: boolean hasWarnings = appModule.hasWarnings();
062:
063: if (!hasErrors && !hasFailures && !hasWarnings)
064: return appModule;
065:
066: ValidationFailedException validationFailedException = null;
067:
068: List<ValidationContext> contexts = appModule
069: .getValidationContexts();
070:
071: for (ValidationContext context : contexts) {
072: logResults(context, level);
073: }
074:
075: ValidationContext uberContext = new ValidationContext(
076: AppModule.class, appModule.getValidation().getJarPath());
077: for (ValidationContext context : contexts) {
078: for (ValidationError error : context.getErrors()) {
079: uberContext.addError(error);
080: }
081: for (ValidationFailure failure : context.getFailures()) {
082: uberContext.addFailure(failure);
083: }
084: for (ValidationWarning warning : context.getWarnings()) {
085: uberContext.addWarning(warning);
086: }
087: }
088:
089: if (!hasErrors && !hasFailures)
090: return appModule;
091:
092: if (level != Level.VERBOSE) {
093: List<Level> levels = Arrays.asList(Level.values());
094: levels = levels.subList(level.ordinal() + 1, levels.size());
095:
096: logger.info("Set the '" + VALIDATION_LEVEL
097: + "' system property to " + join(" or ", levels)
098: + " for increased validation details.");
099: }
100:
101: validationFailedException = new ValidationFailedException(
102: "Module failed validation. "
103: + uberContext.getModuleType() + "(path="
104: + uberContext.getJarPath() + ")", uberContext,
105: validationFailedException);
106:
107: if (validationFailedException != null)
108: throw validationFailedException;
109:
110: return appModule;
111: }
112:
113: private void logResults(ValidationContext context, Level level) {
114:
115: for (ValidationError e : context.getErrors()) {
116: logger.error(e.getPrefix() + " ... " + e.getComponentName()
117: + ":\t" + e.getMessage(level.ordinal() + 1));
118: }
119:
120: for (ValidationFailure e : context.getFailures()) {
121: logger.error(e.getPrefix() + " ... " + e.getComponentName()
122: + ":\t" + e.getMessage(level.ordinal() + 1));
123: }
124:
125: for (ValidationWarning e : context.getWarnings()) {
126: logger.warning(e.getPrefix() + " ... "
127: + e.getComponentName() + ":\t"
128: + e.getMessage(level.ordinal() + 1));
129: }
130:
131: if (context.hasErrors() || context.hasFailures()) {
132:
133: logger.error("Invalid " + context.getModuleType()
134: + "(path=" + context.getJarPath() + ")");
135: // logger.error("Validation: "+errors.length + " errors, "+failures.length+ " failures, in "+context.getModuleType()+"(path="+context.getJarPath()+")");
136: }
137: }
138:
139: }
|