groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "zaxebo yaxebo (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (GROOVY-7493) in java/jvm, there is no chdir() function. So groovy also does not have it. Please add chdir() to groovy development kit. Jruby already has chdir()
Date Sun, 05 Jul 2015 17:42:04 GMT

     [ https://issues.apache.org/jira/browse/GROOVY-7493?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

zaxebo yaxebo updated GROOVY-7493:
----------------------------------
    Description: 
note that in java/jvm, there is no chdir() function. So groovy also does not have it. Please
add chdir() to groovy development kit.

 But please note that JRUBY supports chdir(). Jruby do not support it via JNI, but rather
they use JNR.


For example, when we launch "irb" of jruby, then see the following session
irb(main):001:0> Dir.pwd()
=> "/home/user/dir1"

irb(main):002:0> Dir.mkdir("dir2")
=> 0

irb(main):004:0> Dir.chdir("dir2")
=> 0

irb(main):005:0> Dir.pwd()
=> "/home/user/dir1/dir2"

So, now users subdir has changed.

I request you to please add chdir() to groovy JDK using JNA or JNR

====================================
this functionality of chdir() using JNA is implemented here as below


/*
reference= https://www.java.net/node/643965
You can use chdir() carefully if you know what you are doing. It's sure that in a multithreaded
environment it can be very tricky.
An easy way to implement chdir is using the jna library (https://github.com/twall/jna) like
this:
*/
import com.sun.jna.NativeLibrary;
import com.sun.jna.Platform;
import java.io.*;

/**
* Call native low level Posix functions.
*/
public class Posix
{
	private static final int MAX_PATH = 1024;
	private NativeLibrary cLib = null;
	public void chdir(String dirName)
	{
		int error = getCLib().getFunction(getOsDependentFuntionName("chdir")).invokeInt(
				new Object[] { dirName });
		if (error != 0)
			throw new Error("Could not change working directory to: " + dirName);
		String cwd = getcwd();
		// Change the current directory property used by the java VM
		System.setProperty("user.dir", cwd);
	}
	private NativeLibrary getCLib()
	{
		if (cLib == null)
			cLib = NativeLibrary.getInstance(null); // Will load libcxxx.so or msvcrt.dll
		return cLib;
	}
	public String getcwd()
	{
		String cwd = getCLib().getFunction(getOsDependentFuntionName("getcwd")).invokeString(
				new Object[] { new String(new byte[MAX_PATH]), MAX_PATH }, false);
		return cwd;
	}
	private String getOsDependentFuntionName(String fname)
	{
		if (Platform.isWindows())
			return "_" + fname;
		return fname;
	}
}

================================

We need feature of easily implementing native-backed features. Using the FFI API will be the
preferred way to bind native OS features, instead of JNI. All the code implemented using FFI(Foreign
function interface will be implemented in java, not in C/C++).

Actually, there are two competing mechanism to support this native OS functionality access
in java code:

Method1) JNA(java native access) = https://github.com/twall/jna

Method2) JNR(java native runtime) = used by jruby https://github.com/jnr  
       this JNR will be part be part of of Java in future http://openjdk.java.net/jeps/191
       But for now, it is bundled with jruby itself . open jruby-1.7.20.1/lib/jruby.jar in
any unzip program and then you will see that it already uses jnr containined in 'jnr" direcotry
inside that jruby.jar file
       
Both methods work fine
========================================================
Note that as jruby is using jnr and may be in far future jnr may be part of  jvm itself. 

So, it may be advised to use jnr, but still i have given this above illustrated code using
JNA, which functions identically with the same jna-..jar bundled with on MS-Windows,linux,BSD
etc

IF groovy implements this feature, then groovy will have to include .jar file of jna or jnr
 , whatever route it implements the code.

as groovy aspires to be a realworld scripting language in competition with jruby and jython,
so this inbuilt chdir() function is very much needed, urgent and useful.

  was:
note that in java/jvm, there is no chdir() function. So groovy also does not have it. Please
add chdir() to groovy development kit.

 But please note that JRUBY supports chdir(). Jruby do not support it via JNI, but rather
they use JNR.


For example, when we launch "irb" of jruby, then see the following session
irb(main):001:0> Dir.pwd()
=> "/home/user/dir1"

irb(main):002:0> Dir.mkdir("dir2")
=> 0

irb(main):004:0> Dir.chdir("dir2")
=> 0

irb(main):005:0> Dir.pwd()
=> "/home/user/dir1/dir2"

So, now users subdir has changed.

I request you to please add chdir() to groovy JDK using JNA or JNR

================================

We need feature of easily implementing native-backed features. Using the FFI API will be the
preferred way to bind native OS features, instead of JNI. All the code implemented using FFI(Foreign
function interface will be implemented in java, not in C/C++).

Actually, there are two competing mechanism to support this native OS functionality access
in java code:

Method1) JNA(java native access) = https://github.com/twall/jna

Method2) JNR(java native runtime) = used by jruby https://github.com/jnr  
       this JNR will be part be part of of Java in future http://openjdk.java.net/jeps/191
       But for now, it is bundled with jruby itself . open jruby-1.7.20.1/lib/jruby.jar in
any unzip program and then you will see that it already uses jnr containined in 'jnr" direcotry
inside that jruby.jar file
       
================
this functionality of chdir() using JNA is implemented here as below


/*
reference= https://www.java.net/node/643965
You can use chdir() carefully if you know what you are doing. It's sure that in a multithreaded
environment it can be very tricky.
An easy way to implement chdir is using the jna library (https://github.com/twall/jna) like
this:
*/
import com.sun.jna.NativeLibrary;
import com.sun.jna.Platform;
import java.io.*;

