# HG changeset patch # User Milos Kleint # Date 1246444679 -7200 # Node ID 7274fb0c27c28840d96995e706e9870c5c5b681b # Parent e037d7739650febe63f7f7ad809cacdce7f60c53 #138116 add api to allow pluggable means of destroying an external process. diff -r e037d7739650 -r 7274fb0c27c2 extexecution/apichanges.xml --- a/extexecution/apichanges.xml Mon Jun 29 15:19:39 2009 +0200 +++ b/extexecution/apichanges.xml Wed Jul 01 12:37:59 2009 +0200 @@ -107,6 +107,22 @@ + + + + New SPI for improved external process termination + + + + + + + + + + + + Configurable charset for process streams diff -r e037d7739650 -r 7274fb0c27c2 extexecution/manifest.mf --- a/extexecution/manifest.mf Mon Jun 29 15:19:39 2009 +0200 +++ b/extexecution/manifest.mf Wed Jul 01 12:37:59 2009 +0200 @@ -2,5 +2,5 @@ AutoUpdate-Show-In-Client: false OpenIDE-Module: org.netbeans.modules.extexecution/2 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/extexecution/resources/Bundle.properties -OpenIDE-Module-Specification-Version: 1.15 +OpenIDE-Module-Specification-Version: 1.16 diff -r e037d7739650 -r 7274fb0c27c2 extexecution/nbproject/project.xml --- a/extexecution/nbproject/project.xml Mon Jun 29 15:19:39 2009 +0200 +++ b/extexecution/nbproject/project.xml Wed Jul 01 12:37:59 2009 +0200 @@ -110,6 +110,7 @@ org.netbeans.api.extexecution org.netbeans.api.extexecution.print org.netbeans.api.extexecution.input + org.netbeans.spi.extexecution.destroy diff -r e037d7739650 -r 7274fb0c27c2 extexecution/src/org/netbeans/api/extexecution/ExternalProcessBuilder.java --- a/extexecution/src/org/netbeans/api/extexecution/ExternalProcessBuilder.java Mon Jun 29 15:19:39 2009 +0200 +++ b/extexecution/src/org/netbeans/api/extexecution/ExternalProcessBuilder.java Wed Jul 01 12:37:59 2009 +0200 @@ -41,18 +41,24 @@ import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.UUID; import java.util.concurrent.Callable; import java.util.logging.Level; import java.util.logging.Logger; import java.util.prefs.Preferences; import org.netbeans.api.annotations.common.CheckReturnValue; import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.modules.extexecution.WrapperProcess; +import org.netbeans.spi.extexecution.destroy.DestroyUtils; import org.openide.util.NbPreferences; import org.openide.util.Parameters; import org.openide.util.Utilities; @@ -282,10 +288,13 @@ Map pbEnv = pb.environment(); Map env = buildEnvironment(pbEnv); pbEnv.putAll(env); + String uuid = UUID.randomUUID().toString(); + pbEnv.put(DestroyUtils.KEY_UUID, uuid); adjustProxy(pb); pb.redirectErrorStream(redirectErrorStream); logProcess(Level.FINE, pb); - return pb.start(); + WrapperProcess wp = new WrapperProcess(pb.start(), uuid); + return wp; } /** @@ -472,4 +481,6 @@ return this; } } + + } diff -r e037d7739650 -r 7274fb0c27c2 extexecution/src/org/netbeans/modules/extexecution/WrapperProcess.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extexecution/src/org/netbeans/modules/extexecution/WrapperProcess.java Wed Jul 01 12:37:59 2009 +0200 @@ -0,0 +1,90 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.extexecution; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Collections; +import org.netbeans.spi.extexecution.destroy.DestroyUtils; + +/** + * + * @author mkleint + */ +public class WrapperProcess extends Process { + private final String uuid; + private final Process del; + + public WrapperProcess(Process delegate, String uuid) { + this.del = delegate; + this.uuid = uuid; + } + + @Override + public OutputStream getOutputStream() { + return del.getOutputStream(); + } + + @Override + public InputStream getInputStream() { + return del.getInputStream(); + } + + @Override + public InputStream getErrorStream() { + return del.getErrorStream(); + } + + @Override + public int waitFor() throws InterruptedException { + return del.waitFor(); + } + + @Override + public int exitValue() { + return del.exitValue(); + } + + @Override + public void destroy() { + DestroyUtils.destroy(del, Collections.singletonMap(DestroyUtils.KEY_UUID, uuid)); + } + +} diff -r e037d7739650 -r 7274fb0c27c2 extexecution/src/org/netbeans/spi/extexecution/destroy/DestroyUtils.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extexecution/src/org/netbeans/spi/extexecution/destroy/DestroyUtils.java Wed Jul 01 12:37:59 2009 +0200 @@ -0,0 +1,85 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.spi.extexecution.destroy; + +import java.util.Map; +import org.netbeans.modules.extexecution.WrapperProcess; +import org.openide.util.Lookup; + +/** + * Utility class capable of properly terminating external process along with any + * child processes created during execution. + * + * @author mkleint + * @since 1.16 + */ +public class DestroyUtils { + + private DestroyUtils() { + + } + + /** + * The default environment variable key to be used to identify process trees + * in destroy method. + */ + public static final String KEY_UUID = "NB_EXEC_PROCESS_UUID"; //NOI18N + + /** + * Destroys the process passed as parameter and attempts to terminate all child + * processes created during the process' execution. + * @param prcs process to kill + * @param env Map containing environment variable names and values. + * Any process running with such envvar's value will be terminated. + * Improves localization of child processes. + */ + public static void destroy(Process prcs, Map env) { + if (prcs instanceof WrapperProcess) { + prcs.destroy(); + return; + } + ProcessDestroyPerformer pdp = Lookup.getDefault().lookup(ProcessDestroyPerformer.class); + if (pdp != null) { + pdp.destroy(prcs, env); + } else { + prcs.destroy(); + } + } +} diff -r e037d7739650 -r 7274fb0c27c2 extexecution/src/org/netbeans/spi/extexecution/destroy/ProcessDestroyPerformer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extexecution/src/org/netbeans/spi/extexecution/destroy/ProcessDestroyPerformer.java Wed Jul 01 12:37:59 2009 +0200 @@ -0,0 +1,69 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.spi.extexecution.destroy; + +import java.util.Map; + +/** + * A service capable of properly terminating external process along with any + * child processes created during execution. + * + * Note: not to be implemented by modules, might not be present in all versions of + * the application. + * Please use DestroyUtils.destroy() for accessing the service. + * + * @author mkleint + * @since 1.16 + */ +public interface ProcessDestroyPerformer { + + /** + * Destroys the process passed as parameter and attempts to terminate all child + * processes created during the process' execution. + * @param process process to kill + * @param env Map containing environment variable names and values. + * Any process running with such envvar's value will be terminated. + * Improves localization of child processes. + * + * @param process + * @param env + */ + void destroy(Process process, Map env); +} diff -r e037d7739650 -r 7274fb0c27c2 extexecution/src/org/netbeans/spi/extexecution/destroy/package-info.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extexecution/src/org/netbeans/spi/extexecution/destroy/package-info.java Wed Jul 01 12:37:59 2009 +0200 @@ -0,0 +1,48 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + +/** + * The support SPI for terminating external processes. + * + * @see org.netbeans.spi.extexecution.destroy.DestroyUtils + */ +package org.netbeans.spi.extexecution.destroy; +