File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\cli\CliManagerImpl.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.cli;
25
26 import hudson.remoting.CallableFilter;
27 import hudson.remoting.Channel;
28 import hudson.remoting.Pipe;
29 import org.acegisecurity.Authentication;
30 import org.acegisecurity.context.SecurityContext;
31 import org.acegisecurity.context.SecurityContextHolder;
32
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.io.PrintStream;
36 import java.io.Serializable;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.concurrent.Callable;
41 import java.util.logging.Logger;
42
43 /**
44 * {@link CliEntryPoint} implementation exposed to the remote CLI.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public class CliManagerImpl implements CliEntryPoint, Serializable {
49 private transient final Channel channel;
50
51 private Authentication transportAuth;
52
53 /**
54 * Runs callable from this CLI client with the transport authentication credential.
55 */
56 private final CallableFilter authenticationFilter = new CallableFilter() {
57 public <V> V call(Callable<V> callable) throws Exception {
58 SecurityContext context = SecurityContextHolder.getContext();
59 Authentication old = context.getAuthentication();
60 if (transportAuth!=null)
61 context.setAuthentication(transportAuth);
62 try {
63 return callable.call();
64 } finally {
65 context.setAuthentication(old);
66 }
67 }
68 };
69
70 public CliManagerImpl(Channel channel) {
71 this.channel = channel;
72 channel.addLocalExecutionInterceptor(authenticationFilter);
73 }
74
75 public int main(List<String> args, Locale locale, InputStream stdin, OutputStream stdout, OutputStream stderr) {
76 // remoting sets the context classloader to the RemoteClassLoader,
77 // which slows down the classloading. we don't load anything from CLI,
78 // so counter that effect.
79 Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
80
81 PrintStream out = new PrintStream(stdout);
82 PrintStream err = new PrintStream(stderr);
83
84 String subCmd = args.get(0);
85 CLICommand cmd = CLICommand.clone(subCmd);
86 if(cmd!=null) {
87 cmd.channel = Channel.current();
88 final CLICommand old = CLICommand.setCurrent(cmd);
89 try {
90 transportAuth = Channel.current().getProperty(CLICommand.TRANSPORT_AUTHENTICATION);
91 cmd.setTransportAuth(transportAuth);
92 return cmd.main(args.subList(1,args.size()),locale, stdin, out, err);
93 } finally {
94 CLICommand.setCurrent(old);
95 }
96 }
97
98 err.println("No such command: "+subCmd);
99 new HelpCommand().main(Collections.<String>emptyList(), locale, stdin, out, err);
100 return -1;
101 }
102
103 public void authenticate(final String protocol, final Pipe c2s, final Pipe s2c) {
104 for (final CliTransportAuthenticator cta : CliTransportAuthenticator.all()) {
105 if (cta.supportsProtocol(protocol)) {
106 new Thread() {
107 @Override
108 public void run() {
109 cta.authenticate(protocol,channel,new Connection(c2s.getIn(), s2c.getOut()));
110 }
111 }.start();
112 return;
113 }
114 }
115 throw new UnsupportedOperationException("Unsupported authentication protocol: "+protocol);
116 }
117
118 public boolean hasCommand(String name) {
119 return CLICommand.clone(name)!=null;
120 }
121
122 public int protocolVersion() {
123 return VERSION;
124 }
125
126 private Object writeReplace() {
127 return Channel.current().export(CliEntryPoint.class,this);
128 }
129
130 private static final Logger LOGGER = Logger.getLogger(CliManagerImpl.class.getName());
131 }
132