Author: simoneg
Date: Wed Dec 16 15:08:06 2009
New Revision: 891272
URL: http://svn.apache.org/viewvc?rev=891272&view=rev
Log:
Multiple values for calling the same method more than once
Modified:
labs/magma/trunk/lateconfig-impl/src/main/java/org/apache/magma/lateconfig/LateConfigurator.java
labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigBean.java
labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigTest.java
labs/magma/trunk/lateconfig-impl/src/test/resources/META-INF/lateconfig.properties
Modified:
labs/magma/trunk/lateconfig-impl/src/main/java/org/apache/magma/lateconfig/LateConfigurator.java
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/lateconfig-impl/src/main/java/org/apache/magma/lateconfig/LateConfigurator.java?rev=891272&r1=891271&r2=891272&view=diff
==============================================================================
---
labs/magma/trunk/lateconfig-impl/src/main/java/org/apache/magma/lateconfig/LateConfigurator.java
(original)
+++
labs/magma/trunk/lateconfig-impl/src/main/java/org/apache/magma/lateconfig/LateConfigurator.java
Wed Dec 16 15:08:06 2009
@@ -41,36 +41,45 @@
prop = null;
prop = holder.getEntry(keyname);
if (prop == null) continue;
- // TODO cache this conversion
- Class<?>[] types =
method.getParameterTypes();
- Object[] params = new
Object[types.length];
- if (types.length > 0) {
- // Special case of a single
string
- if (types.length == 1 &&
types[0].equals(String.class)) {
- params[0] =
prop[prop.length - 1];
- } else {
- String val =
prop[prop.length - 1];
- String[] strparams =
parseArguments(val, prop);
- // TODO how to handle
overridden methods?
- if (strparams.length !=
types.length) throw new MagmaException("Wrong number of parameters for late
config of {0},{1}, expected {2} but found {3} in {4}", bean.getClass(),
method.getName(), types.length, strparams.length, Arrays.toString(prop));
- for (int i = 0; i <
types.length; i++) {
- Converter<?>
converter = Converters.getConverterFor(types[i]);
- if (converter
== null) {
- throw
new MagmaException("Cannot find a converter for {0}, needed for late
configuration of {1}", types[i], Arrays.toString(prop));
+ String val = prop[prop.length - 1];
+ String vals[] = null;
+ // TODO the "=" could be escaped
between quotes
+ if (val.indexOf('=') != -1) {
+ vals = val.split("=");
+ } else {
+ vals = new String[] { val };
+ }
+ for (String acval : vals) {
+ // TODO cache this conversion
+ Class<?>[] types =
method.getParameterTypes();
+ Object[] params = new
Object[types.length];
+ if (types.length > 0) {
+ // Special case of a
single string
+ if (types.length == 1
&& types[0].equals(String.class)) {
+ params[0] =
acval;
+ } else {
+ String[]
strparams = parseArguments(acval, prop);
+ // TODO how to
handle overridden methods?
+ if
(strparams.length != types.length) throw new MagmaException("Wrong number of
parameters for late config of {0},{1}, expected {2} but found {3} in {4}",
bean.getClass(), method.getName(), types.length, strparams.length,
Arrays.toString(prop));
+ for (int i = 0;
i < types.length; i++) {
+
Converter<?> converter = Converters.getConverterFor(types[i]);
+ if
(converter == null) {
+
throw new MagmaException("Cannot find a converter for {0}, needed for late
configuration of {1}", types[i], Arrays.toString(prop));
+ }
+
params[i] = converter.from(strparams[i]);
}
- params[i] =
converter.from(strparams[i]);
}
}
- }
- try {
- method.invoke(bean, params);
- ret.add(method.getName());
- } catch (IllegalArgumentException e) {
- throw new MagmaException(e,
"There has been an error calling {0}.{1} using late configuration {2}",
method.getDeclaringClass(), method.getName(), Arrays.toString(prop));
- } catch (IllegalAccessException e) {
- throw new MagmaException(e,
"Method {0}.{1} is not accessible", method.getDeclaringClass(),
method.getName());
- } catch (InvocationTargetException e) {
- throw new MagmaException(e,
"There has been an error calling {0}.{1} using late configuration {2}",
method.getDeclaringClass(), method.getName(), Arrays.toString(prop));
+ try {
+ method.invoke(bean,
params);
+
ret.add(method.getName());
+ } catch
(IllegalArgumentException e) {
+ throw new
MagmaException(e, "There has been an error calling {0}.{1} using late
configuration {2}", method.getDeclaringClass(), method.getName(),
Arrays.toString(prop));
+ } catch (IllegalAccessException
e) {
+ throw new
MagmaException(e, "Method {0}.{1} is not accessible",
method.getDeclaringClass(), method.getName());
+ } catch
(InvocationTargetException e) {
+ throw new
MagmaException(e, "There has been an error calling {0}.{1} using late
configuration {2}", method.getDeclaringClass(), method.getName(),
Arrays.toString(prop));
+ }
}
}
}
Modified:
labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigBean.java
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigBean.java?rev=891272&r1=891271&r2=891272&view=diff
==============================================================================
---
labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigBean.java
(original)
+++
labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigBean.java
Wed Dec 16 15:08:06 2009
@@ -1,6 +1,8 @@
package org.apache.magma.lateconfig;
import java.lang.annotation.ElementType;
+import java.util.ArrayList;
+import java.util.List;
@LateConfigurable
public class LateConfigBean {
@@ -10,6 +12,8 @@
private String twostrings;
private int exoticInt;
private ElementType exoticEnum;
+ private List<String> list = new ArrayList<String>();
+
@LateConfigurationTrigger
public LateConfigBean() {
@@ -56,4 +60,12 @@
public ElementType getExoticEnum() {
return exoticEnum;
}
+
+ @LateConfigurable
+ public void addElem(String elem) {
+ this.list.add(elem);
+ }
+ public List<String> getList() {
+ return list;
+ }
}
Modified:
labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigTest.java
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigTest.java?rev=891272&r1=891271&r2=891272&view=diff
==============================================================================
---
labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigTest.java
(original)
+++
labs/magma/trunk/lateconfig-impl/src/test/java/org/apache/magma/lateconfig/LateConfigTest.java
Wed Dec 16 15:08:06 2009
@@ -1,9 +1,11 @@
package org.apache.magma.lateconfig;
import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
+import java.util.List;
import org.apache.magma.basics.MagmaException;
import org.apache.magma.basics.context.RunningContext;
@@ -18,6 +20,16 @@
LateConfigBean bean = new LateConfigBean();
assertEquals("Simple configuration not working", "configured
correctly", bean.getStringProp());
}
+
+ @Test
+ public void multipleValues() throws Exception {
+ LateConfigBean bean = new LateConfigBean();
+ List<String> list = bean.getList();
+ assertThat(list.size(), equalTo(3));
+ assertThat(list.get(0), equalTo("A"));
+ assertThat(list.get(1), equalTo("B"));
+ assertThat(list.get(2), equalTo("C"));
+ }
@After
public void cleanContext() {
Modified:
labs/magma/trunk/lateconfig-impl/src/test/resources/META-INF/lateconfig.properties
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/lateconfig-impl/src/test/resources/META-INF/lateconfig.properties?rev=891272&r1=891271&r2=891272&view=diff
==============================================================================
---
labs/magma/trunk/lateconfig-impl/src/test/resources/META-INF/lateconfig.properties
(original)
+++
labs/magma/trunk/lateconfig-impl/src/test/resources/META-INF/lateconfig.properties
Wed Dec 16 15:08:06 2009
@@ -3,4 +3,7 @@
chained=Configured Correctly
twostrings=configured,it
wrongmulti.twostrings=configured
-inexotic.exotic=5,METHOD
\ No newline at end of file
+inexotic.exotic=5,METHOD
+addElem=A\
+ =B\
+ =C
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]