You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2009/11/20 09:02:09 UTC

svn commit: r882435 - in /labs/magma/trunk/foundation-basics/src: main/java/org/apache/magma/settings/ContextSettingsHolder.java main/java/org/apache/magma/settings/Settings.java test/java/org/apache/magma/settings/ContextSettingsHolderTest.java

Author: simoneg
Date: Fri Nov 20 08:02:08 2009
New Revision: 882435

URL: http://svn.apache.org/viewvc?rev=882435&view=rev
Log:
LABS-365 LABS-494 : small fixes and optimizations

Modified:
    labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java
    labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/Settings.java
    labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java

Modified: labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java?rev=882435&r1=882434&r2=882435&view=diff
==============================================================================
--- labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java (original)
+++ labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/ContextSettingsHolder.java Fri Nov 20 08:02:08 2009
@@ -47,9 +47,9 @@
  */
 public class ContextSettingsHolder extends SettingsHolder {
 
-	protected static final String CACHE_NONE = "___NONE___";
+	protected static final String[] CACHE_NONE = new String[] { "___NONE___" };
 		
-	protected Map<String, String> cache = new HashMap<String, String>();
+	protected Map<String, String[]> cache = new HashMap<String, String[]>();
 	protected Map<String, List<String[]>> firstLookup = new HashMap<String, List<String[]>>();
 	
 	@Override
@@ -75,6 +75,7 @@
 	}
 	
 	protected static String[] tokenize(String key, String value) {
+		key = key.toLowerCase();
 		String[] parts = key.split("\\.");
 		parts = Arrays.copyOf(parts, parts.length + 1);
 		parts[parts.length - 1] = value;
@@ -87,15 +88,48 @@
 	}
 	
 	/**
-	 * Gets the translation for the given string and the given context. 
+	 * Gets the property value in the given context. 
 	 * 
 	 * The context is usually a {@link SubRunningContext} instance.
 	 * 
 	 * @param ct A stack of elements representing the context.
-	 * @param original The original string to translate.
-	 * @return The translation or the original string if no translation found.
+	 * @param property The name of the property
+	 * @return The property value if resolved, null otherwise
 	 */
 	public String get(Stack<ContextElement> ct, String property) {
+		String[] entry = getEntry(ct, property);
+		if (entry == null) return null;
+		return entry[entry.length - 1];
+	}
+
+	/**
+	 * Gets the entire property entry suitable for actual {@link RunningContext}. 
+	 * 
+	 * The entry is an array of tokens as parsed from the property file
+	 * plus the given value as last element.
+	 * 
+	 * @param name The name of the property
+	 * @return The entry if resolved, null otherwise
+	 */	
+	public String[] getEntry(String name) {
+		return getEntry(RunningContext.get(), name);
+	}
+	
+	
+	/**
+	 * Gets the entire property entry suitable for the given context. 
+	 * 
+	 * The context is usually a {@link SubRunningContext} instance.
+	 * 
+	 * The entry is an array of tokens as parsed from the property file
+	 * plus the given value as last element.
+	 * 
+	 * @param ct A stack of elements representing the context.
+	 * @param original The name of the property
+	 * @return The entry if resolved, null otherwise
+	 */
+	public String[] getEntry(Stack<ContextElement> ct, String property) {
+		property = property.toLowerCase();
 		List<String[]> list = firstLookup.get(property);
 		// Fast not found case
 		if (list == null || list.size() == 0) return null;
@@ -103,7 +137,7 @@
 		// Fast found xxxx=yyyyy case
 		if (list.size() == 1) {
 			if (list.get(0).length == 2) {
-				return list.get(0)[1];
+				return list.get(0);
 			}
 		}
 		
@@ -113,8 +147,9 @@
 			keysb.append(string);
 			keysb.append('.');
 		}
+		keysb.append(property);
 		String key = keysb.toString();
-		String winner = cache.get(key);
+		String[] winner = cache.get(key);
 		if (winner == CACHE_NONE) return null;
 		if (winner == null) {
 			int topscore = -1;
@@ -122,7 +157,7 @@
 				int score = getScore(cm, strings);
 				if (score > topscore) {
 					topscore = score;
-					winner = cm[cm.length - 1];
+					winner = cm;
 				}
 			}
 			//ct.pop();
