You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2012/02/09 10:11:08 UTC
svn commit: r1242247 - in
/openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command:
CatCommand.java LsCommand.java PartCommand.java PathCommand.java
Author: rmannibucau
Date: Thu Feb 9 09:11:07 2012
New Revision: 1242247
URL: http://svn.apache.org/viewvc?rev=1242247&view=rev
Log:
adding part, cat and ls commands to common cli module
Added:
openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/CatCommand.java
openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/LsCommand.java
openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PartCommand.java
openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PathCommand.java
Added: openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/CatCommand.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/CatCommand.java?rev=1242247&view=auto
==============================================================================
--- openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/CatCommand.java (added)
+++ openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/CatCommand.java Thu Feb 9 09:11:07 2012
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.server.cli.command;
+
+import org.apache.openejb.loader.IO;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+@Command(name = "cat", usage = "cat <path>", description = "cat a file (in tomee directories only)")
+public class CatCommand extends PathCommand {
+ @Override
+ public void execute(final String cmd) {
+ final File file;
+ try {
+ file = resolve("cat", cmd);
+ } catch (IllegalArgumentException iae) {
+ streamManager.writeErr(iae.getMessage());
+ return;
+ }
+
+ if (file.isDirectory() && file.exists()) {
+ streamManager.writeOut("file " + file.getPath() + " is a directory, please specify a regular file");
+ } else if (file.exists()) {
+ try {
+ cat(file);
+ } catch (IOException e) {
+ streamManager.writeErr(e);
+ }
+ } else {
+ streamManager.writeOut("file " + file.getPath() + " doesn't exist");
+ }
+ }
+
+ private void cat(final File file) throws IOException {
+ final BufferedReader br = new BufferedReader(new FileReader(file));
+ String line;
+ int lineNumber = 0;
+ try {
+ while ((line = br.readLine()) != null) {
+ lineNumber++;
+ streamManager.writeOut(String.format("%3d. %s", lineNumber, line));
+ }
+ } finally {
+ IO.close(br);
+ }
+ }
+}
Added: openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/LsCommand.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/LsCommand.java?rev=1242247&view=auto
==============================================================================
--- openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/LsCommand.java (added)
+++ openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/LsCommand.java Thu Feb 9 09:11:07 2012
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.server.cli.command;
+
+import java.io.File;
+
+@Command(name = "ls", usage = "ls [<path>]", description = "list files (in tomee directories only)")
+public class LsCommand extends PathCommand {
+ @Override
+ public void execute(final String cmd) {
+ final File file;
+ try {
+ file = resolve("ls", cmd);
+ } catch (IllegalArgumentException iae) {
+ streamManager.writeErr(iae.getMessage());
+ return;
+ }
+
+ if (file.isDirectory() && file.exists()) {
+ list(file.getAbsolutePath(), file);
+ } else if (file.exists()) {
+ streamManager.writeOut("file " + file.getPath() + " exists");
+ } else {
+ streamManager.writeOut("file " + file.getPath() + " doesn't exist");
+ }
+ }
+
+ private void list(final String base, final File dir) {
+ String removed = base;
+ if (!base.endsWith("/")) {
+ removed = removed + "/";
+ }
+ for (File file : dir.listFiles()) { // not recursive otherwise it starts to be complicated
+ streamManager.writeOut(type(file) + " " + file.getAbsolutePath().replace(removed, ""));
+ }
+ }
+
+ private static String type(final File file) {
+ if (file.isDirectory()) {
+ return "D";
+ }
+ return "F";
+ }
+}
Added: openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PartCommand.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PartCommand.java?rev=1242247&view=auto
==============================================================================
--- openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PartCommand.java (added)
+++ openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PartCommand.java Thu Feb 9 09:11:07 2012
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.server.cli.command;
+
+import org.apache.openejb.loader.IO;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+@Command(name = "part", usage = "part <first line>-<last line> <path>", description = "print the specified line range of a file (in tomee directories only)")
+public class PartCommand extends PathCommand {
+ private static final Pattern PATTERN = Pattern.compile("part ([0-9]*)-([0-9]*) (.*)");
+ @Override
+ public void execute(final String cmd) {
+ final Matcher matcher = PATTERN.matcher(cmd);
+ if (!matcher.matches()) {
+ streamManager.writeErr("you have to specify a numbe of line, please see help for more information");
+ return;
+ }
+
+ int firstLine;
+ int secondLine;
+ try {
+ firstLine = getInt(matcher.group(1));
+ secondLine = getInt(matcher.group(2));
+ } catch (NumberFormatException nfe) {
+ streamManager.writeErr("line number was not parsable");
+ return;
+ }
+
+ final String path = matcher.group(3);
+ final File file;
+ try {
+ file = resolve(null, path);
+ } catch (IllegalArgumentException iae) {
+ streamManager.writeErr(iae.getMessage());
+ return;
+ }
+
+ if (file.isDirectory() && file.exists()) {
+ streamManager.writeOut("file " + file.getPath() + " is a directory, please specify a regular file");
+ } else if (file.exists()) {
+ try {
+ part(file, firstLine, secondLine);
+ } catch (IOException e) {
+ streamManager.writeErr(e);
+ }
+ } else {
+ streamManager.writeOut("file " + file.getPath() + " doesn't exist");
+ }
+ }
+
+ private int getInt(String group) {
+ try {
+ return Integer.parseInt(group);
+ } catch (NumberFormatException nfe) {
+ streamManager.writeErr("line number should be an integer");
+ throw nfe;
+ }
+ }
+
+ private void part(final File file, int firstLine, int secondLine) throws IOException {
+ BufferedReader br = new BufferedReader(new FileReader(file));
+ String line;
+ int totalLineNumber = 0;
+ try {
+ while (br.readLine() != null) {
+ totalLineNumber++;
+ }
+ } finally {
+ IO.close(br);
+ }
+
+ br = new BufferedReader(new FileReader(file));
+ try {
+ int firstLineToPrint = Math.max(1, firstLine);
+ int lastLine = Math.min(totalLineNumber, secondLine);
+ totalLineNumber = 0;
+ while ((line = br.readLine()) != null) {
+ totalLineNumber++;
+ if (totalLineNumber >= firstLineToPrint && totalLineNumber <= lastLine) {
+ streamManager.writeOut(String.format("%3d. %s", totalLineNumber, line));
+ }
+ if (totalLineNumber > lastLine) {
+ break;
+ }
+ }
+ } finally {
+ IO.close(br);
+ }
+ }
+}
Added: openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PathCommand.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PathCommand.java?rev=1242247&view=auto
==============================================================================
--- openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PathCommand.java (added)
+++ openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/PathCommand.java Thu Feb 9 09:11:07 2012
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.server.cli.command;
+
+import org.apache.openejb.loader.SystemInstance;
+
+import java.io.File;
+
+public abstract class PathCommand extends AbstractCommand {
+ public static final String HOME = "$home";
+ public static final String BASE = "$base";
+
+ protected File resolve(final String prefix, final String cmd) {
+ if (cmd == null || (prefix != null && cmd.trim().equals(prefix)) || cmd.trim().isEmpty()) {
+ return SystemInstance.get().getBase().getDirectory();
+ }
+ int idx = 0;
+ if (prefix != null) {
+ idx = prefix.length() + 1;
+ }
+ String path = cmd.substring(idx);
+ File workingFile = new File(path);
+ if ((!path.startsWith(HOME) && !path.startsWith(BASE) && workingFile.getPath().equals(workingFile.getAbsolutePath())) || path.startsWith("..")) {
+ throw new IllegalArgumentException("path should start with " + BASE + " or " + HOME + " or be relative");
+ }
+
+ if (path.startsWith(HOME)) {
+ return new File(path.replace(HOME, SystemInstance.get().getHome().getDirectory().getAbsolutePath()));
+ } else if(path.startsWith(BASE)) {
+ return new File(path.replace(BASE, SystemInstance.get().getBase().getDirectory().getAbsolutePath()));
+ }
+ return new File(SystemInstance.get().getBase().getDirectory().getAbsolutePath(), path);
+ }
+}