use of org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService in project pentaho-platform by pentaho.
the class NonPooledDatasourceService method retrieve.
@Override
protected DataSource retrieve(String dsName) throws DBDatasourceServiceException {
DataSource ds = null;
try {
IDatasourceMgmtService datasourceMgmtSvc = getDatasourceMgmtService();
IDatabaseConnection databaseConnection = datasourceMgmtSvc.getDatasourceByName(dsName);
if (databaseConnection != null) {
ds = resolveDatabaseConnection(databaseConnection);
if (ds != null) {
cacheManager.putInRegionCache(IDBDatasourceService.JDBC_DATASOURCE, dsName, ds);
}
} else {
throw new DBDatasourceServiceException(Messages.getInstance().getErrorString(// $NON-NLS-1$
"DatasourceService.ERROR_0002_UNABLE_TO_GET_DATASOURCE"));
}
} catch (DatasourceMgmtServiceException daoe) {
throw new DBDatasourceServiceException(Messages.getInstance().getErrorString("DatasourceService.ERROR_0002_UNABLE_TO_GET_DATASOURCE"), // $NON-NLS-1$
daoe);
}
return ds;
}
use of org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService in project pentaho-platform by pentaho.
the class NonPooledDatasourceSystemListener method getListOfDatabaseConnections.
@VisibleForTesting
protected List<IDatabaseConnection> getListOfDatabaseConnections(final IPentahoSession session) throws ObjectFactoryException, DatasourceMgmtServiceException {
IDatasourceMgmtService datasourceMgmtSvc = (IDatasourceMgmtService) PentahoSystem.getObjectFactory().get(IDatasourceMgmtService.class, session);
List<IDatabaseConnection> databaseConnections = datasourceMgmtSvc.getDatasources();
return databaseConnections;
}
use of org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService in project pentaho-platform by pentaho.
the class NonPooledOrJndiDatasourceService method retrieve.
@Override
protected DataSource retrieve(String dsName) throws DBDatasourceServiceException {
DataSource ds = null;
requestedDatasourceName = dsName;
try {
IDatasourceMgmtService datasourceMgmtSvc = getDatasourceMgmtService();
IDatabaseConnection databaseConnection = datasourceMgmtSvc.getDatasourceByName(dsName);
if (databaseConnection != null && !databaseConnection.getAccessType().equals(DatabaseAccessType.JNDI)) {
ds = resolveDatabaseConnection(databaseConnection);
// Database does not have the datasource, look in jndi now
} else {
try {
ds = getJndiDataSource(dsName);
} catch (DBDatasourceServiceException e) {
// Ignore, Maybe jndi name is specified as database name in the connection
}
}
if (ds == null && databaseConnection != null) {
ds = getJndiDataSource(databaseConnection.getDatabaseName());
}
// if the resulting datasource is not null then store it in the cache
if (ds != null) {
cacheManager.putInRegionCache(IDBDatasourceService.JDBC_DATASOURCE, dsName, ds);
}
} catch (DatasourceMgmtServiceException daoe) {
log.debug(Messages.getInstance().getErrorString("DatasourceService.DEBUG_0001_UNABLE_TO_FIND_DATASOURCE_IN_REPOSITORY", daoe.getLocalizedMessage()), daoe);
try {
return getJndiDataSource(dsName);
} catch (DBDatasourceServiceException dse) {
throw new DBDatasourceServiceException(Messages.getInstance().getErrorString("DatasourceService.ERROR_0003_UNABLE_TO_GET_JNDI_DATASOURCE"), // $NON-NLS-1$
dse);
}
}
return ds;
}
use of org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService in project pentaho-platform by pentaho.
the class SolutionImportHandler method importFile.
@Override
public void importFile(IPlatformImportBundle bundle) throws PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException, IOException {
RepositoryFileImportBundle importBundle = (RepositoryFileImportBundle) bundle;
ZipInputStream zipImportStream = new ZipInputStream(bundle.getInputStream());
SolutionRepositoryImportSource importSource = new SolutionRepositoryImportSource(zipImportStream);
LocaleFilesProcessor localeFilesProcessor = new LocaleFilesProcessor();
setOverwriteFile(bundle.overwriteInRepository());
// importSession.set(ImportSession.getSession());
IPlatformImporter importer = PentahoSystem.get(IPlatformImporter.class);
cachedImports = new HashMap<String, RepositoryFileImportBundle.Builder>();
// Process Manifest Settings
ExportManifest manifest = getImportSession().getManifest();
String manifestVersion = null;
if (manifest != null) {
manifestVersion = manifest.getManifestInformation().getManifestVersion();
}
// Process Metadata
if (manifest != null) {
// import the users
Map<String, List<String>> roleToUserMap = importUsers(manifest.getUserExports());
// import the roles
importRoles(manifest.getRoleExports(), roleToUserMap);
List<ExportManifestMetadata> metadataList = manifest.getMetadataList();
for (ExportManifestMetadata exportManifestMetadata : metadataList) {
String domainId = exportManifestMetadata.getDomainId();
if (domainId != null && !domainId.endsWith(XMI_EXTENSION)) {
domainId = domainId + XMI_EXTENSION;
}
boolean overWriteInRepository = isOverwriteFile();
RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder().charSet("UTF-8").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).preserveDsw(bundle.isPreserveDsw()).overwriteFile(overWriteInRepository).mime("text/xmi+xml").withParam("domain-id", domainId);
cachedImports.put(exportManifestMetadata.getFile(), bundleBuilder);
}
// Process Mondrian
List<ExportManifestMondrian> mondrianList = manifest.getMondrianList();
for (ExportManifestMondrian exportManifestMondrian : mondrianList) {
String catName = exportManifestMondrian.getCatalogName();
Parameters parametersMap = exportManifestMondrian.getParameters();
StringBuilder parametersStr = new StringBuilder();
for (String s : parametersMap.keySet()) {
parametersStr.append(s).append("=").append(parametersMap.get(s)).append(sep);
}
RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder().charSet("UTF_8").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).name(catName).overwriteFile(isOverwriteFile()).mime("application/vnd.pentaho.mondrian+xml").withParam("parameters", parametersStr.toString()).withParam("domain-id", // TODO: this is
catName);
// definitely
// named wrong
// at the very
// least.
// pass as param if not in parameters string
String xmlaEnabled = "" + exportManifestMondrian.isXmlaEnabled();
bundleBuilder.withParam("EnableXmla", xmlaEnabled);
cachedImports.put(exportManifestMondrian.getFile(), bundleBuilder);
String annotationsFile = exportManifestMondrian.getAnnotationsFile();
if (annotationsFile != null) {
RepositoryFileImportBundle.Builder annotationsBundle = new RepositoryFileImportBundle.Builder().path(MondrianCatalogRepositoryHelper.ETC_MONDRIAN_JCR_FOLDER + RepositoryFile.SEPARATOR + catName).name("annotations.xml").charSet("UTF_8").overwriteFile(isOverwriteFile()).mime("text/xml").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).withParam("domain-id", catName);
cachedImports.put(annotationsFile, annotationsBundle);
}
}
}
importMetaStore(manifest, bundle.overwriteInRepository());
for (IRepositoryFileBundle fileBundle : importSource.getFiles()) {
String fileName = fileBundle.getFile().getName();
String actualFilePath = fileBundle.getPath();
if (manifestVersion != null) {
fileName = ExportFileNameEncoder.decodeZipFileName(fileName);
actualFilePath = ExportFileNameEncoder.decodeZipFileName(actualFilePath);
}
String repositoryFilePath = RepositoryFilenameUtils.concat(PentahoPlatformImporter.computeBundlePath(actualFilePath), fileName);
if (this.cachedImports.containsKey(repositoryFilePath)) {
byte[] bytes = IOUtils.toByteArray(fileBundle.getInputStream());
RepositoryFileImportBundle.Builder builder = cachedImports.get(repositoryFilePath);
builder.input(new ByteArrayInputStream(bytes));
importer.importFile(build(builder));
continue;
}
RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder();
InputStream bundleInputStream = null;
String decodedFilePath = fileBundle.getPath();
RepositoryFile decodedFile = fileBundle.getFile();
if (manifestVersion != null) {
decodedFile = new RepositoryFile.Builder(decodedFile).path(decodedFilePath).name(fileName).title(fileName).build();
decodedFilePath = ExportFileNameEncoder.decodeZipFileName(fileBundle.getPath());
}
if (fileBundle.getFile().isFolder()) {
bundleBuilder.mime("text/directory");
bundleBuilder.file(decodedFile);
fileName = repositoryFilePath;
repositoryFilePath = importBundle.getPath();
} else {
byte[] bytes = IOUtils.toByteArray(fileBundle.getInputStream());
bundleInputStream = new ByteArrayInputStream(bytes);
// If is locale file store it for later processing.
if (localeFilesProcessor.isLocaleFile(fileBundle, importBundle.getPath(), bytes)) {
log.trace("Skipping [" + repositoryFilePath + "], it is a locale property file");
continue;
}
bundleBuilder.input(bundleInputStream);
bundleBuilder.mime(solutionHelper.getMime(fileName));
String filePath = (decodedFilePath.equals("/") || decodedFilePath.equals("\\")) ? "" : decodedFilePath;
repositoryFilePath = RepositoryFilenameUtils.concat(importBundle.getPath(), filePath);
}
bundleBuilder.name(fileName);
bundleBuilder.path(repositoryFilePath);
String sourcePath;
if (fileBundle.getFile().isFolder()) {
sourcePath = fileName;
} else {
sourcePath = RepositoryFilenameUtils.concat(PentahoPlatformImporter.computeBundlePath(actualFilePath), fileName);
}
// may not have rights to such as /home or /public
if (manifest != null && manifest.getExportManifestEntity(sourcePath) == null && fileBundle.getFile().isFolder()) {
continue;
}
getImportSession().setCurrentManifestKey(sourcePath);
bundleBuilder.charSet(bundle.getCharset());
bundleBuilder.overwriteFile(bundle.overwriteInRepository());
bundleBuilder.applyAclSettings(bundle.isApplyAclSettings());
bundleBuilder.retainOwnership(bundle.isRetainOwnership());
bundleBuilder.overwriteAclSettings(bundle.isOverwriteAclSettings());
bundleBuilder.acl(getImportSession().processAclForFile(sourcePath));
RepositoryFile file = getFile(importBundle, fileBundle);
ManifestFile manifestFile = getImportSession().getManifestFile(sourcePath, file != null);
bundleBuilder.hidden(isFileHidden(file, manifestFile, sourcePath));
boolean isSchedulable = isSchedulable(file, manifestFile);
if (isSchedulable) {
bundleBuilder.schedulable(isSchedulable);
} else {
bundleBuilder.schedulable(fileIsScheduleInputSource(manifest, sourcePath));
}
IPlatformImportBundle platformImportBundle = build(bundleBuilder);
importer.importFile(platformImportBundle);
if (bundleInputStream != null) {
bundleInputStream.close();
bundleInputStream = null;
}
}
if (manifest != null) {
importSchedules(manifest.getScheduleList());
// Add Hitachi Vantara Connections
List<org.pentaho.database.model.DatabaseConnection> datasourceList = manifest.getDatasourceList();
if (datasourceList != null) {
IDatasourceMgmtService datasourceMgmtSvc = PentahoSystem.get(IDatasourceMgmtService.class);
for (org.pentaho.database.model.DatabaseConnection databaseConnection : datasourceList) {
if (databaseConnection.getDatabaseType() == null) {
// don't try to import the connection if there is no type it will cause an error
// However, if this is the DI Server, and the connection is defined in a ktr, it will import automatically
log.warn("Can't import connection " + databaseConnection.getName() + " because it doesn't have a databaseType");
continue;
}
try {
IDatabaseConnection existingDBConnection = datasourceMgmtSvc.getDatasourceByName(databaseConnection.getName());
if (existingDBConnection != null && existingDBConnection.getName() != null) {
if (isOverwriteFile()) {
databaseConnection.setId(existingDBConnection.getId());
datasourceMgmtSvc.updateDatasourceByName(databaseConnection.getName(), databaseConnection);
}
} else {
datasourceMgmtSvc.createDatasource(databaseConnection);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// Process locale files.
localeFilesProcessor.processLocaleFiles(importer);
}
use of org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService in project pentaho-platform by pentaho.
the class PentahoPlatformExporterTest method testExportDatasources.
@Test
public void testExportDatasources() throws Exception {
IDatasourceMgmtService svc = mock(IDatasourceMgmtService.class);
exporterSpy.setDatasourceMgmtService(svc);
List<IDatabaseConnection> datasources = new ArrayList<>();
IDatabaseConnection conn = mock(DatabaseConnection.class);
IDatabaseConnection icon = mock(IDatabaseConnection.class);
datasources.add(conn);
datasources.add(icon);
when(svc.getDatasources()).thenReturn(datasources);
exporterSpy.exportDatasources();
assertEquals(1, exporterSpy.getExportManifest().getDatasourceList().size());
assertEquals(conn, exporterSpy.getExportManifest().getDatasourceList().get(0));
}
Aggregations