diff --git a/core/src/main/java/org/jruby/RubyProcess.java b/core/src/main/java/org/jruby/RubyProcess.java index 8a8aa9a83e2..ebe521142a1 100644 --- a/core/src/main/java/org/jruby/RubyProcess.java +++ b/core/src/main/java/org/jruby/RubyProcess.java @@ -113,7 +113,7 @@ public static RubyModule createProcessModule(Ruby runtime) { public static final String CLOCK_UNIT_FLOAT_MILLISECOND = "float_millisecond"; public static final String CLOCK_UNIT_FLOAT_SECOND = "float_second"; public static final String CLOCK_UNIT_HERTZ = "hertz"; - + @JRubyClass(name="Process::Status") public static class RubyStatus extends RubyObject { private final long status; @@ -573,16 +573,12 @@ public static IRubyObject exit_bang(IRubyObject recv, IRubyObject[] args) { @JRubyMethod(name = "groups", module = true, visibility = PRIVATE) public static IRubyObject groups(IRubyObject recv) { - if(Platform.IS_WINDOWS) { - throw recv.getRuntime().newNotImplementedError("groups() function is unimplemented on this machine"); - } else { - long[] groups = (new com.sun.security.auth.module.UnixSystem()).getGroups(); - RubyArray ary = RubyArray.newArray(recv.getRuntime(), groups.length); - for(int i = 0; i < groups.length; i++) { - ary.push(RubyFixnum.newFixnum(recv.getRuntime(), groups[i])); - } - return ary; + long[] groups = Platform.getPlatform().getGroups(recv); + RubyArray ary = RubyArray.newArray(recv.getRuntime(), groups.length); + for(int i = 0; i < groups.length; i++) { + ary.push(RubyFixnum.newFixnum(recv.getRuntime(), groups[i])); } + return ary; } @JRubyMethod(name = "setrlimit", rest = true, module = true, visibility = PRIVATE) diff --git a/core/src/main/java/org/jruby/platform/NTPlatform.java b/core/src/main/java/org/jruby/platform/NTPlatform.java new file mode 100644 index 00000000000..2e24f4d7de3 --- /dev/null +++ b/core/src/main/java/org/jruby/platform/NTPlatform.java @@ -0,0 +1,57 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: EPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Eclipse Public + * License Version 1.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.eclipse.org/legal/epl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2014 Timur Duehr + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the EPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the EPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.jruby.platform; + +import org.jruby.runtime.builtin.IRubyObject; + +public class NTPlatform extends Platform { + private final Class systemClass; + private final Object system; + + protected NTPlatform() { + Class sClass = null; + Object s = null; + + try { + sClass = Class.forName("com.sun.security.auth.module.NTSystem"); + s = sClass.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + throw new UnsupportedOperationException(e.getMessage(), e); + } + + systemClass = sClass; + system = s; + } + + + @Override + public long[] getGroups(IRubyObject recv) { + throw recv.getRuntime().newNotImplementedError("groups() function is unimplemented on Windows"); + } +} diff --git a/core/src/main/java/org/jruby/platform/Platform.java b/core/src/main/java/org/jruby/platform/Platform.java index 0f51e8d07f1..6c10d3f0e01 100644 --- a/core/src/main/java/org/jruby/platform/Platform.java +++ b/core/src/main/java/org/jruby/platform/Platform.java @@ -29,6 +29,7 @@ package org.jruby.platform; import org.jruby.util.SafePropertyAccessor; +import org.jruby.runtime.builtin.IRubyObject; import java.nio.ByteOrder; import java.util.HashMap; @@ -38,8 +39,8 @@ /** * Platform specific constants. */ -public class Platform { - private static final Platform INSTANCE = new Platform(); +public abstract class Platform { + private static final Platform INSTANCE = initPlatform(); public static Platform getPlatform() { return INSTANCE; } @@ -103,6 +104,7 @@ private static String initArchitecture() { } return arch; } + public static final String ARCH = initArchitecture(); public static final String OS = initOperatingSystem(); public static final String JVM = getProperty("java.vm.name", "unknown"); @@ -124,6 +126,21 @@ private static String initArchitecture() { public static final boolean IS_GCJ = JVM.equals(GCJ); public static final boolean IS_IBM = JVM.equals(IBM); + private static Platform initPlatform(){ + try { + if (IS_WINDOWS) + return new NTPlatform(); + + if (IS_SOLARIS) + return new SolarisPlatform(); + + // Punt + return new UnixPlatform(); + } catch (UnsupportedOperationException e) { + return new UnsupportedPlatform(); + } + } + /** * An extension over System.getProperty method. * Handles security restrictions, and returns the default @@ -140,4 +157,6 @@ public static String getProperty(String property, String defValue) { return defValue; } } + + public abstract long[] getGroups(IRubyObject recv); } diff --git a/core/src/main/java/org/jruby/platform/SolarisPlatform.java b/core/src/main/java/org/jruby/platform/SolarisPlatform.java new file mode 100644 index 00000000000..4999d78c31d --- /dev/null +++ b/core/src/main/java/org/jruby/platform/SolarisPlatform.java @@ -0,0 +1,69 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: EPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Eclipse Public + * License Version 1.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.eclipse.org/legal/epl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2014 Timur Duehr + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the EPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the EPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.jruby.platform; + +import org.jruby.runtime.builtin.IRubyObject; + +import java.lang.Class; +import java.lang.reflect.Method; + +public class SolarisPlatform extends Platform { + private final Class systemClass; + private final Object system; + private final Method groupsMethod; + + protected SolarisPlatform() { + Class sClass = null; + Object s = null; + Method g = null; + try { + sClass = Class.forName("com.sun.security.auth.module.SolarisSystem"); + s = sClass.getDeclaredConstructor().newInstance(); + g = sClass.getDeclaredMethod("getGroups"); + } catch (Exception e) { + throw new UnsupportedOperationException(e.getMessage(), e); + } + + systemClass = sClass; + system = s; + groupsMethod = g; + } + + @Override + public long[] getGroups(IRubyObject recv) { + if (groupsMethod == null) + throw recv.getRuntime().newNotImplementedError("groups() function is unimplemented on this platform"); + + try { + return (long[])groupsMethod.invoke(system); + } catch (Exception e) { + throw new UnsupportedOperationException("groups() function is unimplemented on this platform", e); + } + } +} diff --git a/core/src/main/java/org/jruby/platform/UnixPlatform.java b/core/src/main/java/org/jruby/platform/UnixPlatform.java new file mode 100644 index 00000000000..7ea75800886 --- /dev/null +++ b/core/src/main/java/org/jruby/platform/UnixPlatform.java @@ -0,0 +1,69 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: EPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Eclipse Public + * License Version 1.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.eclipse.org/legal/epl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2014 Timur Duehr + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the EPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the EPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.jruby.platform; + +import org.jruby.runtime.builtin.IRubyObject; + +import java.lang.Class; +import java.lang.reflect.Method; + +public class UnixPlatform extends Platform { + private final Class systemClass; + private final Object system; + private final Method groupsMethod; + + protected UnixPlatform() { + Class sClass = null; + Object s = null; + Method g = null; + try { + sClass = Class.forName("com.sun.security.auth.module.UnixSystem"); + s = sClass.getDeclaredConstructor().newInstance(); + g = sClass.getDeclaredMethod("getGroups"); + } catch (Exception e) { + throw new UnsupportedOperationException(e.getMessage(), e); + } + + systemClass = sClass; + system = s; + groupsMethod = g; + } + + @Override + public long[] getGroups(IRubyObject recv) { + if (groupsMethod == null) + throw recv.getRuntime().newNotImplementedError("groups() function is unimplemented on this platform"); + + try { + return (long[])groupsMethod.invoke(system); + } catch (Exception e) { + throw new UnsupportedOperationException("groups() function is unimplemented on this platform", e); + } + } +} diff --git a/core/src/main/java/org/jruby/platform/UnsupportedPlatform.java b/core/src/main/java/org/jruby/platform/UnsupportedPlatform.java new file mode 100644 index 00000000000..564584a4f5b --- /dev/null +++ b/core/src/main/java/org/jruby/platform/UnsupportedPlatform.java @@ -0,0 +1,37 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: EPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Eclipse Public + * License Version 1.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.eclipse.org/legal/epl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2014 Timur Duehr + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the EPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the EPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ + +package org.jruby.platform; + +import org.jruby.runtime.builtin.IRubyObject; + +public class UnsupportedPlatform extends Platform { + public long[] getGroups(IRubyObject recv) { + throw recv.getRuntime().newNotImplementedError("groups() function is unimplemented on this platform"); + } +}