Author: asoldano
Date: 2015-03-11 16:28:46 -0400 (Wed, 11 Mar 2015)
New Revision: 19551
Added:
stack/cxf/trunk/modules/client/src/main/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverter.java
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanA.java
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanB.java
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverterTest.java
Modified:
stack/cxf/trunk/modules/client/pom.xml
stack/cxf/trunk/pom.xml
Log:
[JBWS-3879] Initial implementation of a property map to beans converter, internally based
on Apache Commons Beanutils
Modified: stack/cxf/trunk/modules/client/pom.xml
===================================================================
--- stack/cxf/trunk/modules/client/pom.xml 2015-03-10 16:58:48 UTC (rev 19550)
+++ stack/cxf/trunk/modules/client/pom.xml 2015-03-11 20:28:46 UTC (rev 19551)
@@ -251,6 +251,11 @@
<artifactId>log4j</artifactId>
</dependency>
<dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
Added:
stack/cxf/trunk/modules/client/src/main/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverter.java
===================================================================
---
stack/cxf/trunk/modules/client/src/main/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverter.java
(rev 0)
+++
stack/cxf/trunk/modules/client/src/main/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverter.java 2015-03-11
20:28:46 UTC (rev 19551)
@@ -0,0 +1,130 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2015, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.stack.cxf.client.configuration;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.commons.beanutils.BeanUtils;
+import org.jboss.ws.common.utils.DelegateClassLoader;
+import org.jboss.wsf.spi.classloading.ClassLoaderProvider;
+
+
+/**
+ * Converts a properties map (String to String) into bean(s).
+ *
+ * key0 | Fdfdsfs
+ * key1 | ##foo
+ * ##foo | com.company.Foo
+ * ##foo.par1 | AbcDe
+ * ##foo.par2 | ##par
+ * ##par | com.company.Bar
+ * ##par.name | John
+ * ##par.surname | Black
+ * ##foo.par3 | ##par
+ *
+ * A call to get("key1") returns the same you would get with:
+ *
+ * com.company.Foo foo = new com.company.Foo();
+ * foo.setPar1("AbcDe");
+ * com.company.Bar par = new com.company.Bar();
+ * par.setName("John");
+ * par.setSurname("Black");
+ * foo.setPar2(par);
+ * foo.setPar3(par);
+ * return foo;
+ *
+ *
+ * @author alessio.soldano(a)jboss.com
+ * @since 11-Mar-2015
+ *
+ */
+public class MapToBeanConverter
+{
+ public static final String BEAN_ID_PREFIX = "##";
+ public static final String DOT = ".";
+ private final Map<String, String> map;
+ private final Map<String, Object> builtObjectsMap;
+
+ public MapToBeanConverter(Map<String, String> map) {
+ this.map = map;
+ this.builtObjectsMap = new HashMap<String, Object>();
+ }
+
+ public Object get(String key) throws Exception {
+ if (key == null || !key.startsWith(BEAN_ID_PREFIX)) {
+ throw new IllegalArgumentException("Provided key does not start with "
+ BEAN_ID_PREFIX + ": " + key);
+ }
+ Object result = builtObjectsMap.get(key);
+ if (result == null) {
+ result = build(key);
+ builtObjectsMap.put(key, result);
+ }
+ return result;
+ }
+
+ protected Object build(String key) throws Exception {
+ Object bean = newInstance(map.get(key));
+ Map<String, String> attributes = attributesByBeanRef(key);
+ if (!attributes.isEmpty()) {
+ for (Entry<String, String> e : attributes.entrySet()) {
+ final String v = e.getValue();
+ BeanUtils.setProperty(bean, e.getKey(), v.startsWith(BEAN_ID_PREFIX) ? get(v)
: v);
+ }
+ }
+ return bean;
+ }
+
+ protected Object newInstance(String className) throws Exception {
+ ClassLoader loader = new
DelegateClassLoader(ClassLoaderProvider.getDefaultProvider()
+ .getServerIntegrationClassLoader(),
SecurityActions.getContextClassLoader());
+ Class<?> clazz = SecurityActions.loadClass(loader, className);
+ return clazz.newInstance();
+ }
+
+ /**
+ * Return an attribute name to attribute value map
+ *
+ * @param beanRef
+ * @return
+ */
+ protected Map<String, String> attributesByBeanRef(String beanRef) {
+ Map<String, String> result = null;
+ for (Entry<String, String> e : map.entrySet()) {
+ final String k = e.getKey();
+ if (k.startsWith(beanRef) && k.startsWith(DOT, beanRef.length())) {
+ if (result == null) {
+ result = new HashMap<String,String>();
+ }
+ result.put(k.substring(beanRef.length() + DOT.length()), e.getValue());
+ }
+ }
+ if (result == null) {
+ return Collections.emptyMap();
+ } else {
+ return result;
+ }
+ }
+
+}
\ No newline at end of file
Property changes on:
stack/cxf/trunk/modules/client/src/main/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverter.java
___________________________________________________________________
Added: svn:keywords
+ Rev Date
Added: svn:eol-style
+ native
Added:
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanA.java
===================================================================
---
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanA.java
(rev 0)
+++
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanA.java 2015-03-11
20:28:46 UTC (rev 19551)
@@ -0,0 +1,86 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2015, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.stack.cxf.client.configuration;
+
+public class BeanA
+{
+
+ private String name;
+
+ private int num;
+
+ private Long bigNumber;
+
+ private BeanB beanB;
+
+ private boolean pretty;
+
+ public boolean isPretty()
+ {
+ return pretty;
+ }
+
+ public void setPretty(boolean pretty)
+ {
+ this.pretty = pretty;
+ }
+
+ public BeanB getBeanB()
+ {
+ return beanB;
+ }
+
+ public void setBeanB(BeanB beanB)
+ {
+ this.beanB = beanB;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public int getNum()
+ {
+ return num;
+ }
+
+ public void setNum(int num)
+ {
+ this.num = num;
+ }
+
+ public Long getBigNumber()
+ {
+ return bigNumber;
+ }
+
+ public void setBigNumber(Long bigNumber)
+ {
+ this.bigNumber = bigNumber;
+ }
+}
Property changes on:
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanA.java
___________________________________________________________________
Added: svn:keywords
+ Rev Date
Added: svn:eol-style
+ native
Added:
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanB.java
===================================================================
---
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanB.java
(rev 0)
+++
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanB.java 2015-03-11
20:28:46 UTC (rev 19551)
@@ -0,0 +1,86 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2015, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.stack.cxf.client.configuration;
+
+public class BeanB
+{
+
+ private BeanA firstBeanA;
+
+ private BeanA secondBeanA;
+
+ private String name;
+
+ private int num;
+
+ private Long bigNumber;
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public int getNum()
+ {
+ return num;
+ }
+
+ public void setNum(int num)
+ {
+ this.num = num;
+ }
+
+ public Long getBigNumber()
+ {
+ return bigNumber;
+ }
+
+ public void setBigNumber(Long bigNumber)
+ {
+ this.bigNumber = bigNumber;
+ }
+
+ public BeanA getFirstBeanA()
+ {
+ return firstBeanA;
+ }
+
+ public void setFirstBeanA(BeanA firstBeanA)
+ {
+ this.firstBeanA = firstBeanA;
+ }
+
+ public BeanA getSecondBeanA()
+ {
+ return secondBeanA;
+ }
+
+ public void setSecondBeanA(BeanA secondBeanA)
+ {
+ this.secondBeanA = secondBeanA;
+ }
+}
Property changes on:
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/BeanB.java
___________________________________________________________________
Added: svn:keywords
+ Rev Date
Added: svn:eol-style
+ native
Added:
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverterTest.java
===================================================================
---
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverterTest.java
(rev 0)
+++
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverterTest.java 2015-03-11
20:28:46 UTC (rev 19551)
@@ -0,0 +1,109 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2015, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.stack.cxf.client.configuration;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+/**
+ * A test case for the MapToBeanConverter
+ *
+ * @author alessio.soldano(a)jboss.com
+ * @since 11-Mar-2015
+ *
+ */
+public class MapToBeanConverterTest extends TestCase
+{
+ private Map<String, String> getTestMap() {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("bean1", "invalid");
+ map.put("##bean1",
"org.jboss.wsf.stack.cxf.client.configuration.BeanA");
+ map.put("##bean1.name", "Bean1");
+ map.put("##bean1.num", "1");
+ map.put("##bean1.bigNumber", "1000000000");
+ map.put("##bean1.beanB", "##bean2");
+ map.put("##bean2",
"org.jboss.wsf.stack.cxf.client.configuration.BeanB");
+ map.put("##bean2.name", "Bean2");
+ map.put("##bean2.num", "2");
+ map.put("##bean2.bigNumber", "2000000000");
+ map.put("##bean2.firstBeanA", "##bean3");
+ map.put("##bean2.secondBeanA", "##bean3");
+ map.put("##bean3",
"org.jboss.wsf.stack.cxf.client.configuration.BeanA");
+ map.put("##bean3.name", "Bean3");
+ map.put("##bean3.num", "3");
+ map.put("##bean3.pretty", "true");
+ map.put("bean2", "invalid");
+ map.put("bean3", "invalid");
+ map.put("##bean4", "bean4Class");
+ return map;
+ }
+
+ public void testBasicConversion() throws Exception
+ {
+ Map<String, String> map = getTestMap();
+ MapToBeanConverter converter = new MapToBeanConverter(map);
+ BeanA bean1 = (BeanA)converter.get("##bean1");
+ assertNotNull(bean1);
+ assertEquals("Bean1", bean1.getName());
+ assertEquals(1, bean1.getNum());
+ assertEquals(new Long(1000000000), bean1.getBigNumber());
+ assertFalse(bean1.isPretty());
+ BeanB bean2 = bean1.getBeanB();
+ assertNotNull(bean2);
+ assertEquals("Bean2", bean2.getName());
+ assertEquals(2, bean2.getNum());
+ assertEquals(new Long(2000000000), bean2.getBigNumber());
+ BeanA bean3 = bean2.getFirstBeanA();
+ assertNotNull(bean3);
+ assertEquals("Bean3", bean3.getName());
+ assertEquals(3, bean3.getNum());
+ assertTrue(bean3.isPretty());
+ assertNull(bean3.getBigNumber());
+ assertEquals(bean3, bean2.getSecondBeanA());
+ assertEquals(bean1, converter.get("##bean1"));
+ assertEquals(bean2, converter.get("##bean2"));
+ assertEquals(bean3, converter.get("##bean3"));
+ }
+
+ public void testInvalidGet() throws Exception
+ {
+ try {
+ new MapToBeanConverter(getTestMap()).get("foo");
+ fail("IllegalArgumentException expected!");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("foo"));
+ }
+ }
+
+ public void testMissingClass() throws Exception
+ {
+ try {
+ new MapToBeanConverter(getTestMap()).get("##bean4");
+ fail("ClassNotFoundException expected!");
+ } catch (ClassNotFoundException e) {
+ assertTrue(e.getMessage().contains("bean4Class"));
+ }
+ }
+
+}
Property changes on:
stack/cxf/trunk/modules/client/src/test/java/org/jboss/wsf/stack/cxf/client/configuration/MapToBeanConverterTest.java
___________________________________________________________________
Added: svn:keywords
+ Rev Date
Added: svn:eol-style
+ native
Modified: stack/cxf/trunk/pom.xml
===================================================================
--- stack/cxf/trunk/pom.xml 2015-03-10 16:58:48 UTC (rev 19550)
+++ stack/cxf/trunk/pom.xml 2015-03-11 20:28:46 UTC (rev 19551)
@@ -92,6 +92,7 @@
<commons-lang.version>2.6</commons-lang.version>
<commons.logging.version>1.1.1</commons.logging.version>
<log4j.version>1.2.16</log4j.version>
+ <org.slf4j.version>1.6.1</org.slf4j.version>
<activation.version>1.1</activation.version>
<fastinfoset.version>1.2.12</fastinfoset.version>
<neethi.version>3.0.3</neethi.version>
@@ -1159,6 +1160,11 @@
<version>${log4j.version}</version>
</dependency>
<dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>${org.slf4j.version}</version>
+ </dependency>
+ <dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>${activation.version}</version>