private DeviceRepository createJDBCDeviceRepository(
final String repositoryURL,
final String unknownDevicesLogFileName)
throws DeviceRepositoryException {
ensureDriverClassesLoaded();
JDBCDeviceRepositoryConfiguration configuration =
deviceRepositoryFactory.createJDBCDeviceRepositoryConfiguration();
Map params = getJDBCURLParams(repositoryURL);
String userName = (String) params.remove(JDBC_USERNAME);
String password = (String) params.remove(JDBC_PASSWORD);
String defaultProject = (String) params.remove(JDBC_DEFAULT_PROJECT);
if (defaultProject == null) {
throw new DeviceRepositoryException(EXCEPTION_LOCALIZER.
format(INVALID_JDBC_URL_KEY, new String [] {
repositoryURL, JDBC_URL_MESSAGE}));
}
// remove the processed parameters
int paramStart = repositoryURL.indexOf("?");
final String baseURL;
if (paramStart != -1) {
baseURL = repositoryURL.substring(0, paramStart);
} else {
baseURL = repositoryURL;
}
final StringBuffer baseURLBuffer = new StringBuffer(baseURL);
boolean first = true;
for (Iterator iter = params.entrySet().iterator(); iter.hasNext();) {
final Map.Entry entry = (Map.Entry) iter.next();
if (first) {
baseURLBuffer.append('?');
first = false;
} else {
baseURLBuffer.append('&');
}
final String key = (String) entry.getKey();
baseURLBuffer.append(key);
final String value = (String) entry.getValue();
if (value != null) {
baseURLBuffer.append('=');
baseURLBuffer.append(value);
}
}
DataSource dataSource = new DriverDataSource(baseURLBuffer.toString());
if (userName != null) {
// Since we have a username lets use a confusingly
// named AnonymousDataSource.
dataSource = jdbcRepositoryFactory.createAnonymousDataSource(
dataSource, userName, password);
}
configuration.setDataSource(dataSource);
configuration.setDefaultProject(defaultProject);
configuration.setUnknownDevicesLogFileName(unknownDevicesLogFileName);
return deviceRepositoryFactory.createDeviceRepository(configuration);
}