Skip to content
Snippets Groups Projects
Commit a3c4ecb1 authored by felmer's avatar felmer
Browse files

LMS-1622 IDataSourceProvider and two implementations introduced

SVN: 16914
parent c73ba4b9
No related branches found
No related tags found
No related merge requests found
/*
* 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 + "'.");
}
}
/*
* 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);
}
/*
* 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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment