KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > system > launch > CommandReader


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: CommandReader.java 1:04:18 PM alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.system.launch;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28
29 import javax.naming.NamingException JavaDoc;
30
31 import org.apache.commons.io.FileUtils;
32
33 import org.objectweb.petals.kernel.admin.service.PetalsAdminServiceMBean;
34 import org.objectweb.petals.util.JNDIUtil;
35 import org.objectweb.petals.util.SystemUtil;
36
37 /**
38  * Used to read command from the console
39  *
40  * @author alouis - eBMWebsourcing
41  */

42 public class CommandReader {
43
44     protected static int STOP_CODE = 0;
45
46     protected static int ERROR_CODE = -1;
47
48     protected static int OK_CODE = 1;
49
50     protected File JavaDoc currentPath;
51
52     protected File JavaDoc installPath;
53
54     protected File JavaDoc installedPath;
55
56     protected File JavaDoc uninstalledPath;
57
58     protected PetalsAdminServiceMBean petalsService;
59     
60     public CommandReader(PetalsAdminServiceMBean petalsService) {
61         currentPath = new File JavaDoc(".");
62         File JavaDoc petalsPath = SystemUtil.getPetalsInstallDirectory();
63         installPath = new File JavaDoc(petalsPath, "install");
64         installedPath = new File JavaDoc(petalsPath, "installed");
65         uninstalledPath = new File JavaDoc(petalsPath, "uninstalled");
66         this.petalsService = petalsService;
67     }
68
69     /**
70      * Read command line. This method ends with an "exit" command line
71      *
72      */

73     public void read() {
74         System.out.println("Petals prompt. Tape 'help' for help.");
75         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
76
77         int code=1;
78
79         while (code>0) {
80             try {
81                 System.out.println();
82                 System.out.print("> ");
83                 code = processCommandLine(br.readLine());
84             } catch (IOException JavaDoc e) {
85                 e.printStackTrace();
86             }
87         }
88         System.out.println("exiting Petals...");
89         System.exit(code);
90     }
91
92     /**
93      * Process the command, and return a code if "exit" command, return -1
94      *
95      * @param command
96      * @return
97      */

98     protected int processCommandLine(String JavaDoc command) {
99         if (command != null) {
100             if ("help".equalsIgnoreCase(command)
101                 || "h".equalsIgnoreCase(command)) {
102                 return help();
103             } else if (command.toLowerCase().startsWith("hotdeploy")) {
104                 return hotdeploy(command.substring(9, command.length()).trim());
105             } else if (command.toLowerCase().startsWith("hd")) {
106                 return hotdeploy(command.substring(2, command.length()).trim());
107             } else if (command.toLowerCase().startsWith("hotundeploy")) {
108                 return hotundeploy(command.substring(11, command.length()).trim());
109             } else if (command.toLowerCase().startsWith("hu")) {
110                 return hotundeploy(command.substring(2, command.length()).trim());
111             } else if (command.toLowerCase().startsWith("setpath")) {
112                 return setpath(command.substring(7, command.length()).trim());
113             } else if (command.toLowerCase().startsWith("sp")) {
114                 return setpath(command.substring(2, command.length()).trim());
115             } else if ("path".equalsIgnoreCase(command)
116                 || "p".equalsIgnoreCase(command)) {
117                 return path();
118             } else if ("jndi".equalsIgnoreCase(command)
119                 || "d".equalsIgnoreCase(command)) {
120                 return jndi();
121             } else if ("stop".equalsIgnoreCase(command)
122                 || "q".equalsIgnoreCase(command)) {
123                 return stop();
124             } else if ("exit".equalsIgnoreCase(command)
125                     || "x".equalsIgnoreCase(command)) {
126                     return shutdown();
127             } else {
128                 System.out.println("Unknown command");
129             }
130         }
131         return OK_CODE;
132     }
133
134     /**
135      * Help
136      *
137      * @return 1
138      */

139     protected int help() {
140         System.out.println("commands:");
141         System.out.println("---------");
142         System.out
143             .println(" - hotdeploy [ZIP filePath] (or hd): install and start a component, a service assembly or a share library");
144         System.out
145             .println(" - hotundeploy [ZIP fileName] (or hu): shutdown and uninstall a component, a service assembly or a share library");
146         System.out.println(" - path (or p) : display current file system path");
147         System.out
148             .println(" - setpath [Path] (or sp): change current file system path");
149         System.out.println(" - jndi (or d) : display the naming directory");
150         System.out.println();
151         System.out.println(" - help (or h) : this help");
152         System.out.println(" - stop (or q) : stop Petals");
153         System.out.println(" - shutdown (or x) : shutdown Petals (remove from the Petals network");
154         return OK_CODE;
155     }
156
157     /**
158      * show current path
159      *
160      * @return 1
161      */

162     protected int path() {
163         System.out.println("current path is :");
164         System.out.println(currentPath.getAbsolutePath());
165         return OK_CODE;
166     }
167
168     /**
169      * set current path
170      *
171      * @return 1
172      */

173     protected int setpath(String JavaDoc newPath) {
174         currentPath = new File JavaDoc(newPath);
175         System.out.println("new current path is :");
176         System.out.println(currentPath.getAbsolutePath());
177         return OK_CODE;
178     }
179
180     /**
181      * hot deploy
182      *
183      * @return 1
184      */

185     protected int hotdeploy(String JavaDoc zip) {
186         File JavaDoc source = new File JavaDoc(zip);
187         if (!source.exists()) {
188             source = new File JavaDoc(currentPath, zip);
189         }
190
191         if (!source.exists()) {
192             System.out.println("File not found : " + source.getAbsolutePath());
193         } else {
194             try {
195                 FileUtils.copyFileToDirectory(source, installPath);
196                 Thread.sleep(1000);
197             } catch (IOException JavaDoc e) {
198                 System.out
199                     .println("File found, but can not copy and deploy file");
200                 e.printStackTrace();
201             } catch (InterruptedException JavaDoc e) {
202                 e.printStackTrace();
203             }
204         }
205         return OK_CODE;
206     }
207
208     /**
209      * hot deploy
210      *
211      * @return 1
212      */

213     protected int hotundeploy(String JavaDoc zip) {
214         File JavaDoc source = new File JavaDoc(installedPath, zip);
215
216         if (!source.exists()) {
217             System.out.println("File to undeploy not found : "
218                 + source.getAbsolutePath());
219         } else {
220             File JavaDoc uninstalledFile = new File JavaDoc(uninstalledPath, zip);
221             source.renameTo(uninstalledFile);
222         }
223         return OK_CODE;
224     }
225
226     /**
227      * display the naming directory
228      *
229      * @return 1
230      */

231     protected int jndi() {
232         try {
233             System.out.println(JNDIUtil.browseJNDI(null));
234         } catch (NamingException JavaDoc e) {
235             e.printStackTrace();
236         }
237         return OK_CODE;
238     }
239     
240     /**
241      * Stop the container, nothing to perform
242      * @return
243      */

244     protected int stop(){
245         System.out.println("quit Petals... (stop the server)");
246         return STOP_CODE;
247     }
248     
249     /**
250      * Shutdown the container, remove it from
251      * the network
252      * @return
253      */

254     protected int shutdown(){
255         try {
256             System.out.println("exit Petals... (shutdown the server)");
257             petalsService.shutdownContainer();
258             return STOP_CODE;
259         } catch (Exception JavaDoc e) {
260             e.printStackTrace();
261             return ERROR_CODE;
262         }
263     }
264
265 }
266
Popular Tags