KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > igfay > util > MavenFig


1 package org.igfay.util;
2
3 import org.apache.log4j.Logger;
4
5 import org.igfay.jfig.JFig;
6
7 import java.lang.reflect.InvocationTargetException JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9
10
11 /**
12  * @author conrad4
13  *
14  * This class brings the power and flexibility of JFig to Maven.
15  * It takes advantage of the JFig functionality to automatically load selected values as properties.
16  * You can put your Maven properties in your config file, then use MavenFig to process configuration and call Maven.
17  *
18  * This gives you the advantage of storing all your configuration in one central location in
19  * addition to the additional JFig functionality.
20  */

21 public class MavenFig {
22     private static Logger logger = Logger.getLogger(MavenFig.class);
23
24     private static final String JavaDoc FOREHEAD_CONF_FILE = "forehead.conf.file";
25     private static final String JavaDoc MAVEN_HOME = "maven.home";
26     private static final String JavaDoc MAVEN_MAIN_CLASS = "maven.main.class";
27     private static final String JavaDoc JAVA_ENDORSED_DIRS = "java.endorsed.dirs";
28     private static final String JavaDoc JAVAX_XML_PARSERS_SAXPARSERFACTORY = "javax.xml.parsers.SAXParserFactory";
29     private static final String JavaDoc JAVAX_XML_PARSERS_DOCUMENTBUILDERFACTORY = "javax.xml.parsers.DocumentBuilderFactory";
30     private static final String JavaDoc TOOLS_JAR = "tools.jar";
31
32     public static void main(String JavaDoc[] args) {
33         JFig.getInstance();
34
35         String JavaDoc mavenClass = System.getProperty(MAVEN_MAIN_CLASS,"com.werken.forehead.Forehead");
36         
37         if (!requiredPropertiesAreDefined()) {
38             logger.fatal("ERROR: You must define certain system properties for MavenFig to operate correctly. " +
39                     "You can use the mavenfig script provided in the jFig distribution to automatically set them, " +
40                     "or you can refer to the maven.config.xml if you want to set these properties via jFig and invoke this class directly. " +
41                     " These properties are required: "
42                     + FOREHEAD_CONF_FILE + ", "
43                     + MAVEN_HOME + ", "
44                     + MAVEN_MAIN_CLASS
45                     + JAVA_ENDORSED_DIRS + ", "
46                     + JAVAX_XML_PARSERS_SAXPARSERFACTORY + ", "
47                     + JAVAX_XML_PARSERS_DOCUMENTBUILDERFACTORY + ", "
48                     + TOOLS_JAR + ", "
49                     );
50             return;
51         }
52
53         try {
54             // Run maven via reflection.
55
// The Maven bat file has the maven class name parameterized so we maintain that functionality here.
56
Class JavaDoc clazz = Class.forName(mavenClass);
57             Object JavaDoc object = clazz.newInstance();
58             Class JavaDoc[] parameterTypes = new Class JavaDoc[] { String JavaDoc[].class };
59             Method JavaDoc mainMethod = clazz.getMethod("main", parameterTypes);
60             Object JavaDoc[] arguments = new Object JavaDoc[] { args };
61
62             mainMethod.invoke(object, arguments);
63         } catch (SecurityException JavaDoc e) {
64             logger.debug("Exception",e);
65         } catch (IllegalArgumentException JavaDoc e) {
66             logger.debug("Exception",e);
67         } catch (NoSuchMethodException JavaDoc e) {
68             logger.debug("Exception",e);
69         } catch (InvocationTargetException JavaDoc e) {
70             logger.debug("Exception",e);
71         } catch (InstantiationException JavaDoc e) {
72             logger.debug("Exception",e);
73         } catch (IllegalAccessException JavaDoc e) {
74             logger.debug("Exception",e);
75         } catch (ClassNotFoundException JavaDoc e) {
76             logger.debug("Exception",e);
77         }
78     }
79
80     /**
81      * Checks if the system properties required by maven are all defined correctly.
82      *
83      * @return true if they are defined correctly, false if not.
84      */

85     private static boolean requiredPropertiesAreDefined() {
86         
87         if (!isPropertyDefined(FOREHEAD_CONF_FILE)) {
88             return false;
89         }
90         
91         if (!isPropertyDefined(JAVAX_XML_PARSERS_DOCUMENTBUILDERFACTORY)) {
92             return false;
93         }
94         
95         if (!isPropertyDefined(JAVAX_XML_PARSERS_SAXPARSERFACTORY)) {
96             return false;
97         }
98         
99         if (!isPropertyDefined(MAVEN_HOME)) {
100             return false;
101         }
102         
103         if (!isPropertyDefined(TOOLS_JAR)) {
104             return false;
105         }
106         
107         return true;
108     }
109
110     /**
111      * Check a single property for existence.
112      *
113      * @param propertyToCheck
114      * @return true if the property is set, false if not
115      */

116     private static boolean isPropertyDefined(String JavaDoc propertyToCheck) {
117         if (System.getProperty(propertyToCheck) == null) {
118             logger.fatal("ERROR: Missing property - " + propertyToCheck);
119             return false;
120         }
121         return true;
122     }
123 }
124
Popular Tags