提交 01fc0c46 编写于 作者: K kohsuke

Fixed a regression in the <tt>groovy</tt> CLI command

    <a href="http://d.hatena.ne.jp/tanamon/20090630/1246372887">report</a>

CLICommand may get serialized and sent to somewhere else, so don't have its initializers do anything harmful

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@19339 71c3de6d-444a-0410-be80-ed276b4c234a
上级 11b0d19a
......@@ -203,21 +203,4 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
}
return null;
}
static {
// register option handlers that are defined
ClassLoaders cls = new ClassLoaders();
cls.put(Hudson.getInstance().getPluginManager().uberClassLoader);
ResourceNameIterator servicesIter =
new DiscoverServiceNames(cls).findResourceNames(OptionHandler.class.getName());
final ResourceClassIterator itr =
new DiscoverClasses(cls).findResourceClasses(servicesIter);
while(itr.hasNext()) {
Class h = itr.nextResourceClass().loadClass();
Class c = Types.erasure(Types.getTypeArgument(Types.getBaseClass(h, OptionHandler.class), 0));
CmdLineParser.registerHandler(c,h);
}
}
}
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.cli;
import hudson.remoting.Channel;
import hudson.model.Hudson;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.discovery.resource.ClassLoaders;
import org.apache.commons.discovery.resource.classes.DiscoverClasses;
import org.apache.commons.discovery.resource.names.DiscoverServiceNames;
import org.apache.commons.discovery.ResourceNameIterator;
import org.apache.commons.discovery.ResourceClassIterator;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.CmdLineParser;
import org.jvnet.tiger_types.Types;
import java.util.List;
import java.util.Locale;
import java.util.Collections;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Serializable;
/**
* {@link CliEntryPoint} implementation exposed to the remote CLI.
*
* @author Kohsuke Kawaguchi
*/
public class CliManagerImpl implements CliEntryPoint, Serializable {
/**
* CLI should be executed under this credential.
*/
private final Authentication auth;
public CliManagerImpl(Authentication auth) {
this.auth = auth;
}
public int main(List<String> args, Locale locale, InputStream stdin, OutputStream stdout, OutputStream stderr) {
// remoting sets the context classloader to the RemoteClassLoader,
// which slows down the classloading. we don't load anything from CLI,
// so counter that effect.
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
PrintStream out = new PrintStream(stdout);
PrintStream err = new PrintStream(stderr);
String subCmd = args.get(0);
CLICommand cmd = CLICommand.clone(subCmd);
if(cmd!=null) {
Authentication old = SecurityContextHolder.getContext().getAuthentication();
SecurityContextHolder.getContext().setAuthentication(auth);
try {
// execute the command, do so with the originator of the request as the principal
return cmd.main(args.subList(1,args.size()),stdin, out, err);
} finally {
SecurityContextHolder.getContext().setAuthentication(old);
}
}
err.println("No such command: "+subCmd);
new HelpCommand().main(Collections.<String>emptyList(), stdin, out, err);
return -1;
}
public int protocolVersion() {
return VERSION;
}
private Object writeReplace() {
return Channel.current().export(CliEntryPoint.class,this);
}
static {
// register option handlers that are defined
ClassLoaders cls = new ClassLoaders();
cls.put(Hudson.getInstance().getPluginManager().uberClassLoader);
ResourceNameIterator servicesIter =
new DiscoverServiceNames(cls).findResourceNames(OptionHandler.class.getName());
final ResourceClassIterator itr =
new DiscoverClasses(cls).findResourceClasses(servicesIter);
while(itr.hasNext()) {
Class h = itr.nextResourceClass().loadClass();
Class c = Types.erasure(Types.getTypeArgument(Types.getBaseClass(h, OptionHandler.class), 0));
CmdLineParser.registerHandler(c,h);
}
}
}
......@@ -49,8 +49,7 @@ import hudson.Extension;
import hudson.tools.ToolInstallation;
import hudson.tools.ToolDescriptor;
import hudson.cli.CliEntryPoint;
import hudson.cli.CLICommand;
import hudson.cli.HelpCommand;
import hudson.cli.CliManagerImpl;
import hudson.logging.LogRecorderManager;
import hudson.lifecycle.Lifecycle;
import hudson.model.Descriptor.FormException;
......@@ -2730,55 +2729,6 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
rsp.getWriter().println("GCed");
}
/**
* {@link CliEntryPoint} implementation exposed to the remote CLI.
*/
private final class CliManager implements CliEntryPoint, Serializable {
/**
* CLI should be executed under this credential.
*/
private final Authentication auth;
private CliManager(Authentication auth) {
this.auth = auth;
}
public int main(List<String> args, Locale locale, InputStream stdin, OutputStream stdout, OutputStream stderr) {
// remoting sets the context classloader to the RemoteClassLoader,
// which slows down the classloading. we don't load anything from CLI,
// so counter that effect.
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
PrintStream out = new PrintStream(stdout);
PrintStream err = new PrintStream(stderr);
String subCmd = args.get(0);
CLICommand cmd = CLICommand.clone(subCmd);
if(cmd!=null) {
Authentication old = SecurityContextHolder.getContext().getAuthentication();
SecurityContextHolder.getContext().setAuthentication(auth);
try {
// execute the command, do so with the originator of the request as the principal
return cmd.main(args.subList(1,args.size()),stdin, out, err);
} finally {
SecurityContextHolder.getContext().setAuthentication(old);
}
}
err.println("No such command: "+subCmd);
new HelpCommand().main(Collections.<String>emptyList(), stdin, out, err);
return -1;
}
public int protocolVersion() {
return VERSION;
}
private Object writeReplace() {
return Channel.current().export(CliEntryPoint.class,this);
}
}
private transient final Map<UUID,FullDuplexHttpChannel> duplexChannels = new HashMap<UUID, FullDuplexHttpChannel>();
/**
......@@ -2800,7 +2750,7 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
if(req.getHeader("Side").equals("download")) {
duplexChannels.put(uuid,server=new FullDuplexHttpChannel(uuid, !hasPermission(ADMINISTER)) {
protected void main(Channel channel) throws IOException, InterruptedException {
channel.setProperty(CliEntryPoint.class.getName(),new CliManager(auth));
channel.setProperty(CliEntryPoint.class.getName(),new CliManagerImpl(auth));
}
});
try {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册