Author: jcouteau Date: 2009-10-16 17:44:08 +0200 (Fri, 16 Oct 2009) New Revision: 156 Modified: trunk/src/main/java/org/nuiton/j2r/RProxy.java trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java Log: Throw exception if jri not found Modified: trunk/src/main/java/org/nuiton/j2r/RProxy.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/RProxy.java 2009-10-16 13:30:37 UTC (rev 155) +++ trunk/src/main/java/org/nuiton/j2r/RProxy.java 2009-10-16 15:44:08 UTC (rev 156) @@ -50,9 +50,15 @@ /** * Constructor. Launches the initialization. + * + * @throws RException if initialisation failed */ - public RProxy() { - init(); + public RProxy() throws RException { + Boolean init = init(); + if (init == false) { + throw new RException( + "R initialisation failed, please check your installation"); + } } /** @@ -74,7 +80,7 @@ //if no success on net, tries on JNI if (log.isErrorEnabled()) { log.error( - "Initialization of R with Network failed, trying JNI"); + "Initialization of R with Network failed, trying JNI"); } initSucceded = initOnJNI(); } @@ -85,7 +91,7 @@ //if no success on jni, tries on net if (log.isErrorEnabled()) { log.error( - "Initialization of R over JNI failed, trying with Network"); + "Initialization of R over JNI failed, trying with Network"); } initSucceded = initOnNet(); } @@ -142,7 +148,7 @@ public Object eval(String expr) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return null; } return engine.eval(expr); @@ -158,7 +164,7 @@ String rType = System.getProperty("R.type"); if (rType == null || "".equals(rType)) { log.warn("No R type specified.\nusage:\n\tjava -DR.type=net ...\n" + - "or\n\tjava -DR.type=jni ...\n\nConsidering network type"); + "or\n\tjava -DR.type=jni ...\n\nConsidering network type"); rType = "net"; } if (!init(rType)) { @@ -179,7 +185,7 @@ public void terminate() throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); } else { engine.terminate(); } @@ -196,7 +202,7 @@ public void voidEval(String expr) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.voidEval(expr); @@ -216,7 +222,7 @@ public void loadRData(File directory) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.setwd(directory); @@ -237,7 +243,7 @@ public void saveRData(File directory) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.setwd(directory); @@ -258,7 +264,7 @@ public void setwd(File directory) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.voidEval("setwd(\"" + directory.getAbsolutePath() + "\")"); @@ -279,7 +285,7 @@ public File getwd() throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return null; } return engine.getwd(); @@ -303,7 +309,7 @@ public void dput(String rObject, String outputFileName) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } String rInstruction = "dput(%s,file=\"%s\")"; @@ -329,7 +335,7 @@ public void dget(String rObject, String inputFileName) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } String rInstruction = "%s <- dget(\"%s\")"; @@ -353,14 +359,14 @@ public void dput(String rObject, File outputFile) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } File workingdir = getwd(); engine.setwd(outputFile.getParentFile()); String rInstruction = "dput(%s,file=\"%s\")"; engine.voidEval(String.format(rInstruction, rObject, - outputFile.getName())); + outputFile.getName())); setwd(workingdir); } @@ -381,14 +387,14 @@ public void dget(String rObject, File inputFile) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } File workingdir = getwd(); engine.setwd(inputFile.getParentFile()); String rInstruction = "%s <- dget(\"%s\")"; engine.voidEval( - String.format(rInstruction, rObject, inputFile.getName())); + String.format(rInstruction, rObject, inputFile.getName())); setwd(workingdir); } @@ -407,7 +413,7 @@ public void remove(String rObject) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.remove(rObject); @@ -430,7 +436,7 @@ public void mv(String source, String destination) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.mv(source, destination); @@ -454,7 +460,7 @@ public void cp(String inputObject, String outputObject) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.cp(inputObject, outputObject); @@ -476,7 +482,7 @@ public String[] ls() throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return null; } return engine.ls(); @@ -495,7 +501,7 @@ public void clearSession() throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.clearSession(); @@ -515,7 +521,7 @@ public void commit() throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.commit(); @@ -538,7 +544,7 @@ public void setAutoCommit(Boolean autocommit) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return; } engine.setAutoCommit(autocommit); @@ -558,7 +564,7 @@ public Boolean isAutoCommit() { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); return null; } return engine.isAutoCommit(); @@ -582,7 +588,7 @@ public void saveRData(File directory, String fileName) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); } engine.saveRData(directory, fileName); } @@ -606,8 +612,9 @@ public void loadRData(File directory, String fileName) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + "occured during the initialization."); } engine.loadRData(directory, fileName); } } //RProxy + Modified: trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2009-10-16 13:30:37 UTC (rev 155) +++ trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2009-10-16 15:44:08 UTC (rev 156) @@ -69,7 +69,21 @@ if (engine == null) { try { String[] args = {"--no-save"}; + + //Set the property so that rJava does not make a System.exit(1) + //System.setProperty("jri.ignore.ule", "yes"); + + //jriLoaded is false is rJava did not find jri library + if (!Rengine.jriLoaded) { + if (log.isErrorEnabled()) { + log.error( + "Cannot find jri library, make sure it is correctly installed"); + } + return false; + } + engine = new Rengine(args, false, null); + if (!engine.waitForR()) { if (log.isErrorEnabled()) { log.error("Cannot load the R engine"); @@ -78,7 +92,7 @@ } } catch (Exception eee) { log.error("An error occured during R/JNI initialization.", - eee); + eee); return false; } } @@ -107,7 +121,7 @@ //if the "class" attribute of the R expression is "try-error" //throw a new exception with the error message from R. String klass = - result.getAttribute(RInstructions.ATTRIBUTE_CLASS).asString(); + result.getAttribute(RInstructions.ATTRIBUTE_CLASS).asString(); if (klass.equals(RInstructions.CLASS_ERROR)) { throw new RException(result.asString()); } @@ -211,7 +225,7 @@ //get the class of the vector (to successfully detect data.frames) String klass = ""; REXP klassAttribute = rexp.getAttribute( - RInstructions.ATTRIBUTE_CLASS); + RInstructions.ATTRIBUTE_CLASS); if (klassAttribute != null) { klass = klassAttribute.asString(); } @@ -224,7 +238,7 @@ //create the data list. List<List<? extends Object>> data = - new ArrayList<List<? extends Object>>(); + new ArrayList<List<? extends Object>>(); //get rexp as a list (data.frame is a list of vectors) org.rosuda.JRI.RList dataList = rexp.asList(); for (int i = 0; i < dataList.keys().length; i++) { @@ -233,7 +247,7 @@ List<Object> templist = new ArrayList<Object>(); REXP tempREXP = dataList.at(i); Object[] convertedREXP = (Object[]) convertResult( - tempREXP); + tempREXP); templist = Arrays.asList(convertedREXP); //add this list to the data list. data.add(templist); @@ -243,9 +257,9 @@ //gotten from rexp. It has no variable name so throws a //RException. temp = new RDataFrame((REngine) this, rexp.getAttribute( - RInstructions.ATTRIBUTE_NAMES).asStringArray(), - rexp.getAttribute(RInstructions.ATTRIBUTE_ROWNAMES).asStringArray(), - data, ""); + RInstructions.ATTRIBUTE_NAMES).asStringArray(), + rexp.getAttribute(RInstructions.ATTRIBUTE_ROWNAMES).asStringArray(), + data, ""); result = temp; } else if (list != null) { RList temp = new RList((REngine) this); @@ -255,7 +269,7 @@ //for each object of the list, convert it to java. REXP tempREXP = dataList.at(i); Object convertedREXP = (Object) convertResult( - tempREXP); + tempREXP); //add this object to the data list. data.add(convertedREXP); @@ -265,13 +279,13 @@ //RException. try { temp = new RList( - rexp.getAttribute(RInstructions.ATTRIBUTE_NAMES).asStringArray(), - data, (REngine) this, ""); + rexp.getAttribute(RInstructions.ATTRIBUTE_NAMES).asStringArray(), + data, (REngine) this, ""); } catch (RException re) { //don't propagate the error as it is normal. Log it for debug. if (log.isDebugEnabled()) { log.debug( - "Converting REXP to RList. Creating list without variable name"); + "Converting REXP to RList. Creating list without variable name"); } } result = temp; @@ -283,7 +297,7 @@ default: //if don't know the type, throw an exception. log.error("Unknown return type [" + type + "] " + "on : " + - rexp.toString()); + rexp.toString()); break; } return result; @@ -358,3 +372,4 @@ rInstructions = new LinkedList<String>(); } } // RJniEngine +