1 package org
.argeo
.server
.json
;
3 import java
.util
.HashMap
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.argeo
.ArgeoException
;
9 import org
.codehaus
.jackson
.map
.ObjectMapper
;
10 import org
.osgi
.framework
.BundleContext
;
11 import org
.springframework
.beans
.factory
.InitializingBean
;
12 import org
.springframework
.osgi
.context
.BundleContextAware
;
13 import org
.springframework
.osgi
.util
.BundleDelegatingClassLoader
;
15 public class JsonObjectFactoryImpl
implements JsonObjectFactory
,
16 BundleContextAware
, InitializingBean
{
17 private final static Log log
= LogFactory
18 .getLog(JsonObjectFactoryImpl
.class);
20 private BundleContext bundleContext
;
21 private ClassLoader classLoader
= getClass().getClassLoader();
23 private ObjectMapper objectMapper
= new ObjectMapper();
24 private Map
<String
, Class
<?
>> supportedTypes
= new HashMap
<String
, Class
<?
>>();
26 public Boolean
supports(String type
) {
27 if (supportedTypes
.containsKey(type
))
30 return loadClass(type
) != null ?
true : false;
33 @SuppressWarnings("unchecked")
34 public <T
> T
readValue(String type
, String str
) {
36 if (supportedTypes
.containsKey(type
))
37 clss
= supportedTypes
.get(type
);
39 clss
= loadClass(type
);
41 throw new ArgeoException("Cannot find type " + type
);
45 return (T
) objectMapper
.readValue(str
, clss
);
46 } catch (Exception e
) {
47 throw new ArgeoException("Cannot deserialize " + str
48 + " (type=" + type
+ ")", e
);
52 public void setSupportedTypes(Map
<String
, Class
<?
>> supportedTypes
) {
53 this.supportedTypes
= supportedTypes
;
56 protected Class
<?
> loadClass(String type
) {
58 return classLoader
.loadClass(type
);
59 } catch (ClassNotFoundException e
) {
60 if (log
.isDebugEnabled())
61 log
.debug("BundleDelegatingClassLoader.loadClass failed: " + e
);
67 public void setBundleContext(BundleContext bundleContext
) {
68 this.bundleContext
= bundleContext
;
71 public void afterPropertiesSet() throws Exception
{
72 classLoader
= BundleDelegatingClassLoader
73 .createBundleClassLoaderFor(bundleContext
.getBundle());