diff --git a/LICENSE b/LICENSE index d812c1b4e0..ebbdad835a 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/NOTICE b/NOTICE index b65173f30e..21e9da62a9 100644 --- a/NOTICE +++ b/NOTICE @@ -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 + diff --git a/framework/base/config/ofbiz-containers.xml b/framework/base/config/ofbiz-containers.xml index d12125ab0c..0fd8ba4d35 100644 --- a/framework/base/config/ofbiz-containers.xml +++ b/framework/base/config/ofbiz-containers.xml @@ -268,4 +268,9 @@ under the License. + + + + + diff --git a/framework/base/lib/scripting/jansi-1.2.1.jar b/framework/base/lib/scripting/jansi-1.2.1.jar new file mode 100644 index 0000000000..d1094e3bdf Binary files /dev/null and b/framework/base/lib/scripting/jansi-1.2.1.jar differ diff --git a/framework/base/lib/scripting/jline-0.9.94.jar b/framework/base/lib/scripting/jline-0.9.94.jar new file mode 100644 index 0000000000..dafca7c46e Binary files /dev/null and b/framework/base/lib/scripting/jline-0.9.94.jar differ diff --git a/framework/base/src/org/ofbiz/base/container/GroovyShellContainer.java b/framework/base/src/org/ofbiz/base/container/GroovyShellContainer.java new file mode 100644 index 0000000000..f984d3809c --- /dev/null +++ b/framework/base/src/org/ofbiz/base/container/GroovyShellContainer.java @@ -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."); + } + } +} diff --git a/framework/base/src/org/ofbiz/base/container/groovy/GroovyService.java b/framework/base/src/org/ofbiz/base/container/groovy/GroovyService.java new file mode 100644 index 0000000000..d7bb685244 --- /dev/null +++ b/framework/base/src/org/ofbiz/base/container/groovy/GroovyService.java @@ -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 bindings; + private boolean launchAtStart; + private Thread serverThread; + + public GroovyService() { + } + + public GroovyService(Map 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 bindings) { + this.bindings = bindings; + } + + public boolean isLaunchAtStart() { + return launchAtStart; + } + + public void setLaunchAtStart(boolean launchAtStart) { + this.launchAtStart = launchAtStart; + } +} diff --git a/framework/base/src/org/ofbiz/base/container/groovy/GroovyShellService.java b/framework/base/src/org/ofbiz/base/container/groovy/GroovyShellService.java new file mode 100644 index 0000000000..bb6876d319 --- /dev/null +++ b/framework/base/src/org/ofbiz/base/container/groovy/GroovyShellService.java @@ -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 clientSockets = new ArrayList(); + + public GroovyShellService() { + super(); + } + + public GroovyShellService(int port) { + super(); + this.port = port; + } + + public GroovyShellService(Map 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); + } + } +} diff --git a/framework/base/src/org/ofbiz/base/container/groovy/GroovyShellThread.java b/framework/base/src/org/ofbiz/base/container/groovy/GroovyShellThread.java new file mode 100644 index 0000000000..c39dc24651 --- /dev/null +++ b/framework/base/src/org/ofbiz/base/container/groovy/GroovyShellThread.java @@ -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; + } +}