You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2006/08/29 12:09:55 UTC

svn commit: r438040 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main: java/org/apache/harmony/luni/platform/ native/luni/linux/ native/luni/shared/ native/luni/windows/

Author: pyang
Date: Tue Aug 29 03:09:53 2006
New Revision: 438040

URL: http://svn.apache.org/viewvc?rev=438040&view=rev
Log:
Patch applied for HARMONY-1222 ([classlib][luni]org.apache.harmony.luni.platform.Environment fails to recognize environment name case-incensitively on win32 platform.)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/hyenv.c
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/libhyluni.exp
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/hyenv.h
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyenv.c
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyluni.def

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java?rev=438040&r1=438039&r2=438040&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/Environment.java Tue Aug 29 03:09:53 2006
@@ -23,28 +23,28 @@
  */
 public class Environment {
 
-	private static Map<String, String> envMap = new HashMap<String, String>();
-
-	static {
-        byte[] bytes = getEnvBytes();
-        if (bytes == null) {
-            throw new Error("Failed to get environment variables.");
-        }
-        String[] envStrings = new String(bytes).split("\0");
-        for (int i = 0; i < envStrings.length; i++) {
-            int separator = envStrings[i].indexOf("=");
-            envMap.put(envStrings[i].substring(0, separator), envStrings[i]
-                    .substring(separator + 1));
-        }
-	}
+	private static Map<String, String> envMap = null;
 
 	/**
-     * Returns a Map of the current environment variables, containing key and
-     * value pairs.
-     * 
-     * @return a Map containing the environment variables and their values
-     */
+	 * Returns a Map of the current environment variables, containing key and
+	 * value pairs.
+	 * 
+	 * @return a Map containing the environment variables and their values
+	 1*/
 	public static Map<String, String> getenv() {
+		if (null == envMap) {
+			envMap = new HashMap<String, String>();
+			byte[] bytes = getEnvBytes();
+			if (bytes == null) {
+				throw new Error("Failed to get environment variables.");
+			}
+			String[] envStrings = new String(bytes).split("\0");
+			for (int i = 0; i < envStrings.length; i++) {
+				int separator = envStrings[i].indexOf("=");
+				envMap.put(envStrings[i].substring(0, separator), envStrings[i]
+						.substring(separator + 1));
+			}
+		}
 		return envMap;
 	}
 
@@ -58,8 +58,14 @@
 	 * @return the value of the environment variable specified
 	 */
 	public static String getenv(String name) {
-		return envMap.get(name);
+		byte[] env = getEnvByName(name.getBytes());
+		if (null == env) {
+			return null;
+		}
+		return new String(env);
 	}
 
 	private static native byte[] getEnvBytes();
+
+	private static native byte[] getEnvByName(byte[] name);
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/hyenv.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/hyenv.c?rev=438040&r1=438039&r2=438040&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/hyenv.c (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/hyenv.c Tue Aug 29 03:09:53 2006
@@ -13,14 +13,13 @@
  * limitations under the License.
  */
 #include <string.h>
-#include <unistd.h>
+#include <vmi.h>
 #include "hyenv.h"
 
 JNIEXPORT jbyteArray JNICALL Java_org_apache_harmony_luni_platform_Environment_getEnvBytes
   (JNIEnv *env, jclass obj){
-  jstring returnvar;
-  char* var;
   extern char** environ;
+  char* var;  
   jbyteArray byteArray;
   int bufsize=0,i=0,start=0,len=0;
   for(i=0;*(environ+i);i++){
@@ -35,3 +34,27 @@
   return byteArray;
 }
 
+JNIEXPORT jbyteArray JNICALL Java_org_apache_harmony_luni_platform_Environment_getEnvByName
+  (JNIEnv *env, jclass obj, jbyteArray name){
+  jsize len = 0;
+  jbyteArray byteArray;
+  char *envname, *envvalue;
+  PORT_ACCESS_FROM_ENV(env);
+
+  len = (*env)->GetArrayLength(env, name);
+  envname = (char *)hymem_allocate_memory(len+1);  
+  (*env)->GetByteArrayRegion(env, name, 0, len,(jbyte *)envname);
+  envname[len] = 0;
+
+  envvalue = getenv(envname);
+  hymem_free_memory(envname);
+  
+  if(NULL == envvalue){
+    return NULL;
+  }
+  
+  len = strlen(envvalue);
+  byteArray = (*env)->NewByteArray(env,len);
+  (*env)->SetByteArrayRegion(env,byteArray, 0, len, (jbyte *)envvalue);
+  return byteArray;
+}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/libhyluni.exp
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/libhyluni.exp?rev=438040&r1=438039&r2=438040&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/libhyluni.exp (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/linux/libhyluni.exp Tue Aug 29 03:09:53 2006
@@ -223,5 +223,6 @@
         Java_org_apache_harmony_luni_platform_OSNetworkSystem_isReachableByICMPImpl;
         Java_org_apache_harmony_luni_platform_OSNetworkSystem_inheritedChannelImpl;
 	Java_org_apache_harmony_luni_platform_Environment_getEnvBytes;
+	Java_org_apache_harmony_luni_platform_Environment_getEnvByName;
 	local : *;
 };

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/hyenv.h
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/hyenv.h?rev=438040&r1=438039&r2=438040&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/hyenv.h (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/hyenv.h Tue Aug 29 03:09:53 2006
@@ -29,6 +29,14 @@
 JNIEXPORT jbyteArray JNICALL Java_org_apache_harmony_luni_platform_Environment_getEnvBytes
   (JNIEnv *, jclass);
 
+/*
+ * Class:     org_apache_harmony_luni_platform_Environment
+ * Method:    getEnvByName
+ * Signature: ([B)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_harmony_luni_platform_Environment_getEnvByName
+  (JNIEnv *, jclass, jbyteArray);
+
 #ifdef __cplusplus
 }
 #endif

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyenv.c
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyenv.c?rev=438040&r1=438039&r2=438040&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyenv.c (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyenv.c Tue Aug 29 03:09:53 2006
@@ -13,6 +13,7 @@
  * limitations under the License.
  */
 #include <windows.h>
+#include <vmi.h>
 #include "hyenv.h"
 
 
@@ -24,7 +25,7 @@
   LPVOID lpvEnv;
   jbyteArray byteArray;
   lpvEnv = GetEnvironmentStrings();
-  if (lpvEnv == NULL){
+  if (NULL == lpvEnv){
     return NULL;
   }
   lpszVars = (LPTSTR)lpvEnv;
@@ -38,3 +39,50 @@
   FreeEnvironmentStrings((LPTCH)lpvEnv);
   return byteArray;
 }
+
+JNIEXPORT jbyteArray JNICALL Java_org_apache_harmony_luni_platform_Environment_getEnvByName
+  (JNIEnv *env, jclass obj, jbyteArray name){
+  DWORD dwRet,dwErr;
+  LPTSTR envvalue;
+  jsize len = 0;
+  const DWORD BUFSIZE = 1024;
+  jbyteArray byteArray = NULL;
+  char *envname;
+  PORT_ACCESS_FROM_ENV(env);
+
+  len = (*env)->GetArrayLength(env, name);
+  envname = (char *)hymem_allocate_memory(len+1);
+  (*env)->GetByteArrayRegion(env, name, 0, len,(jbyte *)envname);
+  envname[len] = 0;
+
+  envvalue = (LPTSTR)hymem_allocate_memory(BUFSIZE*sizeof(TCHAR));
+  dwRet = GetEnvironmentVariable(envname, envvalue, BUFSIZE);
+  
+  if(0 == dwRet)
+  {
+    dwErr = GetLastError();
+    if( ERROR_ENVVAR_NOT_FOUND == dwErr ){
+      goto free_resource;
+    }
+  }
+  else if(BUFSIZE < dwRet)
+  {
+    envvalue = (LPTSTR)hymem_reallocate_memory(envvalue, dwRet*sizeof(TCHAR));   
+    if(NULL == envvalue){
+      goto free_resource;
+    }
+    dwRet = GetEnvironmentVariable((LPCSTR)envname, envvalue, dwRet);
+    if(!dwRet){
+      goto free_resource;
+    }
+  }
+  
+  byteArray = (*env)->NewByteArray(env,dwRet);
+  (*env)->SetByteArrayRegion(env,byteArray, 0, dwRet, (jbyte *)envvalue);
+  free_resource:
+  hymem_free_memory(envname);
+  hymem_free_memory(envvalue);
+  return byteArray;
+}
+
+

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyluni.def
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyluni.def?rev=438040&r1=438039&r2=438040&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyluni.def (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/hyluni.def Tue Aug 29 03:09:53 2006
@@ -215,4 +215,5 @@
 	Java_org_apache_harmony_luni_util_NumberConverter_bigIntDigitGeneratorInstImpl
 	Java_org_apache_harmony_luni_platform_OSNetworkSystem_isReachableByICMPImpl
 	Java_org_apache_harmony_luni_platform_OSNetworkSystem_inheritedChannelImpl
-	Java_org_apache_harmony_luni_platform_Environment_getEnvBytes
\ No newline at end of file
+	Java_org_apache_harmony_luni_platform_Environment_getEnvBytes
+	Java_org_apache_harmony_luni_platform_Environment_getEnvByName