~ubuntu-branches/ubuntu/vivid/jaxb/vivid-proposed

« back to all changes in this revision

Viewing changes to samples/src/processor/AntBuildProcessor.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2014-09-01 14:26:44 UTC
  • Revision ID: package-import@ubuntu.com-20140901142644-nox3y6t2unq2wxjf
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright (c) 1997-2011 Oracle and/or its affiliates. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common Development
 
8
 * and Distribution License("CDDL") (collectively, the "License").  You
 
9
 * may not use this file except in compliance with the License.  You can
 
10
 * obtain a copy of the License at
 
11
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 
12
 * or packager/legal/LICENSE.txt.  See the License for the specific
 
13
 * language governing permissions and limitations under the License.
 
14
 *
 
15
 * When distributing the software, include this License Header Notice in each
 
16
 * file and include the License file at packager/legal/LICENSE.txt.
 
17
 *
 
18
 * GPL Classpath Exception:
 
19
 * Oracle designates this particular file as subject to the "Classpath"
 
20
 * exception as provided by Oracle in the GPL Version 2 section of the License
 
21
 * file that accompanied this code.
 
22
 *
 
23
 * Modifications:
 
24
 * If applicable, add the following below the License Header, with the fields
 
25
 * enclosed by brackets [] replaced by your own identifying information:
 
26
 * "Portions Copyright [year] [name of copyright owner]"
 
27
 *
 
28
 * Contributor(s):
 
29
 * If you wish your version of this file to be governed by only the CDDL or
 
30
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 
31
 * elects to include this software in this distribution under the [CDDL or GPL
 
32
 * Version 2] license."  If you don't indicate a single choice of license, a
 
33
 * recipient has the option to distribute your version of this file under
 
34
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 
35
 * its licensees as provided above.  However, if you add GPL Version 2 code
 
36
 * and therefore, elected the GPL Version 2 license, then the option applies
 
37
 * only if the new code is made subject to such option by the copyright
 
38
 * holder.
 
39
 */
 
40
package processor;
 
41
 
 
42
import java.io.File;
 
43
import java.io.FileOutputStream;
 
44
import java.io.PrintStream;
 
45
 
 
46
import org.apache.tools.ant.BuildEvent;
 
47
import org.apache.tools.ant.BuildListener;
 
48
import org.apache.tools.ant.DefaultLogger;
 
49
import org.apache.tools.ant.Project;
 
50
import org.apache.tools.ant.ProjectHelper;
 
51
import org.kohsuke.args4j.CmdLineParser;
 
52
 
 
53
/**
 
54
 * Processor that runs samples.
 
55
 * 
 
56
 * @author <ul>
 
57
 *         <li>Ryan Shoemaker, Sun Microsystems, Inc.</li>
 
58
 *         </ul>
 
59
 * @version $Revision: 1.1 $
 
60
 */
 
61
public class AntBuildProcessor implements Processor {
 
62
 
 
63
    /*
 
64
     * (non-Javadoc)
 
65
     * 
 
66
     * @see processor.Processor#process(java.io.File)
 
67
     */
 
68
    public boolean process(File dir, boolean verbose) {
 
69
        boolean continueProcessing = true;
 
70
 
 
71
        // find build.xml
 
72
        File buildDotXml =
 
73
            new File(dir,"build.xml");
 
74
 
 
75
        // buildDotXml should exist, so just attempt to launch it
 
76
        continueProcessing = launchAnt(dir, buildDotXml, verbose);
 
77
 
 
78
        return continueProcessing;
 
79
    }
 
80
 
 
81
    /**
 
82
     * @param buildDotXml
 
83
     * @return
 
84
     */
 
85
    private boolean launchAnt(File dir, File buildDotXml, boolean verbose) {
 
86
        boolean continueProcessing = true;
 
87
        File buildDotOut =
 
88
            new File(dir,"build.out");
 
89
 
 
90
        BuildResultListener buildResultListener = new BuildResultListener();
 
91
        
 
92
        try {
 
93
            trace("launching " + buildDotXml, verbose);
 
94
 
 
95
            final Project project = new Project();
 
96
            //AntListener listener = new AntListener();
 
97
            DefaultLogger listener = new DefaultLogger();
 
98
            PrintStream logTo =
 
99
                new PrintStream(new FileOutputStream(buildDotOut));
 
100
            listener.setOutputPrintStream(logTo);
 
101
            listener.setErrorPrintStream(logTo);
 
102
            listener.setMessageOutputLevel(Project.MSG_INFO);
 
103
            project.addBuildListener(listener);
 
104
            project.addBuildListener(buildResultListener);
 
105
 
 
106
            Throwable exception = null;
 
107
            try {
 
108
                project.fireBuildStarted();
 
109
                project.init();
 
110
                ProjectHelper.configureProject(project, buildDotXml);
 
111
                project.executeTarget(project.getDefaultTarget());
 
112
            } catch (Throwable t) {
 
113
                exception = t;
 
114
            } finally {
 
115
                project.fireBuildFinished(exception);
 
116
            }
 
117
 
 
118
            if( buildResultListener.getCause()!=null ) {
 
119
                System.out.println("build failed!");
 
120
                buildResultListener.getCause().printStackTrace(System.out);
 
121
                continueProcessing = false;
 
122
            }
 
123
        } catch (Exception e) {
 
124
            e.printStackTrace();
 
125
            continueProcessing = false;
 
126
        }
 
127
 
 
128
        return continueProcessing;
 
129
    }
 
130
 
 
131
    private void trace(String msg, boolean verbose) {
 
132
        if(verbose)
 
133
            System.out.println("AntBuildProcessor: " + msg);
 
134
    }
 
135
 
 
136
    public void addCmdLineOptions(CmdLineParser parser) {
 
137
        // no-op
 
138
    }
 
139
    
 
140
    /**
 
141
     * {@link BuildListener} that checks if the build
 
142
     * was successful or a failure.
 
143
     */
 
144
    static final class BuildResultListener implements BuildListener {
 
145
        
 
146
        /**
 
147
         * Cause of the failure.
 
148
         */
 
149
        private Throwable cause;
 
150
        
 
151
        Throwable getCause() {
 
152
            return cause;
 
153
        }
 
154
        
 
155
        public void buildStarted(BuildEvent event) {
 
156
            cause = null;
 
157
        }
 
158
        public void buildFinished(BuildEvent event) {
 
159
            cause = event.getException();
 
160
        }
 
161
        // no-op
 
162
        public void targetStarted(BuildEvent event) {
 
163
        }
 
164
        public void targetFinished(BuildEvent event) {
 
165
        }
 
166
        public void taskStarted(BuildEvent event) {
 
167
        }
 
168
        public void taskFinished(BuildEvent event) {
 
169
        }
 
170
        public void messageLogged(BuildEvent event) {
 
171
        }
 
172
    }
 
173
}