diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/DataStoreServerBasedDataSourceProvider.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/DataStoreServerBasedDataSourceProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..71e2d22015107612d85dc43a3e39e25da3182c7b --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/DataStoreServerBasedDataSourceProvider.java @@ -0,0 +1,115 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import javax.annotation.Resource; +import javax.sql.DataSource; + +import org.springframework.beans.factory.InitializingBean; + +import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; +import ch.systemsx.cisd.common.exceptions.UserFailureException; +import ch.systemsx.cisd.common.spring.ExposablePropertyPaceholderConfigurer; +import ch.systemsx.cisd.common.utilities.ExtendedProperties; +import ch.systemsx.cisd.common.utilities.PropertyParametersUtil; +import ch.systemsx.cisd.common.utilities.PropertyParametersUtil.SectionProperties; +import ch.systemsx.cisd.dbmigration.SimpleDatabaseConfigurationContext; +import ch.systemsx.cisd.openbis.generic.shared.dto.DataPE; + +/** + * + * + * @author Franz-Josef Elmer + */ +public class DataStoreServerBasedDataSourceProvider implements IDataSourceProvider, InitializingBean +{ + private static final String ROOT_KEY = "dss-based-data-source-provider"; + private static final String DATA_STORE_SERVERS_KEY = "data-store-servers"; + + @Resource(name = "propertyConfigurer") + private ExposablePropertyPaceholderConfigurer configurer; + + private final IDAOFactory daoFactory; + + private Map<String, DataSource> dataSources; + + public DataStoreServerBasedDataSourceProvider(IDAOFactory daoFactory) + { + this.daoFactory = daoFactory; + } + + public void afterPropertiesSet() throws Exception + { + dataSources = new HashMap<String, DataSource>(); + ExtendedProperties props = + ExtendedProperties.getSubset(configurer.getResolvedProps(), ROOT_KEY + ".", true); + SectionProperties[] sectionsProperties = + PropertyParametersUtil.extractSectionProperties(props, DATA_STORE_SERVERS_KEY, + false); + for (SectionProperties sectionProperties : sectionsProperties) + { + String key = sectionProperties.getKey().toUpperCase(); + Properties properties = sectionProperties.getProperties(); + dataSources.put(key, create(properties)); + } + } + + public DataSource getDataSourceByDataSetCode(String dataSetCode, String technology) + { + DataPE dataSet = daoFactory.getExternalDataDAO().tryToFindDataSetByCode(dataSetCode); + if (dataSet == null) + { + throw new UserFailureException("Unknown data set " + dataSetCode); + } + return getDataSource(dataSet.getDataStore().getCode()); + } + + private DataSource getDataSource(String dssCode) + { + DataSource dataSource = dataSources.get(dssCode); + if (dataSource == null) + { + throw new ConfigurationFailureException( + "No data source configured for Data Store Server '" + dssCode + "'"); + } + return dataSource; + } + + private DataSource create(Properties properties) + { + return new SimpleDatabaseConfigurationContext(properties).getDataSource(); + } + + public DataSource getDataSourceByExperimentPermID(String experimentPermID, String technology) + { + throw new IllegalArgumentException( + "Getting data source by experiment permID is not supported for technology '" + + technology + "'."); + } + + public DataSource getDataSourceBySamplePermID(String samplePermID, String technology) + { + throw new IllegalArgumentException( + "Getting data source by sample permID is not supported for technology '" + + technology + "'."); + } + +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/IDataSourceProvider.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/IDataSourceProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..a8caedc02e4ccc80acd7b8b82a12214c6af93963 --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/IDataSourceProvider.java @@ -0,0 +1,57 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess; + +import javax.sql.DataSource; + +import ch.systemsx.cisd.common.exceptions.UserFailureException; + +/** + * Interface for providing a {@link DataSource} for a technology specific database based on data set + * code or epxeriment/sample permID. + * + * @author Franz-Josef Elmer + */ +public interface IDataSourceProvider +{ + /** + * Returns an appropriated data source for specified data set code and technology. + * + * @throws IllegalArgumentException if getting data source by data set code isn't supported for + * the specified technology. + * @throws UserFailureException if the specified data set doesn't exist. + */ + public DataSource getDataSourceByDataSetCode(String dataSetCode, String technology); + + /** + * Returns an appropriated data source for specified experiment permID and technology. + * + * @throws IllegalArgumentException if getting data source by experiment permID isn't supported + * for the specified technology. + * @throws UserFailureException if the specified experiment doesn't exist. + */ + public DataSource getDataSourceByExperimentPermID(String experimentPermID, String technology); + + /** + * Returns an appropriated data source for specified sample permID and technology. + * + * @throws IllegalArgumentException if getting data source by sample permID isn't supported + * for the specified technology. + * @throws UserFailureException if the specified sample doesn't exist. + */ + public DataSource getDataSourceBySamplePermID(String samplePermID, String technology); +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/SingleDataSourceProvider.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/SingleDataSourceProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..7acc645f218d7f74f3d498eea0c5df6ba7c37c3f --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/SingleDataSourceProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess; + +import javax.sql.DataSource; + +import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext; + +/** + * {@link DataSource} provider which always returns the same data source. + * + * @author Franz-Josef Elmer + */ +public class SingleDataSourceProvider implements IDataSourceProvider +{ + private DataSource dataSource; + + /** + * Creates a new instance based on the data source of the specified context. + */ + public SingleDataSourceProvider(DatabaseConfigurationContext context) + { + dataSource = context.getDataSource(); + } + + public DataSource getDataSourceByDataSetCode(String dataSetCode, String technology) + { + return dataSource; + } + + public DataSource getDataSourceByExperimentPermID(String experimentPermID, String technology) + { + return dataSource; + } + + public DataSource getDataSourceBySamplePermID(String samplePermID, String technology) + { + return dataSource; + } + +}