KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > invoice > applications > BasicShell


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

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 JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 /**
36  * @author P. Dechamboux
37  */

38 public final class BasicShell {
39     private final static String JavaDoc PROMPT = "jormexsh";
40     private final static int MAXCMDSIZE = 20;
41     private static BasicShell singleton = new BasicShell();
42     private ShellExtent shellExtent;
43     protected InputStream JavaDoc in;
44     PMapper activeMapper = null;
45
46     // BasicShell SINGLETON INITIALIZATION
47

48     static BasicShell getInstance() {
49         return singleton;
50     }
51
52     private BasicShell() {
53     }
54
55     // PUBLIC METHODS FROM BasicShell
56

57     public String JavaDoc promptString(String JavaDoc 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 JavaDoc(cmd, 0, nextbyte - 1);
75                 else
76                     return "";
77             } else
78                 return null;
79         } catch (Exception JavaDoc e) {
80             return null;
81         }
82     }
83
84     public void shellLoop(String JavaDoc propfilename, ShellExtent se, InputStream JavaDoc in) throws Exception JavaDoc {
85         Properties JavaDoc prop = new Properties JavaDoc();
86         prop.load(new FileInputStream JavaDoc(propfilename));
87         JormInit.getInstance().initLogSystem(prop);
88         JormInit.getInstance().initResourceAdapter(prop);
89         prop = null;
90         this.in = in;
91         shellExtent = se;
92         String JavaDoc 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 JavaDoc msg) {
105         System.out.println("\tCommand failed: " + msg + ".\n");
106     }
107
108     public void printStringBlankApp(String JavaDoc 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     // PRIVATE METHODS FROM BasicShell
120

121     private String JavaDoc readCmd() {
122         String JavaDoc 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 JavaDoc cmd) throws Exception JavaDoc {
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     /**
162      * Lists the mappers that have been created and registered with JormInit.
163      */

164     private void listMapper() throws Exception JavaDoc {
165         Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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