1 /***
2 * License Agreement.
3 *
4 * JSPA (POJO-SP)
5 *
6 * Copyright (C) 2009 HRX Pty Ltd
7 *
8 * This file is part of JSPA.
9 *
10 * JSPA is free software: you can redistribute it and/or modify it under the
11 * terms of the GNU Lesser General Public License version 3 as published by the
12 * Free Software Foundation.
13 *
14 * JSPA is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with JSPA. If not, see <http://www.gnu.org/licenses/>.
20 */
21 package com.hrx.rasp.util.dao.ejb.injection;
22
23 import java.lang.reflect.Field;
24
25 import javax.annotation.PostConstruct;
26 import javax.annotation.Resource;
27 import javax.ejb.EJBException;
28 import javax.ejb.PostActivate;
29 import javax.ejb.PrePassivate;
30 import javax.interceptor.InvocationContext;
31 import javax.sql.DataSource;
32
33 import org.apache.log4j.Logger;
34
35 import com.hrx.rasp.util.dao.DAOManager;
36 import com.hrx.rasp.util.dao.ejb.DAOManagerFactory;
37 import com.hrx.rasp.util.dao.ejb.PersistenceDAO;
38
39 /***
40 * @author dan.stoica <dan.stoica@acslink.net.au>
41 *
42 */
43 public class DAOManagerInjector
44 {
45 private static Logger log = Logger.getLogger(DAOManagerInjector.class);
46
47 @Resource(name = DAOManagerFactory.DEFAULT_DATA_SOURCE_NAME)
48 private DataSource ds;
49
50 public DAOManagerInjector()
51 {
52 log.trace("RASP: Initialize DAOManagerInjector.");
53 }
54
55 @PostConstruct
56 @PostActivate
57 public void injectDAOManager(InvocationContext invocation)
58 {
59 Object target = invocation.getTarget();
60 Class<?> clazz = target.getClass();
61
62 try
63 {
64 invocation.proceed();
65 }
66 catch (Exception e)
67 {
68 throw new EJBException(e);
69 }
70
71 do
72 {
73 setPersistenceDAO(target, clazz);
74 clazz = clazz.getSuperclass();
75 }
76 while (clazz != Object.class);
77 }
78
79 /***
80 * @param target
81 * @param clazz
82 */
83 private void setPersistenceDAO(Object target, Class<?> clazz)
84 {
85 Field[] fields = clazz.getDeclaredFields();
86
87
88 try
89 {
90 for (Field field : fields)
91 {
92 if (field.isAnnotationPresent(PersistenceDAO.class))
93 {
94 log.debug("Initilize the DAOManager.");
95 PersistenceDAO manager = field.getAnnotation(PersistenceDAO.class);
96 String dataSourceName = manager.dataSourceMappedName();
97 field.setAccessible(true);
98 DAOManager dm = dataSourceName.isEmpty() ? DAOManagerFactory.create(ds) : DAOManagerFactory.create(dataSourceName);
99 field.set(target, dm);
100 }
101 }
102 }
103 catch (Exception e)
104 {
105 throw new EJBException("Failed to inject DAOManager ", e);
106 }
107 }
108
109 @PrePassivate
110 public void removeDAOManager(InvocationContext invocation)
111 {
112 Object target = invocation.getTarget();
113 Class<?> clazz = target.getClass();
114 do
115 {
116 removeDAOManagers(target, clazz);
117 clazz = clazz.getSuperclass();
118 }
119 while (clazz != Object.class);
120
121 try
122 {
123 invocation.proceed();
124 }
125 catch (Exception e)
126 {
127 throw new EJBException(e);
128 }
129 }
130
131 /***
132 * @param target
133 * @param clazz
134 */
135 private void removeDAOManagers(Object target, Class<?> clazz)
136 {
137 Field[] fields = clazz.getDeclaredFields();
138
139 try
140 {
141 for (Field field : fields)
142 {
143 if (field.isAnnotationPresent(PersistenceDAO.class))
144 {
145 log.debug("PrePassivate: Set the DAOManager field to null, before passivate.");
146
147 field.setAccessible(true);
148 field.set(target, null);
149 }
150 }
151 }
152 catch (Exception e)
153 {
154 throw new EJBException("PrePassivate: Failed to set a null value for the DAOManager field." + target.getClass().getCanonicalName(), e);
155 }
156 }
157 }