1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.miloss.fgsms.osagent;
18
19 import java.io.PrintStream;
20
21 import java.util.Arrays;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.hyperic.sigar.Sigar;
28 import org.hyperic.sigar.SigarProxy;
29 import org.hyperic.sigar.SigarException;
30 import org.hyperic.sigar.cmd.Shell;
31
32 import org.hyperic.sigar.pager.PageControl;
33 import org.hyperic.sigar.pager.PageFetchException;
34 import org.hyperic.sigar.pager.StaticPageFetcher;
35
36 import org.hyperic.sigar.util.GetlineCompleter;
37 import org.hyperic.sigar.util.PrintfFormat;
38
39 import org.hyperic.sigar.shell.CollectionCompleter;
40 import org.hyperic.sigar.shell.ProcessQueryCompleter;
41 import org.hyperic.sigar.shell.ShellCommandBase;
42 import org.hyperic.sigar.shell.ShellCommandExecException;
43 import org.hyperic.sigar.shell.ShellCommandUsageException;
44
45 public abstract class SigarCommandBase
46 extends ShellCommandBase
47 implements GetlineCompleter {
48
49 protected Shell shell;
50 protected PrintStream out = System.out;
51 protected PrintStream err = System.err;
52 protected Sigar sigar;
53 protected SigarProxy proxy;
54 protected List output = new ArrayList();
55 private CollectionCompleter completer;
56 private GetlineCompleter ptqlCompleter;
57 private Collection completions = new ArrayList();
58 private PrintfFormat formatter;
59 private ArrayList printfItems = new ArrayList();
60
61 public SigarCommandBase(Shell shell) {
62 this.shell = shell;
63 this.out = shell.getOutStream();
64 this.err = shell.getErrStream();
65 this.sigar = shell.getSigar();
66 this.proxy = shell.getSigarProxy();
67
68
69 this.completer = new CollectionCompleter(shell);
70 if (isPidCompleter()) {
71 this.ptqlCompleter = new ProcessQueryCompleter(shell);
72 }
73 }
74
75 public SigarCommandBase() {
76 this(new Shell());
77 this.shell.setPageSize(PageControl.SIZE_UNLIMITED);
78 }
79
80 public void setOutputFormat(String format) {
81 this.formatter = new PrintfFormat(format);
82 }
83
84 public PrintfFormat getFormatter() {
85 return this.formatter;
86 }
87
88 public String sprintf(String format, Object[] items) {
89 return new PrintfFormat(format).sprintf(items);
90 }
91
92 public void printf(String format, Object[] items) {
93 println(sprintf(format, items));
94 }
95
96 public void printf(Object[] items) {
97 PrintfFormat formatter = getFormatter();
98 if (formatter == null) {
99
100 this.printfItems.add(items);
101 }
102 else {
103 println(formatter.sprintf(items));
104 }
105 }
106
107 public void printf(List items) {
108 printf((Object[])items.toArray(new Object[0]));
109 }
110
111 public void println(String line) {
112 if (this.shell.isInteractive()) {
113 this.output.add(line);
114 }
115 else {
116 this.out.println(line);
117 }
118 }
119
120 private void flushPrintfItems() {
121 if (this.printfItems.size() == 0) {
122 return;
123 }
124
125
126 int[] max = null;
127
128 for (Iterator it=this.printfItems.iterator();
129 it.hasNext();)
130 {
131 Object[] items = (Object[])it.next();
132 if (max == null) {
133 max = new int[items.length];
134 Arrays.fill(max, 0);
135 }
136 for (int i=0; i<items.length; i++) {
137 int len = items[i].toString().length();
138 if (len > max[i]) {
139 max[i] = len;
140 }
141 }
142 }
143
144 StringBuilder format = new StringBuilder();
145 if (max!=null){
146 for (int i=0; i<max.length; i++) {
147 format.append("%-" + max[i] + "s");
148 if (i < max.length-1) {
149 format.append(" ");
150 }
151 }
152 }
153 for (Iterator it=this.printfItems.iterator();
154 it.hasNext();)
155 {
156 printf(format.toString(), (Object[])it.next());
157 }
158 this.printfItems.clear();
159 }
160
161 public void flush() {
162 flushPrintfItems();
163
164 try {
165 this.shell.performPaging(new StaticPageFetcher(this.output));
166 } catch(PageFetchException e) {
167 this.err.println("Error paging: " + e.getMessage());
168 } finally {
169 this.output.clear();
170 }
171 }
172
173
174
175 protected boolean validateArgs(String[] args) {
176 return args.length == 0;
177 }
178
179
180 public Collection getCompletions() {
181 return this.completions;
182 }
183
184 public GetlineCompleter getCompleter() {
185 return null;
186 }
187
188 public boolean isPidCompleter() {
189 return false;
190 }
191
192 public String completePid(String line) {
193 if ((line.length() >= 1) &&
194 Character.isDigit(line.charAt(0)))
195 {
196 return line;
197 }
198
199 return this.ptqlCompleter.complete(line);
200 }
201
202 public String complete(String line) {
203 if (isPidCompleter()) {
204 return completePid(line);
205 }
206 GetlineCompleter c = getCompleter();
207 if (c != null) {
208 return c.complete(line);
209 }
210
211 this.completer.setCollection(getCompletions());
212 return this.completer.complete(line);
213 }
214 }