|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.apache.tools.ant.module.bridge.impl;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DemuxOutputStream;
import org.apache.tools.ant.IntrospectionHelper;
import org.apache.tools.ant.Main;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.module.AntModule;
import org.apache.tools.ant.module.AntSettings;
import org.apache.tools.ant.module.api.IntrospectedInfo;
import org.apache.tools.ant.module.bridge.AntBridge;
import org.apache.tools.ant.module.bridge.BridgeInterface;
import org.apache.tools.ant.module.bridge.IntrospectionHelperProxy;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.Path;
import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.windows.OutputWriter;
/**
* Implements the BridgeInterface using the current version of Ant.
* @author Jesse Glick
*/
public class BridgeImpl implements BridgeInterface {
private static boolean classpathInitialized = false;
public BridgeImpl() {
}
public String getAntVersion() {
try {
return Main.getAntVersion();
} catch (BuildException be) {
AntModule.err.notify(ErrorManager.INFORMATIONAL, be);
return NbBundle.getMessage(BridgeImpl.class, "LBL_ant_version_unknown");
}
}
public boolean isAnt16() {
try {
Class.forName("org.apache.tools.ant.taskdefs.Antlib"); // NOI18N
return true;
} catch (ClassNotFoundException e) {
// Fine, 1.5
return false;
}
}
public IntrospectionHelperProxy getIntrospectionHelper(Class clazz) {
return new IntrospectionHelperImpl(clazz);
}
public boolean toBoolean(String val) {
return Project.toBoolean(val);
}
public String[] getEnumeratedValues(Class c) {
if (EnumeratedAttribute.class.isAssignableFrom(c)) {
try {
return ((EnumeratedAttribute)c.newInstance()).getValues();
} catch (Exception e) {
AntModule.err.notify(ErrorManager.INFORMATIONAL, e);
}
}
return null;
}
public boolean run(File buildFile, List targets, InputStream in, OutputWriter out, OutputWriter err,
Properties properties, int verbosity, String displayName) {
if (!classpathInitialized) {
classpathInitialized = true;
// #46171: Ant expects this path to have itself and whatever else you loaded with it,
// or AntClassLoader.getResources will not be able to find anything in the Ant loader.
Path.systemClasspath = new Path(null, AntBridge.getMainClassPath());
}
boolean ok = false;
// Important for various other stuff.
final boolean ant16 = isAnt16();
// Make sure "main Ant loader" is used as context loader for duration of the
// run. Otherwise some code, e.g. JAXP, will accidentally pick up NB classes,
// which can cause various undesirable effects.
ClassLoader oldCCL = Thread.currentThread().getContextClassLoader();
ClassLoader newCCL = Project.class.getClassLoader();
if (AntModule.err.isLoggable(ErrorManager.INFORMATIONAL)) {
AntModule.err.log("Fixing CCL: " + oldCCL + " -> " + newCCL);
}
Thread.currentThread().setContextClassLoader(newCCL);
AntBridge.fakeJavaClassPath();
try {
Project project = null;
// first use the ProjectHelper to create the project object
// from the given build file.
NbBuildLogger logger = new NbBuildLogger(buildFile, out, err, verbosity, displayName);
Vector targs;
try {
project = new Project();
project.addBuildListener(logger);
project.init();
try {
addCustomDefs(project);
} catch (IOException e) {
throw new BuildException(e);
}
project.setUserProperty("ant.file", buildFile.getAbsolutePath()); // NOI18N
// #14993:
project.setUserProperty("ant.version", Main.getAntVersion()); // NOI18N
project.setUserProperty("ant.home", AntSettings.getDefault().getAntHomeWithDefault().getAbsolutePath()); // NOI18N
Iterator it = properties.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
project.setUserProperty((String) entry.getKey(), (String) entry.getValue());
}
if (in != null && ant16) {
try {
Method m = Project.class.getMethod("setDefaultInputStream", new Class[] {InputStream.class}); // NOI18N
m.invoke(project, new Object[] {in});
} catch (Exception e) {
AntModule.err.notify(ErrorManager.INFORMATIONAL, e);
}
}
if (AntModule.err.isLoggable(ErrorManager.INFORMATIONAL)) {
AntModule.err.log("CCL when configureProject is called: " + Thread.currentThread().getContextClassLoader());
}
ProjectHelper projhelper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", projhelper); // NOI18N
projhelper.parse(project, buildFile);
project.setInputHandler(new NbInputHandler());
if (targets != null) {
targs = new Vector(targets);
} else {
targs = new Vector(1);
targs.add(project.getDefaultTarget());
}
logger.setActualTargets(targets != null ? (String[])targets.toArray(new String[targets.size()]) : null);
}
catch (BuildException be) {
logger.buildInitializationFailed(be);
out.close();
err.close();
if (in != null) {
try {
in.close();
} catch (IOException e) {
AntModule.err.notify(e);
}
}
return false;
}
project.fireBuildStarted();
// Save & restore system output streams.
InputStream is = System.in;
if (in != null && ant16) {
try {
Class dis = Class.forName("org.apache.tools.ant.DemuxInputStream"); // NOI18N
Constructor c = dis.getConstructor(new Class[] {Project.class});
is = (InputStream)c.newInstance(new Object[] {project});
} catch (Exception e) {
AntModule.err.notify(ErrorManager.INFORMATIONAL, e);
}
}
AntBridge.pushSystemInOutErr(is,
new PrintStream(new DemuxOutputStream(project, false)),
new PrintStream(new DemuxOutputStream(project, true)));
try {
// Execute the configured project
//writer.println("#4"); // NOI18N
project.executeTargets(targs);
//writer.println("#5"); // NOI18N
project.fireBuildFinished(null);
ok = true;
} catch (Throwable t) {
// Really need to catch everything, else AntClassLoader.cleanup may
// not be called, resulting in a memory leak and/or locked JARs (#42431).
project.fireBuildFinished(t);
} finally {
AntBridge.restoreSystemInOutErr();
out.close();
err.close();
if (in != null) {
try {
in.close();
} catch (IOException e) {
AntModule.err.notify(e);
}
}
}
// Now check to see if the Project defined any cool new custom tasks.
final Project p2 = project;
RequestProcessor.getDefault().post(new Runnable() {
public void run() {
IntrospectedInfo custom = AntSettings.getDefault().getCustomDefs();
Map defs = new HashMap(); // Map
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.