2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.server
.dao
;
18 import java
.beans
.PropertyEditor
;
19 import java
.io
.InputStream
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Collection
;
22 import java
.util
.HashMap
;
23 import java
.util
.List
;
25 import java
.util
.TreeMap
;
27 import org
.apache
.commons
.io
.IOUtils
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.ArgeoException
;
31 import org
.springframework
.beans
.BeanWrapper
;
32 import org
.springframework
.beans
.BeanWrapperImpl
;
33 import org
.springframework
.beans
.BeansException
;
34 import org
.springframework
.beans
.factory
.InitializingBean
;
35 import org
.springframework
.context
.ApplicationContext
;
36 import org
.springframework
.context
.ApplicationContextAware
;
37 import org
.springframework
.core
.io
.Resource
;
39 public abstract class AbstractMemoryDaoSupport
implements LightDaoSupport
,
40 ApplicationContextAware
, InitializingBean
{
41 private final static Log log
= LogFactory
42 .getLog(AbstractMemoryDaoSupport
.class);
44 private ClassLoader classLoader
= getClass().getClassLoader();
45 private ApplicationContext applicationContext
;
46 private List
<Class
<?
>> additionalClasses
= new ArrayList
<Class
<?
>>();
48 private Map
<Class
<?
>, Map
<Object
, Object
>> model
= new HashMap
<Class
<?
>, Map
<Object
, Object
>>();
50 private Map
<String
, Object
> externalRefs
= new HashMap
<String
, Object
>();
52 private List
<String
> scannedPackages
= new ArrayList
<String
>();
54 private List
<Resource
> resources
= new ArrayList
<Resource
>();
56 private Map
<Class
<?
>, PropertyEditor
> customEditors
= new HashMap
<Class
<?
>, PropertyEditor
>();;
58 protected abstract void load(InputStream in
, List
<Reference
> references
);
60 protected abstract Object
findInternalRef(Reference reference
);
62 public void afterPropertiesSet() throws Exception
{
67 for (PropertyEditor propertyEditor
: customEditors
.values())
68 if (propertyEditor
instanceof LightDaoAware
) {
69 ((LightDaoAware
) propertyEditor
).setLightDaoSupport(this);
73 List
<Reference
> references
= new ArrayList
<Reference
>();
75 for (Resource res
: resources
) {
76 InputStream in
= null;
78 in
= res
.getInputStream();
80 } catch (Exception e
) {
81 throw new ArgeoException("Cannot load stream", e
);
83 IOUtils
.closeQuietly(in
);
88 for (Reference ref
: references
) {
91 if (log
.isDebugEnabled())
92 log
.debug(references
.size() + " references linked");
95 public List
<Class
<?
>> getSupportedClasses() {
96 List
<Class
<?
>> res
= new ArrayList
<Class
<?
>>();
97 res
.addAll(additionalClasses
);
98 res
.addAll(model
.keySet());
102 protected void injectReference(Reference reference
) {
103 BeanWrapper bw
= new BeanWrapperImpl(reference
.object
);
105 if (reference
.getExternalRef() != null) {
106 String ref
= reference
.getExternalRef();
107 if (externalRefs
.containsKey(ref
))
108 targetObject
= externalRefs
.get(ref
);
109 else if (applicationContext
!= null)
110 targetObject
= applicationContext
.getBean(ref
);
113 log
.warn("Ref " + ref
+ " not found");
116 targetObject
= findInternalRef(reference
);
118 bw
.setPropertyValue(reference
.property
, targetObject
);
122 protected BeanWrapper
newBeanWrapper(Class
<?
> targetClass
) {
123 BeanWrapperImpl bw
= new BeanWrapperImpl(targetClass
);
124 for (Class
<?
> clss
: customEditors
.keySet())
125 bw
.registerCustomEditor(clss
, customEditors
.get(clss
));
129 @SuppressWarnings("unchecked")
130 public <T
> T
getByKey(Class
<T
> clss
, Object key
) {
132 throw new ArgeoException("Key is null for " + clss
);
133 return (T
) model
.get(findClass(clss
)).get(key
);
139 * @return the first found
141 public <T
> T
getByField(Class
<T
> clss
, String field
, Object value
) {
142 List
<T
> all
= list(clss
, null);
145 if (new BeanWrapperImpl(obj
).getPropertyValue(field
).equals(value
)) {
153 @SuppressWarnings("unchecked")
154 public <T
> List
<T
> list(Class
<T
> clss
, Object filter
) {
155 List
<T
> res
= new ArrayList
<T
>();
157 Class classToUse
= findClass(clss
);
158 if (classToUse
!= null)
159 res
.addAll((Collection
<T
>) model
.get(classToUse
).values());
161 if (applicationContext
!= null)
162 res
.addAll(applicationContext
.getBeansOfType(clss
).values());
167 @SuppressWarnings("unchecked")
168 protected Class
findClass(Class parent
) {
169 if (model
.containsKey(parent
))
172 for (Class clss
: model
.keySet()) {
173 if (parent
.isAssignableFrom(clss
))
174 return clss
;// return the first found
176 if (log
.isDebugEnabled())
177 log
.warn("No class found for " + parent
.getName());
181 public void setApplicationContext(ApplicationContext applicationContext
)
182 throws BeansException
{
183 this.applicationContext
= applicationContext
;
187 * When it should be stored under a different class (e.g. super class or
190 public void saveOrUpdate(Object key
, Object value
, Class
<?
> clss
) {
191 if (!model
.containsKey(clss
))
192 model
.put(clss
, new TreeMap
<Object
, Object
>());
193 model
.get(clss
).put(key
, value
);
196 protected ClassLoader
getClassLoader() {
200 public void setExternalRefs(Map
<String
, Object
> externalRefs
) {
201 this.externalRefs
= externalRefs
;
204 public Map
<String
, Object
> getExternalRefs() {
208 public void setScannedPackages(List
<String
> scannedPackages
) {
209 this.scannedPackages
= scannedPackages
;
212 public List
<String
> getScannedPackages() {
213 return scannedPackages
;
216 public void setResources(List
<Resource
> workbooks
) {
217 this.resources
= workbooks
;
220 public List
<Resource
> getResources() {
224 public void setClassLoader(ClassLoader classLoader
) {
225 this.classLoader
= classLoader
;
228 public List
<Class
<?
>> getAdditionalClasses() {
229 return additionalClasses
;
232 public void setAdditionalClasses(List
<Class
<?
>> additionalClasses
) {
233 this.additionalClasses
= additionalClasses
;
236 public void setCustomEditors(Map
<Class
<?
>, PropertyEditor
> propertyEditors
) {
237 this.customEditors
= propertyEditors
;
240 protected static class Reference
{
241 private Object object
;
242 private String property
;
243 private String externalRef
;
245 public Reference(Object object
, String property
, String externalRef
) {
246 this.object
= object
;
247 this.property
= property
;
248 this.externalRef
= externalRef
;
251 public Object
getObject() {
255 public String
getProperty() {
259 public String
getExternalRef() {