Source code: com/opencms/core/CmsShell.java
1 /*
2 * File : $Source: /usr/local/cvs/opencms/src/com/opencms/core/Attic/CmsShell.java,v $
3 * Date : $Date: 2003/06/16 11:18:43 $
4 * Version: $Revision: 1.75.2.1 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 package com.opencms.core;
30
31 import com.opencms.boot.CmsBase;
32 import com.opencms.file.CmsObject;
33
34 import java.io.FileInputStream;
35 import java.io.InputStreamReader;
36 import java.io.LineNumberReader;
37 import java.io.StreamTokenizer;
38 import java.io.StringReader;
39 import java.lang.reflect.InvocationTargetException;
40 import java.lang.reflect.Method;
41 import java.util.Vector;
42
43 import source.org.apache.java.util.Configurations;
44 import source.org.apache.java.util.ExtendedProperties;
45
46 /**
47 * This class is a commad line interface to OpenCms which
48 * can be used for the initial setup and to test the system.<p>
49 *
50 * @author Andreas Schouten
51 * @author Anders Fugmann
52 *
53 * @version $Revision: 1.75.2.1 $ $Date: 2003/06/16 11:18:43 $
54 */
55 public class CmsShell implements I_CmsConstants {
56
57 /**
58 * The resource broker to get access to the cms.
59 */
60 protected CmsObject m_cms;
61
62 /**
63 * The open-cms.
64 */
65 private OpenCms m_openCms;
66
67 /** Comment Char. */
68 public static final String COMMENT_CHAR = "#";
69 private CmsShellCommands shellCommands;
70
71 /**
72 * If this member is set to true, all commands are echoed
73 */
74 static boolean m_echo = false;
75
76 /**
77 * If this member is set to true the memory-logging is enabled.
78 */
79 boolean m_logMemory = false;
80
81 /**
82 * if m_shortException is true then print only the short version of the Exception in the commandshell
83 */
84 static boolean m_shortException = false;
85
86 /**
87 * m_exitCalled indicates if the 'exit' command has been called
88 */
89 static boolean m_exitCalled = false;
90
91 /**
92 * the prompt for ecmaShell
93 */
94 String ecmaPrompt;
95
96 /**
97 * Creates a new CmsShell-Object.
98 */
99 public CmsShell() {
100 A_OpenCms.initVersion(this);
101 try {
102 String propsPath = CmsBase.getPropertiesPath(true);
103 System.out.println("%%% props: " + propsPath);
104 Configurations conf = new Configurations(new ExtendedProperties(propsPath));
105 m_openCms = new OpenCms(conf);
106 m_cms = new CmsObject();
107
108 m_logMemory = conf.getBoolean("log.memory", false);
109 //log in default user.
110 m_openCms.initUser(m_cms, null, null, C_USER_GUEST, C_GROUP_GUEST, C_PROJECT_ONLINE_ID, null);
111 }
112 catch(Exception exc) {
113 printException(exc);
114 }
115 }
116
117 /**
118 * Calls a command
119 *
120 * @param command The command to be called.
121 */
122 private void call(Vector command) {
123 if(m_echo) {
124 // all commands should be echoed to the shell
125 for(int i = 0;i < command.size();i++) {
126 System.out.print(command.elementAt(i) + " ");
127 }
128 System.out.println();
129 }
130 if((command == null) || (command.size() == 0)) {
131 return ;
132 }
133 String splittet[] = new String[command.size()];
134 String toCall;
135 command.copyInto(splittet);
136 toCall = splittet[0];
137 if(toCall == null) {
138 return ;
139 }
140 Class paramClasses[] = new Class[splittet.length - 1];
141 String params[] = new String[splittet.length - 1];
142 for(int z = 0;z < splittet.length - 1;z++) {
143 params[z] = splittet[z + 1];
144 paramClasses[z] = String.class;
145 }
146 try {
147 shellCommands.getClass().getMethod(toCall, paramClasses).invoke(shellCommands, params);
148 }
149 catch(InvocationTargetException ite) {
150 System.err.println("Got Exception while using reflection:");
151 ite.getTargetException().printStackTrace();
152 }
153 catch(NoSuchMethodException nsm) {
154 System.out.println("The requested command was not found.\n-----------------------------------------------");
155 shellCommands.printHelpText();
156 }
157 catch(Exception exc) {
158 System.err.println("Got Nullpointer Exception while using reflection:");
159 printException(exc);
160 }
161 }
162
163 /**
164 * The commandlineinterface.
165 */
166 public void commands(FileInputStream fis) {
167 try {
168 this.shellCommands = new CmsShellCommands(m_openCms, m_cms);
169 LineNumberReader lnr = new LineNumberReader(new InputStreamReader(fis));
170 while(!m_exitCalled) { // ever
171 printPrompt();
172 StringReader reader = new StringReader(lnr.readLine());
173 StreamTokenizer st = new StreamTokenizer(reader);
174 st.eolIsSignificant(true);
175
176 //put all tokens into a vector.
177 Vector args = new Vector();
178 while(st.nextToken() != StreamTokenizer.TT_EOF) {
179 if(st.ttype == StreamTokenizer.TT_NUMBER) {
180 args.addElement(new Double(st.nval).intValue() + "");
181 } else {
182 args.addElement(st.sval);
183 }
184 }
185 reader.close();
186
187 //exec the command
188 call(args);
189 }
190 }
191 catch(Exception exc) {
192 printException(exc);
193 }
194 }
195
196 /**
197 * The ecmaScript welcome text
198 */
199 public void printEcmaHelpText(){
200 System.out.println("");
201 System.out.println("help(); Gives a list of available commands with signature");
202 System.out.println("help(\"<command>\" Shows signature of command");
203 System.out.println("exit(); Quit the Shell");
204 System.out.println("");
205 }
206
207 /**
208 * The ecmaScript help-command
209 */
210 public void cmsHelp(Method m, String par) {
211 if(!m.getName().equals(par)){
212 System.out.print(par+"."+m.getName());
213
214 Class[] parameterTypes=m.getParameterTypes();
215
216 System.out.print("(");
217 for(int k=0;k<parameterTypes.length;k++){
218 if(k>0)System.out.print(", ");
219 System.out.print(""+ parameterTypes[k].getName());
220 }
221
222 String returnString=m.getReturnType().toString();
223 if(!returnString.equals("void") && !returnString.equals("int")){
224 System.out.println("); returns: "+returnString);
225 }else{
226 System.out.println(");");
227 }
228 }
229 }
230
231 /**
232 * Prints a exception with the stacktrace.
233 *
234 * @param exc The exception to print.
235 */
236 protected static void printException(Exception exc) {
237 if(CmsShell.m_shortException) {
238 String exceptionText;
239
240 if(exc instanceof CmsException) {
241 exceptionText = ((CmsException)exc).getTypeText(); // this is a cms-exception: print a very short exeption-text
242 } else {
243 exceptionText = exc.getMessage(); // only return the exception message
244 }
245 if((exceptionText == null) || (exceptionText.length() == 0)) {
246 // the exception-text was empty, return the class-name of the exeption
247 exceptionText = exc.getClass().getName();
248 }
249 System.out.println(exceptionText);
250 }
251 else {
252 exc.printStackTrace();
253 }
254 }
255
256 /**
257 * Prints the full name and signature of a method.<br>
258 * Called by help-methods.
259 * Creation date: (09/28/00)
260 * @author Jan Krag
261 * @param param java.lang.reflect.Method
262 */
263 protected static void printMethod(Method method) {
264 System.out.print(" " + method.getName() + " (");
265 Class[] params = method.getParameterTypes();
266 for(int i = 0;i < params.length;i++) {
267 String par = params[i].getName();
268 par = par.substring(par.lastIndexOf(".") + 1);
269 if(i == 0) {
270 System.out.print(par);
271 }
272 else {
273 System.out.print(", " + par);
274 }
275 }
276 System.out.println(")");
277 }
278
279 /**
280 * Prints the current prompt.
281 * Creation date: (10/03/00 %r)
282 * @author Jan Krag
283 */
284 private void printPrompt() {
285 System.out.print("{" + m_cms.getRequestContext().currentUser().getName() + "@"
286 + m_cms.getRequestContext().currentProject().getName() + "}");
287
288 // print out memory-informations, if needed
289 if(m_logMemory) {
290 long total = Runtime.getRuntime().totalMemory() / 1024;
291 long free = Runtime.getRuntime().freeMemory() / 1024;
292 System.out.print(("[" + total + "/" + free + "/" + (total - free) + "]"));
293 }
294 System.out.print("> ");
295 }
296 }