01: /*
02: * Copyright (c) Mateusz Prokopowicz. All Rights Reserved.
03: */
04:
05: package com.technoetic.xplanner.domain.repository;
06:
07: import java.lang.reflect.InvocationTargetException;
08: import java.util.List;
09:
10: import net.sf.hibernate.HibernateException;
11: import org.springframework.beans.BeansException;
12: import org.springframework.beans.factory.BeanFactory;
13: import org.springframework.beans.factory.BeanFactoryAware;
14: import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
15:
16: /**
17: * User: mprokopowicz
18: * Date: Feb 8, 2006
19: * Time: 10:05:47 AM
20: */
21: public class ObjectRepositororyFactory implements BeanFactoryAware {
22: private AutowireCapableBeanFactory beanFactory;
23: private List delegates;
24:
25: public void setDelegates(List delegateList) {
26: this .delegates = delegateList;
27: }
28:
29: public ObjectRepository create(Class objectClass)
30: throws HibernateException, NoSuchMethodException,
31: IllegalAccessException, InvocationTargetException,
32: InstantiationException {
33: return create(objectClass, HibernateObjectRepository.class);
34: }
35:
36: public ObjectRepository create(Class objectClass,
37: Class objectRepositoryClass) throws HibernateException,
38: NoSuchMethodException, IllegalAccessException,
39: InvocationTargetException, InstantiationException {
40: ObjectRepository repository = (ObjectRepository) objectRepositoryClass
41: .getConstructor(new Class[] { Class.class })
42: .newInstance(new Object[] { objectClass });
43: beanFactory.autowireBeanProperties(repository,
44: AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
45: for (int i = 0; i < delegates.size(); i++) {
46: Class aClass = (Class) delegates.get(i);
47: repository = (ObjectRepository) aClass
48: .getConstructor(
49: new Class[] { Class.class,
50: ObjectRepository.class })
51: .newInstance(
52: new Object[] { objectClass, repository });
53: beanFactory.autowireBeanProperties(repository,
54: AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
55: }
56: return repository;
57: }
58:
59: public void setBeanFactory(BeanFactory beanFactory)
60: throws BeansException {
61: this .beanFactory = (AutowireCapableBeanFactory) beanFactory;
62: }
63: }
|