1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.ds;
17
18 import java.io.IOException;
19 import java.io.ObjectInputStream;
20 import java.sql.PreparedStatement;
21 import java.sql.SQLException;
22
23 import javax.naming.InitialContext;
24 import javax.naming.NamingException;
25 import javax.sql.DataSource;
26
27
28
29
30 public final class JndiDataSourceProxy implements DataSourceProxy
31 {
32
33
34
35
36
37
38
39 private static final long serialVersionUID = 1L;
40
41
42
43
44
45
46
47
48 private final String mappedName;
49
50
51
52
53 private transient DataSource dataSource;
54
55
56
57
58
59
60
61
62
63
64
65
66 public JndiDataSourceProxy(final String mappedName) throws NamingException
67 {
68 this.mappedName = mappedName;
69 this.dataSource = fetchDataSource(mappedName);
70 }
71
72
73
74 private static DataSource fetchDataSource(final String mappedName)
75 throws NamingException
76 {
77 final InitialContext context = new InitialContext();
78 final DataSource dataSource = (DataSource) context.lookup(mappedName);
79 return dataSource;
80 }
81
82
83
84
85
86
87
88 @Override
89 public String getDataSourceId()
90 {
91 return mappedName;
92 }
93
94 @Override
95 public DataSource getDataSource()
96 {
97 return dataSource;
98 }
99
100 @Override
101 public String getCreateTableSqlTemplate()
102 {
103 return null;
104 }
105
106 @Override
107 public String getInsertOrUpdateSqlTemplate()
108 {
109 return null;
110 }
111
112 @Override
113 public void setInsertOrUpdate(final PreparedStatement statement,
114 final String config, final String name, final String value)
115 throws SQLException
116 {
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130 private void readObject(final ObjectInputStream in) throws IOException,
131 ClassNotFoundException
132 {
133 in.defaultReadObject();
134
135 try
136 {
137 this.dataSource = fetchDataSource(mappedName);
138 }
139 catch (final NamingException e)
140 {
141 throw new IOException(
142 "Cannot restore data source from JNDI context with mapped name '"
143 + mappedName + "'.", e);
144 }
145 }
146
147
148
149
150
151
152 @Override
153 public String toString()
154 {
155 final StringBuilder buffer = new StringBuilder();
156
157 buffer.append("Data Source Mapped Name: ").append(mappedName);
158
159 return buffer.toString();
160 }
161 }