View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package gboat2.admin.osgi.action;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.PrintStream;
26  
27  import org.apache.felix.shell.ShellService;
28  import org.apache.struts2.convention.annotation.ResultPath;
29  import org.apache.struts2.osgi.DefaultBundleAccessor;
30  import org.osgi.framework.ServiceReference;
31  
32  import com.opensymphony.xwork2.Action;
33  import com.opensymphony.xwork2.ActionSupport;
34  
35  /**
36   * This action executes commands on the Felix Shell
37   * @date 2014-2-26
38   * @since 1.0
39   */
40  @ResultPath("/content")
41  //@gboat2.base.core.annotation.Module(name = "shell命令", desc = "执行命令,改变bundle的运行状态")
42  public class ShellAction extends ActionSupport {
43  
44  	private static final long serialVersionUID = 1L;
45  
46  	private String command;
47  
48  	private String output;
49  
50  	@Override
51  	public String execute() {
52  		System.setProperty("java.protocol.handler.pkgs", "org.ops4j.pax.url.assembly");
53  
54  		// get service
55  		ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
56  		ByteArrayOutputStream errByteStream = new ByteArrayOutputStream();
57  		PrintStream outStream = new PrintStream(outByteStream);
58  		PrintStream errStream = new PrintStream(errByteStream);
59  
60  		String outString = null; // 标准信息输出流
61  		String errString = null; // 错误信息输出流
62  		try {
63  		    // 执行命令
64  			executeCommand(command, outStream, errStream);
65  			outString = outByteStream.toString().trim();
66  			errString = errByteStream.toString().trim();
67  		} catch (Exception e) {
68  			errString = e.getMessage();
69  		} finally {
70  			outStream.close();
71  			errStream.close();
72  		}
73  
74  		output = (errString != null && errString.length() > 0) ? errString : outString;
75  
76  		return Action.SUCCESS;
77  	}
78  
79  	/**
80  	 * 执行 Felix 的 shell 命令
81  	 * @param commandLine 命令行的内容,包括命令及其参数
82  	 * @param out 标准信息输出流
83  	 * @param err 错误信息输出流
84  	 * @throws Exception 执行 shell 命令发生错误时,抛出异常
85  	 */
86  	public void executeCommand(String commandLine, PrintStream out, PrintStream err) throws Exception {
87  		ShellService shellService = getShellService();
88  		if (shellService != null)
89  			shellService.executeCommand(commandLine, out, err);
90  		else
91  			err.println("Apache Felix Shell service is not installed");
92  	}
93  
94  	/**
95  	 * @return Felix 执行 Shell 命令的服务实例
96  	 */
97  	private ShellService getShellService() {
98  		//bundle can be de-activated, so keeping a reference aorund is not a good idea
99  		DefaultBundleAccessor bundleAcessor = DefaultBundleAccessor.getInstance();
100 		ServiceReference<?> ref = bundleAcessor.getServiceReference(ShellService.class.getName());
101 		return (ShellService) bundleAcessor.getService(ref);
102 	}
103 
104     public String getCommand() {
105         return command;
106     }
107 
108     public void setCommand(String command) {
109         this.command = command;
110     }
111 
112     public String getOutput() {
113         return output;
114     }
115 
116 }