├── .gitignore ├── .gitmodules ├── src ├── main │ └── java │ │ └── org │ │ └── pangratz │ │ └── couchapp │ │ ├── ICouchAppUtility.java │ │ └── RuntimeCouchAppUtility.java └── test │ └── java │ └── org │ └── pangratz │ └── couchapp │ ├── RuntimeCouchAppUtilityTest.java │ ├── ICouchAppUtilityTest.java │ └── jython │ └── Test.java ├── README.md └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # OS X 2 | .DS_Store 3 | 4 | # Maven 5 | target -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "src/main/resources/couchapp"] 2 | path = src/main/resources/couchapp 3 | url = https://github.com/couchapp/couchapp.git 4 | -------------------------------------------------------------------------------- /src/main/java/org/pangratz/couchapp/ICouchAppUtility.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | 6 | package org.pangratz.couchapp; 7 | 8 | /** 9 | * 10 | * @author clemens 11 | */ 12 | public interface ICouchAppUtility { 13 | 14 | public void generateApp(String dir, String name); 15 | 16 | public void generateView(String dir, String name); 17 | 18 | public void pushApp(String src, String dest); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/org/pangratz/couchapp/RuntimeCouchAppUtilityTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | 6 | package org.pangratz.couchapp; 7 | 8 | /** 9 | * 10 | * @author clemens 11 | */ 12 | public class RuntimeCouchAppUtilityTest extends ICouchAppUtilityTest { 13 | 14 | @Override 15 | protected ICouchAppUtility getCouchAppUtility() { 16 | return new RuntimeCouchAppUtility(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project ports [CouchApp](https://github.com/couchapp/couchapp) to Java using the Jython library. 2 | 3 | ## some links regarding invocation of python within java 4 | 5 | * [Simple and Efficient Jython Object Factories](http://wiki.python.org/jython/JythonMonthly/Articles/October2006/3) 6 | * [Using Jython Within Java Applications](http://jythonpodcast.hostjava.net/jythonbook/en/1.0/JythonAndJavaIntegration.html#using-jython-within-java-applications) 7 | * [Calling Jython from a Java Servlet](http://blogs.sun.com/tdw/entry/calling_jython_from_a_java) 8 | * [Embedding Jython](http://wiki.python.org/jython/JythonFaq/EmbeddingJython) 9 | * [invoking python from java (jython?)](http://www.justlinux.com/forum/showthread.php?t=153064) 10 | * [Distributing Jython Scripts](http://wiki.python.org/jython/JythonFaq/DistributingJythonScripts) 11 | * [What's a really easy way to distribute my app as a single jar?](http://wiki.python.org/jython/JythonFaq/DistributingJythonScripts#A.22What.27sareallyeasywaytodistributemyappasasinglejar.3F.22) -------------------------------------------------------------------------------- /src/test/java/org/pangratz/couchapp/ICouchAppUtilityTest.java: -------------------------------------------------------------------------------- 1 | package org.pangratz.couchapp; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import junit.framework.TestCase; 6 | 7 | public abstract class ICouchAppUtilityTest extends TestCase { 8 | 9 | private ICouchAppUtility couchAppUtil; 10 | 11 | @Override 12 | protected void setUp() throws Exception { 13 | super.setUp(); 14 | 15 | couchAppUtil = getCouchAppUtility(); 16 | } 17 | 18 | protected abstract ICouchAppUtility getCouchAppUtility(); 19 | 20 | public void testGenerateApp() throws IOException { 21 | File tmpFile = File.createTempFile("tmpDirPrefix", "suffix"); 22 | 23 | File parent = tmpFile.getParentFile(); 24 | parent = new File("/tmp/tmpocuhapp"); 25 | couchAppUtil.generateApp(parent.getPath(), "tmpCouchApp"); 26 | 27 | // check if the app structure has been created 28 | File couchappJsonFile = new File(parent, "couchapp.json"); 29 | assertTrue("there is no couchapp.json", couchappJsonFile.exists()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | org.pangratz.couchapp 6 | jython-couchapp 7 | jar 8 | 0.1-SNAPSHOT 9 | 10 | jython-couchapp 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | UTF-8 16 | 17 | 18 | 19 | 20 | 21 | org.python 22 | jython 23 | 2.5.0 24 | 25 | 26 | 27 | junit 28 | junit 29 | 3.8.2 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-compiler-plugin 39 | 40 | 1.6 41 | 1.6 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/main/java/org/pangratz/couchapp/RuntimeCouchAppUtility.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package org.pangratz.couchapp; 6 | 7 | import java.io.IOException; 8 | import java.net.URL; 9 | import java.util.logging.Level; 10 | import java.util.logging.Logger; 11 | 12 | /** 13 | * 14 | * @author clemens 15 | */ 16 | public class RuntimeCouchAppUtility implements ICouchAppUtility { 17 | 18 | private final Runtime runtime; 19 | private final String pathToCouchApp; 20 | 21 | public RuntimeCouchAppUtility() { 22 | super(); 23 | 24 | URL couchappUrl = RuntimeCouchAppUtility.class.getResource("/couchapp"); 25 | pathToCouchApp = couchappUrl.getPath() + "/Couchapp.py"; 26 | 27 | runtime = Runtime.getRuntime(); 28 | } 29 | 30 | @Override 31 | public void generateApp(String dir, String name) { 32 | generate("app", dir, name); 33 | } 34 | 35 | @Override 36 | public void pushApp(String src, String dest) { 37 | throw new UnsupportedOperationException("Not supported yet."); 38 | } 39 | 40 | @Override 41 | public void generateView(String dir, String name) { 42 | generate("view", dir, name); 43 | } 44 | 45 | private void generate(String command, String dir, String param) { 46 | try { 47 | String exec = "python " + pathToCouchApp + " generate " + command + " " + dir + " " + param; 48 | System.out.println(exec); 49 | runtime.exec(exec); 50 | } catch (IOException ex) { 51 | Logger.getLogger(RuntimeCouchAppUtility.class.getName()).log(Level.SEVERE, null, ex); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/org/pangratz/couchapp/jython/Test.java: -------------------------------------------------------------------------------- 1 | package org.pangratz.couchapp.jython; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | import java.util.Properties; 7 | import junit.framework.TestCase; 8 | import org.python.core.Py; 9 | import org.python.core.PyString; 10 | import org.python.core.PySystemState; 11 | import org.python.util.PythonInterpreter; 12 | 13 | public class Test extends TestCase { 14 | 15 | public void test() throws IOException { 16 | 17 | System.out.println("prop = " + System.getProperty("python.home")); 18 | 19 | // initialize python env 20 | Properties props = new Properties(); 21 | props.setProperty("python.console.encoding", "UTF8"); 22 | props.setProperty("file.encoding", "UTF8"); 23 | PySystemState.initialize(PySystemState.getBaseProperties(), props, null); 24 | 25 | PySystemState engineSys = new PySystemState(); 26 | 27 | // add the standard python library 28 | // TODO get a jar where these files are included or find another solution to this hard coded way 29 | // engineSys.path.append(Py.newString("/usr/local/Cellar/jython/2.5.1/libexec/Lib")); 30 | 31 | // add the couchapp pyton files 32 | engineSys.path.append(getOSPath("/couchapp")); 33 | 34 | Py.setSystemState(engineSys); 35 | 36 | PythonInterpreter interp = new PythonInterpreter(null, engineSys); 37 | interp.setOut(System.out); 38 | // invoke commands from couchapp lib 39 | // interp.exec("from couchapp.dispatch import run"); 40 | // interp.exec("run()"); 41 | // interp.exec("from couchapp.dispatch import run"); 42 | // interp.exec("import couchapp\nfrom couchapp.dispatch import run\n"); 43 | Runtime runtime = Runtime.getRuntime(); 44 | String cmd = "python " + getOSPath("/couchapp") + "/Couchapp.py help"; 45 | System.out.println(cmd); 46 | Process exec = runtime.exec(cmd); 47 | InputStream in = exec.getInputStream(); 48 | byte[] b = new byte[1024]; 49 | while (in.read(b) != -1) { 50 | System.out.println(new String(b)); 51 | } 52 | 53 | } 54 | 55 | private PyString getOSPath(String ressource) { 56 | String tmp = Test.class.getResource(ressource).getPath(); 57 | return Py.newString(tmp); 58 | } 59 | } 60 | --------------------------------------------------------------------------------