You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2016/03/21 17:53:07 UTC
svn commit: r1735995 [1/4] - in /felix/trunk/gogo: ./ jline/ jline/doc/
jline/src/ jline/src/main/ jline/src/main/java/ jline/src/main/java/org/
jline/src/main/java/org/apache/ jline/src/main/java/org/apache/felix/
jline/src/main/java/org/apache/felix/...
Author: gnodet
Date: Mon Mar 21 16:53:06 2016
New Revision: 1735995
URL: http://svn.apache.org/viewvc?rev=1735995&view=rev
Log:
Add a JLine based shell
Added:
felix/trunk/gogo/jline/
felix/trunk/gogo/jline/DEPENDENCIES
felix/trunk/gogo/jline/LICENSE
felix/trunk/gogo/jline/NOTICE
felix/trunk/gogo/jline/doc/
felix/trunk/gogo/jline/doc/changelog.txt
felix/trunk/gogo/jline/pom.xml
felix/trunk/gogo/jline/src/
felix/trunk/gogo/jline/src/main/
felix/trunk/gogo/jline/src/main/java/
felix/trunk/gogo/jline/src/main/java/org/
felix/trunk/gogo/jline/src/main/java/org/apache/
felix/trunk/gogo/jline/src/main/java/org/apache/felix/
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Activator.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCommands.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCompletionEnvironment.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Main.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/ParsedLineImpl.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Parser.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Procedural.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Shell.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/ssh/
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/ssh/ShellCommand.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/ssh/ShellCommandFactory.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/ssh/ShellFactoryImpl.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/ssh/Ssh.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/BootException.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/Connection.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/ConnectionData.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/ConnectionEvent.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/ConnectionFilter.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/ConnectionListener.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/ConnectionManager.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/PortListener.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/Telnet.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/telnet/TelnetIO.java
felix/trunk/gogo/jline/src/main/resources/
felix/trunk/gogo/jline/src/main/resources/gosh_profile
felix/trunk/gogo/jline/src/main/resources/motd
Modified:
felix/trunk/gogo/pom.xml
Added: felix/trunk/gogo/jline/DEPENDENCIES
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/DEPENDENCIES?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/DEPENDENCIES (added)
+++ felix/trunk/gogo/jline/DEPENDENCIES Mon Mar 21 16:53:06 2016
@@ -0,0 +1,20 @@
+Apache Felix Gogo Shell
+Copyright 2011 The Apache Software Foundation
+
+This software was developed at the Apache Software Foundation
+(http://www.apache.org) and may have dependencies on other
+Apache software licensed under Apache License 2.0.
+
+I. Included Third-Party Software
+
+None.
+
+II. Used Third-Party Software
+
+This product uses software developed at
+The OSGi Alliance (http://www.osgi.org/).
+Copyright (c) OSGi Alliance (2000, 2009).
+Licensed under the Apache License 2.0.
+
+III. License Summary
+- Apache License 2.0
Added: felix/trunk/gogo/jline/LICENSE
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/LICENSE?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/LICENSE (added)
+++ felix/trunk/gogo/jline/LICENSE Mon Mar 21 16:53:06 2016
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed 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.
Added: felix/trunk/gogo/jline/NOTICE
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/NOTICE?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/NOTICE (added)
+++ felix/trunk/gogo/jline/NOTICE Mon Mar 21 16:53:06 2016
@@ -0,0 +1,6 @@
+Apache Felix Gogo Shell
+Copyright 2011 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+Licensed under the Apache License 2.0.
Added: felix/trunk/gogo/jline/doc/changelog.txt
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/doc/changelog.txt?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/doc/changelog.txt (added)
+++ felix/trunk/gogo/jline/doc/changelog.txt Mon Mar 21 16:53:06 2016
@@ -0,0 +1,67 @@
+Changes from 0.8.0 to 0.10.0
+----------------------------
+
+** Improvement
+ * Added gosh_profile work around for issue with OSGi R4.3 API
+ ambiguity.
+
+Changes from 0.6.1 to 0.8.0
+---------------------------
+
+** Bug
+ * [FELIX-2651] - [Gogo] MOTD formatting is broken under Windows
+
+** Improvement
+ * [FELIX-2661] - [Gogo] It should be easier to start Gogo shell
+ non-interactively
+
+** New Feature
+ * [FELIX-2767] - gogo telnet IP address
+
+Changes from 0.6.0 to 0.6.1
+---------------------------
+
+** Bug
+ * [FELIX-2446] - [Gogo] The bundle context command is not used with a
+ scope in gosh_profile
+ * [FELIX-2477] - [gogo] shell procedural commands don't inherit closure
+ arguments
+
+** Improvement
+ * [FELIX-2445] - [Gogo] Default gosh_profile should be updated to use
+ system bundle to load java.lang.System
+ * [FELIX-2543] - [Gogo] Should avoid using System.getProperty() to get
+ configuration properties
+
+Gogo Shell 0.6.0
+----------------
+
+** Bug
+ * [FELIX-1473] - [gogo] The syntax does not provide a way to call methods
+ on a string
+ * [FELIX-1474] - [gogo] result of commands is implicitly written to pipe
+ * [FELIX-1493] - [gogo] automatic expansion of $args in Closure stops
+ direct access to $args list
+ * [FELIX-2337] - [gogo] no way to access array[] elements produced by
+ assignment
+ * [FELIX-2375] - [gogo] when supplied args can't be coerced, the error
+ message prints the arg values, rather than their types
+ * [FELIX-2380] - [gogo] lock contention in piped writer when reader
+ doesn't read all input
+
+** Improvement
+ * [FELIX-1487] - Support for commands on multiple lines
+ * [FELIX-2328] - [gogo] tidy-up runtime to remove optional code etc
+ * [FELIX-2339] - [gogo] add support for running scripts
+ * [FELIX-2342] - [gogo] remove old felix command adaptor
+
+** New Feature
+ * [FELIX-2363] - [Gogo] Add annotations for creating commands with
+ optional and out-of-order arguments
+
+** Task
+ * [FELIX-1670] - [gogo] launcher bundle not required
+ * [FELIX-1889] - Gogo should depend on the official OSGi jars
+ * [FELIX-2334] - [Gogo] Use org.apache.felix as Maven groupId
+ * [FELIX-2367] - [Gogo] Use org.apache.felix namespace to avoid any
+ perceived legal issues
Added: felix/trunk/gogo/jline/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/pom.xml?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/pom.xml (added)
+++ felix/trunk/gogo/jline/pom.xml Mon Mar 21 16:53:06 2016
@@ -0,0 +1,111 @@
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>gogo-parent</artifactId>
+ <version>0.6.0</version>
+ <relativePath>../gogo-parent/pom.xml</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <packaging>bundle</packaging>
+ <name>Apache Felix Gogo JLine Shell</name>
+ <artifactId>org.apache.felix.gogo.jline</artifactId>
+ <version>0.11.0-SNAPSHOT</version>
+ <properties>
+ <!-- Skip because of NPE -->
+ <animal.sniffer.skip>true</animal.sniffer.skip>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>4.2.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <version>4.0.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.gogo.runtime</artifactId>
+ <version>0.16.3-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>jline</groupId>
+ <artifactId>jline</artifactId>
+ <version>3.0.0-SNAPSHOT</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sshd</groupId>
+ <artifactId>sshd-core</artifactId>
+ <version>1.0.0</version>
+ <optional>true</optional>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Export-Package>
+ </Export-Package>
+ <Import-Package>
+ org.apache.felix.service.command; status="provisional",
+ *
+ </Import-Package>
+ <Private-Package>
+ org.apache.felix.gogo.shell,
+ org.apache.felix.gogo.options
+ </Private-Package>
+ <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
+ <Bundle-Vendor>The Apache Software Foundation</Bundle-Vendor>
+ <Bundle-Activator>org.apache.felix.gogo.jline.Activator</Bundle-Activator>
+ <Include-Resource>{maven-resources},META-INF/LICENSE=LICENSE,META-INF/NOTICE=NOTICE,META-INF/DEPENDENCIES=DEPENDENCIES</Include-Resource>
+ <_versionpolicy>[$(version;==;$(@)),$(version;+;$(@)))</_versionpolicy>
+ <_removeheaders>Private-Package,Ignore-Package,Include-Resource</_removeheaders>
+ </instructions>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.rat</groupId>
+ <artifactId>apache-rat-plugin</artifactId>
+ <configuration>
+ <excludes>
+ <param>src/main/resources/motd</param>
+ </excludes>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.8</source>
+ <target>1.8</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
Added: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Activator.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Activator.java?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Activator.java (added)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Activator.java Mon Mar 21 16:53:06 2016
@@ -0,0 +1,182 @@
+/*
+ * 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.felix.gogo.jline;
+
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.felix.gogo.jline.Shell.Context;
+import org.apache.felix.gogo.jline.telnet.Telnet;
+import org.apache.felix.service.command.CommandProcessor;
+import org.apache.felix.service.command.CommandSession;
+import org.apache.felix.service.command.Converter;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class Activator implements BundleActivator {
+ private BundleContext context;
+ private ServiceTracker commandProcessorTracker;
+ private Set<ServiceRegistration> regs;
+
+ private ExecutorService executor;
+
+ public Activator() {
+ regs = new HashSet<ServiceRegistration>();
+ }
+
+ public void start(BundleContext context) throws Exception {
+ this.context = context;
+ this.commandProcessorTracker = createCommandProcessorTracker();
+ this.commandProcessorTracker.open();
+ }
+
+ public void stop(BundleContext context) throws Exception {
+ Iterator<ServiceRegistration> iterator = regs.iterator();
+ while (iterator.hasNext()) {
+ ServiceRegistration reg = iterator.next();
+ reg.unregister();
+ iterator.remove();
+ }
+
+ stopShell();
+
+ this.commandProcessorTracker.close();
+ }
+
+ private ServiceTracker createCommandProcessorTracker() {
+ return new ServiceTracker(context, CommandProcessor.class.getName(), null) {
+ @Override
+ public Object addingService(ServiceReference reference) {
+ CommandProcessor processor = (CommandProcessor) super.addingService(reference);
+ startShell(context, processor);
+ return processor;
+ }
+
+ @Override
+ public void removedService(ServiceReference reference, Object service) {
+ stopShell();
+ super.removedService(reference, service);
+ }
+ };
+ }
+
+ private void startShell(final BundleContext context, CommandProcessor processor) {
+ Dictionary<String, Object> dict = new Hashtable<String, Object>();
+ dict.put(CommandProcessor.COMMAND_SCOPE, "gogo");
+
+ // register converters
+ regs.add(context.registerService(Converter.class.getName(), new Converters(context.getBundle(0).getBundleContext()), null));
+
+ // register commands
+
+ dict.put(CommandProcessor.COMMAND_FUNCTION, Builtin.functions);
+ regs.add(context.registerService(Builtin.class.getName(), new Builtin(), dict));
+
+ dict.put(CommandProcessor.COMMAND_FUNCTION, Procedural.functions);
+ regs.add(context.registerService(Procedural.class.getName(), new Procedural(), dict));
+
+ dict.put(CommandProcessor.COMMAND_FUNCTION, Posix.functions);
+ regs.add(context.registerService(Posix.class.getName(), new Posix(), dict));
+
+ dict.put(CommandProcessor.COMMAND_FUNCTION, Telnet.functions);
+ regs.add(context.registerService(Telnet.class.getName(), new Telnet(processor), dict));
+
+ Shell shell = new Shell(new ShellContext(), processor, null);
+ dict.put(CommandProcessor.COMMAND_FUNCTION, Shell.functions);
+ regs.add(context.registerService(Shell.class.getName(), shell, dict));
+
+ // start shell on a separate thread...
+ executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
+ public Thread newThread(Runnable runnable) {
+ return new Thread(runnable, "Gogo shell");
+ }
+ });
+ executor.submit(new StartShellJob(context, processor));
+ }
+
+ private void stopShell() {
+ if (executor != null && !(executor.isShutdown() || executor.isTerminated())) {
+ executor.shutdownNow();
+
+ try {
+ if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
+ System.err.println("!!! FAILED TO STOP EXECUTOR !!!");
+ }
+ } catch (InterruptedException e) {
+ // Restore administration...
+ Thread.currentThread().interrupt();
+ }
+ executor = null;
+ }
+ }
+
+ private static class StartShellJob implements Runnable {
+ private final BundleContext context;
+ private final CommandProcessor processor;
+
+ public StartShellJob(BundleContext context, CommandProcessor processor) {
+ this.context = context;
+ this.processor = processor;
+ }
+
+ public void run() {
+ CommandSession session = processor.createSession(System.in, System.out, System.err);
+ try {
+ // wait for gosh command to be registered
+ for (int i = 0; (i < 100) && session.get("gogo:gosh") == null; ++i) {
+ TimeUnit.MILLISECONDS.sleep(10);
+ }
+
+ String args = context.getProperty("gosh.args");
+ args = (args == null) ? "" : args;
+ session.execute("gosh --login " + args);
+ } catch (Exception e) {
+ Object loc = session.get(".location");
+ if (null == loc || !loc.toString().contains(":")) {
+ loc = "gogo";
+ }
+
+ System.err.println(loc + ": " + e.getClass().getSimpleName() + ": " + e.getMessage());
+ e.printStackTrace();
+ } finally {
+ session.close();
+ }
+ }
+ }
+
+ private class ShellContext implements Context {
+ public String getProperty(String name) {
+ return context.getProperty(name);
+ }
+
+ public void exit() throws Exception {
+ context.getBundle(0).stop();
+ }
+ }
+}
\ No newline at end of file
Added: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java (added)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java Mon Mar 21 16:53:06 2016
@@ -0,0 +1,498 @@
+/*
+ * 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.felix.gogo.jline;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.StringWriter;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.apache.felix.service.command.CommandSession;
+import org.apache.felix.service.command.Converter;
+import org.jline.builtins.Options;
+
+/**
+ * gosh built-in commands.
+ */
+public class Builtin {
+
+ static final String[] functions = {"format", "getopt", "new", "set", "tac", "type"};
+
+ private static final String[] packages = {"java.lang", "java.io", "java.net",
+ "java.util"};
+ private final static Set<String> KEYWORDS = new HashSet<String>(
+ Arrays.asList(new String[]{"abstract", "continue", "for", "new", "switch",
+ "assert", "default", "goto", "package", "synchronized", "boolean", "do",
+ "if", "private", "this", "break", "double", "implements", "protected",
+ "throw", "byte", "else", "import", "public", "throws", "case", "enum",
+ "instanceof", "return", "transient", "catch", "extends", "int", "short",
+ "try", "char", "final", "interface", "static", "void", "class",
+ "finally", "long", "strictfp", "volatile", "const", "float", "native",
+ "super", "while"}));
+
+ @SuppressWarnings("unchecked")
+ static Set<String> getCommands(CommandSession session) {
+ return (Set<String>) session.get(".commands");
+ }
+
+ public CharSequence format(CommandSession session) {
+ return format(session, session.get("_")); // last result
+ }
+
+ public CharSequence format(CommandSession session, Object arg) {
+ CharSequence result = session.format(arg, Converter.INSPECT);
+ System.out.println(result);
+ return result;
+ }
+
+ /**
+ * script access to Options.
+ */
+ public Options getopt(List<Object> spec, Object[] args) {
+ String[] optSpec = new String[spec.size()];
+ for (int i = 0; i < optSpec.length; ++i) {
+ optSpec[i] = spec.get(i).toString();
+ }
+ return Options.compile(optSpec).parse(args);
+ }
+
+ // FIXME: the "new" command should be provided by runtime,
+ // so it can leverage same argument coercion mechanism, used to invoke methods.
+ public Object _new(CommandSession session, Object name, Object[] argv) throws Exception {
+ Class<?> clazz;
+
+ if (name instanceof Class<?>) {
+ clazz = (Class<?>) name;
+ } else {
+ clazz = loadClass(name.toString());
+ }
+
+ for (Constructor<?> c : clazz.getConstructors()) {
+ Class<?>[] types = c.getParameterTypes();
+ if (types.length != argv.length) {
+ continue;
+ }
+
+ boolean match = true;
+
+ Object[] transformed = argv.clone();
+ for (int i = 0; i < transformed.length; ++i) {
+ try {
+ transformed[i] = session.convert(types[i], transformed[i]);
+ } catch (IllegalArgumentException e) {
+ match = false;
+ break;
+ }
+ }
+
+ if (!match) {
+ continue;
+ }
+
+ try {
+ return c.newInstance(transformed);
+ } catch (InvocationTargetException ite) {
+ Throwable cause = ite.getCause();
+ if (cause instanceof Exception) {
+ throw (Exception) cause;
+ }
+ throw ite;
+ }
+ }
+
+ throw new IllegalArgumentException("can't coerce " + Arrays.asList(argv)
+ + " to any of " + Arrays.asList(clazz.getConstructors()));
+ }
+
+ private Class<?> loadClass(String name) throws ClassNotFoundException {
+ if (!name.contains(".")) {
+ for (String p : packages) {
+ String pkg = p + "." + name;
+ try {
+ return Class.forName(pkg);
+ } catch (ClassNotFoundException e) {
+ }
+ }
+ }
+ return Class.forName(name);
+ }
+
+ public void set(CommandSession session, String[] argv) throws Exception {
+ final String[] usage = {
+ "set - show session variables",
+ "Usage: set [OPTIONS] [PREFIX]",
+ " -? --help show help",
+ " -a --all show all variables, including those starting with .",
+ " -x set xtrace option",
+ " +x unset xtrace option",
+ "If PREFIX given, then only show variable(s) starting with PREFIX"};
+
+ Options opt = Options.compile(usage).parse(argv);
+
+ if (opt.isSet("help")) {
+ opt.usage(System.err);
+ return;
+ }
+
+ List<String> args = opt.args();
+ String prefix = (args.isEmpty() ? "" : args.get(0));
+
+ if (opt.isSet("x")) {
+ session.put("echo", true);
+ } else if ("+x".equals(prefix)) {
+ session.put("echo", null);
+ } else {
+ boolean all = opt.isSet("all");
+ for (String key : new TreeSet<String>(Shell.getVariables(session))) {
+ if (!key.startsWith(prefix))
+ continue;
+
+ if (key.startsWith(".") && !(all || prefix.length() > 0))
+ continue;
+
+ Object target = session.get(key);
+ String type = null;
+ String value = null;
+
+ if (target != null) {
+ Class<? extends Object> clazz = target.getClass();
+ type = clazz.getSimpleName();
+ value = target.toString();
+ }
+
+ String trunc = value == null || value.length() < 55 ? "" : "...";
+ System.out.println(String.format("%-15.15s %-15s %.45s%s", type, key,
+ value, trunc));
+ }
+ }
+ }
+
+ /*
+ * the following methods depend on the internals of the runtime implementation.
+ * ideally, they should be available via some API.
+ */
+
+ public Object tac(CommandSession session, String[] argv) throws IOException {
+ final String[] usage = {
+ "tac - capture stdin as String or List and optionally write to file.",
+ "Usage: tac [-al] [FILE]",
+ " -a --append append to FILE",
+ " -l --list return List<String>",
+ " -? --help show help"};
+
+ Options opt = Options.compile(usage).parse(argv);
+
+ if (opt.isSet("help")) {
+ opt.usage(System.err);
+ return null;
+ }
+
+ List<String> args = opt.args();
+ BufferedWriter fw = null;
+
+ if (args.size() == 1) {
+ String path = args.get(0);
+ File file = new File(Posix._pwd(session), path);
+ fw = new BufferedWriter(new FileWriter(file, opt.isSet("append")));
+ }
+
+ StringWriter sw = new StringWriter();
+ BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
+
+ ArrayList<String> list = null;
+
+ if (opt.isSet("list")) {
+ list = new ArrayList<String>();
+ }
+
+ boolean first = true;
+ String s;
+
+ while ((s = rdr.readLine()) != null) {
+ if (list != null) {
+ list.add(s);
+ } else {
+ if (!first) {
+ sw.write(' ');
+ }
+ first = false;
+ sw.write(s);
+ }
+
+ if (fw != null) {
+ fw.write(s);
+ fw.newLine();
+ }
+ }
+
+ if (fw != null) {
+ fw.close();
+ }
+
+ return list != null ? list : sw.toString();
+ }
+
+ // FIXME: expose API in runtime so type command doesn't have to duplicate the runtime
+ // command search strategy.
+ public boolean type(CommandSession session, String[] argv) throws Exception {
+ final String[] usage = {"type - show command type",
+ "Usage: type [OPTIONS] [name[:]]",
+ " -a --all show all matches",
+ " -? --help show help",
+ " -q --quiet don't print anything, just return status",
+ " -s --scope=NAME list all commands in named scope",
+ " -t --types show full java type names"};
+
+ Options opt = Options.compile(usage).parse(argv);
+ List<String> args = opt.args();
+
+ if (opt.isSet("help")) {
+ opt.usage(System.err);
+ return true;
+ }
+
+ boolean all = opt.isSet("all");
+
+ String optScope = null;
+ if (opt.isSet("scope")) {
+ optScope = opt.get("scope");
+ }
+
+ if (args.size() == 1) {
+ String arg = args.get(0);
+ if (arg.endsWith(":")) {
+ optScope = args.remove(0);
+ }
+ }
+
+ if (optScope != null || (args.isEmpty() && all)) {
+ Set<String> snames = new TreeSet<String>();
+
+ for (String sname : (getCommands(session))) {
+ if ((optScope == null) || sname.startsWith(optScope)) {
+ snames.add(sname);
+ }
+ }
+
+ for (String sname : snames) {
+ System.out.println(sname);
+ }
+
+ return true;
+ }
+
+ if (args.size() == 0) {
+ Map<String, Integer> scopes = new TreeMap<String, Integer>();
+
+ for (String sname : getCommands(session)) {
+ int colon = sname.indexOf(':');
+ String scope = sname.substring(0, colon);
+ Integer count = scopes.get(scope);
+ if (count == null) {
+ count = 0;
+ }
+ scopes.put(scope, ++count);
+ }
+
+ for (Entry<String, Integer> entry : scopes.entrySet()) {
+ System.out.println(entry.getKey() + ":" + entry.getValue());
+ }
+
+ return true;
+ }
+
+ final String name = args.get(0).toLowerCase();
+
+ final int colon = name.indexOf(':');
+ final String MAIN = "_main"; // FIXME: must match Reflective.java
+
+ StringBuilder buf = new StringBuilder();
+ Set<String> cmds = new LinkedHashSet<String>();
+
+ // get all commands
+ if ((colon != -1) || (session.get(name) != null)) {
+ cmds.add(name);
+ } else if (session.get(MAIN) != null) {
+ cmds.add(MAIN);
+ } else {
+ String path = session.get("SCOPE") != null ? session.get("SCOPE").toString()
+ : "*";
+
+ for (String s : path.split(":")) {
+ if (s.equals("*")) {
+ for (String sname : getCommands(session)) {
+ if (sname.endsWith(":" + name)) {
+ cmds.add(sname);
+ if (!all) {
+ break;
+ }
+ }
+ }
+ } else {
+ String sname = s + ":" + name;
+ if (session.get(sname) != null) {
+ cmds.add(sname);
+ if (!all) {
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ for (String key : cmds) {
+ Object target = session.get(key);
+ if (target == null) {
+ continue;
+ }
+
+ CharSequence source = getClosureSource(session, key);
+
+ if (source != null) {
+ buf.append(name);
+ buf.append(" is function {");
+ buf.append(source);
+ buf.append("}");
+ continue;
+ }
+
+ for (Method m : getMethods(session, key)) {
+ StringBuilder params = new StringBuilder();
+
+ for (Class<?> type : m.getParameterTypes()) {
+ if (params.length() > 0) {
+ params.append(", ");
+ }
+ params.append(type.getSimpleName());
+ }
+
+ String rtype = m.getReturnType().getSimpleName();
+
+ if (buf.length() > 0) {
+ buf.append("\n");
+ }
+
+ if (opt.isSet("types")) {
+ String cname = m.getDeclaringClass().getName();
+ buf.append(String.format("%s %s.%s(%s)", rtype, cname, m.getName(),
+ params));
+ } else {
+ buf.append(String.format("%s is %s %s(%s)", name, rtype, key, params));
+ }
+ }
+ }
+
+ if (buf.length() > 0) {
+ if (!opt.isSet("quiet")) {
+ System.out.println(buf);
+ }
+ return true;
+ }
+
+ if (!opt.isSet("quiet")) {
+ System.err.println("type: " + name + " not found.");
+ }
+
+ return false;
+ }
+
+ private boolean isClosure(Object target) {
+ return target.getClass().getSimpleName().equals("Closure");
+ }
+
+ private boolean isCommand(Object target) {
+ return target.getClass().getSimpleName().equals("CommandProxy");
+ }
+
+ private CharSequence getClosureSource(CommandSession session, String name)
+ throws Exception {
+ Object target = session.get(name);
+
+ if (target == null) {
+ return null;
+ }
+
+ if (!isClosure(target)) {
+ return null;
+ }
+
+ Field sourceField = target.getClass().getDeclaredField("source");
+ sourceField.setAccessible(true);
+ return (CharSequence) sourceField.get(target);
+ }
+
+ private List<Method> getMethods(CommandSession session, String scmd) throws Exception {
+ final int colon = scmd.indexOf(':');
+ final String function = colon == -1 ? scmd : scmd.substring(colon + 1);
+ final String name = KEYWORDS.contains(function) ? ("_" + function) : function;
+ final String get = "get" + function;
+ final String is = "is" + function;
+ final String set = "set" + function;
+ final String MAIN = "_main"; // FIXME: must match Reflective.java
+
+ Object target = session.get(scmd);
+ if (target == null) {
+ return null;
+ }
+
+ if (isClosure(target)) {
+ return null;
+ }
+
+ if (isCommand(target)) {
+ Method method = target.getClass().getMethod("getTarget", (Class[]) null);
+ method.setAccessible(true);
+ target = method.invoke(target, (Object[]) null);
+ }
+
+ ArrayList<Method> list = new ArrayList<Method>();
+ Class<?> tc = (target instanceof Class<?>) ? (Class<?>) target
+ : target.getClass();
+ Method[] methods = tc.getMethods();
+
+ for (Method m : methods) {
+ String mname = m.getName().toLowerCase();
+
+ if (mname.equals(name) || mname.equals(get) || mname.equals(set)
+ || mname.equals(is) || mname.equals(MAIN)) {
+ list.add(m);
+ }
+ }
+
+ return list;
+ }
+
+}
Added: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java (added)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java Mon Mar 21 16:53:06 2016
@@ -0,0 +1,243 @@
+/*
+ * 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.felix.gogo.jline;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+import java.util.Formatter;
+
+import org.apache.felix.service.command.Converter;
+import org.apache.felix.service.command.Function;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.startlevel.StartLevel;
+
+public class Converters implements Converter {
+ private final BundleContext context;
+
+ public Converters(BundleContext context) {
+ this.context = context;
+ }
+
+ private CharSequence print(Bundle bundle) {
+ // [ ID ] [STATE ] [ SL ] symname
+ StartLevel sl = null;
+ ServiceReference ref = context.getServiceReference(StartLevel.class.getName());
+ if (ref != null) {
+ sl = (StartLevel) context.getService(ref);
+ }
+
+ if (sl == null) {
+ return String.format("%5d|%-11s|%s (%s)", bundle.getBundleId(),
+ getState(bundle), bundle.getSymbolicName(), bundle.getVersion());
+ }
+
+ int level = sl.getBundleStartLevel(bundle);
+ context.ungetService(ref);
+
+ return String.format("%5d|%-11s|%5d|%s (%s)", bundle.getBundleId(),
+ getState(bundle), level, bundle.getSymbolicName(), bundle.getVersion());
+ }
+
+ private CharSequence print(ServiceReference ref) {
+ StringBuilder sb = new StringBuilder();
+ Formatter f = new Formatter(sb);
+
+ String spid = "";
+ Object pid = ref.getProperty("service.pid");
+ if (pid != null) {
+ spid = pid.toString();
+ }
+
+ f.format("%06d %3s %-40s %s", ref.getProperty("service.id"),
+ ref.getBundle().getBundleId(),
+ getShortNames((String[]) ref.getProperty("objectclass")), spid);
+ return sb;
+ }
+
+ private CharSequence getShortNames(String[] list) {
+ StringBuilder sb = new StringBuilder();
+ String del = "";
+ for (String s : list) {
+ sb.append(del + getShortName(s));
+ del = " | ";
+ }
+ return sb;
+ }
+
+ private CharSequence getShortName(String name) {
+ int n = name.lastIndexOf('.');
+ if (n < 0) {
+ n = 0;
+ } else {
+ n++;
+ }
+ return name.subSequence(n, name.length());
+ }
+
+ private String getState(Bundle bundle) {
+ switch (bundle.getState()) {
+ case Bundle.ACTIVE:
+ return "Active";
+
+ case Bundle.INSTALLED:
+ return "Installed";
+
+ case Bundle.RESOLVED:
+ return "Resolved";
+
+ case Bundle.STARTING:
+ return "Starting";
+
+ case Bundle.STOPPING:
+ return "Stopping";
+
+ case Bundle.UNINSTALLED:
+ return "Uninstalled ";
+ }
+ return null;
+ }
+
+ public Bundle bundle(Bundle i) {
+ return i;
+ }
+
+ public Object convert(Class<?> desiredType, final Object in) throws Exception {
+ if (desiredType == Bundle.class) {
+ return convertBundle(in);
+ }
+
+ if (desiredType == ServiceReference.class) {
+ return convertServiceReference(in);
+ }
+
+ if (desiredType == Class.class) {
+ try {
+ return Class.forName(in.toString());
+ } catch (ClassNotFoundException e) {
+ return null;
+ }
+ }
+
+ if (desiredType.isAssignableFrom(String.class) && in instanceof InputStream) {
+ return read(((InputStream) in));
+ }
+
+ if (in instanceof Function && desiredType.isInterface()
+ && desiredType.getDeclaredMethods().length == 1) {
+ return Proxy.newProxyInstance(desiredType.getClassLoader(),
+ new Class[]{desiredType}, new InvocationHandler() {
+ Function command = ((Function) in);
+
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+ return command.execute(null, Arrays.asList(args));
+ }
+ });
+ }
+
+ return null;
+ }
+
+ private Object convertServiceReference(Object in) throws InvalidSyntaxException {
+ String s = in.toString();
+ if (s.startsWith("(") && s.endsWith(")")) {
+ ServiceReference refs[] = context.getServiceReferences(null, String.format(
+ "(|(service.id=%s)(service.pid=%s))", in, in));
+ if (refs != null && refs.length > 0) {
+ return refs[0];
+ }
+ }
+
+ ServiceReference refs[] = context.getServiceReferences(null, String.format(
+ "(|(service.id=%s)(service.pid=%s))", in, in));
+ if (refs != null && refs.length > 0) {
+ return refs[0];
+ }
+ return null;
+ }
+
+ private Object convertBundle(Object in) {
+ String s = in.toString();
+ try {
+ long id = Long.parseLong(s);
+ return context.getBundle(id);
+ } catch (NumberFormatException nfe) {
+ // Ignore
+ }
+
+ Bundle bundles[] = context.getBundles();
+ for (Bundle b : bundles) {
+ if (b.getLocation().equals(s)) {
+ return b;
+ }
+
+ if (b.getSymbolicName().equals(s)) {
+ return b;
+ }
+ }
+
+ return null;
+ }
+
+ public CharSequence format(Object target, int level, Converter converter)
+ throws IOException {
+ if (level == INSPECT && target instanceof InputStream) {
+ return read(((InputStream) target));
+ }
+ if (level == LINE && target instanceof Bundle) {
+ return print((Bundle) target);
+ }
+ if (level == LINE && target instanceof ServiceReference) {
+ return print((ServiceReference) target);
+ }
+ if (level == PART && target instanceof Bundle) {
+ return ((Bundle) target).getSymbolicName();
+ }
+ if (level == PART && target instanceof ServiceReference) {
+ return getShortNames((String[]) ((ServiceReference) target).getProperty("objectclass"));
+ }
+ return null;
+ }
+
+ private CharSequence read(InputStream in) throws IOException {
+ int c;
+ StringBuffer sb = new StringBuffer();
+ while ((c = in.read()) > 0) {
+ if (c >= 32 && c <= 0x7F || c == '\n' || c == '\r') {
+ sb.append((char) c);
+ } else {
+ String s = Integer.toHexString(c).toUpperCase();
+ sb.append("\\");
+ if (s.length() < 1) {
+ sb.append(0);
+ }
+ sb.append(s);
+ }
+ }
+ return sb;
+ }
+
+}
Added: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java (added)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Highlighter.java Mon Mar 21 16:53:06 2016
@@ -0,0 +1,274 @@
+/*
+ * 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.felix.gogo.jline;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.felix.gogo.runtime.CommandSessionImpl;
+import org.apache.felix.gogo.runtime.EOFError;
+import org.apache.felix.gogo.runtime.Parser.Program;
+import org.apache.felix.gogo.runtime.Parser.Statement;
+import org.apache.felix.gogo.runtime.SyntaxError;
+import org.apache.felix.gogo.runtime.Token;
+import org.apache.felix.service.command.CommandSession;
+import org.apache.felix.service.command.Function;
+import org.jline.reader.LineReader;
+import org.jline.reader.LineReader.RegionType;
+import org.jline.reader.impl.DefaultHighlighter;
+import org.jline.utils.AttributedString;
+import org.jline.utils.AttributedStringBuilder;
+import org.jline.utils.AttributedStyle;
+import org.jline.utils.WCWidth;
+
+public class Highlighter extends DefaultHighlighter {
+
+ private final CommandSession session;
+
+ public Highlighter(CommandSession session) {
+ this.session = session;
+ }
+
+ public AttributedString highlight(LineReader reader, String buffer) {
+ try {
+ Program program = null;
+ List<Token> tokens = null;
+ List<Statement> statements = null;
+ String repaired = buffer + " ";
+ while (program == null) {
+ try {
+ org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(repaired);
+ program = parser.program();
+ tokens = parser.tokens();
+ statements = parser.statements();
+ } catch (EOFError e) {
+ repaired = repaired + e.repair();
+ }
+ }
+
+ int underlineStart = -1;
+ int underlineEnd = -1;
+ int negativeStart = -1;
+ int negativeEnd = -1;
+ String search = reader.getSearchTerm();
+ if (search != null && search.length() > 0) {
+ underlineStart = buffer.indexOf(search);
+ if (underlineStart >= 0) {
+ underlineEnd = underlineStart + search.length() - 1;
+ }
+ }
+ if (reader.getRegionActive() != RegionType.NONE) {
+ negativeStart = reader.getRegionMark();
+ negativeEnd = reader.getBuffer().cursor();
+ if (negativeStart > negativeEnd) {
+ int x = negativeEnd;
+ negativeEnd = negativeStart;
+ negativeStart = x;
+ }
+ if (reader.getRegionActive() == RegionType.LINE) {
+ while (negativeStart > 0 && reader.getBuffer().atChar(negativeStart - 1) != '\n') {
+ negativeStart--;
+ }
+ while (negativeEnd < reader.getBuffer().length() - 1 && reader.getBuffer().atChar(negativeEnd + 1) != '\n') {
+ negativeEnd++;
+ }
+ }
+ }
+
+ Type[] types = new Type[repaired.length()];
+
+ Arrays.fill(types, Type.Unknown);
+
+ int cur = 0;
+ for (Token token : tokens) {
+ // We're on the repair side, so exit now
+ if (token.start() >= buffer.length()) {
+ break;
+ }
+ if (token.start() > cur) {
+// ansi.a(buffer.substring(cur, token.start()));
+ cur = token.start();
+ }
+ // Find corresponding statement
+ Statement statement = null;
+ for (int i = statements.size() - 1; i >= 0; i--) {
+ Statement s = statements.get(i);
+ if (s.start() <= cur && cur < s.start() + s.length()) {
+ statement = s;
+ break;
+ }
+ }
+
+ // Reserved tokens
+ Type type = Type.Unknown;
+ if (Token.eq(token, "{")
+ || Token.eq(token, "}")
+ || Token.eq(token, "(")
+ || Token.eq(token, ")")
+ || Token.eq(token, "[")
+ || Token.eq(token, "]")
+ || Token.eq(token, "|")
+ || Token.eq(token, ";")
+ || Token.eq(token, "=")) {
+ type = Type.Reserved;
+ } else if (token.charAt(0) == '\'' || token.charAt(0) == '"') {
+ type = Type.String;
+ } else if (token.toString().matches("^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$")) {
+ type = Type.Number;
+ } else if (token.charAt(0) == '$') {
+ type = Type.Variable;
+ } else if (((Set) session.get(CommandSessionImpl.CONSTANTS)).contains(token.toString())
+ || Token.eq(token, "null") || Token.eq(token, "false") || Token.eq(token, "true")) {
+ type = Type.Constant;
+ } else {
+ boolean isFirst = statement != null && statement.tokens().size() > 0
+ && token == statement.tokens().get(0);
+ boolean isThirdWithNext = statement != null && statement.tokens().size() > 3
+ && token == statement.tokens().get(2);
+ boolean isAssign = statement != null && statement.tokens().size() > 1
+ && Token.eq(statement.tokens().get(1), "=");
+ if (isFirst && isAssign) {
+ type = Type.VariableName;
+ }
+ if (isFirst && !isAssign || isAssign && isThirdWithNext) {
+ Object v = session.get(Shell.resolve(session, token.toString()));
+ type = (v instanceof Function) ? Type.Function : Type.BadFunction;
+ }
+ }
+ Arrays.fill(types, token.start(), Math.min(token.start() + token.length(), types.length), type);
+ /*
+ String valid;
+ if (token.start() + token.length() <= buffer.length()) {
+ valid = token.toString();
+ } else {
+ valid = token.subSequence(0, buffer.length() - token.start()).toString();
+ }
+ switch (type) {
+ case Reserved:
+ ansi.fg(Color.MAGENTA).a(valid).fg(Color.DEFAULT);
+ break;
+ case String:
+ case Number:
+ case Constant:
+ ansi.fg(Color.GREEN).a(valid).fg(Color.DEFAULT);
+ break;
+ case Variable:
+ case VariableName:
+ ansi.fg(Color.CYAN).a(valid).fg(Color.DEFAULT);
+ break;
+ case Function:
+ ansi.fg(Color.BLUE).a(valid).fg(Color.DEFAULT);
+ break;
+ case BadFunction:
+ ansi.fg(Color.RED).a(valid).fg(Color.DEFAULT);
+ break;
+ default:
+ ansi.a(valid);
+ break;
+ }
+ */
+ cur = Math.min(token.start() + token.length(), buffer.length());
+ }
+
+ if (buffer.length() < repaired.length()) {
+ Arrays.fill(types, buffer.length(), repaired.length(), Type.Repair);
+ }
+
+ AttributedStringBuilder sb = new AttributedStringBuilder();
+ Type prevType = Type.Unknown;
+ for (int i = 0; i < repaired.length(); i++) {
+ if (i == underlineStart) {
+ sb.style(sb.style().underline());
+ }
+ if (i == negativeStart) {
+ sb.style(sb.style().inverse());
+ }
+ if (types[i] != prevType) {
+ prevType = types[i];
+ switch (prevType) {
+ case Reserved:
+ sb.style(sb.style().foreground(AttributedStyle.MAGENTA));
+ break;
+ case String:
+ case Number:
+ case Constant:
+ sb.style(sb.style().foreground(AttributedStyle.GREEN));
+ break;
+ case Variable:
+ case VariableName:
+ sb.style(sb.style().foreground(AttributedStyle.CYAN));
+ break;
+ case Function:
+ sb.style(sb.style().foreground(AttributedStyle.BLUE));
+ break;
+ case BadFunction:
+ sb.style(sb.style().foreground(AttributedStyle.RED));
+ break;
+ case Repair:
+ sb.style(sb.style().foreground(AttributedStyle.BLACK + AttributedStyle.BRIGHT));
+ break;
+ default:
+ sb.style(sb.style().foregroundDefault());
+ break;
+ }
+ }
+ char c = repaired.charAt(i);
+ if (c == '\t' || c == '\n') {
+ sb.append(c);
+ } else if (c < 32) {
+ sb.style(sb.style().inverseNeg())
+ .append('^')
+ .append((char) (c + '@'))
+ .style(sb.style().inverseNeg());
+ } else {
+ int w = WCWidth.wcwidth(c);
+ if (w > 0) {
+ sb.append(c);
+ }
+ }
+ if (i == underlineEnd) {
+ sb.style(sb.style().underlineOff());
+ }
+ if (i == negativeEnd) {
+ sb.style(sb.style().inverseOff());
+ }
+ }
+
+ return sb.toAttributedString();
+ } catch (SyntaxError e) {
+ return super.highlight(reader, buffer);
+ }
+ }
+
+ enum Type {
+ Reserved,
+ String,
+ Number,
+ Variable,
+ VariableName,
+ Function,
+ BadFunction,
+ Value,
+ Constant,
+ Unknown,
+ Repair
+ }
+
+}
Added: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCommands.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCommands.java?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCommands.java (added)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCommands.java Mon Mar 21 16:53:06 2016
@@ -0,0 +1,266 @@
+/*
+ * 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.felix.gogo.jline;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.felix.gogo.jline.Shell.Context;
+import org.apache.felix.gogo.runtime.CommandSessionImpl;
+import org.apache.felix.service.command.CommandProcessor;
+import org.apache.felix.service.command.CommandSession;
+import org.apache.felix.service.command.Function;
+import org.jline.builtins.Completers.DirectoriesCompleter;
+import org.jline.builtins.Completers.FilesCompleter;
+import org.jline.builtins.Options;
+import org.jline.reader.Candidate;
+import org.jline.reader.LineReader;
+import org.jline.reader.ParsedLine;
+import org.jline.reader.Widget;
+import org.jline.terminal.Attributes;
+import org.jline.terminal.Terminal;
+import org.jline.utils.InfoCmp.Capability;
+
+public class JLineCommands {
+
+ public static final String[] functions = {
+ "keymap", "setopt", "unsetopt", "complete", "history",
+ "less", "watch", "nano", "widget", "tmux",
+ "__files", "__directories", "__usage_completion"
+ };
+
+ private final CommandProcessor processor;
+
+ private final org.jline.builtins.Commands commands = new org.jline.builtins.Commands();
+
+ public JLineCommands(CommandProcessor processor) {
+ this.processor = processor;
+ }
+
+ public void tmux(final CommandSession session, String[] argv) throws Exception {
+ commands.tmux(Shell.getTerminal(session),
+ System.out, System.err,
+ () -> session.get(".tmux"),
+ t -> session.put(".tmux", t),
+ c -> startShell(session, c), argv);
+ }
+
+ private void startShell(CommandSession session, Terminal terminal) {
+ new Thread(() -> runShell(session, terminal), terminal.getName() + " shell").start();
+ }
+
+ private void runShell(CommandSession session, Terminal terminal) {
+ InputStream in = terminal.input();
+ PrintStream out = new PrintStream(terminal.output());
+ CommandSession newSession = processor.createSession(in, out, out);
+ newSession.put(Shell.VAR_TERMINAL, terminal);
+ newSession.put(".tmux", session.get(".tmux"));
+ Context context = new Context() {
+ public String getProperty(String name) {
+ return System.getProperty(name);
+ }
+ public void exit() throws Exception {
+ terminal.close();
+ }
+ };
+ try {
+ new Shell(context, processor, terminal).gosh(newSession, new String[]{"--login"});
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ terminal.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void nano(final CommandSession session, String[] argv) throws Exception {
+ commands.nano(Shell.getTerminal(session), System.out, System.err, Shell.cwd(session), argv);
+ }
+
+ public void watch(final CommandSession session, String[] argv) throws IOException, InterruptedException {
+ final String[] usage = {
+ "watch - watches & refreshes the output of a command",
+ "Usage: watch [OPTIONS] COMMAND",
+ " -? --help Show help",
+ " -n --interval Interval between executions of the command in seconds",
+ " -a --append The output should be appended but not clear the console"
+ };
+ final Options opt = Options.compile(usage).parse(argv);
+ if (opt.isSet("help")) {
+ opt.usage(System.err);
+ return;
+ }
+ List<String> args = opt.args();
+ if (args.isEmpty()) {
+ System.err.println("Argument expected");
+ return;
+ }
+ ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+ final Terminal terminal = Shell.getTerminal(session);
+ final CommandProcessor processor = Shell.getProcessor(session);
+ try {
+ int interval = 1;
+ if (opt.isSet("interval")) {
+ interval = opt.getNumber("interval");
+ if (interval < 1) {
+ interval = 1;
+ }
+ }
+ final String cmd = String.join(" ", args);
+ Runnable task = () -> {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ InputStream is = new ByteArrayInputStream(new byte[0]);
+ PrintStream os = new PrintStream(baos);
+ if (opt.isSet("append") || !terminal.puts(Capability.clear_screen)) {
+ terminal.writer().println();
+ }
+ try {
+ CommandSession ns = processor.createSession(is, os, os);
+ Set<String> vars = Shell.getCommands(session);
+ for (String n : vars) {
+ ns.put(n, session.get(n));
+ }
+ ns.execute(cmd);
+ } catch (Throwable t) {
+ t.printStackTrace(os);
+ }
+ os.flush();
+ terminal.writer().print(baos.toString());
+ terminal.writer().flush();
+ };
+ executorService.scheduleAtFixedRate(task, 0, interval, TimeUnit.SECONDS);
+ Attributes attr = terminal.enterRawMode();
+ terminal.reader().read();
+ terminal.setAttributes(attr);
+ } finally {
+ executorService.shutdownNow();
+ }
+ }
+
+ public void less(CommandSession session, String[] argv) throws IOException, InterruptedException {
+ commands.less(Shell.getTerminal(session), System.out, System.err, Shell.cwd(session), argv);
+ }
+
+ public void history(CommandSession session, String[] argv) throws IOException {
+ commands.history(Shell.getReader(session), System.out, System.err, argv);
+ }
+
+ public void complete(CommandSession session, String[] argv) {
+ commands.complete(Shell.getReader(session), System.out, System.err, Shell.getCompletions(session), argv);
+ }
+
+ public void widget(final CommandSession session, String[] argv) throws Exception {
+ java.util.function.Function<String, Widget> creator = func -> () -> {
+ try {
+ session.execute(func);
+ } catch (Exception e) {
+ // TODO: log exception ?
+ return false;
+ }
+ return true;
+ };
+ commands.widget(Shell.getReader(session), System.out, System.err, creator, argv);
+ }
+
+ public void keymap(CommandSession session, String[] argv) {
+ commands.keymap(Shell.getReader(session), System.out, System.err, argv);
+ }
+
+ public void setopt(CommandSession session, String[] argv) {
+ commands.setopt(Shell.getReader(session), System.out, System.err, argv);
+ }
+
+ public void unsetopt(CommandSession session, String[] argv) {
+ commands.unsetopt(Shell.getReader(session), System.out, System.err, argv);
+ }
+
+ public List<Candidate> __files(CommandSession session) {
+ ParsedLine line = Shell.getParsedLine(session);
+ LineReader reader = Shell.getReader(session);
+ List<Candidate> candidates = new ArrayList<>();
+ new FilesCompleter(new File(Shell.cwd(session))).complete(reader, line, candidates);
+ return candidates;
+ }
+
+ public List<Candidate> __directories(CommandSession session) {
+ ParsedLine line = Shell.getParsedLine(session);
+ LineReader reader = Shell.getReader(session);
+ List<Candidate> candidates = new ArrayList<>();
+ new DirectoriesCompleter(new File(Shell.cwd(session))).complete(reader, line, candidates);
+ return candidates;
+ }
+
+ public void __usage_completion(CommandSession session, String command) throws Exception {
+ Object func = session.get(command.contains(":") ? command : "*:" + command);
+ if (func instanceof Function) {
+ ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ByteArrayOutputStream baes = new ByteArrayOutputStream();
+ CommandSession ts = ((CommandSessionImpl) session).processor().createSession(bais, new PrintStream(baos), new PrintStream(baes));
+ ts.execute(command + " --help");
+
+ String regex = "(?x)\\s*" + "(?:-([^-]))?" + // 1: short-opt-1
+ "(?:,?\\s*-(\\w))?" + // 2: short-opt-2
+ "(?:,?\\s*--(\\w[\\w-]*)(=\\w+)?)?" + // 3: long-opt-1 and 4:arg-1
+ "(?:,?\\s*--(\\w[\\w-]*))?" + // 5: long-opt-2
+ ".*?(?:\\(default=(.*)\\))?\\s*" + // 6: default
+ "(.*)"; // 7: description
+ Pattern pattern = Pattern.compile(regex);
+ for (String l : baes.toString().split("\n")) {
+ Matcher matcher = pattern.matcher(l);
+ if (matcher.matches()) {
+ List<String> args = new ArrayList<>();
+ if (matcher.group(1) != null) {
+ args.add("--short-option");
+ args.add(matcher.group(1));
+ }
+ if (matcher.group(3) != null) {
+ args.add("--long-option");
+ args.add(matcher.group(1));
+ }
+ if (matcher.group(4) != null) {
+ args.add("--argument");
+ args.add("");
+ }
+ if (matcher.group(7) != null) {
+ args.add("--description");
+ args.add(matcher.group(7));
+ }
+ complete(session, args.toArray(new String[args.size()]));
+ }
+ }
+ }
+ }
+
+}
Added: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCompletionEnvironment.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCompletionEnvironment.java?rev=1735995&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCompletionEnvironment.java (added)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/JLineCompletionEnvironment.java Mon Mar 21 16:53:06 2016
@@ -0,0 +1,60 @@
+/*
+ * 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.felix.gogo.jline;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.felix.service.command.CommandSession;
+import org.jline.builtins.Completers.CompletionData;
+import org.jline.builtins.Completers.CompletionEnvironment;
+import org.jline.reader.LineReader;
+import org.jline.reader.ParsedLine;
+
+public class JLineCompletionEnvironment implements CompletionEnvironment {
+
+ private final CommandSession session;
+
+ public JLineCompletionEnvironment(CommandSession session) {
+ this.session = session;
+ }
+
+ public Map<String, List<CompletionData>> getCompletions() {
+ return Shell.getCompletions(session);
+ }
+
+ public Set<String> getCommands() {
+ return Shell.getCommands(session);
+ }
+
+ public String resolveCommand(String command) {
+ return Shell.resolve(session, command);
+ }
+
+ public String commandName(String command) {
+ int idx = command.indexOf(':');
+ return idx >= 0 ? command.substring(idx + 1) : command;
+ }
+
+ public Object evaluate(LineReader reader, ParsedLine line, String func) throws Exception {
+ session.put(Shell.VAR_COMMAND_LINE, line);
+ return session.execute(func);
+ }
+}