1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.example.sequencer;
25
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.net.URL;
31 import java.util.List;
32 import org.modeshape.repository.sequencer.SequencingService;
33
34
35
36
37 public class ConsoleInput implements UserInterface {
38
39 protected static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
40
41 public ConsoleInput( final SequencingClient client ) {
42 try {
43 System.out.println();
44 System.out.print("Starting ModeShape repository and sequencing service ... ");
45 client.startRepository();
46 System.out.println("done.");
47 System.out.println();
48
49 System.out.println(getMenu());
50 Thread eventThread = new Thread(new Runnable() {
51
52 private boolean quit = false;
53
54 public void run() {
55 try {
56 while (!quit) {
57 System.out.print(">");
58 try {
59 String input = in.readLine();
60 if (input.length() != 1) {
61 System.out.println("Please enter a valid option.");
62 continue;
63 }
64
65 char option = input.charAt(0);
66 switch (option) {
67 case 'u':
68 client.uploadFile();
69 break;
70 case 's':
71 client.search();
72 break;
73 case 'm':
74 case '?':
75 case 'h':
76 System.out.println(getMenu());
77 break;
78 case 'd':
79 System.out.println(getStatistics(client.getStatistics()));
80 break;
81 case 'q':
82 quit = true;
83 break;
84 default:
85 System.out.println("Invalid option.");
86 break;
87 }
88 } catch (NumberFormatException e) {
89 System.out.println("Invalid integer " + e.getMessage());
90 } catch (IllegalArgumentException e) {
91 System.out.println(e.getMessage());
92 } catch (IOException e) {
93 e.printStackTrace();
94 } catch (Throwable e) {
95 e.printStackTrace();
96 }
97 }
98 } finally {
99 try {
100
101 System.out.println();
102 System.out.print("Shutting down repository ... ");
103 client.shutdownRepository();
104 System.out.print("done.");
105 System.out.println();
106 System.out.println();
107 } catch (Exception err) {
108 System.out.println("Error shutting down sequencing service and repository: "
109 + err.getLocalizedMessage());
110 err.printStackTrace(System.err);
111 }
112 }
113 }
114 });
115
116 eventThread.start();
117 } catch (Exception err) {
118 System.out.println("Error: " + err.getLocalizedMessage());
119 err.printStackTrace(System.err);
120 }
121 }
122
123 protected String getMenu() {
124 StringBuilder buffer = new StringBuilder();
125 buffer.append("-----------------------------------\n");
126 buffer.append("Menu:\n");
127 buffer.append("\n");
128 buffer.append("u) Upload a file to the repository\n");
129 buffer.append("s) Search the repository using extracted metadata\n");
130 buffer.append("\n");
131 buffer.append("d) Display statistics\n");
132 buffer.append("\n");
133 buffer.append("?) Show this menu\n");
134 buffer.append("q) Quit");
135 return buffer.toString();
136 }
137
138
139
140
141 public URL getFileToUpload() throws IllegalArgumentException, IOException {
142 System.out.println("Please enter the file to upload:");
143 String path = in.readLine();
144 File file = new File(path);
145 if (!file.exists()) {
146 throw new IllegalArgumentException("The file \"" + file.getAbsolutePath() + "\" does not exist.");
147 }
148 if (!file.canRead()) {
149 throw new IllegalArgumentException("Unable to read \"" + file.getAbsolutePath() + "\".");
150 }
151 if (!file.isFile()) {
152 throw new IllegalArgumentException("Please specify a file. The file \"" + file.getAbsolutePath()
153 + "\" is a directory.");
154 }
155 return file.toURI().toURL();
156 }
157
158 public String getRepositoryPath( String defaultPath ) throws IllegalArgumentException, IOException {
159 if (defaultPath != null) defaultPath = defaultPath.trim();
160 if (defaultPath.length() == 0) defaultPath = null;
161 String displayDefaultPath = defaultPath == null ? "" : " [" + defaultPath.trim() + "]";
162 System.out.println("Please enter the repository path where the file should be placed" + displayDefaultPath + ":");
163 String path = in.readLine().trim();
164 if (path.length() == 0) {
165 if (defaultPath == null) {
166 throw new IllegalArgumentException("The path \"" + path + "\" is not valid.");
167 }
168 path = defaultPath;
169 }
170 return path;
171 }
172
173 public void displaySearchResults( List<ContentInfo> contentInfos ) {
174 System.out.println();
175 if (contentInfos.isEmpty()) {
176 System.out.println("No results were found.");
177 System.out.println();
178 return;
179 }
180 if (contentInfos.size() == 1) {
181 System.out.println("1 result was found:");
182 } else {
183 System.out.println("" + contentInfos.size() + " results were found:");
184 }
185 int counter = 1;
186 for (ContentInfo info : contentInfos) {
187 System.out.println(" " + info.getInfoType() + " " + counter++);
188 System.out.println(info.toString());
189 }
190 System.out.println();
191 }
192
193 public String getStatistics( SequencingService.Statistics stats ) {
194 StringBuilder sb = new StringBuilder();
195 sb.append("\n");
196 sb.append("# nodes sequenced: ").append(stats.getNumberOfNodesSequenced()).append("\n");
197 sb.append("# nodes skipped: ").append(stats.getNumberOfNodesSkipped()).append("\n");
198 sb.append("\n");
199 return sb.toString();
200 }
201
202 public void displayError( Exception e ) {
203 System.err.println(e.getMessage());
204 }
205 }