git_sitools_idoc / flarecast / workspace / sitools-install-izpack / src / fr / cnes / sitools / izpack / validator / JDBCConnectionValidator.java @ master
1 |
/*******************************************************************************
|
---|---|
2 |
* Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
|
3 |
*
|
4 |
* This file is part of SITools2.
|
5 |
*
|
6 |
* SITools2 is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* SITools2 is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with SITools2. If not, see <http://www.gnu.org/licenses/>.
|
18 |
******************************************************************************/
|
19 |
package fr.cnes.sitools.izpack.validator; |
20 |
|
21 |
import java.sql.Connection; |
22 |
import java.sql.DriverManager; |
23 |
import java.sql.ResultSet; |
24 |
import java.sql.SQLException; |
25 |
import java.sql.Statement; |
26 |
import java.util.ArrayList; |
27 |
import java.util.Iterator; |
28 |
|
29 |
import com.izforge.izpack.installer.AutomatedInstallData; |
30 |
import com.izforge.izpack.installer.DataValidator; |
31 |
|
32 |
import fr.cnes.sitools.izpack.model.JDBCConnectionModel; |
33 |
import fr.cnes.sitools.izpack.model.JDBCConnectionModelFactory; |
34 |
|
35 |
/**
|
36 |
* IzPackValidator for JDBCConnexion Validate that the connection settings
|
37 |
* entered by the user are good and that the database can be accessed
|
38 |
*
|
39 |
* @author m.gond (AKKA Technologies)
|
40 |
*
|
41 |
*/
|
42 |
public class JDBCConnectionValidator implements DataValidator { |
43 |
/**
|
44 |
* The model of the connection model
|
45 |
*/
|
46 |
private JDBCConnectionModel jdbcModel;
|
47 |
|
48 |
/**
|
49 |
* Constants for JDBC driver classes
|
50 |
*/
|
51 |
|
52 |
/** Contains the error trace */
|
53 |
private ArrayList<String> trace; |
54 |
|
55 |
/**
|
56 |
* Validate the data
|
57 |
*
|
58 |
* @param aid
|
59 |
* the data
|
60 |
* @return a Status telling whether it is OK or ERROR
|
61 |
*/
|
62 |
@Override
|
63 |
public Status validateData(AutomatedInstallData aid) {
|
64 |
trace = new ArrayList<String>(); |
65 |
this.mapParams(aid);
|
66 |
if (jdbcModel.getDbType().equals("hsqldb")) { |
67 |
return Status.OK;
|
68 |
} |
69 |
return testDatasource();
|
70 |
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public boolean getDefaultAnswer() { |
75 |
return false; |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public String getErrorMessageId() { |
80 |
String errorMsg = ""; |
81 |
for (Iterator<String> iterator = trace.iterator(); iterator.hasNext();) { |
82 |
String err = iterator.next();
|
83 |
errorMsg += err + "\n";
|
84 |
} |
85 |
return errorMsg;
|
86 |
|
87 |
} |
88 |
|
89 |
@Override
|
90 |
public String getWarningMessageId() { |
91 |
return "input.warning.database"; |
92 |
} |
93 |
|
94 |
/**
|
95 |
* Test the datasource with the class parameters
|
96 |
*
|
97 |
* @return true if the datasource is correctly configurate, false otherwise
|
98 |
*/
|
99 |
private Status testDatasource() {
|
100 |
Connection cnxUsr = null; |
101 |
Statement statUsr = null; |
102 |
|
103 |
Status result = Status.OK; |
104 |
try {
|
105 |
do {
|
106 |
trace.add("Test jdbc data source connection for user ...");
|
107 |
try {
|
108 |
Class.forName(jdbcModel.getDbDriverClassName());
|
109 |
trace.add("Load driver class : OK");
|
110 |
} |
111 |
catch (Exception e) { |
112 |
result = Status.ERROR; |
113 |
trace.add("Load driver class failed. Cause: " + e.getMessage());
|
114 |
break;
|
115 |
} |
116 |
|
117 |
try {
|
118 |
cnxUsr = DriverManager.getConnection(jdbcModel.getDbUrl(), jdbcModel.getDbUser(), jdbcModel.getDbPassword());
|
119 |
} |
120 |
catch (Exception e) { |
121 |
result = Status.ERROR; |
122 |
trace.add("Get connection failed. Cause: " + e.getMessage());
|
123 |
break;
|
124 |
} |
125 |
|
126 |
try {
|
127 |
statUsr = cnxUsr.createStatement(); |
128 |
statUsr.executeQuery("SELECT 1");
|
129 |
trace.add("Execute statement on connection : OK");
|
130 |
|
131 |
if (jdbcModel.getDbType().equals("postgresql")) { |
132 |
statUsr = cnxUsr.createStatement(); |
133 |
ResultSet rs = statUsr
|
134 |
.executeQuery("select exists (select * from pg_catalog.pg_namespace where nspname = '"
|
135 |
+ jdbcModel.getDbSchema() + "');");
|
136 |
|
137 |
rs.next(); |
138 |
boolean exists = rs.getBoolean(1); |
139 |
if (exists) {
|
140 |
result = Status.WARNING; |
141 |
} |
142 |
} |
143 |
} |
144 |
catch (Exception e) { |
145 |
result = Status.ERROR; |
146 |
trace.add("Execute statement on connection failed. Cause: " + e.getMessage());
|
147 |
break;
|
148 |
} |
149 |
|
150 |
} while (false); |
151 |
|
152 |
} |
153 |
finally {
|
154 |
if (statUsr != null) { |
155 |
try {
|
156 |
statUsr.close(); |
157 |
} |
158 |
catch (SQLException e) { |
159 |
|
160 |
} |
161 |
} |
162 |
if (cnxUsr != null) { |
163 |
try {
|
164 |
cnxUsr.close(); |
165 |
} |
166 |
catch (SQLException e) { |
167 |
|
168 |
} |
169 |
} |
170 |
} |
171 |
return result;
|
172 |
} |
173 |
|
174 |
/**
|
175 |
* Map the ProcessingClient parameters to the class properties.
|
176 |
*
|
177 |
* @param aid
|
178 |
* the data
|
179 |
*/
|
180 |
private void mapParams(AutomatedInstallData aid) { |
181 |
jdbcModel = JDBCConnectionModelFactory.getModel(aid); |
182 |
|
183 |
} |
184 |
} |