From dbfb82aa95c5262cb8769302d3120907e17e6d6b Mon Sep 17 00:00:00 2001 From: jdewinne Date: Tue, 17 May 2016 15:06:46 -0700 Subject: [PATCH] Creating draft release --- .gitignore | 1 + README.md | 2 +- build.gradle | 11 + .../caucho/quercus/StringBuilderStream.java | 37 ++ .../java/com/caucho/quercus/XLRQuercus.java | 62 ++++ .../java/net/collegeman/phpinjava/PHP.java | 340 ++++++++++++++++++ .../net/collegeman/phpinjava/PHPObject.java | 83 +++++ src/main/resources/ipsw/wzRequest.py | 7 - src/main/resources/synthetic.xml | 18 +- src/main/resources/xlrelease/phpRequest.py | 16 + 10 files changed, 563 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/caucho/quercus/StringBuilderStream.java create mode 100644 src/main/java/com/caucho/quercus/XLRQuercus.java create mode 100644 src/main/java/net/collegeman/phpinjava/PHP.java create mode 100644 src/main/java/net/collegeman/phpinjava/PHPObject.java delete mode 100644 src/main/resources/ipsw/wzRequest.py create mode 100644 src/main/resources/xlrelease/phpRequest.py diff --git a/.gitignore b/.gitignore index e0d53d8..b414509 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .gradle .idea build +src/main/resources/ispw/cmd diff --git a/README.md b/README.md index 6b91419..fe7e1cd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Preface # -This document describes the functionality provided by the xlr-ipsw-plugin. +This document describes the functionality provided by the xlr-ispw-plugin. See the **[XL Release Documentation](https://docs.xebialabs.com/xl-release/index.html)** for background information on XL Release and release concepts. diff --git a/build.gradle b/build.gradle index 67e7433..7b25bc9 100644 --- a/build.gradle +++ b/build.gradle @@ -9,6 +9,17 @@ apply plugin: 'maven' version="1.0.0" +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + compile 'com.caucho:quercus:4.0.45' + compile 'org.apache.commons:commons-lang3:3.4' +} + + license { header rootProject.file('src/main/license/xebialabs_community.license') strictCheck true diff --git a/src/main/java/com/caucho/quercus/StringBuilderStream.java b/src/main/java/com/caucho/quercus/StringBuilderStream.java new file mode 100644 index 0000000..fd7ccd4 --- /dev/null +++ b/src/main/java/com/caucho/quercus/StringBuilderStream.java @@ -0,0 +1,37 @@ +package com.caucho.quercus; + +import com.caucho.vfs.StreamImpl; + +import java.io.IOException; + +public class StringBuilderStream extends StreamImpl { + private static StringBuilderStream _builder; + public StringBuilder output; + + private StringBuilderStream() { + } + + public static com.caucho.quercus.StringBuilderStream create() { + if(_builder == null) { + _builder = new StringBuilderStream(); + _builder.output = new StringBuilder(); + } + + return _builder; + } + + public boolean canWrite() { + return true; + } + + public void write(byte[] buf, int offset, int length, boolean isEnd) throws IOException { + String text2 = new String(buf, "ISO-8859-1"); + output.append(text2.toCharArray(),offset,length); + } + + @Override + public String toString() { + return output.toString(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/caucho/quercus/XLRQuercus.java b/src/main/java/com/caucho/quercus/XLRQuercus.java new file mode 100644 index 0000000..8a9d828 --- /dev/null +++ b/src/main/java/com/caucho/quercus/XLRQuercus.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 1998-2012 Caucho Technology -- all rights reserved + * + * This file is part of Resin(R) Open Source + * + * Each copy or derived work must preserve the copyright notice and this + * notice unmodified. + * + * Resin Open Source is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Resin Open Source is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty + * of NON-INFRINGEMENT. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with Resin Open Source; if not, write to the + * + * Free Software Foundation, Inc. + * 59 Temple Place, Suite 330 + * Boston, MA 02111-1307 USA + * + * @author Nam Nguyen + */ + +package com.caucho.quercus; + +import com.caucho.quercus.env.CliEnv; +import com.caucho.quercus.env.Env; +import com.caucho.quercus.page.QuercusPage; +import com.caucho.quercus.servlet.api.QuercusHttpServletRequest; +import com.caucho.quercus.servlet.api.QuercusHttpServletResponse; +import com.caucho.vfs.WriteStream; + +public class XLRQuercus extends Quercus +{ + @Override + public Env createEnv(QuercusPage page, + WriteStream out, + QuercusHttpServletRequest request, + QuercusHttpServletResponse response) + { + return new CliEnv(this, page, out, getArgv()); + } + + public void processArguments(String [] args) { + super.parseArgs(args); + + } + + /** + * Hard-coded to true for CLI according to php.net. + */ + @Override + public boolean isRegisterArgv() { + return true; + } +} \ No newline at end of file diff --git a/src/main/java/net/collegeman/phpinjava/PHP.java b/src/main/java/net/collegeman/phpinjava/PHP.java new file mode 100644 index 0000000..8f0efba --- /dev/null +++ b/src/main/java/net/collegeman/phpinjava/PHP.java @@ -0,0 +1,340 @@ +package net.collegeman.phpinjava; + +/** + * PHP-in-Java PHP wrapper for Java and Groovy. + * Copyright (C) 2009-2010 Collegeman.net, LLC. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +import com.caucho.quercus.*; +import com.caucho.quercus.env.ConstStringValue; +import com.caucho.quercus.env.Env; +import com.caucho.quercus.env.QuercusClass; +import com.caucho.quercus.env.Value; +import com.caucho.quercus.page.QuercusPage; +import com.caucho.quercus.servlet.api.QuercusHttpServletRequest; +import com.caucho.quercus.servlet.api.QuercusHttpServletResponse; +import com.caucho.vfs.FilePath; +import com.caucho.vfs.StringStream; +import com.caucho.vfs.WriteStream; +import org.apache.commons.lang3.ArrayUtils; + +import java.io.*; +import java.net.URL; +import java.net.URLConnection; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + *

