use of org.apache.cayenne.modeler.FileClassLoadingService in project cayenne by apache.
the class DataSourcePreferences method testDataSourceAction.
/**
* Tries to establish a DB connection, reporting the status of this
* operation.
*/
public void testDataSourceAction() {
DBConnectionInfo currentDataSource = getConnectionInfo();
if (currentDataSource == null) {
return;
}
if (currentDataSource.getJdbcDriver() == null) {
JOptionPane.showMessageDialog(null, "No JDBC Driver specified", "Warning", JOptionPane.WARNING_MESSAGE);
return;
}
if (currentDataSource.getUrl() == null) {
JOptionPane.showMessageDialog(null, "No Database URL specified", "Warning", JOptionPane.WARNING_MESSAGE);
return;
}
try {
FileClassLoadingService classLoader = new FileClassLoadingService();
List<File> oldPathFiles = ((FileClassLoadingService) getApplication().getClassLoadingService()).getPathFiles();
Collection<String> details = new ArrayList<>();
for (File oldPathFile : oldPathFiles) {
details.add(oldPathFile.getAbsolutePath());
}
Preferences classPathPreferences = getApplication().getPreferencesNode(ClasspathPreferences.class, "");
if (editor.getChangedPreferences().containsKey(classPathPreferences)) {
Map<String, String> map = editor.getChangedPreferences().get(classPathPreferences);
for (Map.Entry<String, String> en : map.entrySet()) {
String key = en.getKey();
if (!details.contains(key)) {
details.add(key);
}
}
}
if (editor.getRemovedPreferences().containsKey(classPathPreferences)) {
Map<String, String> map = editor.getRemovedPreferences().get(classPathPreferences);
for (Map.Entry<String, String> en : map.entrySet()) {
String key = en.getKey();
if (details.contains(key)) {
details.remove(key);
}
}
}
if (details.size() > 0) {
classLoader.setPathFiles(details.stream().map(File::new).collect(Collectors.toList()));
}
Class<Driver> driverClass = classLoader.loadClass(Driver.class, currentDataSource.getJdbcDriver());
Driver driver = driverClass.newInstance();
// connect via Cayenne DriverDataSource - it addresses some driver
// issues...
// can't use try with resource here as we can loose meaningful exception
Connection c = new DriverDataSource(driver, currentDataSource.getUrl(), currentDataSource.getUserName(), currentDataSource.getPassword()).getConnection();
try {
c.close();
} catch (SQLException ignored) {
// i guess we can ignore this...
}
JOptionPane.showMessageDialog(null, "Connected Successfully", "Success", JOptionPane.INFORMATION_MESSAGE);
} catch (Throwable th) {
th = Util.unwindException(th);
String message = "Error connecting to DB: " + th.getLocalizedMessage();
StringTokenizer st = new StringTokenizer(message);
StringBuilder sbMessage = new StringBuilder();
int len = 0;
String tempString;
while (st.hasMoreTokens()) {
tempString = st.nextElement().toString();
if (len < 110) {
len = len + tempString.length() + 1;
} else {
sbMessage.append("\n");
len = 0;
}
sbMessage.append(tempString).append(" ");
}
JOptionPane.showMessageDialog(null, sbMessage.toString(), "Warning", JOptionPane.WARNING_MESSAGE);
}
}
Aggregations