001 /* 002 * JBoss, Home of Professional Open Source. 003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors 004 * as indicated by the @author tags. See the copyright.txt file in the 005 * distribution for a full listing of individual contributors. 006 * 007 * This is free software; you can redistribute it and/or modify it 008 * under the terms of the GNU Lesser General Public License as 009 * published by the Free Software Foundation; either version 2.1 of 010 * the License, or (at your option) any later version. 011 * 012 * This software is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this software; if not, write to the Free 019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 021 */ 022 package org.jboss.example.dna.sequencers; 023 024 import java.io.BufferedReader; 025 import java.io.File; 026 import java.io.IOException; 027 import java.io.InputStreamReader; 028 import java.net.URL; 029 import java.util.List; 030 import org.jboss.dna.repository.sequencers.SequencingService; 031 032 /** 033 * @author Randall Hauch 034 */ 035 public class ConsoleInput implements UserInterface { 036 037 protected static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 038 039 public ConsoleInput( final SequencingClient client ) { 040 try { 041 System.out.println(); 042 System.out.print("Starting repository and sequencing service ... "); 043 client.startRepository(); 044 System.out.print("done.\nStarting sequencing service ... "); 045 client.startDnaServices(); 046 System.out.println("done."); 047 System.out.println(); 048 049 System.out.println(getMenu()); 050 Thread eventThread = new Thread(new Runnable() { 051 052 private boolean quit = false; 053 054 public void run() { 055 try { 056 while (!quit) { 057 System.out.print(">"); 058 try { 059 String input = in.readLine(); 060 if (input.length() != 1) { 061 System.out.println("Please enter a valid option."); 062 continue; 063 } 064 065 char option = input.charAt(0); 066 switch (option) { 067 case 'u': 068 client.uploadFile(); 069 break; 070 case 's': 071 client.search(); 072 break; 073 case 'm': 074 case '?': 075 case 'h': 076 System.out.println(getMenu()); 077 break; 078 case 'd': 079 System.out.println(getStatistics(client.getStatistics())); 080 break; 081 case 'q': 082 quit = true; 083 break; 084 default: 085 System.out.println("Invalid option."); 086 break; 087 } 088 } catch (NumberFormatException e) { 089 System.out.println("Invalid integer " + e.getMessage()); 090 } catch (IllegalArgumentException e) { 091 System.out.println(e.getMessage()); 092 } catch (IOException e) { 093 e.printStackTrace(); 094 } catch (Throwable e) { 095 e.printStackTrace(); 096 } 097 } 098 } finally { 099 try { 100 // Terminate ... 101 System.out.println(); 102 System.out.print("Shutting down sequencing service ... "); 103 client.shutdownDnaServices(); 104 System.out.print("done.\nShutting down repository ... "); 105 client.shutdownRepository(); 106 System.out.print("done."); 107 System.out.println(); 108 System.out.println(); 109 } catch (Exception err) { 110 System.out.println("Error shutting down sequencing service and repository: " 111 + err.getLocalizedMessage()); 112 err.printStackTrace(System.err); 113 } 114 } 115 } 116 }); 117 118 eventThread.start(); 119 } catch (Exception err) { 120 System.out.println("Error: " + err.getLocalizedMessage()); 121 err.printStackTrace(System.err); 122 } 123 } 124 125 protected String getMenu() { 126 StringBuilder buffer = new StringBuilder(); 127 buffer.append("-----------------------------------\n"); 128 buffer.append("Menu:\n"); 129 buffer.append("\n"); 130 buffer.append("u) Upload a file to the repository\n"); 131 buffer.append("s) Search the repository using extracted metadata\n"); 132 buffer.append("\n"); 133 buffer.append("d) Display statistics\n"); 134 buffer.append("\n"); 135 buffer.append("?) Show this menu\n"); 136 buffer.append("q) Quit"); 137 return buffer.toString(); 138 } 139 140 /** 141 * {@inheritDoc} 142 */ 143 public URL getFileToUpload() throws IllegalArgumentException, IOException { 144 System.out.println("Please enter the file to upload:"); 145 String path = in.readLine(); 146 File file = new File(path); 147 if (!file.exists()) { 148 throw new IllegalArgumentException("The file \"" + file.getAbsolutePath() + "\" does not exist."); 149 } 150 if (!file.canRead()) { 151 throw new IllegalArgumentException("Unable to read \"" + file.getAbsolutePath() + "\"."); 152 } 153 if (!file.isFile()) { 154 throw new IllegalArgumentException("Please specify a file. The file \"" + file.getAbsolutePath() 155 + "\" is a directory."); 156 } 157 return file.toURI().toURL(); 158 } 159 160 public String getRepositoryPath( String defaultPath ) throws IllegalArgumentException, IOException { 161 if (defaultPath != null) defaultPath = defaultPath.trim(); 162 if (defaultPath.length() == 0) defaultPath = null; 163 String displayDefaultPath = defaultPath == null ? "" : " [" + defaultPath.trim() + "]"; 164 System.out.println("Please enter the repository path where the file should be placed" + displayDefaultPath + ":"); 165 String path = in.readLine().trim(); 166 if (path.length() == 0) { 167 if (defaultPath == null) { 168 throw new IllegalArgumentException("The path \"" + path + "\" is not valid."); 169 } 170 path = defaultPath; 171 } 172 return path; 173 } 174 175 public void displaySearchResults( List<ContentInfo> contentInfos ) { 176 System.out.println(); 177 if (contentInfos.isEmpty()) { 178 System.out.println("No results were found."); 179 System.out.println(); 180 return; 181 } 182 if (contentInfos.size() == 1) { 183 System.out.println("1 result was found:"); 184 } else { 185 System.out.println("" + contentInfos.size() + " results were found:"); 186 } 187 int counter = 1; 188 for (ContentInfo info : contentInfos) { 189 System.out.println(" " + info.getInfoType() + " " + counter++); 190 System.out.println(info.toString()); 191 } 192 System.out.println(); 193 } 194 195 public String getStatistics( SequencingService.Statistics stats ) { 196 StringBuilder sb = new StringBuilder(); 197 sb.append("\n"); 198 sb.append("# nodes sequenced: ").append(stats.getNumberOfNodesSequenced()).append("\n"); 199 sb.append("# nodes skipped: ").append(stats.getNumberOfNodesSkipped()).append("\n"); 200 sb.append("\n"); 201 return sb.toString(); 202 } 203 }