View Javadoc

1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.web.impl.ui.page.admin;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  
25  import org.vaadin.console.Console;
26  import org.vaadin.console.Console.Command;
27  import org.vaadin.navigator7.Page;
28  
29  import com.vaadin.ui.VerticalLayout;
30  
31  /**
32   * The Class ConsolePage.
33   */
34  @Page(uriName = "console",crawlable=true)
35  @SuppressWarnings("serial")
36  public final class ConsolePage extends AbstractAdminPage {
37  
38  	/** The content. */
39  	private final VerticalLayout content = new VerticalLayout();
40  
41  	/**
42  	 * Instantiates a new console page.
43  	 */
44  	public ConsolePage() {
45  		super();
46  		content.setSizeFull();
47  		content.setMargin(true);
48  		content.setSpacing(true);
49  		setCompositionRoot(content);
50  
51  		// Create a console
52  		final Console console = new Console();
53  		content.addComponent(console);
54  		console.setPs("}> ");
55  		console.setCols(68);
56  		console.setRows(20);
57  		console.setMaxBufferSize(20);
58  		console.setGreeting("Welcome.");
59  		console.reset();
60  		console.focus();
61  
62  		final Command systemCommand = new Command() {
63  		    private static final long serialVersionUID = -5733237166568671987L;
64  
65  			@Override
66  			public Object execute(final Console console, final String[] argv)
67  					throws Exception {
68  				  final Process p = Runtime.getRuntime().exec(argv);
69  			        final InputStream in = p.getInputStream();
70  			        final StringBuilder o = new StringBuilder();
71  			        final InputStreamReader r = new InputStreamReader(in);
72  			        int c = -1;
73  			        try {
74  			            while ((c = r.read()) != -1) {
75  			                o.append((char) c);
76  			            }
77  			        } catch (final IOException e) {
78  			            o.append("[truncated]");
79  			        }
80  			        return o.toString();
81  			}
82  
83  			@Override
84  			public String getUsage(final Console console, final String[] argv) {
85  				return null;
86  			}
87  		};
88  
89  		console.addCommand("free", systemCommand);
90  
91  
92  		setHeight(getScreenHeight());
93  	}
94  
95  	/* (non-Javadoc)
96  	 * @see com.hack23.cia.web.impl.ui.page.common.AbstractPage#getPageTitle()
97  	 */
98  	@Override
99  	public String getPageTitle() {
100 		return "";
101 	}
102 
103 }