001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.example;
018:
019: import org.apache.commons.vfs.FileContent;
020: import org.apache.commons.vfs.FileObject;
021: import org.apache.commons.vfs.FileSystemException;
022: import org.apache.commons.vfs.FileSystemManager;
023: import org.apache.commons.vfs.FileType;
024: import org.apache.commons.vfs.FileUtil;
025: import org.apache.commons.vfs.Selectors;
026: import org.apache.commons.vfs.VFS;
027:
028: import java.io.BufferedReader;
029: import java.io.IOException;
030: import java.io.InputStreamReader;
031: import java.text.DateFormat;
032: import java.util.ArrayList;
033: import java.util.Date;
034: import java.util.StringTokenizer;
035:
036: /**
037: * A simple command-line shell for performing file operations.
038: *
039: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
040: * @author Gary D. Gregory
041: * @version $Id:Shell.java 232419 2005-08-13 07:23:40 +0200 (Sa, 13 Aug 2005) imario $
042: */
043: public class Shell {
044: private static final String CVS_ID = "$Id:Shell.java 232419 2005-08-13 07:23:40 +0200 (Sa, 13 Aug 2005) imario $";
045: private final FileSystemManager mgr;
046: private FileObject cwd;
047: private BufferedReader reader;
048:
049: public static void main(final String[] args) {
050: try {
051: (new Shell()).go();
052: } catch (Exception e) {
053: e.printStackTrace();
054: System.exit(1);
055: }
056: System.exit(0);
057: }
058:
059: private Shell() throws FileSystemException {
060: mgr = VFS.getManager();
061: cwd = mgr.resolveFile(System.getProperty("user.dir"));
062: reader = new BufferedReader(new InputStreamReader(System.in));
063: }
064:
065: private void go() throws Exception {
066: System.out.println("VFS Shell [" + CVS_ID + "]");
067: while (true) {
068: final String[] cmd = nextCommand();
069: if (cmd == null) {
070: return;
071: }
072: if (cmd.length == 0) {
073: continue;
074: }
075: final String cmdName = cmd[0];
076: if (cmdName.equalsIgnoreCase("exit")
077: || cmdName.equalsIgnoreCase("quit")) {
078: return;
079: }
080: try {
081: handleCommand(cmd);
082: } catch (final Exception e) {
083: System.err.println("Command failed:");
084: e.printStackTrace(System.err);
085: }
086: }
087: }
088:
089: /**
090: * Handles a command.
091: */
092: private void handleCommand(final String[] cmd) throws Exception {
093: final String cmdName = cmd[0];
094: if (cmdName.equalsIgnoreCase("cat")) {
095: cat(cmd);
096: } else if (cmdName.equalsIgnoreCase("cd")) {
097: cd(cmd);
098: } else if (cmdName.equalsIgnoreCase("cp")) {
099: cp(cmd);
100: } else if (cmdName.equalsIgnoreCase("help")) {
101: help();
102: } else if (cmdName.equalsIgnoreCase("ls")) {
103: ls(cmd);
104: } else if (cmdName.equalsIgnoreCase("pwd")) {
105: pwd();
106: } else if (cmdName.equalsIgnoreCase("rm")) {
107: rm(cmd);
108: } else if (cmdName.equalsIgnoreCase("touch")) {
109: touch(cmd);
110: } else {
111: System.err.println("Unknown command \"" + cmdName + "\".");
112: }
113: }
114:
115: /**
116: * Does a 'help' command.
117: */
118: private void help() {
119: System.out.println("Commands:");
120: System.out
121: .println("cat <file> Displays the contents of a file.");
122: System.out
123: .println("cd [folder] Changes current folder.");
124: System.out
125: .println("cp <src> <dest> Copies a file or folder.");
126: System.out.println("help Shows this message.");
127: System.out
128: .println("ls [-R] [path] Lists contents of a file or folder.");
129: System.out
130: .println("pwd Displays current folder.");
131: System.out
132: .println("rm <path> Deletes a file or folder.");
133: System.out
134: .println("touch <path> Sets the last-modified time of a file.");
135: System.out.println("exit Exits this program.");
136: System.out.println("quit Exits this program.");
137: }
138:
139: /**
140: * Does an 'rm' command.
141: */
142: private void rm(final String[] cmd) throws Exception {
143: if (cmd.length < 2) {
144: throw new Exception("USAGE: rm <path>");
145: }
146:
147: final FileObject file = mgr.resolveFile(cwd, cmd[1]);
148: file.delete(Selectors.SELECT_SELF);
149: }
150:
151: /**
152: * Does a 'cp' command.
153: */
154: private void cp(final String[] cmd) throws Exception {
155: if (cmd.length < 3) {
156: throw new Exception("USAGE: cp <src> <dest>");
157: }
158:
159: final FileObject src = mgr.resolveFile(cwd, cmd[1]);
160: FileObject dest = mgr.resolveFile(cwd, cmd[2]);
161: if (dest.exists() && dest.getType() == FileType.FOLDER) {
162: dest = dest.resolveFile(src.getName().getBaseName());
163: }
164:
165: dest.copyFrom(src, Selectors.SELECT_ALL);
166: }
167:
168: /**
169: * Does a 'cat' command.
170: */
171: private void cat(final String[] cmd) throws Exception {
172: if (cmd.length < 2) {
173: throw new Exception("USAGE: cat <path>");
174: }
175:
176: // Locate the file
177: final FileObject file = mgr.resolveFile(cwd, cmd[1]);
178:
179: // Dump the contents to System.out
180: FileUtil.writeContent(file, System.out);
181: System.out.println();
182: }
183:
184: /**
185: * Does a 'pwd' command.
186: */
187: private void pwd() {
188: System.out.println("Current folder is " + cwd.getName());
189: }
190:
191: /**
192: * Does a 'cd' command.
193: * If the taget directory does not exist, a message is printed to <code>System.err</code>.
194: */
195: private void cd(final String[] cmd) throws Exception {
196: final String path;
197: if (cmd.length > 1) {
198: path = cmd[1];
199: } else {
200: path = System.getProperty("user.home");
201: }
202:
203: // Locate and validate the folder
204: FileObject tmp = mgr.resolveFile(cwd, path);
205: if (tmp.exists()) {
206: cwd = tmp;
207: } else {
208: System.out.println("Folder does not exist: "
209: + tmp.getName());
210: }
211: System.out.println("Current folder is " + cwd.getName());
212: }
213:
214: /**
215: * Does an 'ls' command.
216: */
217: private void ls(final String[] cmd) throws FileSystemException {
218: int pos = 1;
219: final boolean recursive;
220: if (cmd.length > pos && cmd[pos].equals("-R")) {
221: recursive = true;
222: pos++;
223: } else {
224: recursive = false;
225: }
226:
227: final FileObject file;
228: if (cmd.length > pos) {
229: file = mgr.resolveFile(cwd, cmd[pos]);
230: } else {
231: file = cwd;
232: }
233:
234: if (file.getType() == FileType.FOLDER) {
235: // List the contents
236: System.out.println("Contents of " + file.getName());
237: listChildren(file, recursive, "");
238: } else {
239: // Stat the file
240: System.out.println(file.getName());
241: final FileContent content = file.getContent();
242: System.out
243: .println("Size: " + content.getSize() + " bytes.");
244: final DateFormat dateFormat = DateFormat
245: .getDateTimeInstance(DateFormat.MEDIUM,
246: DateFormat.MEDIUM);
247: final String lastMod = dateFormat.format(new Date(content
248: .getLastModifiedTime()));
249: System.out.println("Last modified: " + lastMod);
250: }
251: }
252:
253: /**
254: * Does a 'touch' command.
255: */
256: private void touch(final String[] cmd) throws Exception {
257: if (cmd.length < 2) {
258: throw new Exception("USAGE: touch <path>");
259: }
260: final FileObject file = mgr.resolveFile(cwd, cmd[1]);
261: if (!file.exists()) {
262: file.createFile();
263: }
264: file.getContent().setLastModifiedTime(
265: System.currentTimeMillis());
266: }
267:
268: /**
269: * Lists the children of a folder.
270: */
271: private void listChildren(final FileObject dir,
272: final boolean recursive, final String prefix)
273: throws FileSystemException {
274: final FileObject[] children = dir.getChildren();
275: for (int i = 0; i < children.length; i++) {
276: final FileObject child = children[i];
277: System.out.print(prefix);
278: System.out.print(child.getName().getBaseName());
279: if (child.getType() == FileType.FOLDER) {
280: System.out.println("/");
281: if (recursive) {
282: listChildren(child, recursive, prefix + " ");
283: }
284: } else {
285: System.out.println();
286: }
287: }
288: }
289:
290: /**
291: * Returns the next command, split into tokens.
292: */
293: private String[] nextCommand() throws IOException {
294: System.out.print("> ");
295: final String line = reader.readLine();
296: if (line == null) {
297: return null;
298: }
299: final ArrayList cmd = new ArrayList();
300: final StringTokenizer tokens = new StringTokenizer(line);
301: while (tokens.hasMoreTokens()) {
302: cmd.add(tokens.nextToken());
303: }
304: return (String[]) cmd.toArray(new String[cmd.size()]);
305: }
306: }
|