A patch from René Scheibe "Groovy shell." (https://issues.apache.org/jira/browse/OFBIZ-3954) - OFBIZ-3954
A Groovysh container to connect to. Just do a 'telnet localhost 9991' and you have a fully fledged groovysh. This is a Groovy equivalent to the already existing BeanShell container. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1062476 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -68,6 +68,7 @@ ofbiz/trunk/framework/base/lib/j2eespecs/tomcat-6.0.30-servlet-api.jar
|
||||
ofbiz/trunk/framework/base/lib/scripting/bsf-2.4.0.jar
|
||||
ofbiz/trunk/framework/base/lib/scripting/jakarta-oro-2.0.8.jar
|
||||
ofbiz/trunk/framework/base/lib/scripting/groovy-all-1.7.4.jar
|
||||
ofbiz/trunk/framework/base/lib/scripting/jansi-1.2.1.jar
|
||||
ofbiz/trunk/framework/base/lib/xmlrpc-client-3.1.2.jar
|
||||
ofbiz/trunk/framework/base/lib/xmlrpc-common-3.1.2.jar
|
||||
ofbiz/trunk/framework/base/lib/xmlrpc-server-3.1.2.jar
|
||||
@@ -498,6 +499,7 @@ ofbiz/trunk/framework/base/lib/scripting/asm-analysis-3.2.jar
|
||||
ofbiz/trunk/framework/base/lib/scripting/asm-tree-3.2.jar
|
||||
ofbiz/trunk/framework/base/lib/scripting/asm-util-3.2.jar
|
||||
ofbiz/trunk/framework/base/lib/scripting/janino-2.5.15.jar
|
||||
ofbiz/trunk/framework/base/lib/scripting/jline-0.9.94.jar
|
||||
ofbiz/trunk/specialpurpose/ldap/lib/cas-server-core-3.3.jar
|
||||
ofbiz/trunk/specialpurpose/pos/lib/looks-2.0.2.jar
|
||||
|
||||
|
||||
@@ -339,3 +339,9 @@ This product includes software developed by the javacc team
|
||||
|
||||
This product includes software developed by the Indiana University
|
||||
Extreme! Lab (http://www.extreme.indiana.edu/).
|
||||
=========================================================================
|
||||
== jline Notice ==
|
||||
=========================================================================
|
||||
|
||||
This product includes software developed by Marc Prud'hommeaux
|
||||
<mwp1@cornell.edu>
|
||||
|
||||
@@ -268,4 +268,9 @@ under the License.
|
||||
<property name="telnet-port" value="9990"/>
|
||||
<property name="app-name" value="OFBiz"/>
|
||||
</container>
|
||||
|
||||
<!-- load Groovy remote telnet server -->
|
||||
<container name="groovyshell-container" class="org.ofbiz.base.container.GroovyShellContainer">
|
||||
<property name="telnet-port" value="9991"/>
|
||||
</container>
|
||||
</ofbiz-containers>
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,74 @@
|
||||
/*******************************************************************************
|
||||
* 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.ofbiz.base.container;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import org.ofbiz.base.container.ContainerConfig.Container.Property;
|
||||
import org.ofbiz.base.container.groovy.GroovyService;
|
||||
import org.ofbiz.base.container.groovy.GroovyShellService;
|
||||
|
||||
public class GroovyShellContainer implements Container {
|
||||
|
||||
private static final Logger log = Logger.getLogger(GroovyShellContainer.class);
|
||||
|
||||
private String configFileLocation = null;
|
||||
private GroovyService gsh = null;
|
||||
|
||||
/**
|
||||
* @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public void init(String[] args, String configFile) {
|
||||
configFileLocation = configFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.ofbiz.base.container.Container#start()
|
||||
*/
|
||||
@Override
|
||||
public boolean start() throws ContainerException {
|
||||
int port = getTelnetPort();
|
||||
gsh = new GroovyShellService(port);
|
||||
gsh.launchInBackground();
|
||||
|
||||
log.info("Started Groovy telnet service on port [" + port + "].");
|
||||
log.info("NOTICE: The Groovy service port is not secure. Please protect it.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.ofbiz.base.container.Container#stop()
|
||||
*/
|
||||
@Override
|
||||
public void stop() throws ContainerException {
|
||||
gsh = null;
|
||||
}
|
||||
|
||||
private int getTelnetPort() throws ContainerException {
|
||||
ContainerConfig.Container config = ContainerConfig.getContainer("groovyshell-container", configFileLocation);
|
||||
Property telnetPort = config.getProperty("telnet-port");
|
||||
try {
|
||||
return Integer.parseInt(telnetPort.value);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ContainerException("Invalid telnet port [" + telnetPort.value + "] defined in container configuration.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2007 Bruce Fancher
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is a modified version of the original one.
|
||||
*/
|
||||
|
||||
package org.ofbiz.base.container.groovy;
|
||||
|
||||
import groovy.lang.Binding;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public abstract class GroovyService {
|
||||
|
||||
protected Logger log = Logger.getLogger(getClass());
|
||||
|
||||
private Map<String, Object> bindings;
|
||||
private boolean launchAtStart;
|
||||
private Thread serverThread;
|
||||
|
||||
public GroovyService() {
|
||||
}
|
||||
|
||||
public GroovyService(Map<String, Object> bindings) {
|
||||
this.bindings = bindings;
|
||||
}
|
||||
|
||||
public void launchInBackground() {
|
||||
serverThread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
launch();
|
||||
} catch (Exception e) {
|
||||
log.error("Could not launch the Groovy service.", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
serverThread.setDaemon(true);
|
||||
serverThread.start();
|
||||
}
|
||||
|
||||
public abstract void launch();
|
||||
|
||||
protected Binding createBinding() {
|
||||
return new Binding(bindings);
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
if (launchAtStart) {
|
||||
launchInBackground();
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {}
|
||||
|
||||
public void setBindings(final Map<String, Object> bindings) {
|
||||
this.bindings = bindings;
|
||||
}
|
||||
|
||||
public boolean isLaunchAtStart() {
|
||||
return launchAtStart;
|
||||
}
|
||||
|
||||
public void setLaunchAtStart(boolean launchAtStart) {
|
||||
this.launchAtStart = launchAtStart;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2007 Bruce Fancher
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is a modified version of the original one.
|
||||
*/
|
||||
|
||||
package org.ofbiz.base.container.groovy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GroovyShellService extends GroovyService {
|
||||
|
||||
private ServerSocket serverSocket;
|
||||
private int port;
|
||||
private List<Socket> clientSockets = new ArrayList<Socket>();
|
||||
|
||||
public GroovyShellService() {
|
||||
super();
|
||||
}
|
||||
|
||||
public GroovyShellService(int port) {
|
||||
super();
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public GroovyShellService(Map<String, Object> bindings, int port) {
|
||||
super(bindings);
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void launch() {
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
log.info("Opened server socket: " + serverSocket);
|
||||
|
||||
acceptClientConnections();
|
||||
} catch (IOException e) {
|
||||
log.error("Could not open server socket.", e);
|
||||
} finally {
|
||||
closeServerSocket();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
closeServerSocket();
|
||||
|
||||
for (Socket clientSocket : clientSockets) {
|
||||
closeClientSocket(clientSocket);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
private void acceptClientConnections() {
|
||||
while (true) {
|
||||
Socket clientSocket = null;
|
||||
try {
|
||||
clientSocket = serverSocket.accept();
|
||||
log.info("Opened client socket: " + clientSocket);
|
||||
} catch (IOException e) {
|
||||
log.error("Could not open client socket.", e);
|
||||
continue;
|
||||
}
|
||||
|
||||
clientSockets.add(clientSocket);
|
||||
GroovyShellThread clientThread = new GroovyShellThread(clientSocket, createBinding());
|
||||
clientThread.start();
|
||||
}
|
||||
}
|
||||
|
||||
private void closeServerSocket() {
|
||||
try {
|
||||
if (serverSocket != null) {
|
||||
log.info("Closing server socket: " + serverSocket);
|
||||
serverSocket.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Could not close server socket: " + serverSocket, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void closeClientSocket(Socket socket) {
|
||||
try {
|
||||
if (socket != null) {
|
||||
log.info("Closing client socket: " + socket);
|
||||
socket.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Could not close client socket: " + socket, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2007 Bruce Fancher
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is a modified version of the original one.
|
||||
*/
|
||||
|
||||
package org.ofbiz.base.container.groovy;
|
||||
|
||||
import groovy.lang.Binding;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.codehaus.groovy.tools.shell.Groovysh;
|
||||
import org.codehaus.groovy.tools.shell.IO;
|
||||
|
||||
public class GroovyShellThread extends Thread {
|
||||
|
||||
private static final Logger log = Logger.getLogger(GroovyShellThread.class);
|
||||
|
||||
private Socket socket;
|
||||
private Binding binding;
|
||||
|
||||
public GroovyShellThread(Socket socket, Binding binding) {
|
||||
super();
|
||||
this.socket = socket;
|
||||
this.binding = binding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
PrintStream out = null;
|
||||
InputStream in = null;
|
||||
|
||||
try {
|
||||
in = socket.getInputStream();
|
||||
out = new PrintStream(socket.getOutputStream());
|
||||
binding.setVariable("out", out);
|
||||
|
||||
final IO io = new IO(in, out, out);
|
||||
final Groovysh gsh = new Groovysh(binding, io);
|
||||
gsh.run();
|
||||
} catch (Exception e) {
|
||||
log.error("Error running the Groovy shell.", e);
|
||||
} finally {
|
||||
try { if (out != null) out.close(); } catch (Exception e) {}
|
||||
try { if (in != null) in.close(); } catch (Exception e) {}
|
||||
try { if (socket != null) socket.close(); } catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user