Instances of this class wrap one or more PHP scripts, making it possible for + * those scripts to be compiled into memory and

+ * + *

Also, once instantiated, the PHP object can be used to compile and cache + * arbitrary snippets of PHP into a single PHP script.

+ *

Quercus and Resin versus PHP-in-Java

+ *

Our PHP-in-Java library is built on top of Quercus.

+ *

Quercus is a PHP interpreter written in Java, distributed by Caucho. + * Quercus is embedded inside a Java EE compliant application server called Resin. Resin gives + * you the flexibility of writing PHP applications that have access to the Java ecosystem, and is + * many times more efficient than running PHP in Apache.

+ *

If you're wanting to run large-scale PHP applications on Java, you need to take a look at Resin. + * But if you're just looking to make PHP libraries like GeSHi + * available to your Java and Groovy code, our PHP-in-Java library is for you.

+ *

Using PHP-in-Java

+ *

For usage instructions, please refer to README on our Github project.

+ * @author Aaron Collegeman aaron@collegeman.net + */ +public class PHP { + + private static final Logger log = Logger.getLogger(PHP.class.getName()); + private static XLRQuercus xlrQuercus; + + private synchronized XLRQuercus getQuercus() { + if (xlrQuercus == null) { + xlrQuercus = new XLRQuercus(); + xlrQuercus.init(); + xlrQuercus.start(); + } + return xlrQuercus; + } + + /** + * Initialize a PHP wrapper with either an intial PHP script or a local directory. + *

url can take one of several forms:

+ * + * @param url An initial PHP script to load or a local directory + */ + public PHP(String url, String logFileName, String ... args) throws Exception { + this(url, PHP.class.getClassLoader(), logFileName, args); + } + + private QuercusPage main; + + /** + * Create an empty instance of PHP. This instance can be used to execute arbitrary + * snippets of PHP. But if you're looking to load a PHP library and execute snippets against that, + * best to use one of the other constructors + */ + public PHP() {} + + + /** + * Initialize a PHP wrapper with a specific ClassLoader instance. Refer + * to the doc for for a description of the url parameter. + */ + public PHP(String url, ClassLoader classLoader, String logFileName, String ... args) throws Exception { + if (url == null || url.length() < 1) + throw new IllegalArgumentException("[url] parameter must be defined"); + + if (classLoader == null) + throw new IllegalArgumentException("[classLoader] parameter must be defined"); + + // classpath reference + if (url.indexOf("classpath:/") == 0) { + URL resource = classLoader.getResource(url.substring(11)); + File ref = new File(resource.getPath()); + initByFile(ref, logFileName, args); + } + + // remote script + else if (url.indexOf("http://") == 0 || url.indexOf("https://") == 0) { + try { + URLConnection conn = new URL(url).openConnection(); + initByInputStream(conn.getInputStream()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + // file reference + else { + initByFile(new File(url), logFileName, args); + } + + } + + /** + * Initialize a PHP wrapper with a specific File loaded by the host. + * @param file A file full of PHP script + */ + public PHP(File file, String logFileName, String ... args) throws Exception { + initByFile(file, logFileName, args); + } + + private void initByInputStream(InputStream stream) { + try { + StringBuilder sourceCode = new StringBuilder(1024); + BufferedReader in = new BufferedReader(new InputStreamReader(stream)); + char[] buffer = new char[1024]; + int read = 0; + while ((read = in.read(buffer)) != -1) + sourceCode.append(buffer, 0, read); + in.close(); + + snippet(sourceCode.toString()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private void initByFile(File ref, String logFileName, String ... args) throws Exception { + if (!ref.exists()) { + throw new RuntimeException(new FileNotFoundException("No PHP file or directory at ["+ref.getAbsolutePath()+"]")); + } + + if (ref.isDirectory()) { + snippet(""); + getEnv().setPwd(new FilePath(ref.getAbsolutePath())); + } + else { + File dir = ref.getParentFile(); + String [] arguments = ArrayUtils.addAll(new String [] {"-f", ref.getAbsolutePath(),logFileName}, args); + + getQuercus().processArguments(arguments); + + if (dir != null) { + try { + + QuercusPage page = getQuercus().parse(new FilePath(ref.getAbsolutePath())); + initEnv(page); + getEnv().setPwd(new FilePath(ref.getParentFile().getAbsolutePath())); + + getEnv().execute(); + } catch (QuercusDieException var11) { + log.log(Level.FINER, var11.toString(), var11); + throw var11; + } catch (QuercusExitException var12) { + log.log(Level.FINER, var12.toString(), var12); + throw var12; + } catch (QuercusErrorException var13) { + log.log(Level.FINER, var13.toString(), var13); + throw var13; + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + } + + } + } + + private Env env; + private WriteStream os = new WriteStream(StringBuilderStream.create()); + + private void initEnv(QuercusPage page) throws UnsupportedEncodingException { + if (env == null) { + os.setNewlineString("\n"); + os.setEncoding("iso-8859-1"); + + + env = getQuercus().createEnv(page, os, (QuercusHttpServletRequest)null, (QuercusHttpServletResponse)null); + + env.setPwd(new FilePath(System.getProperty("user.dir"))); + + env.start(); + } + } + + /** + * Retrieves the Quercus execution environment, which your Java code + * can use to interact directly with the Quercus parsing engine. + * @link http://www.caucho.com/resin-javadoc/com/caucho/quercus/env/Env.html + * @throws IllegalStateException When environment has not yet been initialized + */ + private Env getEnv() { + if (env == null) + throw new IllegalStateException("Environment not yet initialized"); + return env; + } + + /** + * Set the value of a global variable in the PHP execution environment. + * @param name The name of the global parameter to create/update + * @param obj The value to store there + */ + PHP set(String name, Object obj) { + snippet(""); + getEnv().setGlobalValue(name, toValue(getEnv(), obj)); + return this; + } + + /** + * Retrieve the value of a global varialbe in the PHP execution environment. + * @param name The name of the global parameter tto read + */ + PHPObject get(String name) { + snippet(""); + return new PHPObject(getEnv(), getEnv().getGlobalValue(name)); + } + + /** + * Ensures that obj is of type or wrapped in an instance + * of Quercus' Value, with respect to the given execution + * environment env. + * @return obj or obj wrapped in a Value instance. + */ + static Value toValue(Env env, Object obj) { + if (obj == null) + return env.wrapJava(obj); + else if (obj instanceof PHPObject) + return ((PHPObject) obj).getWrappedValue(); + else if (obj instanceof Value) + return (Value) obj; + else + return env.wrapJava(obj); + } + + /** + * Parse and execute a snippet of PHP script, adding to the + * execution context any artifacts and/or output generated by the code. + */ + PHP snippet(String snippet) { + try { + QuercusPage page = getQuercus().parse(StringStream.open(snippet)); + initEnv(page); + page.executeTop(getEnv()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + return this; + } + + /** + * Retrieve the output generated by all PHP scripts executed in this context. + */ + public String toString() { + if (env != null) { + try { + os.flush(); + return os.getSource().toString(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + else { + return null; + } + } + + /** + * Call the PHP function named fxName with arguments args + * @return An instance of PHPObject, wrapped around the return value of the function. + */ + PHPObject fx(String fxName, Object... args) { + if (args != null && args.length > 0) { + Value[] values = new Value[args.length]; + for (int i=0; iclassName, initialized with + * arguments args. + * @return An instance of PHPObject, wrapping the new instance of className. + */ + PHPObject newInstance(String className, Object... args) { + QuercusClass clazz = getEnv().findClass(className); + if (clazz == null) + throw new RuntimeException(new ClassNotFoundException("PHP:"+className)); + + if (args != null && args.length > 0) { + Value[] values = new Value[args.length]; + for (int i=0; icom.caucho.quercus.env.Value, themselves representing + * instances of PHP objects. This wrapper makes it easier to invoke methods and set and get properties. + * @author Aaron Collegeman aaron@collegeman.net + */ +public class PHPObject { + + private Value wrapped; + + private Env env; + + public PHPObject(Env env, Value value) { + this.env = env; + this.wrapped = value; + } + + /** + * Invoke the method name on wrapped Value object, passing + * into the invocation parameters args. + * @return An instance of PHPObject, wrapping any return value of nameed method. + */ + public final PHPObject invokeMethod(String name, Object ... args) { + if (args != null && args.length > 0) { + Value values[] = new Value[args.length]; + for(int i=0; iValue to value. + * @return This instance of PHPObject, to support method chaining. + */ + public final PHPObject setProperty(String name, Object value) { + wrapped.putField(env, new StringBuilderValue(name), PHP.toValue(env, value)); + return this; + } + + /** + * Retrieve a public property name of the wrapped Value. + * @return A new instance of PHPObject, wrapping the retrieved property. + */ + public final PHPObject getProperty(String name) { + return new PHPObject(env, wrapped.getField(env, new StringBuilderValue(name))); + } + + public Value getWrappedValue() { + return wrapped; + } + + public String toString() { + return wrapped.toJavaString(); + } + +} \ No newline at end of file diff --git a/src/main/resources/ipsw/wzRequest.py b/src/main/resources/ipsw/wzRequest.py deleted file mode 100644 index a09de91..0000000 --- a/src/main/resources/ipsw/wzRequest.py +++ /dev/null @@ -1,7 +0,0 @@ -from net.collegeman.phpinjava import PHP - -php = PHP() -php.snippet(" - - \ No newline at end of file diff --git a/src/main/resources/xlrelease/phpRequest.py b/src/main/resources/xlrelease/phpRequest.py new file mode 100644 index 0000000..184e010 --- /dev/null +++ b/src/main/resources/xlrelease/phpRequest.py @@ -0,0 +1,16 @@ + +from java.io import File +from net.collegeman.phpinjava import PHP + + +arguments = [] +props = getCurrentTask().getPythonScript().getInputProperties() + +for k in props: + arguments.append("%s=%s" % (k.name,getCurrentTask().getPythonScript().getProperty(k.name))) + +php = PHP(File("%s/%s" % (phpHomeFolder,phpScript)), logFileName, arguments) + +output = php.toString() + +print "We received the following [%s]" % output \ No newline at end of file