@@ -138,6 +173,10 @@
 	
 	/**
 	 * Checks the score of two string arrays.
+	 * 
+	 * This check is case sensitive, cause when called from {@link #get(Stack, String)} everything is
+	 * lowercased before.
+	 * 
 	 * @param parts The keys in the property file
 	 * @param mc The keys of the context
 	 * @return -1 for no match, 0 for one step match (x=y), increasingly higher score for more in depth match
@@ -149,7 +188,7 @@
 		int pos = 1;
 		for (String seg : mc) {
 			pos++;
-			if (parts[upto].equalsIgnoreCase(seg)) {
+			if (parts[upto].equals(seg)) {
 				upto++;
 				total += pos;
 				if (upto == parts.length - 2) return total;
@@ -161,7 +200,7 @@
 	/**
 	 * Converts a stack of context elements to an array of strings.
 	 * @param ct The stack of the current context
-	 * @return A string array, interpreting {@link I18nMultiSegmentContextElement} with correct elements
+	 * @return A string array, interpreting {@link I18nMultiSegmentContextElement} with correct elements, and turning to lowercase
 	 */
 	protected static String[] stringify(Stack<ContextElement> ct) {
 		String[] ret = new String[ct.size() * 2];
@@ -169,11 +208,11 @@
 		for (ContextElement ele : ct) {
 			if (ele instanceof SingleSegmentContextElement) {
 				String seg = ((SingleSegmentContextElement)ele).getSegment();
-				ret[i++] = seg;
+				ret[i++] = seg.toLowerCase();
 			} else if (ele instanceof MultipleSegmentContextElement) {
 				String[] segs = ((MultipleSegmentContextElement)ele).getSegments();
 				for (String seg : segs) {
-					ret[i++] = seg;
+					ret[i++] = seg.toLowerCase();
 				}
 			}
 		}

Modified: labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/Settings.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/Settings.java?rev=882435&r1=882434&r2=882435&view=diff
==============================================================================
--- labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/Settings.java (original)
+++ labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/settings/Settings.java Fri Nov 20 08:02:08 2009
@@ -104,7 +104,6 @@
 	public static void init() {
 		String env = environment; 
 		String prefix = username;
-		SettingsHolder defs = null;
 		holder.initing();
 		if (holder.isInited()) return;
 		if (env == null) env = "";
@@ -112,40 +111,38 @@
 		if (prefix == null) prefix = "";
 		if (prefix.length() > 0) prefix += ".";
 		ClassLoader loader = holder.getClass().getClassLoader();
-		if (defs == null) {
-			Enumeration<URL> resources;
-			try {
-				resources = loader.getResources("META-INF/magma.default.properties");
-				while (resources.hasMoreElements()) {
-					holder.overrideWith(resources.nextElement());
-				}
-			} catch (IOException e) {
-				e.printStackTrace();
+		Enumeration<URL> resources;
+		try {
+			resources = loader.getResources("META-INF/magma.default.properties");
+			while (resources.hasMoreElements()) {
+				holder.overrideWith(resources.nextElement());
 			}
-			try {
-				resources = loader.getResources("META-INF/magma.default.properties.xml");
-				while (resources.hasMoreElements()) {
-					holder.overrideWith(resources.nextElement());
-				}
-			} catch (IOException e) {
-				e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		try {
+			resources = loader.getResources("META-INF/magma.default.properties.xml");
+			while (resources.hasMoreElements()) {
+				holder.overrideWith(resources.nextElement());
 			}
-			try {
-				resources = loader.getResources("META-INF/magma" + env + ".default.properties");
-				while (resources.hasMoreElements()) {
-					holder.overrideWith(resources.nextElement());
-				}
-			} catch (IOException e) {
-				e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		try {
+			resources = loader.getResources("META-INF/magma" + env + ".default.properties");
+			while (resources.hasMoreElements()) {
+				holder.overrideWith(resources.nextElement());
 			}
-			try {
-				resources = loader.getResources("META-INF/magma" + env + ".default.properties.xml");
-				while (resources.hasMoreElements()) {
-					holder.overrideWith(resources.nextElement());
-				}
-			} catch (IOException e) {
-				e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		try {
+			resources = loader.getResources("META-INF/magma" + env + ".default.properties.xml");
+			while (resources.hasMoreElements()) {
+				holder.overrideWith(resources.nextElement());
 			}
+		} catch (IOException e) {
+			e.printStackTrace();
 		}
 		holder.overrideWith(loader.getResource("META-INF/magma.properties"));
 		holder.overrideWith(loader.getResource("META-INF/magma.properties.xml"));

Modified: labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java?rev=882435&r1=882434&r2=882435&view=diff
==============================================================================
--- labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java (original)
+++ labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/settings/ContextSettingsHolderTest.java Fri Nov 20 08:02:08 2009
@@ -30,8 +30,8 @@
 		
 		String[] strings = stringify(elements);
 		assertThat(strings[0], equalTo("text"));
-		assertThat(strings[1], equalTo(ContextSettingsHolderTest.class.getSimpleName()));
-		assertThat(strings[2], equalTo("stringifyTest"));
+		assertThat(strings[1], equalTo(ContextSettingsHolderTest.class.getSimpleName().toLowerCase()));
+		assertThat(strings[2], equalTo("stringifytest"));
 	}	
 	
 	@SuppressWarnings("deprecation")



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org