001: package org.igfay.util;
002:
003: import org.apache.log4j.Logger;
004:
005: import org.igfay.jfig.JFig;
006:
007: import java.lang.reflect.InvocationTargetException;
008: import java.lang.reflect.Method;
009:
010: /**
011: * @author conrad4
012: *
013: * This class brings the power and flexibility of JFig to Maven.
014: * It takes advantage of the JFig functionality to automatically load selected values as properties.
015: * You can put your Maven properties in your config file, then use MavenFig to process configuration and call Maven.
016: *
017: * This gives you the advantage of storing all your configuration in one central location in
018: * addition to the additional JFig functionality.
019: */
020: public class MavenFig {
021: private static Logger logger = Logger.getLogger(MavenFig.class);
022:
023: private static final String FOREHEAD_CONF_FILE = "forehead.conf.file";
024: private static final String MAVEN_HOME = "maven.home";
025: private static final String MAVEN_MAIN_CLASS = "maven.main.class";
026: private static final String JAVA_ENDORSED_DIRS = "java.endorsed.dirs";
027: private static final String JAVAX_XML_PARSERS_SAXPARSERFACTORY = "javax.xml.parsers.SAXParserFactory";
028: private static final String JAVAX_XML_PARSERS_DOCUMENTBUILDERFACTORY = "javax.xml.parsers.DocumentBuilderFactory";
029: private static final String TOOLS_JAR = "tools.jar";
030:
031: public static void main(String[] args) {
032: JFig.getInstance();
033:
034: String mavenClass = System.getProperty(MAVEN_MAIN_CLASS,
035: "com.werken.forehead.Forehead");
036:
037: if (!requiredPropertiesAreDefined()) {
038: logger
039: .fatal("ERROR: You must define certain system properties for MavenFig to operate correctly. "
040: + "You can use the mavenfig script provided in the jFig distribution to automatically set them, "
041: + "or you can refer to the maven.config.xml if you want to set these properties via jFig and invoke this class directly. "
042: + " These properties are required: "
043: + FOREHEAD_CONF_FILE
044: + ", "
045: + MAVEN_HOME
046: + ", "
047: + MAVEN_MAIN_CLASS
048: + JAVA_ENDORSED_DIRS
049: + ", "
050: + JAVAX_XML_PARSERS_SAXPARSERFACTORY
051: + ", "
052: + JAVAX_XML_PARSERS_DOCUMENTBUILDERFACTORY
053: + ", " + TOOLS_JAR + ", ");
054: return;
055: }
056:
057: try {
058: // Run maven via reflection.
059: // The Maven bat file has the maven class name parameterized so we maintain that functionality here.
060: Class clazz = Class.forName(mavenClass);
061: Object object = clazz.newInstance();
062: Class[] parameterTypes = new Class[] { String[].class };
063: Method mainMethod = clazz.getMethod("main", parameterTypes);
064: Object[] arguments = new Object[] { args };
065:
066: mainMethod.invoke(object, arguments);
067: } catch (SecurityException e) {
068: logger.debug("Exception", e);
069: } catch (IllegalArgumentException e) {
070: logger.debug("Exception", e);
071: } catch (NoSuchMethodException e) {
072: logger.debug("Exception", e);
073: } catch (InvocationTargetException e) {
074: logger.debug("Exception", e);
075: } catch (InstantiationException e) {
076: logger.debug("Exception", e);
077: } catch (IllegalAccessException e) {
078: logger.debug("Exception", e);
079: } catch (ClassNotFoundException e) {
080: logger.debug("Exception", e);
081: }
082: }
083:
084: /**
085: * Checks if the system properties required by maven are all defined correctly.
086: *
087: * @return true if they are defined correctly, false if not.
088: */
089: private static boolean requiredPropertiesAreDefined() {
090:
091: if (!isPropertyDefined(FOREHEAD_CONF_FILE)) {
092: return false;
093: }
094:
095: if (!isPropertyDefined(JAVAX_XML_PARSERS_DOCUMENTBUILDERFACTORY)) {
096: return false;
097: }
098:
099: if (!isPropertyDefined(JAVAX_XML_PARSERS_SAXPARSERFACTORY)) {
100: return false;
101: }
102:
103: if (!isPropertyDefined(MAVEN_HOME)) {
104: return false;
105: }
106:
107: if (!isPropertyDefined(TOOLS_JAR)) {
108: return false;
109: }
110:
111: return true;
112: }
113:
114: /**
115: * Check a single property for existence.
116: *
117: * @param propertyToCheck
118: * @return true if the property is set, false if not
119: */
120: private static boolean isPropertyDefined(String propertyToCheck) {
121: if (System.getProperty(propertyToCheck) == null) {
122: logger
123: .fatal("ERROR: Missing property - "
124: + propertyToCheck);
125: return false;
126: }
127: return true;
128: }
129: }
|