1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.database;
35
36 import fr.paris.lutece.portal.service.plugin.PluginEvent;
37 import fr.paris.lutece.portal.service.plugin.PluginEventListener;
38 import fr.paris.lutece.portal.service.plugin.PluginService;
39 import fr.paris.lutece.util.sql.TransactionManager;
40
41 import org.apache.commons.lang3.StringUtils;
42 import org.apache.logging.log4j.LogManager;
43 import org.apache.logging.log4j.Logger;
44 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
45
46 import java.io.PrintWriter;
47
48 import java.sql.Connection;
49 import java.sql.SQLException;
50 import java.sql.SQLFeatureNotSupportedException;
51
52 import javax.sql.DataSource;
53
54
55
56
57
58
59 public class DAOUtilTransactionManager extends DataSourceTransactionManager implements PluginEventListener
60 {
61 private static final long serialVersionUID = -654531540978261621L;
62 private transient Logger _logger = LogManager.getLogger( "lutece.debug.sql.tx" );
63 private String _strPluginName;
64 private boolean _bInit;
65
66
67
68
69 public DAOUtilTransactionManager( )
70 {
71 PluginService.registerPluginEventListener( this );
72 }
73
74
75
76
77
78
79 public String getPluginName( )
80 {
81 return _strPluginName;
82 }
83
84
85
86
87
88
89
90 public void setPluginName( String strPluginName )
91 {
92 _strPluginName = strPluginName;
93 }
94
95
96
97
98 @Override
99 public void processPluginEvent( PluginEvent event )
100 {
101 if ( getPluginName( ).equals( event.getPlugin( ).getName( ) ) )
102 {
103 if ( ( event.getEventType( ) == PluginEvent.PLUGIN_INSTALLED ) || ( event.getEventType( ) == PluginEvent.PLUGIN_POOL_CHANGED ) )
104 {
105 if ( StringUtils.isNotBlank( event.getPlugin( ).getDbPoolName( ) )
106 && !AppConnectionService.NO_POOL_DEFINED.equals( event.getPlugin( ).getDbPoolName( ) ) )
107 {
108 try
109 {
110 getLogger( ).debug( "DAOUtilTransactionManager changed datasource status..." );
111 setDataSource( AppConnectionService.getPoolManager( ).getDataSource( event.getPlugin( ).getDbPoolName( ) ) );
112 _bInit = true;
113 }
114 catch( Exception ex )
115 {
116 _bInit = false;
117 getLogger( ).error(
118 "An error occured getting pool for DAOUtilTransactionManager for plugin {} , please check plugin is activated and pool is correctly set : {}",
119 event.getPlugin( ).getName( ), ex.getMessage( ), ex );
120 }
121 }
122 else
123 {
124 getLogger( ).debug( "Pool for plugin {} is set to null, clearing transaction manager", event.getPlugin( ).getName( ) );
125 setDataSource( null );
126 _bInit = false;
127 }
128 }
129 else
130 if ( event.getEventType( ) == PluginEvent.PLUGIN_UNINSTALLED )
131 {
132 setDataSource( null );
133 _bInit = false;
134 }
135 }
136 }
137
138 private Logger getLogger( )
139 {
140 if ( _logger == null )
141 {
142 _logger = LogManager.getLogger( "lutece.debug.sql.tx" );
143 }
144 return _logger;
145 }
146
147
148
149
150
151
152 @Override
153 public DataSource getDataSource( )
154 {
155 if ( _bInit )
156 {
157 return super.getDataSource( );
158 }
159
160
161
162
163 return new EmptyDataSource( );
164 }
165
166
167
168
169 private static class EmptyDataSource implements DataSource
170 {
171 @Override
172 public <T> T unwrap( Class<T> iface ) throws SQLException
173 {
174 return null;
175 }
176
177 @Override
178 public boolean isWrapperFor( Class<?> iface ) throws SQLException
179 {
180 return false;
181 }
182
183 @Override
184 public void setLoginTimeout( int seconds ) throws SQLException
185 {
186
187 }
188
189 @Override
190 public void setLogWriter( PrintWriter out ) throws SQLException
191 {
192
193 }
194
195 @Override
196 public int getLoginTimeout( ) throws SQLException
197 {
198 return 0;
199 }
200
201 @Override
202 public PrintWriter getLogWriter( ) throws SQLException
203 {
204 return null;
205 }
206
207 @Override
208 public Connection getConnection( String username, String password ) throws SQLException
209 {
210 return null;
211 }
212
213 @Override
214 public Connection getConnection( ) throws SQLException
215 {
216 return null;
217 }
218
219
220
221
222
223
224
225
226 public java.util.logging.Logger getParentLogger( ) throws SQLFeatureNotSupportedException
227 {
228 throw new SQLFeatureNotSupportedException( );
229 }
230 }
231 }