You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by gk...@apache.org on 2018/12/21 11:09:35 UTC
svn commit: r1849460 - in /turbine/fulcrum/trunk/json/jackson2/src:
java/org/apache/fulcrum/json/jackson/ test/org/apache/fulcrum/json/jackson/
Author: gk
Date: Fri Dec 21 11:09:35 2018
New Revision: 1849460
URL: http://svn.apache.org/viewvc?rev=1849460&view=rev
Log:
- use foreach streams
- use more specific class loader
Modified:
turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/Jackson2MapperService.java
turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/SimpleNameIntrospector.java
turbine/fulcrum/trunk/json/jackson2/src/test/org/apache/fulcrum/json/jackson/DefaultServiceTest.java
Modified: turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/Jackson2MapperService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/Jackson2MapperService.java?rev=1849460&r1=1849459&r2=1849460&view=diff
==============================================================================
--- turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/Jackson2MapperService.java (original)
+++ turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/Jackson2MapperService.java Fri Dec 21 11:09:35 2018
@@ -22,6 +22,7 @@ package org.apache.fulcrum.json.jackson;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
+import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -580,27 +581,39 @@ public class Jackson2MapperService exten
if (configuredAnnotationInspectors != null) {
Configuration[] nameVal = configuredAnnotationInspectors.getChildren();
- for (int i = 0; i < nameVal.length; i++) {
- String key = nameVal[i].getName();
+ Arrays.stream( nameVal).forEach(c->
+ {
+ String key = c.getName();
getLogger().debug("configured key: " + key);
if (key.equals("features")) {
this.features = new HashMap<>();
this.featureTypes = new HashMap<>();
- Configuration[] localFeatures = nameVal[i].getChildren();
- for (int j = 0; j < localFeatures.length; j++) {
- boolean featureValue = localFeatures[j].getAttributeAsBoolean("value", false);
- String featureType = localFeatures[j].getAttribute("type");
- String feature = localFeatures[j].getValue();
- getLogger().debug("configuredAnnotationInspectors " + feature + ":" + featureValue);
- this.features.put(feature, featureValue);
- this.featureTypes.put(feature, featureType);
- }
+ Arrays.stream( c.getChildren() ).forEach( lf -> {
+ boolean featureValue = lf.getAttributeAsBoolean("value", false);
+ String featureType = null;
+ String feature = null;
+ try {
+ featureType = lf.getAttribute("type");
+ feature = lf.getValue();
+ getLogger().debug("configuredAnnotationInspectors " + feature + ":" + featureValue);
+ this.features.put(feature, featureValue);
+ this.featureTypes.put(feature, featureType);
+ } catch (ConfigurationException e) {
+ throw new RuntimeException(e);
+ }
+ });
} else {
- String val = nameVal[i].getValue();
- getLogger().debug("configuredAnnotationInspectors " + key + ":" + val);
- this.annotationInspectors.put(key, val);
+ String val;
+ try {
+ val = c.getValue();
+ getLogger().debug("configuredAnnotationInspectors " + key + ":" + val);
+ this.annotationInspectors.put(key, val);
+ } catch (ConfigurationException e) {
+ throw new RuntimeException(e);
+ }
+
}
- }
+ });
}
final Configuration configuredDateFormat = conf.getChild(DATE_FORMAT, true);
this.dateFormat = configuredDateFormat.getValue(DEFAULTDATEFORMAT);
@@ -698,18 +711,16 @@ public class Jackson2MapperService exten
private void initFeatures() throws Exception {
if (features != null && !features.isEmpty()) {
-
- for (Entry<String, Boolean> entry : features.entrySet()) {
+ features.entrySet().stream().forEach( entry -> {
String featureKey = entry.getKey();
Boolean featureValue = entry.getValue();
String featureType = featureTypes.get(featureKey);
Class<?> configFeature = null;
try {
getLogger().debug("initializing featureType: " + featureType);
- configFeature = Class.forName(featureType);
+ configFeature = loadClass(featureType);
} catch (Exception e) {
- throw new Exception("JsonMapperService: Error instantiating " + featureType + " for " + featureKey,
- e);
+ throw new RuntimeException("JsonMapperService: Error instantiating " + featureType + " for " + featureKey, e);
}
ConfigFeature feature = null;
if (!StringUtils.isEmpty(featureKey) && featureValue != null) {
@@ -748,11 +759,11 @@ public class Jackson2MapperService exten
mapper.configure(genFeature, featureValue);
}
} catch (Exception e) {
- throw new Exception("JsonMapperService: Error instantiating feature " + featureKey + " with "
+ throw new RuntimeException("JsonMapperService: Error instantiating feature " + featureKey + " with "
+ featureValue, e);
}
}
- }
+ });
}
}
@@ -794,6 +805,32 @@ public class Jackson2MapperService exten
getLogger().info("setting secondary introspector logger: " + secondary.getClass().getSimpleName());
}
}
+
+ /**
+ * Loads the named class using the default class loader.
+ *
+ * @param className the name of the class to load.
+ * @return {@inheritDoc} the loaded class.
+ * @throws ClassNotFoundException if the class was not found.
+ */
+ @SuppressWarnings("unchecked")
+ protected <T> Class<T> loadClass(String className) throws ClassNotFoundException {
+ ClassLoader loader = this.getClass().getClassLoader();
+ try {
+ Class<T> clazz;
+
+ if (loader != null) {
+ clazz = (Class<T>) loader.loadClass(className);
+ } else {
+ clazz = (Class<T>) Class.forName(className);
+ }
+
+ return clazz;
+ } catch (ClassNotFoundException x) {
+ /* Give up. */
+ throw x;
+ }
+ }
public ObjectMapper getMapper() {
return mapper;
Modified: turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/SimpleNameIntrospector.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/SimpleNameIntrospector.java?rev=1849460&r1=1849459&r2=1849460&view=diff
==============================================================================
--- turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/SimpleNameIntrospector.java (original)
+++ turbine/fulcrum/trunk/json/jackson2/src/java/org/apache/fulcrum/json/jackson/SimpleNameIntrospector.java Fri Dec 21 11:09:35 2018
@@ -93,7 +93,7 @@ public class SimpleNameIntrospector exte
public Object findFilterId(Annotated ac) {
Object id = super.findFilterId(ac);
// Let's default to current behavior if annotation is found:
- //Object id = super.findFilterId(ac);
+ // Object id = super.findFilterId(ac);
// but use simple class name if not
if (id == null) {
String name = ac.getName();
Modified: turbine/fulcrum/trunk/json/jackson2/src/test/org/apache/fulcrum/json/jackson/DefaultServiceTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/json/jackson2/src/test/org/apache/fulcrum/json/jackson/DefaultServiceTest.java?rev=1849460&r1=1849459&r2=1849460&view=diff
==============================================================================
--- turbine/fulcrum/trunk/json/jackson2/src/test/org/apache/fulcrum/json/jackson/DefaultServiceTest.java (original)
+++ turbine/fulcrum/trunk/json/jackson2/src/test/org/apache/fulcrum/json/jackson/DefaultServiceTest.java Fri Dec 21 11:09:35 2018
@@ -31,7 +31,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Log4JLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fulcrum.json.JsonService;