1 package org
.argeo
.server
.dao
;
3 import java
.beans
.PropertyEditor
;
4 import java
.io
.InputStream
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Collection
;
7 import java
.util
.HashMap
;
10 import java
.util
.TreeMap
;
12 import org
.apache
.commons
.io
.IOUtils
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.ArgeoException
;
16 import org
.springframework
.beans
.BeanWrapper
;
17 import org
.springframework
.beans
.BeanWrapperImpl
;
18 import org
.springframework
.beans
.BeansException
;
19 import org
.springframework
.beans
.factory
.InitializingBean
;
20 import org
.springframework
.beans
.factory
.generic
.GenericBeanFactoryAccessor
;
21 import org
.springframework
.context
.ApplicationContext
;
22 import org
.springframework
.context
.ApplicationContextAware
;
23 import org
.springframework
.core
.io
.Resource
;
25 public abstract class AbstractMemoryDaoSupport
implements LightDaoSupport
,
26 ApplicationContextAware
, InitializingBean
{
27 private final static Log log
= LogFactory
28 .getLog(AbstractMemoryDaoSupport
.class);
30 private ClassLoader classLoader
= getClass().getClassLoader();
31 private ApplicationContext applicationContext
;
32 private List
<Class
<?
>> additionalClasses
= new ArrayList
<Class
<?
>>();
34 private Map
<Class
<?
>, Map
<Object
, Object
>> model
= new HashMap
<Class
<?
>, Map
<Object
, Object
>>();
36 private Map
<String
, Object
> externalRefs
= new HashMap
<String
, Object
>();
38 private List
<String
> scannedPackages
= new ArrayList
<String
>();
40 private List
<Resource
> resources
= new ArrayList
<Resource
>();
42 private Map
<Class
<?
>, PropertyEditor
> customEditors
= new HashMap
<Class
<?
>, PropertyEditor
>();;
44 protected abstract void load(InputStream in
, List
<Reference
> references
);
46 protected abstract Object
findInternalRef(Reference reference
);
48 public void afterPropertiesSet() throws Exception
{
53 for (PropertyEditor propertyEditor
: customEditors
.values())
54 if (propertyEditor
instanceof LightDaoAware
) {
55 ((LightDaoAware
) propertyEditor
).setLightDaoSupport(this);
59 List
<Reference
> references
= new ArrayList
<Reference
>();
61 for (Resource res
: resources
) {
62 InputStream in
= null;
64 in
= res
.getInputStream();
66 } catch (Exception e
) {
67 throw new ArgeoException("Cannot load stream", e
);
69 IOUtils
.closeQuietly(in
);
74 for (Reference ref
: references
) {
77 if (log
.isDebugEnabled())
78 log
.debug(references
.size() + " references linked");
81 public List
<Class
<?
>> getSupportedClasses() {
82 List
<Class
<?
>> res
= new ArrayList
<Class
<?
>>();
83 res
.addAll(additionalClasses
);
84 res
.addAll(model
.keySet());
88 protected void injectReference(Reference reference
) {
89 BeanWrapper bw
= new BeanWrapperImpl(reference
.object
);
91 if (reference
.getExternalRef() != null) {
92 String ref
= reference
.getExternalRef();
93 if (externalRefs
.containsKey(ref
))
94 targetObject
= externalRefs
.get(ref
);
95 else if (applicationContext
!= null)
96 targetObject
= applicationContext
.getBean(ref
);
99 log
.warn("Ref " + ref
+ " not found");
102 targetObject
= findInternalRef(reference
);
104 bw
.setPropertyValue(reference
.property
, targetObject
);
108 protected BeanWrapper
newBeanWrapper(Class
<?
> targetClass
) {
109 BeanWrapperImpl bw
= new BeanWrapperImpl(targetClass
);
110 for (Class
<?
> clss
: customEditors
.keySet())
111 bw
.registerCustomEditor(clss
, customEditors
.get(clss
));
115 @SuppressWarnings("unchecked")
116 public <T
> T
getByKey(Class
<T
> clss
, Object key
) {
118 throw new ArgeoException("Key is null for " + clss
);
119 return (T
) model
.get(findClass(clss
)).get(key
);
125 * @return the first found
127 public <T
> T
getByField(Class
<T
> clss
, String field
, Object value
) {
128 List
<T
> all
= list(clss
, null);
131 if (new BeanWrapperImpl(obj
).getPropertyValue(field
).equals(value
)) {
139 @SuppressWarnings("unchecked")
140 public <T
> List
<T
> list(Class
<T
> clss
, Object filter
) {
141 List
<T
> res
= new ArrayList
<T
>();
143 Class classToUse
= findClass(clss
);
144 if (classToUse
!= null)
145 res
.addAll((Collection
<T
>) model
.get(classToUse
).values());
147 if (applicationContext
!= null)
148 res
.addAll(new GenericBeanFactoryAccessor(applicationContext
)
149 .getBeansOfType(clss
).values());
154 @SuppressWarnings("unchecked")
155 protected Class
findClass(Class parent
) {
156 if (model
.containsKey(parent
))
159 for (Class clss
: model
.keySet()) {
160 if (parent
.isAssignableFrom(clss
))
161 return clss
;// return the first found
163 if (log
.isDebugEnabled())
164 log
.warn("No class found for " + parent
.getName());
168 public void setApplicationContext(ApplicationContext applicationContext
)
169 throws BeansException
{
170 this.applicationContext
= applicationContext
;
174 * When it should be stored under a different class (e.g. super class or
177 public void saveOrUpdate(Object key
, Object value
, Class
<?
> clss
) {
178 if (!model
.containsKey(clss
))
179 model
.put(clss
, new TreeMap
<Object
, Object
>());
180 model
.get(clss
).put(key
, value
);
183 protected ClassLoader
getClassLoader() {
187 public void setExternalRefs(Map
<String
, Object
> externalRefs
) {
188 this.externalRefs
= externalRefs
;
191 public Map
<String
, Object
> getExternalRefs() {
195 public void setScannedPackages(List
<String
> scannedPackages
) {
196 this.scannedPackages
= scannedPackages
;
199 public List
<String
> getScannedPackages() {
200 return scannedPackages
;
203 public void setResources(List
<Resource
> workbooks
) {
204 this.resources
= workbooks
;
207 public List
<Resource
> getResources() {
211 public void setClassLoader(ClassLoader classLoader
) {
212 this.classLoader
= classLoader
;
215 public List
<Class
<?
>> getAdditionalClasses() {
216 return additionalClasses
;
219 public void setAdditionalClasses(List
<Class
<?
>> additionalClasses
) {
220 this.additionalClasses
= additionalClasses
;
223 public void setCustomEditors(Map
<Class
<?
>, PropertyEditor
> propertyEditors
) {
224 this.customEditors
= propertyEditors
;
227 protected static class Reference
{
228 private Object object
;
229 private String property
;
230 private String externalRef
;
232 public Reference(Object object
, String property
, String externalRef
) {
233 this.object
= object
;
234 this.property
= property
;
235 this.externalRef
= externalRef
;
238 public Object
getObject() {
242 public String
getProperty() {
246 public String
getExternalRef() {