Permalink
Browse files

Merged NativeEnvironment into NativeServices.

  • Loading branch information...
1 parent 6a86dac commit 36d3b2cae1e414db4cccc5d394eb76cf1a280afb @adammurdoch adammurdoch committed Jan 1, 2012
@@ -15,13 +15,15 @@
*/
package org.gradle.os;
-import org.gradle.os.jna.NativeEnvironment;
+import org.gradle.os.jna.Unix;
+import org.gradle.os.jna.UnsupportedEnvironment;
+import org.gradle.os.jna.Windows;
import org.jruby.ext.posix.POSIX;
public class NativeServices {
public <T> T get(Class<T> type) {
if (type.isAssignableFrom(ProcessEnvironment.class)) {
- return type.cast(NativeEnvironment.current());
+ return type.cast(createProcessEnvironment());
}
if (type.isAssignableFrom(OperatingSystem.class)) {
return type.cast(OperatingSystem.current());
@@ -31,4 +33,19 @@
}
throw new IllegalArgumentException(String.format("Do not know how to create service of type %s.", type.getSimpleName()));
}
+
+ protected ProcessEnvironment createProcessEnvironment() {
+ try {
+ if (OperatingSystem.current().isUnix()) {
+ return new Unix();
+ } else if (OperatingSystem.current().isWindows()) {
+ return new Windows();
+ } else {
+ return new UnsupportedEnvironment();
+ }
+ } catch (LinkageError e) {
+ // Thrown when jna cannot initialize the native stuff
+ return new UnsupportedEnvironment();
+ }
+ }
}
@@ -1,47 +0,0 @@
-/*
- * Copyright 2011 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.gradle.os.jna;
-
-import org.gradle.os.OperatingSystem;
-import org.gradle.os.ProcessEnvironment;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Uses jna to update the environment variables.
- *
- * @author: Szczepan Faber, created at: 9/7/11
- */
-public class NativeEnvironment {
- private static final Logger LOGGER = LoggerFactory.getLogger(NativeEnvironment.class);
-
- public static ProcessEnvironment current() {
- try {
- if (OperatingSystem.current().isUnix()) {
- return new Unix();
- } else if (OperatingSystem.current().isWindows()) {
- return new Windows();
- } else {
- LOGGER.warn("Unable to initialize native environment. Updating environment variables and working directory will not be possible. Operating system: {}", OperatingSystem.current());
- return new UnsupportedEnvironment();
- }
- } catch (LinkageError e) {
- // Thrown when jna cannot initialize the native stuff
- return new UnsupportedEnvironment();
- }
- }
-}
@@ -20,7 +20,10 @@
import java.io.File;
-class Unix extends AbstractNativeEnvironment {
+/**
+ * Uses JNA to drive the POSIX API provided by libc
+ */
+public class Unix extends AbstractNativeEnvironment {
private static final int LOTS_OF_CHARS = 2048;
private final UnixLibC libc = (UnixLibC) Native.loadLibrary("c", UnixLibC.class);
@@ -23,7 +23,7 @@
import java.io.File;
import java.util.Map;
-class UnsupportedEnvironment implements ProcessEnvironment {
+public class UnsupportedEnvironment implements ProcessEnvironment {
public boolean maybeSetEnvironment(Map<String, String> source) {
return false;
}
@@ -21,7 +21,10 @@
import java.io.File;
-class Windows extends AbstractNativeEnvironment {
+/**
+ * Uses JNA to drive the functions provided by kernel32.dll
+ */
+public class Windows extends AbstractNativeEnvironment {
private static final int LOTS_OF_CHARS = 2048;
private final Kernel32 kernel32 = Kernel32.INSTANCE;
@@ -15,19 +15,19 @@
*/
package org.gradle.os.jna
+import org.gradle.os.NativeServices
import org.gradle.os.ProcessEnvironment
+import org.gradle.util.Requires
import org.gradle.util.SetSystemProperties
import org.gradle.util.TemporaryFolder
-import org.gradle.util.Requires
import org.gradle.util.TestPrecondition
import org.junit.Rule
import spock.lang.Specification
-import spock.lang.Issue
-class NativeEnvironmentTest extends Specification {
+class ProcessEnvironmentTest extends Specification {
@Rule final TemporaryFolder tmpDir = new TemporaryFolder()
@Rule final SetSystemProperties systemProperties = new SetSystemProperties()
- final ProcessEnvironment env = NativeEnvironment.current()
+ final ProcessEnvironment env = new NativeServices().get(ProcessEnvironment)
@Requires(TestPrecondition.SET_ENV_VARIABLE)
def "can set and remove environment variable"() {
@@ -76,14 +76,4 @@ class NativeEnvironmentTest extends Specification {
env.pid != null
env.maybeGetPid() == env.pid
}
-
- @Issue("GRADLE-1776")
- def "returns an environment even if an unknown OS is encountered (instead of failing)"() {
- System.setProperty("os.arch", "unknown")
- System.setProperty("os.name", "unknown")
- System.setProperty("os.version", "unknown")
-
- expect:
- NativeEnvironment.current() != null
- }
}

0 comments on commit 36d3b2c

Please sign in to comment.