1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.tests.services.persistence.generic.dao;
18
19 import org.springframework.beans.BeansException;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.context.ApplicationContextAware;
22
23 import ch.elca.el4j.core.context.ModuleApplicationContext;
24 import ch.elca.el4j.core.context.ModuleApplicationListener;
25 import ch.elca.el4j.services.persistence.generic.dao.DaoRegistry;
26 import ch.elca.el4j.services.persistence.generic.dao.GenericDao;
27
28 /**
29 * This impatient class tries to access the daoRegistry before the Spring context is initialized.
30 *
31 * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/services/persistence/generic/dao/ImpatientClass.java $
32 *
33 * @author Stefan Wismer (SWI)
34 */
35 public class ImpatientClass implements ApplicationContextAware, ModuleApplicationListener {
36 /**
37 * The Spring application context.
38 */
39 private ModuleApplicationContext m_applicationContext;
40
41 /**
42 * The dao registry.
43 */
44 private DaoRegistry m_daoRegistry;
45
46 /**
47 * The thread that tries to access the daoRegistry too early.
48 */
49 private Thread m_impatientThread;
50
51 /**
52 * Is context ready?
53 */
54 private volatile boolean m_contextIsReady = false;
55
56 /**
57 * Was test successful?
58 */
59 private boolean m_successful = false;
60
61 /** {@inheritDoc} */
62 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
63 m_applicationContext = (ModuleApplicationContext) applicationContext;
64 }
65
66 /** {@inheritDoc} */
67 public void onContextRefreshed() {
68 m_contextIsReady = true;
69 }
70 /**
71 * @return the DAO registry
72 */
73 public DaoRegistry getDaoRegistry() {
74 return m_daoRegistry;
75 }
76
77 /**
78 * @param daoRegistry the DAO registry
79 */
80 public void setDaoRegistry(final DaoRegistry daoRegistry) {
81 m_daoRegistry = daoRegistry;
82
83 // access the dao registry before Spring context is completely initialized
84 m_impatientThread = new Thread(new Runnable() {
85 public void run() {
86 GenericDao<?> dao = daoRegistry.getFor(String.class);
87 dao.getClass();
88
89 // if daoRegistry access is done before context is ready -> waiting for completely
90 // initialized Spring context didn't work!!!
91
92 if (m_contextIsReady || m_applicationContext.isRefreshed()) {
93 m_successful = true;
94 }
95 }
96 });
97 m_impatientThread.start();
98
99 // delay the Spring context initialization (100ms, see thread)
100 try {
101 Thread.sleep(100);
102 } catch (InterruptedException e) { }
103 }
104
105 /**
106 * Wait until impatient thread finally has accessed the DAO registry.
107 *
108 * @return <code>true</code> if test was successful.
109 */
110 public boolean join() {
111 try {
112 m_impatientThread.join();
113 } catch (InterruptedException e) { }
114
115 return m_successful;
116 }
117 }