DataSourcePoolImpl.java
7.39 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.osgi.service.log.LogService
*/
package com.day.commons.datasource.poolservice.impl;
import com.day.commons.datasource.poolservice.DataSourceNotFoundException;
import com.day.commons.datasource.poolservice.DataSourcePool;
import com.day.commons.datasource.poolservice.DataSourcePoolProvider;
import com.day.commons.datasource.poolservice.TypeNotAvailableException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
import org.osgi.service.log.LogService;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class DataSourcePoolImpl
implements DataSourcePool {
private DataSourcePoolProvider[] dataSourcePoolProviders;
private Set<String> jndiNames = new HashSet<String>();
private LogService log;
private InitialContextFactory contextFactory;
private Context context;
@Override
public <AdapterType> AdapterType getDataSource(String name, Class<AdapterType> desiredType) throws DataSourceNotFoundException, TypeNotAvailableException {
Object result = this.getDataSource(name);
TypeNotAvailableException e = TypeNotAvailableException.checkCompatible(result, desiredType, "DataSource with name '" + name + "'");
if (e != null) {
throw e;
}
return (AdapterType)result;
}
@Override
public Object getDataSource(String name) throws DataSourceNotFoundException {
Object result = null;
int nProviders = 0;
DataSourcePoolProvider[] svc = this.getProviders();
if (name != null && svc != null) {
nProviders = svc.length;
for (DataSourcePoolProvider provider : svc) {
if (this.log != null) {
this.log.log(4, "Asking provider " + provider.getClass().getName() + " for data source " + name);
}
if ((result = provider.getDataSource(name)) != null) break;
}
}
if (this.log != null) {
this.log.log(4, "getDataSource(" + name + ")=" + result);
}
if (result == null) {
throw new DataSourceNotFoundException(name, nProviders);
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Set<String> getAllJndiDataSourceNames() {
DataSourcePoolImpl dataSourcePoolImpl = this;
synchronized (dataSourcePoolImpl) {
return new HashSet<String>(this.jndiNames);
}
}
DataSourcePoolProvider[] getProviders() {
return this.dataSourcePoolProviders;
}
synchronized void bindInitialContextFactory(InitialContextFactory initialContextFactory) {
if (this.contextFactory != null) {
this.unbindInitialContextFactory(this.contextFactory);
}
this.contextFactory = initialContextFactory;
if (this.dataSourcePoolProviders != null) {
for (DataSourcePoolProvider dataSourcePoolProvider : this.dataSourcePoolProviders) {
this.jndiBind(dataSourcePoolProvider);
}
}
}
synchronized void unbindInitialContextFactory(InitialContextFactory initialContextFactory) {
if (this.contextFactory == initialContextFactory) {
if (this.dataSourcePoolProviders != null) {
for (DataSourcePoolProvider dataSourcePoolProvider : this.dataSourcePoolProviders) {
this.jndiUnbind(dataSourcePoolProvider);
}
}
this.contextFactory = null;
if (this.context != null) {
try {
this.context.close();
}
catch (NamingException ne) {
// empty catch block
}
this.context = null;
}
}
}
protected synchronized void bindDataSourcePoolProvider(DataSourcePoolProvider dataSourcePoolProvider) {
HashSet<DataSourcePoolProvider> providerSet = new HashSet<DataSourcePoolProvider>();
if (this.dataSourcePoolProviders != null) {
providerSet.addAll(Arrays.asList(this.dataSourcePoolProviders));
}
providerSet.add(dataSourcePoolProvider);
this.dataSourcePoolProviders = providerSet.toArray(new DataSourcePoolProvider[providerSet.size()]);
this.jndiBind(dataSourcePoolProvider);
}
protected synchronized void unbindDataSourcePoolProvider(DataSourcePoolProvider dataSourcePoolProvider) {
this.jndiUnbind(dataSourcePoolProvider);
HashSet<DataSourcePoolProvider> providerSet = new HashSet<DataSourcePoolProvider>();
if (this.dataSourcePoolProviders != null) {
providerSet.addAll(Arrays.asList(this.dataSourcePoolProviders));
}
providerSet.remove(dataSourcePoolProvider);
this.dataSourcePoolProviders = providerSet.toArray(new DataSourcePoolProvider[providerSet.size()]);
}
private void jndiBind(DataSourcePoolProvider dataSourcePoolProvider) {
Context context = this.getContext();
if (context != null) {
String[] names;
for (String name : names = dataSourcePoolProvider.getNames()) {
if (name.indexOf(58) <= 0) continue;
try {
context.bind(name, dataSourcePoolProvider.getDataSource(name));
this.jndiNames.add(name);
this.log(3, "Registered datasource as " + name, null);
continue;
}
catch (NamingException e) {
this.log(2, "Unable to bind data source " + name, e);
}
}
}
}
private void jndiUnbind(DataSourcePoolProvider dataSourcePoolProvider) {
Context context = this.getContext();
if (context != null) {
String[] names;
for (String name : names = dataSourcePoolProvider.getNames()) {
if (name.indexOf(58) <= 0) continue;
try {
this.jndiNames.remove(name);
context.unbind(name);
this.log(3, "Unregistered datasource as " + name, null);
continue;
}
catch (NamingException e) {
this.log(2, "Unable to unbind data source " + name, e);
}
}
}
}
private Context getContext() {
InitialContextFactory contextFactory;
if (this.context == null && (contextFactory = this.contextFactory) != null) {
try {
this.context = contextFactory.getInitialContext(null);
}
catch (NamingException ne) {
this.log(1, "getContext: Cannot get Context from " + contextFactory, ne);
}
}
return this.context;
}
private void log(int level, String message, Throwable t) {
LogService log = this.log;
if (log != null) {
log.log(level, message, t);
}
}
protected void bindLog(LogService logService) {
this.log = logService;
}
protected void unbindLog(LogService logService) {
if (this.log == logService) {
this.log = null;
}
}
}