/**
* Call native low level Posix functions.
*/
public class Posix
{
	private static final int MAX_PATH = 1024;
	private NativeLibrary cLib = null;
	public void chdir(String dirName)
	{
		int error = getCLib().getFunction(getOsDependentFuntionName("chdir")).invokeInt(
				new Object[] { dirName });
		if (error != 0)
			throw new Error("Could not change working directory to: " + dirName);
		String cwd = getcwd();
		// Change the current directory property used by the java VM
		System.setProperty("user.dir", cwd);
	}
	private NativeLibrary getCLib()
	{
		if (cLib == null)
			cLib = NativeLibrary.getInstance(null); // Will load libcxxx.so or msvcrt.dll
		return cLib;
	}
	public String getcwd()
	{
		String cwd = getCLib().getFunction(getOsDependentFuntionName("getcwd")).invokeString(
				new Object[] { new String(new byte[MAX_PATH]), MAX_PATH }, false);
		return cwd;
	}
	private String getOsDependentFuntionName(String fname)
	{
		if (Platform.isWindows())
			return "_" + fname;
		return fname;
	}
}


========================================================
Note that as jruby is using jnr and may be in far future jnr may be part of  jvm itself. 

So, it may be advised to use jnr, but still i have given this above code using JNA, which
functions identically with the same jna-..jar bundled with on MS-Windows,linux,BSD etc

IF groovy implements this feature, then groovy will have to include jna or jnr .jar , whatever
route it implements the code.

as groovy aspires to be scripting language in competition with jruby and jython, so this chdir()
function is very much needed, urgent and useful.


> in java/jvm, there is no chdir() function. So groovy also does not have it. Please add
chdir() to groovy development kit. Jruby already has chdir()
> ---------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-7493
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7493
>             Project: Groovy
>          Issue Type: Improvement
>          Components: groovy-jdk
>         Environment: on all plaforms
>            Reporter: zaxebo yaxebo
>            Assignee: Guillaume Laforge
>              Labels: chdir(), jna, jnr, jruby
>
> note that in java/jvm, there is no chdir() function. So groovy also does not have it.
Please add chdir() to groovy development kit.
>  But please note that JRUBY supports chdir(). Jruby do not support it via JNI, but rather
they use JNR.
> For example, when we launch "irb" of jruby, then see the following session
> irb(main):001:0> Dir.pwd()
> => "/home/user/dir1"
> irb(main):002:0> Dir.mkdir("dir2")
> => 0
> irb(main):004:0> Dir.chdir("dir2")
> => 0
> irb(main):005:0> Dir.pwd()
> => "/home/user/dir1/dir2"
> So, now users subdir has changed.
> I request you to please add chdir() to groovy JDK using JNA or JNR
> ====================================
> this functionality of chdir() using JNA is implemented here as below
> /*
> reference= https://www.java.net/node/643965
> You can use chdir() carefully if you know what you are doing. It's sure that in a multithreaded
environment it can be very tricky.
> An easy way to implement chdir is using the jna library (https://github.com/twall/jna)
like this:
> */
> import com.sun.jna.NativeLibrary;
> import com.sun.jna.Platform;
> import java.io.*;
> /**
> * Call native low level Posix functions.
> */
> public class Posix
> {
> 	private static final int MAX_PATH = 1024;
> 	private NativeLibrary cLib = null;
> 	public void chdir(String dirName)
> 	{
> 		int error = getCLib().getFunction(getOsDependentFuntionName("chdir")).invokeInt(
> 				new Object[] { dirName });
> 		if (error != 0)
> 			throw new Error("Could not change working directory to: " + dirName);
> 		String cwd = getcwd();
> 		// Change the current directory property used by the java VM
> 		System.setProperty("user.dir", cwd);
> 	}
> 	private NativeLibrary getCLib()
> 	{
> 		if (cLib == null)
> 			cLib = NativeLibrary.getInstance(null); // Will load libcxxx.so or msvcrt.dll
> 		return cLib;
> 	}
> 	public String getcwd()
> 	{
> 		String cwd = getCLib().getFunction(getOsDependentFuntionName("getcwd")).invokeString(
> 				new Object[] { new String(new byte[MAX_PATH]), MAX_PATH }, false);
> 		return cwd;
> 	}
> 	private String getOsDependentFuntionName(String fname)
> 	{
> 		if (Platform.isWindows())
> 			return "_" + fname;
> 		return fname;
> 	}
> }
> ================================
> We need feature of easily implementing native-backed features. Using the FFI API will
be the preferred way to bind native OS features, instead of JNI. All the code implemented
using FFI(Foreign function interface will be implemented in java, not in C/C++).
> Actually, there are two competing mechanism to support this native OS functionality access
in java code:
> Method1) JNA(java native access) = https://github.com/twall/jna
> Method2) JNR(java native runtime) = used by jruby https://github.com/jnr  
>        this JNR will be part be part of of Java in future http://openjdk.java.net/jeps/191
>        But for now, it is bundled with jruby itself . open jruby-1.7.20.1/lib/jruby.jar
in any unzip program and then you will see that it already uses jnr containined in 'jnr" direcotry
inside that jruby.jar file
>        
> Both methods work fine
> ========================================================
> Note that as jruby is using jnr and may be in far future jnr may be part of  jvm itself.

> So, it may be advised to use jnr, but still i have given this above illustrated code
using JNA, which functions identically with the same jna-..jar bundled with on MS-Windows,linux,BSD
etc
> IF groovy implements this feature, then groovy will have to include .jar file of jna
or jnr  , whatever route it implements the code.
> as groovy aspires to be a realworld scripting language in competition with jruby and
jython, so this inbuilt chdir() function is very much needed, urgent and useful.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message