Author: nbelaevski
Date: 2011-01-24 07:41:38 -0500 (Mon, 24 Jan 2011)
New Revision: 21171
Modified:
trunk/core/impl/src/main/java/org/richfaces/application/configuration/ConfigurationServiceImpl.java
trunk/core/impl/src/main/java/org/richfaces/application/configuration/ValueExpressionHolder.java
trunk/core/impl/src/test/java/org/richfaces/application/Configuration.java
trunk/core/impl/src/test/java/org/richfaces/application/ConfigurationServiceImplTest.java
Log:
Improved exceptions handling in ConfigurationServiceImpl
Modified:
trunk/core/impl/src/main/java/org/richfaces/application/configuration/ConfigurationServiceImpl.java
===================================================================
---
trunk/core/impl/src/main/java/org/richfaces/application/configuration/ConfigurationServiceImpl.java 2011-01-24
12:08:30 UTC (rev 21170)
+++
trunk/core/impl/src/main/java/org/richfaces/application/configuration/ConfigurationServiceImpl.java 2011-01-24
12:41:38 UTC (rev 21171)
@@ -23,6 +23,7 @@
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
+import java.text.MessageFormat;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -45,16 +46,17 @@
private final ConfigurationItem getConfigurationItemAnnotation(Enum<?> enumKey)
{
try {
- return
enumKey.getClass().getField(enumKey.name()).getAnnotation(ConfigurationItem.class);
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NoSuchFieldException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ ConfigurationItem item =
enumKey.getClass().getField(enumKey.name()).getAnnotation(ConfigurationItem.class);
+ if (item != null) {
+ return item;
+ }
+ } catch (Exception e) {
+ throw new IllegalStateException(MessageFormat.format("Cannot read
@ConfigurationItem annotation from {0}.{1} because of {2}",
+ enumKey.getClass().getName(), enumKey.name(), e.getMessage()));
}
- return null;
+ throw new IllegalStateException(MessageFormat.format("Annotation
@ConfigurationItem is not set at {0}.{1}",
+ enumKey.getClass().getName(), enumKey.name()));
}
private <T> T coerce(FacesContext context, Object value, Class<T>
targetType) {
@@ -81,15 +83,11 @@
}
}
- //TODO message
- throw new IllegalArgumentException();
+ throw new IllegalArgumentException(MessageFormat.format("Cannot convert {0}
to object of {1} type", value, targetType.getName()));
}
protected ValueExpressionHolder createValueExpressionHolder(FacesContext context,
Enum<?> key, Class<?> targetType) {
ConfigurationItem annotation = getConfigurationItemAnnotation(key);
- if (annotation == null) {
- //TODO - handle this
- }
ValueExpression expression = createValueExpression(context, annotation,
targetType);
@@ -109,28 +107,20 @@
ConfigurationItemSource source = annotation.source();
if (source == ConfigurationItemSource.contextInitParameter) {
- Map initMap = context.getExternalContext().getInitParameterMap();
-
for (String name : annotation.names()) {
- String value = (String) initMap.get(name);
+ String value = (String)
context.getExternalContext().getInitParameter(name);
if (Strings.isNullOrEmpty(value)) {
continue;
}
- if (ELUtils.isValueReference(value)) {
+ if (!annotation.literal() && ELUtils.isValueReference(value)) {
ExpressionFactory expressionFactory =
context.getApplication().getExpressionFactory();
if (expressionFactory == null) {
- //TODO message
- throw new IllegalStateException();
+ throw new IllegalStateException("ExpressionFactory is
null");
}
- if (annotation.literal()) {
- //TODO message
- throw new IllegalArgumentException();
- }
-
return
expressionFactory.createValueExpression(context.getELContext(), value, targetType);
} else {
Object coercedValue = coerce(context, value, targetType);
Modified:
trunk/core/impl/src/main/java/org/richfaces/application/configuration/ValueExpressionHolder.java
===================================================================
---
trunk/core/impl/src/main/java/org/richfaces/application/configuration/ValueExpressionHolder.java 2011-01-24
12:08:30 UTC (rev 21170)
+++
trunk/core/impl/src/main/java/org/richfaces/application/configuration/ValueExpressionHolder.java 2011-01-24
12:41:38 UTC (rev 21171)
@@ -21,7 +21,6 @@
*/
package org.richfaces.application.configuration;
-import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
@@ -43,14 +42,10 @@
public Object getValue(FacesContext context) {
if (valueExpression != null) {
- try {
- Object result = valueExpression.getValue(context.getELContext());
+ Object result = valueExpression.getValue(context.getELContext());
- if (result != null) {
- return result;
- }
- } catch (ELException e) {
- // TODO: handle exception
+ if (result != null) {
+ return result;
}
}
Modified: trunk/core/impl/src/test/java/org/richfaces/application/Configuration.java
===================================================================
--- trunk/core/impl/src/test/java/org/richfaces/application/Configuration.java 2011-01-24
12:08:30 UTC (rev 21170)
+++ trunk/core/impl/src/test/java/org/richfaces/application/Configuration.java 2011-01-24
12:41:38 UTC (rev 21171)
@@ -71,6 +71,6 @@
@ConfigurationItem(names = "org.richfaces.LiteralOnlyValue", literal =
true)
literalOnly,
- @ConfigurationItem(names = "org.richfaces.LiteralOnlyFailingValue", literal
= true)
- literalOnlyFailing
+ @ConfigurationItem(names = "org.richfaces.LiteralOnlyWithEl", literal =
true)
+ literalOnlyWithEl
}
Modified:
trunk/core/impl/src/test/java/org/richfaces/application/ConfigurationServiceImplTest.java
===================================================================
---
trunk/core/impl/src/test/java/org/richfaces/application/ConfigurationServiceImplTest.java 2011-01-24
12:08:30 UTC (rev 21170)
+++
trunk/core/impl/src/test/java/org/richfaces/application/ConfigurationServiceImplTest.java 2011-01-24
12:41:38 UTC (rev 21171)
@@ -23,7 +23,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
-import static org.junit.Assert.fail;
import static
org.richfaces.application.configuration.ConfigurationServiceHelper.getBooleanConfigurationValue;
import static
org.richfaces.application.configuration.ConfigurationServiceHelper.getConfigurationValue;
import static
org.richfaces.application.configuration.ConfigurationServiceHelper.getEnumConfigurationValue;
@@ -102,18 +101,12 @@
@Test
@ContextInitParameters({
@ContextInitParameter(name = "org.richfaces.LiteralOnlyValue", value =
"pure literal"),
- @ContextInitParameter(name = "org.richfaces.LiteralOnlyFailingValue",
value = "#{someEl}")
+ @ContextInitParameter(name = "org.richfaces.LiteralOnlyWithEl", value =
"#{someEl}")
})
public void testLiteral() throws Exception {
FacesContext context = FacesContext.getCurrentInstance();
assertEquals("pure literal", getConfigurationValue(context,
Configuration.literalOnly));
- try {
- getConfigurationValue(context, Configuration.literalOnlyFailing);
-
- fail();
- } catch (IllegalArgumentException e) {
- // TODO: handle exception
- }
+ assertEquals("#{someEl}", getConfigurationValue(context,
Configuration.literalOnlyWithEl));
}
}