Author: gnodet
Date: Wed Mar 19 09:29:58 2008
New Revision: 638925
URL: http://svn.apache.org/viewvc?rev=638925&view=rev
Log:
SMX4KNL-24: Add a few utils commands: cat, java, exec, sleep
Added:
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/CatCommand.java
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/ExecuteCommand.java
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/JavaCommand.java
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/SleepCommand.java
Modified:
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml
Added:
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/CatCommand.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/CatCommand.java?rev=638925&view=auto
==============================================================================
---
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/CatCommand.java
(added)
+++
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/CatCommand.java
Wed Mar 19 09:29:58 2008
@@ -0,0 +1,103 @@
+/*
+ * 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.geronimo.gshell.commands.utils;
+
+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.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.clp.Option;
+import org.apache.geronimo.gshell.command.IO;
+import org.apache.geronimo.gshell.command.annotation.CommandComponent;
+import org.apache.geronimo.gshell.support.OsgiCommandSupport;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+
+/**
+ * Concatenate and print files and/or URLs.
+ *
+ * @version $Rev: 593392 $ $Date: 2007-11-09 03:14:15 +0100 (Fri, 09 Nov 2007)
$
+ */
[EMAIL PROTECTED](id="utils:cat", description="Concatenate and print files
and/or URLs")
+public class CatCommand extends OsgiCommandSupport
+{
+ @Option(name="-n", description="Number the output lines, starting at 1")
+ private boolean displayLineNumbers;
+
+ @Argument(description="File or URL", required=true)
+ private List<String> args;
+
+ protected Object doExecute() throws Exception {
+ //
+ // Support "-" if length is one, and read from io.in
+ // This will help test command pipelines.
+ //
+ if (args.size() == 1 && "-".equals(args.get(0))) {
+ log.info("Printing STDIN");
+ cat(new BufferedReader(io.in), io);
+ }
+ else {
+ for (String filename : args) {
+ BufferedReader reader;
+
+ // First try a URL
+ try {
+ URL url = new URL(filename);
+ log.info("Printing URL: " + url);
+ reader = new BufferedReader(new
InputStreamReader(url.openStream()));
+ }
+ catch (MalformedURLException ignore) {
+ // They try a file
+ File file = new File(filename);
+ log.info("Printing file: " + file);
+ reader = new BufferedReader(new FileReader(file));
+ }
+
+ try {
+ cat(reader, io);
+ }
+ finally {
+ IOUtil.close(reader);
+ }
+ }
+ }
+
+ return SUCCESS;
+ }
+
+ private void cat(final BufferedReader reader, final IO io) throws
IOException {
+ String line;
+ int lineno = 1;
+
+ while ((line = reader.readLine()) != null) {
+ if (displayLineNumbers) {
+ String gutter = StringUtils.leftPad(String.valueOf(lineno++),
6);
+ io.out.print(gutter);
+ io.out.print(" ");
+ }
+ io.out.println(line);
+ }
+ }
+}
Added:
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/ExecuteCommand.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/ExecuteCommand.java?rev=638925&view=auto
==============================================================================
---
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/ExecuteCommand.java
(added)
+++
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/ExecuteCommand.java
Wed Mar 19 09:29:58 2008
@@ -0,0 +1,63 @@
+/*
+ * 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.geronimo.gshell.commands.utils;
+
+import java.util.List;
+
+import org.apache.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.command.annotation.CommandComponent;
+import org.apache.geronimo.gshell.common.io.PumpStreamHandler;
+import org.apache.geronimo.gshell.support.OsgiCommandSupport;
+
+/**
+ * Execute system processes.
+ *
+ * @version $Rev: 593392 $ $Date: 2007-11-09 03:14:15 +0100 (Fri, 09 Nov 2007)
$
+ */
[EMAIL PROTECTED](id="utils:exec", description="Execute system processes")
+public class ExecuteCommand extends OsgiCommandSupport
+{
+ private ProcessBuilder builder;
+
+ @Argument(description="Argument", required=true)
+ private List<String> args;
+
+ protected Object doExecute() throws Exception {
+ assert builder != null;
+
+ log.info("Executing: {}", builder.command());
+
+ Process p = builder.start();
+
+ PumpStreamHandler handler = new PumpStreamHandler(io.inputStream,
io.outputStream, io.errorStream);
+ handler.attach(p);
+ handler.start();
+
+ log.debug("Waiting for process to exit...");
+
+ int status = p.waitFor();
+
+
+ log.info("Process exited w/status: {}", status);
+
+ handler.stop();
+
+ return status;
+ }
+}
Modified:
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java?rev=638925&r1=638924&r2=638925&view=diff
==============================================================================
---
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java
(original)
+++
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java
Wed Mar 19 09:29:58 2008
@@ -8,23 +8,54 @@
import org.apache.geronimo.gshell.clp.Option;
import org.apache.geronimo.gshell.command.annotation.CommandComponent;
import org.apache.geronimo.gshell.support.OsgiCommandSupport;
+import org.codehaus.plexus.util.StringUtils;
[EMAIL PROTECTED](id="utils:grep", description="Grep")
[EMAIL PROTECTED](id="utils:grep", description="Print lines matching a pattern")
public class GrepCommand extends OsgiCommandSupport {
@Argument(required=true, description="Regular expression")
private String regex;
- @Option(name = "-v", description = "Inverse matching")
- private boolean inverse;
+ @Option(name="-n", aliases = { "--line-number" }, description="Prefix each
line of output with the line number within its input file.")
+ private boolean lineNumber;
+
+ @Option(name = "-v", aliases = { "--invert-match" }, description = "Invert
the sense of matching, to select non-matching lines.")
+ private boolean invertMatch;
+
+ @Option(name = "-w", aliases = { "--word-regexp" }, description = "Select
only those lines containing matches that form whole " +
+ "words.
The test is that the matching substring must either be " +
+ "at the
beginning of the line, or preceded by a non-word constituent " +
+
"character. Similarly, it must be either at the end of " +
+ "the
line or followed by a non-word constituent character. " +
+
"Word-constituent characters are letters, digits, and the underscore.")
+ private boolean wordRegexp;
+
+ @Option(name = "-x", aliases = { "--line-regexp" }, description = "Select
only those matches that exactly match the whole line.")
+ private boolean lineRegexp;
protected Object doExecute() throws Exception {
- Pattern p = Pattern.compile(regex);
+ String regexp = regex;
+ if (wordRegexp) {
+ regexp = "\\b" + regexp + "\\b";
+ }
+ if (lineRegexp) {
+ regexp = "^" + regexp + "$";
+ } else {
+ regexp = ".*" + regexp + ".*";
+ }
+ Pattern p = Pattern.compile(regexp);
try {
- while (true) {
- String line = readLine(io.in);
- if (p.matcher(line).matches() ^ inverse) {
+ int lineno = 1;
+ String line;
+ while ((line = readLine(io.in)) != null) {
+ if (p.matcher(line).matches() ^ invertMatch) {
+ if (lineNumber) {
+ String gutter =
StringUtils.leftPad(String.valueOf(lineno), 6);
+ io.out.print(gutter);
+ io.out.print(" ");
+ }
io.out.println(line);
+ lineno++;
}
}
} catch (IOException e) {
Added:
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/JavaCommand.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/JavaCommand.java?rev=638925&view=auto
==============================================================================
---
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/JavaCommand.java
(added)
+++
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/JavaCommand.java
Wed Mar 19 09:29:58 2008
@@ -0,0 +1,76 @@
+/*
+ * 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.geronimo.gshell.commands.utils;
+
+import java.lang.reflect.Method;
+import java.util.List;
+
+import org.apache.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.clp.Option;
+import org.apache.geronimo.gshell.command.annotation.CommandComponent;
+import org.apache.geronimo.gshell.common.Arguments;
+import org.apache.geronimo.gshell.support.OsgiCommandSupport;
+
+/**
+ * Execute a Java standard application.
+ *
+ * <p>By default looks for static main(String[]) to execute, but
+ * you can specify a different static method that takes a String[]
+ * to execute instead.
+ *
+ * @version $Rev: 593392 $ $Date: 2007-11-09 03:14:15 +0100 (Fri, 09 Nov 2007)
$
+ */
[EMAIL PROTECTED](id="utils:java", description="Execute a Java standard
application")
+public class JavaCommand extends OsgiCommandSupport
+{
+ @Option(name="-m", aliases={"--method"}, metaVar="METHOD",
description="Invoke a named method")
+ private String methodName = "main";
+
+ @Argument(index=0, metaVar="CLASSNAME", description="The name of the class
to invoke", required=true)
+ private String className;
+
+ @Argument(index=1, metaVar="ARG", description="Arguments to pass to the
METHOD of CLASSNAME")
+ private List<String> args;
+
+ protected Object doExecute() throws Exception {
+ boolean info = log.isInfoEnabled();
+
+ Class type =
Thread.currentThread().getContextClassLoader().loadClass(className);
+ if (info) {
+ log.info("Using type: " + type);
+ }
+
+ Method method = type.getMethod(methodName, String[].class);
+ if (info) {
+ log.info("Using method: " + method);
+ }
+
+ if (info) {
+ log.info("Invoking w/arguments: " + Arguments.asString(args));
+ }
+
+ Object result = method.invoke(null, args);
+
+ if (info) {
+ log.info("Result: " + result);
+ }
+
+ return SUCCESS;
+ }
+}
Added:
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/SleepCommand.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/SleepCommand.java?rev=638925&view=auto
==============================================================================
---
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/SleepCommand.java
(added)
+++
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/SleepCommand.java
Wed Mar 19 09:29:58 2008
@@ -0,0 +1,50 @@
+/*
+ * 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.geronimo.gshell.commands.utils;
+
+import org.apache.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.command.annotation.CommandComponent;
+import org.apache.geronimo.gshell.support.OsgiCommandSupport;
+
+/**
+ * Sleep... zzzZ
+ *
+ * @version $Rev: 593392 $ $Date: 2007-11-09 03:14:15 +0100 (Fri, 09 Nov 2007)
$
+ */
[EMAIL PROTECTED](id="utils:sleep", description="Sleep for a bit then wake up")
+public class SleepCommand extends OsgiCommandSupport
+{
+ @Argument(description="Time in milliseconds", required=true)
+ private int time = -1;
+
+ protected Object doExecute() throws Exception {
+ log.info("Sleeping for " + time);
+
+ try {
+ Thread.sleep(time);
+ }
+ catch (InterruptedException ignore) {
+ log.debug("Sleep was interrupted... :-(");
+ }
+
+ log.info("Awake now");
+
+ return SUCCESS;
+ }
+}
Modified:
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml
URL:
http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml?rev=638925&r1=638924&r2=638925&view=diff
==============================================================================
---
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml
(original)
+++
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml
Wed Mar 19 09:29:58 2008
@@ -54,4 +54,12 @@
<bean id="grep"
class="org.apache.geronimo.gshell.commands.utils.GrepCommand" />
+ <bean id="cat"
class="org.apache.geronimo.gshell.commands.utils.CatCommand" />
+
+ <bean id="exec"
class="org.apache.geronimo.gshell.commands.utils.ExecuteCommand" />
+
+ <bean id="java"
class="org.apache.geronimo.gshell.commands.utils.JavaCommand" />
+
+ <bean id="sleep"
class="org.apache.geronimo.gshell.commands.utils.SleepCommand" />
+
</beans>
Modified:
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml
URL:
http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml?rev=638925&r1=638924&r2=638925&view=diff
==============================================================================
---
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml
(original)
+++
servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml
Wed Mar 19 09:29:58 2008
@@ -102,6 +102,34 @@
</osgi:service-properties>
</osgi:service>
+ <osgi:service ref="cat"
interface="org.apache.geronimo.gshell.command.Command">
+ <osgi:service-properties>
+ <entry key="shell" value="utils"/>
+ <entry key="alias" value="cat"/>
+ </osgi:service-properties>
+ </osgi:service>
+
+ <osgi:service ref="java"
interface="org.apache.geronimo.gshell.command.Command">
+ <osgi:service-properties>
+ <entry key="shell" value="utils"/>
+ <entry key="alias" value="java"/>
+ </osgi:service-properties>
+ </osgi:service>
+
+ <osgi:service ref="exec"
interface="org.apache.geronimo.gshell.command.Command">
+ <osgi:service-properties>
+ <entry key="shell" value="utils"/>
+ <entry key="alias" value="exec"/>
+ </osgi:service-properties>
+ </osgi:service>
+
+ <osgi:service ref="sleep"
interface="org.apache.geronimo.gshell.command.Command">
+ <osgi:service-properties>
+ <entry key="shell" value="utils"/>
+ <entry key="alias" value="sleep"/>
+ </osgi:service-properties>
+ </osgi:service>
+
<osgix:property-placeholder persistent-id="org.apache.servicemix.shell">
<osgix:default-properties>
<prop key="startLocalConsole">true</prop>