You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by io...@apache.org on 2010/12/09 18:48:17 UTC
svn commit: r1044051 - in /karaf/trunk/shell/commands/src/main:
java/org/apache/karaf/shell/commands/
resources/META-INF/services/org/apache/karaf/shell/
resources/OSGI-INF/blueprint/
Author: iocanel
Date: Thu Dec 9 17:48:16 2010
New Revision: 1044051
URL: http://svn.apache.org/viewvc?rev=1044051&view=rev
Log:
[KARAF-316] Added simple implementations of head and tail commands.
Added:
karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java
karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java
Modified:
karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
Added: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java?rev=1044051&view=auto
==============================================================================
--- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java (added)
+++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java Thu Dec 9 17:48:16 2010
@@ -0,0 +1,101 @@
+/*
+ * 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.karaf.shell.commands;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.List;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.karaf.shell.console.AbstractAction;
+
+@Command(scope = "shell", name = "head", description = "Displays the first lines of a file.")
+public class HeadAction extends AbstractAction {
+
+ private static final int DEFAULT_NUMBER_OF_LINES = 10;
+
+ @Option(name = "-n", aliases = {}, description = "The number of lines to display, starting at 1.", required = false, multiValued = false)
+ private int numberOfLines;
+
+ @Argument(index = 0, name = "paths or urls", description = "A list of file paths or urls to display separated by whitespaces.", required = false, multiValued = true)
+ private List<String> paths;
+
+ protected Object doExecute() throws Exception {
+ //If no paths provided assume standar input
+ if (paths == null || paths.size() == 0) {
+ if (log.isDebugEnabled()) {
+ log.debug("Heading STDIN");
+ }
+
+ head(new BufferedReader(new InputStreamReader(System.in)));
+ } else {
+ for (String filename : paths) {
+ BufferedReader reader;
+
+ // First try a URL
+ try {
+ URL url = new URL(filename);
+ if (log.isDebugEnabled()) {
+ log.debug("Heading URL: " + url);
+ }
+ reader = new BufferedReader(new InputStreamReader(url.openStream()));
+ }
+ catch (MalformedURLException ignore) {
+ // They try a file
+ File file = new File(filename);
+ if (log.isDebugEnabled()) {
+ log.debug("Heading file: " + file);
+ }
+ reader = new BufferedReader(new FileReader(file));
+ }
+
+ try {
+ head(reader);
+ }
+ finally {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ // Ignore
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ private void head(final BufferedReader reader) throws IOException {
+ String line;
+ int lineno = 1;
+
+ if (numberOfLines < 1) {
+ numberOfLines = DEFAULT_NUMBER_OF_LINES;
+ }
+
+ while ((line = reader.readLine()) != null && lineno <= numberOfLines) {
+ System.out.println(line);
+ lineno++;
+ }
+ }
+}
Added: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java?rev=1044051&view=auto
==============================================================================
--- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java (added)
+++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java Thu Dec 9 17:48:16 2010
@@ -0,0 +1,108 @@
+/*
+ * 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.karaf.shell.commands;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.karaf.shell.console.AbstractAction;
+
+@Command(scope = "shell", name = "tail", description = "Displays the last lines of a file")
+public class TailAction extends AbstractAction {
+
+ private static final int DEFAULT_NUMBER_OF_LINES = 10;
+
+ @Option(name = "-n", aliases = {}, description = "The number of lines to display, starting at 1.", required = false, multiValued = false)
+ private int numberOfLines;
+
+ @Argument(index = 0, name = "paths or urls", description = "A list of file paths or urls to display separated by whitespaces.", required = false, multiValued = true)
+ private List<String> paths;
+
+ protected Object doExecute() throws Exception {
+ //If no paths provided assume standar input
+ if (paths == null || paths.size() == 0) {
+ if (log.isDebugEnabled()) {
+ log.debug("Tailing STDIN");
+ }
+ tail(new BufferedReader(new InputStreamReader(System.in)));
+ } else {
+ for (String filename : paths) {
+ BufferedReader reader;
+
+ // First try a URL
+ try {
+ URL url = new URL(filename);
+ if (log.isDebugEnabled()) {
+ log.debug("Tailing URL: " + url);
+ }
+ reader = new BufferedReader(new InputStreamReader(url.openStream()));
+ }
+ catch (MalformedURLException ignore) {
+ // They try a file
+ File file = new File(filename);
+ if (log.isDebugEnabled()) {
+ log.debug("Tailing file: " + file);
+ }
+ reader = new BufferedReader(new FileReader(file));
+ }
+
+ try {
+ tail(reader);
+ }
+ finally {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ // Ignore
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private void tail(final BufferedReader reader) throws IOException {
+ List<String> lines = new LinkedList<String>();
+ String line;
+ int lineno = 1;
+
+ while ((line = reader.readLine()) != null) {
+ lines.add(line);
+ }
+
+ if (numberOfLines < 1) {
+ numberOfLines = DEFAULT_NUMBER_OF_LINES;
+ }
+
+ int startLine = lines.size() < numberOfLines ? 0 : lines.size() - numberOfLines;
+
+ for (lineno = startLine; lineno < lines.size(); lineno++) {
+ System.out.println(lines.get(lineno));
+ }
+ }
+}
Modified: karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands?rev=1044051&r1=1044050&r2=1044051&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands (original)
+++ karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands Thu Dec 9 17:48:16 2010
@@ -21,6 +21,7 @@ org.apache.karaf.shell.commands.EchoActi
org.apache.karaf.shell.commands.ExecuteAction
org.apache.karaf.shell.commands.GrepAction
org.apache.karaf.shell.commands.HistoryAction
+org.apache.karaf.shell.commands.HeadAction
org.apache.karaf.shell.commands.IfAction
org.apache.karaf.shell.commands.JavaAction
org.apache.karaf.shell.commands.LogoutAction
@@ -30,3 +31,4 @@ org.apache.karaf.shell.commands.PrintfAc
org.apache.karaf.shell.commands.SleepAction
org.apache.karaf.shell.commands.SortAction
org.apache.karaf.shell.commands.TacAction
+org.apache.karaf.shell.commands.TailAction
Modified: karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=1044051&r1=1044050&r2=1044051&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (original)
+++ karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml Thu Dec 9 17:48:16 2010
@@ -41,6 +41,9 @@
<command name="shell/history">
<action class="org.apache.karaf.shell.commands.HistoryAction"/>
</command>
+ <command name="shell/head">
+ <action class="org.apache.karaf.shell.commands.HeadAction"/>
+ </command>
<command name="shell/if">
<action class="org.apache.karaf.shell.commands.IfAction"/>
</command>
@@ -78,6 +81,9 @@
<command name="shell/tac">
<action class="org.apache.karaf.shell.commands.TacAction"/>
</command>
+ <command name="shell/tail">
+ <action class="org.apache.karaf.shell.commands.TailAction"/>
+ </command>
</command-bundle>
<bean class="org.osgi.util.tracker.BundleTracker" init-method="open"
Re: svn commit: r1044051 - in /karaf/trunk/shell/commands/src/main:
java/org/apache/karaf/shell/commands/ resources/META-INF/services/org/apache/karaf/shell/
resources/OSGI-INF/blueprint/
Posted by Ioannis Canellos <io...@gmail.com>.
OK I will reopen the issue.
On Fri, Dec 10, 2010 at 12:36 AM, Guillaume Nodet <gn...@gmail.com> wrote:
> I haven't yet tried the tail command, from looking at it, I think it
> will fail to behave correctly in a number of cases.
> The first one would be with a huge file, as keeping all the lines in
> memory will certainly be a problem.
> Also i'd love to see the -f option being implemented ...
>
> On Thu, Dec 9, 2010 at 18:48, <io...@apache.org> wrote:
> > Author: iocanel
> > Date: Thu Dec 9 17:48:16 2010
> > New Revision: 1044051
> >
> > URL: http://svn.apache.org/viewvc?rev=1044051&view=rev
> > Log:
> > [KARAF-316] Added simple implementations of head and tail commands.
> >
> > Added:
> >
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java
> >
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java
> > Modified:
> >
> karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
> >
> karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
> >
> > Added:
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java
> > URL:
> http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java?rev=1044051&view=auto
> >
> ==============================================================================
> > ---
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java
> (added)
> > +++
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java
> Thu Dec 9 17:48:16 2010
> > @@ -0,0 +1,101 @@
> > +/*
> > + * 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.karaf.shell.commands;
> > +
> > +import java.io.BufferedReader;
> > +import java.io.File;
> > +import java.io.FileReader;
> > +import java.io.IOException;
> > +import java.io.InputStreamReader;
> > +import java.net.MalformedURLException;
> > +import java.net.URL;
> > +import java.util.List;
> > +
> > +import org.apache.felix.gogo.commands.Argument;
> > +import org.apache.felix.gogo.commands.Command;
> > +import org.apache.felix.gogo.commands.Option;
> > +import org.apache.karaf.shell.console.AbstractAction;
> > +
> > +@Command(scope = "shell", name = "head", description = "Displays the
> first lines of a file.")
> > +public class HeadAction extends AbstractAction {
> > +
> > + private static final int DEFAULT_NUMBER_OF_LINES = 10;
> > +
> > + @Option(name = "-n", aliases = {}, description = "The number of
> lines to display, starting at 1.", required = false, multiValued = false)
> > + private int numberOfLines;
> > +
> > + @Argument(index = 0, name = "paths or urls", description = "A list
> of file paths or urls to display separated by whitespaces.", required =
> false, multiValued = true)
> > + private List<String> paths;
> > +
> > + protected Object doExecute() throws Exception {
> > + //If no paths provided assume standar input
> > + if (paths == null || paths.size() == 0) {
> > + if (log.isDebugEnabled()) {
> > + log.debug("Heading STDIN");
> > + }
> > +
> > + head(new BufferedReader(new InputStreamReader(System.in)));
> > + } else {
> > + for (String filename : paths) {
> > + BufferedReader reader;
> > +
> > + // First try a URL
> > + try {
> > + URL url = new URL(filename);
> > + if (log.isDebugEnabled()) {
> > + log.debug("Heading URL: " + url);
> > + }
> > + reader = new BufferedReader(new
> InputStreamReader(url.openStream()));
> > + }
> > + catch (MalformedURLException ignore) {
> > + // They try a file
> > + File file = new File(filename);
> > + if (log.isDebugEnabled()) {
> > + log.debug("Heading file: " + file);
> > + }
> > + reader = new BufferedReader(new FileReader(file));
> > + }
> > +
> > + try {
> > + head(reader);
> > + }
> > + finally {
> > + try {
> > + reader.close();
> > + } catch (IOException e) {
> > + // Ignore
> > + }
> > + }
> > + }
> > + }
> > + return null;
> > + }
> > +
> > + private void head(final BufferedReader reader) throws IOException {
> > + String line;
> > + int lineno = 1;
> > +
> > + if (numberOfLines < 1) {
> > + numberOfLines = DEFAULT_NUMBER_OF_LINES;
> > + }
> > +
> > + while ((line = reader.readLine()) != null && lineno <=
> numberOfLines) {
> > + System.out.println(line);
> > + lineno++;
> > + }
> > + }
> > +}
> >
> > Added:
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java
> > URL:
> http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java?rev=1044051&view=auto
> >
> ==============================================================================
> > ---
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java
> (added)
> > +++
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java
> Thu Dec 9 17:48:16 2010
> > @@ -0,0 +1,108 @@
> > +/*
> > + * 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.karaf.shell.commands;
> > +
> > +import java.io.BufferedReader;
> > +import java.io.File;
> > +import java.io.FileReader;
> > +import java.io.IOException;
> > +import java.io.InputStreamReader;
> > +import java.net.MalformedURLException;
> > +import java.net.URL;
> > +import java.util.LinkedList;
> > +import java.util.List;
> > +
> > +import org.apache.felix.gogo.commands.Argument;
> > +import org.apache.felix.gogo.commands.Command;
> > +import org.apache.felix.gogo.commands.Option;
> > +import org.apache.karaf.shell.console.AbstractAction;
> > +
> > +@Command(scope = "shell", name = "tail", description = "Displays the
> last lines of a file")
> > +public class TailAction extends AbstractAction {
> > +
> > + private static final int DEFAULT_NUMBER_OF_LINES = 10;
> > +
> > + @Option(name = "-n", aliases = {}, description = "The number of
> lines to display, starting at 1.", required = false, multiValued = false)
> > + private int numberOfLines;
> > +
> > + @Argument(index = 0, name = "paths or urls", description = "A list
> of file paths or urls to display separated by whitespaces.", required =
> false, multiValued = true)
> > + private List<String> paths;
> > +
> > + protected Object doExecute() throws Exception {
> > + //If no paths provided assume standar input
> > + if (paths == null || paths.size() == 0) {
> > + if (log.isDebugEnabled()) {
> > + log.debug("Tailing STDIN");
> > + }
> > + tail(new BufferedReader(new InputStreamReader(System.in)));
> > + } else {
> > + for (String filename : paths) {
> > + BufferedReader reader;
> > +
> > + // First try a URL
> > + try {
> > + URL url = new URL(filename);
> > + if (log.isDebugEnabled()) {
> > + log.debug("Tailing URL: " + url);
> > + }
> > + reader = new BufferedReader(new
> InputStreamReader(url.openStream()));
> > + }
> > + catch (MalformedURLException ignore) {
> > + // They try a file
> > + File file = new File(filename);
> > + if (log.isDebugEnabled()) {
> > + log.debug("Tailing file: " + file);
> > + }
> > + reader = new BufferedReader(new FileReader(file));
> > + }
> > +
> > + try {
> > + tail(reader);
> > + }
> > + finally {
> > + try {
> > + reader.close();
> > + } catch (IOException e) {
> > + // Ignore
> > + }
> > + }
> > + }
> > + }
> > +
> > + return null;
> > + }
> > +
> > + private void tail(final BufferedReader reader) throws IOException {
> > + List<String> lines = new LinkedList<String>();
> > + String line;
> > + int lineno = 1;
> > +
> > + while ((line = reader.readLine()) != null) {
> > + lines.add(line);
> > + }
> > +
> > + if (numberOfLines < 1) {
> > + numberOfLines = DEFAULT_NUMBER_OF_LINES;
> > + }
> > +
> > + int startLine = lines.size() < numberOfLines ? 0 : lines.size()
> - numberOfLines;
> > +
> > + for (lineno = startLine; lineno < lines.size(); lineno++) {
> > + System.out.println(lines.get(lineno));
> > + }
> > + }
> > +}
> >
> > Modified:
> karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
> > URL:
> http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands?rev=1044051&r1=1044050&r2=1044051&view=diff
> >
> ==============================================================================
> > ---
> karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
> (original)
> > +++
> karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
> Thu Dec 9 17:48:16 2010
> > @@ -21,6 +21,7 @@ org.apache.karaf.shell.commands.EchoActi
> > org.apache.karaf.shell.commands.ExecuteAction
> > org.apache.karaf.shell.commands.GrepAction
> > org.apache.karaf.shell.commands.HistoryAction
> > +org.apache.karaf.shell.commands.HeadAction
> > org.apache.karaf.shell.commands.IfAction
> > org.apache.karaf.shell.commands.JavaAction
> > org.apache.karaf.shell.commands.LogoutAction
> > @@ -30,3 +31,4 @@ org.apache.karaf.shell.commands.PrintfAc
> > org.apache.karaf.shell.commands.SleepAction
> > org.apache.karaf.shell.commands.SortAction
> > org.apache.karaf.shell.commands.TacAction
> > +org.apache.karaf.shell.commands.TailAction
> >
> > Modified:
> karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
> > URL:
> http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=1044051&r1=1044050&r2=1044051&view=diff
> >
> ==============================================================================
> > ---
> karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
> (original)
> > +++
> karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
> Thu Dec 9 17:48:16 2010
> > @@ -41,6 +41,9 @@
> > <command name="shell/history">
> > <action
> class="org.apache.karaf.shell.commands.HistoryAction"/>
> > </command>
> > + <command name="shell/head">
> > + <action class="org.apache.karaf.shell.commands.HeadAction"/>
> > + </command>
> > <command name="shell/if">
> > <action class="org.apache.karaf.shell.commands.IfAction"/>
> > </command>
> > @@ -78,6 +81,9 @@
> > <command name="shell/tac">
> > <action class="org.apache.karaf.shell.commands.TacAction"/>
> > </command>
> > + <command name="shell/tail">
> > + <action class="org.apache.karaf.shell.commands.TailAction"/>
> > + </command>
> > </command-bundle>
> >
> > <bean class="org.osgi.util.tracker.BundleTracker" init-method="open"
> >
> >
> >
>
>
>
> --
> Cheers,
> Guillaume Nodet
> ------------------------
> Blog: http://gnodet.blogspot.com/
> ------------------------
> Open Source SOA
> http://fusesource.com
>
--
*Ioannis Canellos*
http://iocanel.blogspot.com
Integration Engineer @ Upstream S.A. <http://www.upstreamsystems.com>
Re: svn commit: r1044051 - in /karaf/trunk/shell/commands/src/main:
java/org/apache/karaf/shell/commands/ resources/META-INF/services/org/apache/karaf/shell/
resources/OSGI-INF/blueprint/
Posted by Guillaume Nodet <gn...@gmail.com>.
I haven't yet tried the tail command, from looking at it, I think it
will fail to behave correctly in a number of cases.
The first one would be with a huge file, as keeping all the lines in
memory will certainly be a problem.
Also i'd love to see the -f option being implemented ...
On Thu, Dec 9, 2010 at 18:48, <io...@apache.org> wrote:
> Author: iocanel
> Date: Thu Dec 9 17:48:16 2010
> New Revision: 1044051
>
> URL: http://svn.apache.org/viewvc?rev=1044051&view=rev
> Log:
> [KARAF-316] Added simple implementations of head and tail commands.
>
> Added:
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java
> karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java
> Modified:
> karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
> karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
>
> Added: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java
> URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java?rev=1044051&view=auto
> ==============================================================================
> --- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java (added)
> +++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/HeadAction.java Thu Dec 9 17:48:16 2010
> @@ -0,0 +1,101 @@
> +/*
> + * 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.karaf.shell.commands;
> +
> +import java.io.BufferedReader;
> +import java.io.File;
> +import java.io.FileReader;
> +import java.io.IOException;
> +import java.io.InputStreamReader;
> +import java.net.MalformedURLException;
> +import java.net.URL;
> +import java.util.List;
> +
> +import org.apache.felix.gogo.commands.Argument;
> +import org.apache.felix.gogo.commands.Command;
> +import org.apache.felix.gogo.commands.Option;
> +import org.apache.karaf.shell.console.AbstractAction;
> +
> +@Command(scope = "shell", name = "head", description = "Displays the first lines of a file.")
> +public class HeadAction extends AbstractAction {
> +
> + private static final int DEFAULT_NUMBER_OF_LINES = 10;
> +
> + @Option(name = "-n", aliases = {}, description = "The number of lines to display, starting at 1.", required = false, multiValued = false)
> + private int numberOfLines;
> +
> + @Argument(index = 0, name = "paths or urls", description = "A list of file paths or urls to display separated by whitespaces.", required = false, multiValued = true)
> + private List<String> paths;
> +
> + protected Object doExecute() throws Exception {
> + //If no paths provided assume standar input
> + if (paths == null || paths.size() == 0) {
> + if (log.isDebugEnabled()) {
> + log.debug("Heading STDIN");
> + }
> +
> + head(new BufferedReader(new InputStreamReader(System.in)));
> + } else {
> + for (String filename : paths) {
> + BufferedReader reader;
> +
> + // First try a URL
> + try {
> + URL url = new URL(filename);
> + if (log.isDebugEnabled()) {
> + log.debug("Heading URL: " + url);
> + }
> + reader = new BufferedReader(new InputStreamReader(url.openStream()));
> + }
> + catch (MalformedURLException ignore) {
> + // They try a file
> + File file = new File(filename);
> + if (log.isDebugEnabled()) {
> + log.debug("Heading file: " + file);
> + }
> + reader = new BufferedReader(new FileReader(file));
> + }
> +
> + try {
> + head(reader);
> + }
> + finally {
> + try {
> + reader.close();
> + } catch (IOException e) {
> + // Ignore
> + }
> + }
> + }
> + }
> + return null;
> + }
> +
> + private void head(final BufferedReader reader) throws IOException {
> + String line;
> + int lineno = 1;
> +
> + if (numberOfLines < 1) {
> + numberOfLines = DEFAULT_NUMBER_OF_LINES;
> + }
> +
> + while ((line = reader.readLine()) != null && lineno <= numberOfLines) {
> + System.out.println(line);
> + lineno++;
> + }
> + }
> +}
>
> Added: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java
> URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java?rev=1044051&view=auto
> ==============================================================================
> --- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java (added)
> +++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/TailAction.java Thu Dec 9 17:48:16 2010
> @@ -0,0 +1,108 @@
> +/*
> + * 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.karaf.shell.commands;
> +
> +import java.io.BufferedReader;
> +import java.io.File;
> +import java.io.FileReader;
> +import java.io.IOException;
> +import java.io.InputStreamReader;
> +import java.net.MalformedURLException;
> +import java.net.URL;
> +import java.util.LinkedList;
> +import java.util.List;
> +
> +import org.apache.felix.gogo.commands.Argument;
> +import org.apache.felix.gogo.commands.Command;
> +import org.apache.felix.gogo.commands.Option;
> +import org.apache.karaf.shell.console.AbstractAction;
> +
> +@Command(scope = "shell", name = "tail", description = "Displays the last lines of a file")
> +public class TailAction extends AbstractAction {
> +
> + private static final int DEFAULT_NUMBER_OF_LINES = 10;
> +
> + @Option(name = "-n", aliases = {}, description = "The number of lines to display, starting at 1.", required = false, multiValued = false)
> + private int numberOfLines;
> +
> + @Argument(index = 0, name = "paths or urls", description = "A list of file paths or urls to display separated by whitespaces.", required = false, multiValued = true)
> + private List<String> paths;
> +
> + protected Object doExecute() throws Exception {
> + //If no paths provided assume standar input
> + if (paths == null || paths.size() == 0) {
> + if (log.isDebugEnabled()) {
> + log.debug("Tailing STDIN");
> + }
> + tail(new BufferedReader(new InputStreamReader(System.in)));
> + } else {
> + for (String filename : paths) {
> + BufferedReader reader;
> +
> + // First try a URL
> + try {
> + URL url = new URL(filename);
> + if (log.isDebugEnabled()) {
> + log.debug("Tailing URL: " + url);
> + }
> + reader = new BufferedReader(new InputStreamReader(url.openStream()));
> + }
> + catch (MalformedURLException ignore) {
> + // They try a file
> + File file = new File(filename);
> + if (log.isDebugEnabled()) {
> + log.debug("Tailing file: " + file);
> + }
> + reader = new BufferedReader(new FileReader(file));
> + }
> +
> + try {
> + tail(reader);
> + }
> + finally {
> + try {
> + reader.close();
> + } catch (IOException e) {
> + // Ignore
> + }
> + }
> + }
> + }
> +
> + return null;
> + }
> +
> + private void tail(final BufferedReader reader) throws IOException {
> + List<String> lines = new LinkedList<String>();
> + String line;
> + int lineno = 1;
> +
> + while ((line = reader.readLine()) != null) {
> + lines.add(line);
> + }
> +
> + if (numberOfLines < 1) {
> + numberOfLines = DEFAULT_NUMBER_OF_LINES;
> + }
> +
> + int startLine = lines.size() < numberOfLines ? 0 : lines.size() - numberOfLines;
> +
> + for (lineno = startLine; lineno < lines.size(); lineno++) {
> + System.out.println(lines.get(lineno));
> + }
> + }
> +}
>
> Modified: karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
> URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands?rev=1044051&r1=1044050&r2=1044051&view=diff
> ==============================================================================
> --- karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands (original)
> +++ karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands Thu Dec 9 17:48:16 2010
> @@ -21,6 +21,7 @@ org.apache.karaf.shell.commands.EchoActi
> org.apache.karaf.shell.commands.ExecuteAction
> org.apache.karaf.shell.commands.GrepAction
> org.apache.karaf.shell.commands.HistoryAction
> +org.apache.karaf.shell.commands.HeadAction
> org.apache.karaf.shell.commands.IfAction
> org.apache.karaf.shell.commands.JavaAction
> org.apache.karaf.shell.commands.LogoutAction
> @@ -30,3 +31,4 @@ org.apache.karaf.shell.commands.PrintfAc
> org.apache.karaf.shell.commands.SleepAction
> org.apache.karaf.shell.commands.SortAction
> org.apache.karaf.shell.commands.TacAction
> +org.apache.karaf.shell.commands.TailAction
>
> Modified: karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
> URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=1044051&r1=1044050&r2=1044051&view=diff
> ==============================================================================
> --- karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (original)
> +++ karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml Thu Dec 9 17:48:16 2010
> @@ -41,6 +41,9 @@
> <command name="shell/history">
> <action class="org.apache.karaf.shell.commands.HistoryAction"/>
> </command>
> + <command name="shell/head">
> + <action class="org.apache.karaf.shell.commands.HeadAction"/>
> + </command>
> <command name="shell/if">
> <action class="org.apache.karaf.shell.commands.IfAction"/>
> </command>
> @@ -78,6 +81,9 @@
> <command name="shell/tac">
> <action class="org.apache.karaf.shell.commands.TacAction"/>
> </command>
> + <command name="shell/tail">
> + <action class="org.apache.karaf.shell.commands.TailAction"/>
> + </command>
> </command-bundle>
>
> <bean class="org.osgi.util.tracker.BundleTracker" init-method="open"
>
>
>
--
Cheers,
Guillaume Nodet
------------------------
Blog: http://gnodet.blogspot.com/
------------------------
Open Source SOA
http://fusesource.com