1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39
40 @ResultPath("/content")
41
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
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
81
82
83
84
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
96
97 private ShellService getShellService() {
98
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 }