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 java.io.File;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.commons.cli.CommandLine;
023: import org.apache.commons.cli.CommandLineParser;
024: import org.apache.commons.cli.HelpFormatter;
025: import org.apache.commons.cli.Option;
026: import org.apache.commons.cli.OptionBuilder;
027: import org.apache.commons.cli.Options;
028: import org.apache.commons.cli.ParseException;
029: import org.apache.commons.cli.PosixParser;
030: import org.apache.openejb.OpenEJBException;
031: import org.apache.openejb.cli.SystemExitException;
032: import org.apache.openejb.config.rules.CheckClasses;
033: import org.apache.openejb.config.rules.CheckMethods;
034: import org.apache.openejb.config.rules.CheckAssemblyBindings;
035: import org.apache.openejb.config.rules.CheckCallbacks;
036: import org.apache.openejb.config.rules.CheckInjectionTargets;
037: import org.apache.openejb.config.rules.CheckPersistenceRefs;
038: import org.apache.openejb.util.Messages;
039: import org.apache.openejb.util.OpenEjbVersion;
040:
041: /**
042: * @version $Rev: 602721 $ $Date: 2007-12-09 11:09:08 -0800 $
043: */
044: public class AppValidator {
045:
046: protected static final Messages _messages = new Messages(
047: "org.apache.openejb.config.rules");
048:
049: int LEVEL = 2;
050: boolean PRINT_XML = false;
051: boolean PRINT_WARNINGS = true;
052: boolean PRINT_COUNT = false;
053:
054: private List<ValidationResults> sets = new ArrayList<ValidationResults>();
055:
056: /*------------------------------------------------------*/
057: /* Constructors */
058: /*------------------------------------------------------*/
059: public AppValidator() throws OpenEJBException {
060: }
061:
062: public AppValidator(int LEVEL, boolean PRINT_XML,
063: boolean PRINT_WARNINGS, boolean PRINT_COUNT) {
064: this .LEVEL = LEVEL;
065: this .PRINT_XML = PRINT_XML;
066: this .PRINT_WARNINGS = PRINT_WARNINGS;
067: this .PRINT_COUNT = PRINT_COUNT;
068: }
069:
070: public void addValidationResults(ValidationResults set) {
071: sets.add(set);
072: }
073:
074: public ValidationResults[] getValidationResultsSets() {
075: ValidationResults[] ejbSets = new ValidationResults[sets.size()];
076: return sets.toArray(ejbSets);
077: }
078:
079: public AppModule validate(final AppModule appModule) {
080: try {
081: ValidationRule[] rules = getValidationRules();
082: for (int i = 0; i < rules.length; i++) {
083: rules[i].validate(appModule);
084: }
085: } catch (Throwable e) {
086: e.printStackTrace(System.out);
087: ValidationError err = new ValidationError("cannot.validate");
088: err.setCause(e);
089: err.setDetails(e.getMessage());
090: appModule.getValidation().addError(err);
091: }
092: return appModule;
093: }
094:
095: protected ValidationRule[] getValidationRules() {
096: ValidationRule[] rules = new ValidationRule[] {
097: new CheckClasses(), new CheckMethods(),
098: new CheckCallbacks(), new CheckAssemblyBindings(),
099: new CheckInjectionTargets(), new CheckPersistenceRefs() };
100: return rules;
101: }
102:
103: public void printResults(ValidationResults set) {
104: if (!set.hasErrors() && !set.hasFailures()
105: && (!PRINT_WARNINGS || !set.hasWarnings())) {
106: return;
107: }
108: System.out
109: .println("------------------------------------------");
110: System.out.println("JAR " + set.getJarPath());
111: System.out
112: .println(" ");
113:
114: printValidationExceptions(set.getErrors());
115: printValidationExceptions(set.getFailures());
116:
117: if (PRINT_WARNINGS) {
118: printValidationExceptions(set.getWarnings());
119: }
120: }
121:
122: protected void printValidationExceptions(
123: ValidationException[] exceptions) {
124: for (int i = 0; i < exceptions.length; i++) {
125: System.out.print(" ");
126: System.out.print(exceptions[i].getPrefix());
127: System.out.print(" ... ");
128: if (!(exceptions[i] instanceof ValidationError)) {
129: System.out.print(exceptions[i].getComponentName());
130: System.out.print(": ");
131: }
132: if (LEVEL > 2) {
133: System.out.println(exceptions[i].getMessage(1));
134: System.out.println();
135: System.out.print('\t');
136: System.out.println(exceptions[i].getMessage(LEVEL));
137: System.out.println();
138: } else {
139: System.out.println(exceptions[i].getMessage(LEVEL));
140: }
141: }
142: if (PRINT_COUNT && exceptions.length > 0) {
143: System.out.println();
144: System.out.print(" " + exceptions.length + " ");
145: System.out.println(exceptions[0].getCategory());
146: System.out.println();
147: }
148:
149: }
150:
151: public void printResultsXML(ValidationResults set) {
152: if (!set.hasErrors() && !set.hasFailures()
153: && (!PRINT_WARNINGS || !set.hasWarnings())) {
154: return;
155: }
156:
157: System.out.println("<jar>");
158: System.out.print(" <path>");
159: System.out.print(set.getJarPath());
160: System.out.println("</path>");
161:
162: printValidationExceptionsXML(set.getErrors());
163: printValidationExceptionsXML(set.getFailures());
164:
165: if (PRINT_WARNINGS) {
166: printValidationExceptionsXML(set.getWarnings());
167: }
168: System.out.println("</jar>");
169: }
170:
171: protected void printValidationExceptionsXML(
172: ValidationException[] exceptions) {
173: for (int i = 0; i < exceptions.length; i++) {
174: System.out.print(" <");
175: System.out.print(exceptions[i].getPrefix());
176: System.out.println(">");
177: if (!(exceptions[i] instanceof ValidationError)) {
178: System.out.print(" <ejb-name>");
179: System.out.print(exceptions[i].getComponentName());
180: System.out.println("</ejb-name>");
181: }
182: System.out.print(" <summary>");
183: System.out.print(exceptions[i].getMessage(1));
184: System.out.println("</summary>");
185: System.out.println(" <description><![CDATA[");
186: System.out.println(exceptions[i].getMessage(3));
187: System.out.println("]]></description>");
188: System.out.print(" </");
189: System.out.print(exceptions[i].getPrefix());
190: System.out.println(">");
191: }
192: }
193:
194: public void displayResults(ValidationResults[] sets) {
195: if (PRINT_XML) {
196: System.out.println("<results>");
197: for (int i = 0; i < sets.length; i++) {
198: printResultsXML(sets[i]);
199: }
200: System.out.println("</results>");
201: } else {
202: for (int i = 0; i < sets.length; i++) {
203: printResults(sets[i]);
204: }
205: for (int i = 0; i < sets.length; i++) {
206: if (sets[i].hasErrors() || sets[i].hasFailures()) {
207: if (LEVEL < 3) {
208: System.out.println();
209: System.out
210: .println("For more details, use the -vvv option");
211: }
212: i = sets.length;
213: }
214: }
215: }
216: }
217:
218: public static void main(String[] args) throws SystemExitException {
219: CommandLineParser parser = new PosixParser();
220:
221: // create the Options
222: Options options = new Options();
223: options.addOption(AppValidator.option("v", "version",
224: "cmd.validate.opt.version"));
225: options.addOption(AppValidator.option("h", "help",
226: "cmd.validate.opt.help"));
227:
228: CommandLine line = null;
229: try {
230: line = parser.parse(options, args);
231: } catch (ParseException exp) {
232: AppValidator.help(options);
233: throw new SystemExitException(-1);
234: }
235:
236: if (line.hasOption("help")) {
237: AppValidator.help(options);
238: return;
239: } else if (line.hasOption("version")) {
240: OpenEjbVersion.get().print(System.out);
241: return;
242: }
243:
244: if (line.getArgList().size() == 0) {
245: System.out.println("Must specify an module id.");
246: AppValidator.help(options);
247: }
248:
249: DeploymentLoader deploymentLoader = new DeploymentLoader();
250:
251: try {
252: AppValidator validator = new AppValidator();
253: for (Object obj : line.getArgList()) {
254: String module = (String) obj;
255: File file = new File(module);
256: AppModule appModule = deploymentLoader.load(file);
257: validator.validate(appModule);
258: }
259: } catch (Exception e) {
260: e.printStackTrace();
261: }
262: }
263:
264: private static void help(Options options) {
265: HelpFormatter formatter = new HelpFormatter();
266: formatter.printHelp("validate [options] <file> [<file>...]",
267: "\n" + AppValidator.i18n("cmd.validate.description"),
268: options, "\n");
269: }
270:
271: private static Option option(String shortOpt, String longOpt,
272: String description) {
273: return OptionBuilder.withLongOpt(longOpt).withDescription(
274: AppValidator.i18n(description)).create(shortOpt);
275: }
276:
277: private static Option option(String shortOpt, String longOpt,
278: String argName, String description) {
279: return OptionBuilder.withLongOpt(longOpt).withArgName(argName)
280: .hasArg().withDescription(
281: AppValidator.i18n(description))
282: .create(shortOpt);
283: }
284:
285: private static String i18n(String key) {
286: return AppValidator._messages.format(key);
287: }
288: }
|