1 23 24 package examples.invoice.applications; 25 26 import org.objectweb.jorm.api.PClassMapping; 27 import org.objectweb.jorm.api.PMapper; 28 import org.objectweb.jorm.api.PException; 29 30 import java.io.FileInputStream ; 31 import java.io.InputStream ; 32 import java.util.Iterator ; 33 import java.util.Properties ; 34 35 38 public final class BasicShell { 39 private final static String PROMPT = "jormexsh"; 40 private final static int MAXCMDSIZE = 20; 41 private static BasicShell singleton = new BasicShell(); 42 private ShellExtent shellExtent; 43 protected InputStream in; 44 PMapper activeMapper = null; 45 46 48 static BasicShell getInstance() { 49 return singleton; 50 } 51 52 private BasicShell() { 53 } 54 55 57 public String promptString(String prompt, int maxsize) { 58 byte[] cmd = new byte[maxsize]; 59 int nextbyte = 0, inbyte; 60 try { 61 System.out.print(prompt); 62 System.out.flush(); 63 do { 64 inbyte = in.read(); 65 if (nextbyte < maxsize) 66 cmd[nextbyte] = (byte) inbyte; 67 nextbyte++; 68 } while ((inbyte != '\r') && (inbyte != '\n')); 69 if (inbyte == '\r') { 70 in.read(); 71 } 72 if (nextbyte <= maxsize) { 73 if (nextbyte != 1) 74 return new String (cmd, 0, nextbyte - 1); 75 else 76 return ""; 77 } else 78 return null; 79 } catch (Exception e) { 80 return null; 81 } 82 } 83 84 public void shellLoop(String propfilename, ShellExtent se, InputStream in) throws Exception { 85 Properties prop = new Properties (); 86 prop.load(new FileInputStream (propfilename)); 87 JormInit.getInstance().initLogSystem(prop); 88 JormInit.getInstance().initResourceAdapter(prop); 89 prop = null; 90 this.in = in; 91 shellExtent = se; 92 String cmd; 93 do { 94 cmd = readCmd(); 95 System.out.println(); 96 if (cmd.equals("q") || cmd.equals("quit")) 97 return; 98 if (!interpCmd(cmd)) 99 errorMsg("unknown"); 100 System.out.println(); 101 } while (true); 102 } 103 104 public void errorMsg(String msg) { 105 System.out.println("\tCommand failed: " + msg + ".\n"); 106 } 107 108 public void printStringBlankApp(String str, int size) { 109 System.out.print(str); 110 if (str.length() >= size) 111 return; 112 int bl = size - str.length(); 113 while (bl > 0) { 114 System.out.print(" "); 115 bl--; 116 } 117 } 118 119 121 private String readCmd() { 122 String cmd; 123 do { 124 cmd = promptString( 125 PROMPT + ((activeMapper == null) 126 ? "" 127 : ":mapper=" + activeMapper.getMapperName()) + "> ", 128 MAXCMDSIZE); 129 if (cmd == null) 130 errorMsg("wrong format"); 131 else if (cmd.length() > 0) 132 return cmd; 133 } while (true); 134 } 135 136 private boolean interpCmd(String cmd) throws Exception { 137 if (shellExtent.interpCmd(cmd)) 138 return true; 139 if (cmd.equals("lm") || cmd.equals("listmapper")) 140 listMapper(); 141 else if (cmd.equals("am") || cmd.equals("activatemapper")) 142 activateMapper(); 143 else if (cmd.equals("map")) 144 mapPClass(); 145 else if (cmd.equals("?") || cmd.equals("h") || cmd.equals("help")) 146 help(); 147 else 148 return false; 149 return true; 150 } 151 152 private void help() { 153 System.out.println("\tAvailable commands:"); 154 System.out.println("\t\t- help | h | ? : this command. It provides help information about available commands of this application."); 155 System.out.println("\t\t- listmapper | lm : lists all available mappers."); 156 System.out.println("\t\t- activatemapper | am : activates a mapper as the default one used by commands that require a mapper. It does it for the active mapper if any, or requests this mapper."); 157 System.out.println("\t\t- map : maps all the persistent classes involved in this application."); 158 shellExtent.help(); 159 } 160 161 164 private void listMapper() throws Exception { 165 Iterator it = JormInit.getInstance().registeredMapper(); 166 PMapper mapper; 167 if (!it.hasNext()) { 168 System.out.println("\n\tNo mapper registered yet.\n"); 169 return; 170 } 171 System.out.println("\n\tMAPPER NAME | URL"); 172 System.out.println("\t--------------------------------------------------"); 173 while (it.hasNext()) { 174 mapper = (PMapper) it.next(); 175 System.out.print("\t"); 176 printStringBlankApp(mapper.getMapperName(), 16); 177 System.out.print(" | "); 178 printStringBlankApp(JormInit.getInstance().getURL(mapper), 10); 179 System.out.println(); 180 } 181 } 182 183 private void mapPClass() throws PException { 184 String name; 185 PMapper m; 186 if (activeMapper != null) { 187 m = activeMapper; 188 } else { 189 name = promptString("\tMapper name: ", 16); 190 if (name == null) { 191 errorMsg("wrong input name for mapper"); 192 return; 193 } 194 m = JormInit.getInstance().getMapper(name); 195 if (m == null) { 196 errorMsg("no associated mapper"); 197 return; 198 } 199 } 200 String rem = promptString("\tInit OP (REMS, REMD, CREATE, NONE): ", 6); 201 byte cleanup_action; 202 if ((rem == null) || (rem.length() == 0)) 203 cleanup_action = PClassMapping.CREATE_STRUCTURE_IF_NEEDED; 204 else if (rem.equals("REMS")) 205 cleanup_action = PClassMapping.CLEANUP_REMOVEALL; 206 else if (rem.equals("REMD")) 207 cleanup_action = PClassMapping.CLEANUP_REMOVEDATA; 208 else if (rem.equals("NONE")) 209 cleanup_action = PClassMapping.CLEANUP_DONOTHING; 210 else 211 cleanup_action = PClassMapping.CREATE_STRUCTURE_IF_NEEDED; 212 try { 213 shellExtent.getHelper().map(m, 214 JormInit.getInstance().getLoggerFactory(), 215 cleanup_action); 216 System.out.println("Persistent classes mapped."); 217 } catch (Exception e) { 218 System.out.println("\n\tEXCEPTION"); 219 e.printStackTrace(); 220 System.out.println(); 221 } 222 } 223 224 private void activateMapper() throws PException { 225 String name = promptString("\tMapper name: ", 16); 226 if (name == null) { 227 errorMsg("wrong input names for mapper"); 228 return; 229 } 230 activeMapper = JormInit.getInstance().getMapper(name); 231 if (activeMapper == null) { 232 errorMsg("no associated mapper"); 233 return; 234 } 235 System.out.println("Mapper <" + name + "> activated."); 236 } 237 } 238 | Popular Tags |