From ddc734232f38a38e3a2fcf6ee6bbb11504575d6a Mon Sep 17 00:00:00 2001 From: gpawel <gpawel> Date: Mon, 30 Jul 2012 12:31:35 +0000 Subject: [PATCH] SP-215 BIS-130: Standard technologies application context should include needed parts depending on enabled-technologies property SVN: 26283 --- .../cisd/common/spring/Bootstrapper.java | 101 ++++++++++++++++++ .../installer/bin/finish-installation.sh | 13 --- ...andard-technologies-applicationContext.xml | 16 ++- 3 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 common/source/java/ch/systemsx/cisd/common/spring/Bootstrapper.java diff --git a/common/source/java/ch/systemsx/cisd/common/spring/Bootstrapper.java b/common/source/java/ch/systemsx/cisd/common/spring/Bootstrapper.java new file mode 100644 index 00000000000..4f96c2f8e87 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/spring/Bootstrapper.java @@ -0,0 +1,101 @@ +/* + * Copyright 2012 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.common.spring; + +import java.util.ArrayList; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.support.AbstractRefreshableConfigApplicationContext; + +/** + * Bootstrapper bean responsible for loading resources conditionally basing on property values. + * Normally properties are initialized by Spring after all beans are being instanced - this class + * workarounds this problem by refreshing the application context after properties are being loaded. + * + * @author Pawel Glyzewski + */ +public class Bootstrapper implements ApplicationContextAware, InitializingBean +{ + private AbstractRefreshableConfigApplicationContext context; + + private String[] configLocations; + + private String[] conditionalConfigLocations; + + public void setConfigLocation(final String configLocation) + { + this.configLocations = new String[] + { configLocation }; + } + + public void setConditionalConfigLocations(final String[] conditionalConfigLocations) + { + this.conditionalConfigLocations = conditionalConfigLocations; + } + + @Override + public void setApplicationContext(final ApplicationContext applicationContext) + throws BeansException + { + context = (AbstractRefreshableConfigApplicationContext) applicationContext; + } + + @Override + public void afterPropertiesSet() throws Exception + { + ArrayList<String> allConfigLocations = new ArrayList<String>(); + for (String configLocation : configLocations) + { + allConfigLocations.add(configLocation); + } + + if (conditionalConfigLocations != null) + { + for (String conditionalConfigLocationString : conditionalConfigLocations) + { + int index = conditionalConfigLocationString.indexOf(":"); + if (index != -1) + { + String condition = conditionalConfigLocationString.substring(0, index); + if (evaluateCondition(condition)) + { + allConfigLocations + .add(conditionalConfigLocationString.substring(index + 1)); + } + } + } + } + + context.setConfigLocations(allConfigLocations.toArray(new String[allConfigLocations.size()])); + context.refresh(); + } + + private static boolean evaluateCondition(String condition) + { + if (condition.contains("~")) + { + int index = condition.indexOf("~"); + String property = condition.substring(0, index); + String containedText = condition.substring(index + 1); + return property.contains(containedText); + } + return false; + } +} diff --git a/installation/resource/installer/bin/finish-installation.sh b/installation/resource/installer/bin/finish-installation.sh index 68720726724..0acf6e62797 100644 --- a/installation/resource/installer/bin/finish-installation.sh +++ b/installation/resource/installer/bin/finish-installation.sh @@ -31,16 +31,3 @@ if [ -d "$DATA_TMPEXTRACT" ]; then done rm -rf "$DATA_TMPEXTRACT" fi - -APPLICATION_CONTEXT_FILE="$INSTALL_PATH/servers/openBIS-server/jetty/webapps/openbis/WEB-INF/classes/standard-technologies-applicationContext.xml" -if [ -f "$APPLICATION_CONTEXT_FILE" ]; then - tmpFile="$BASE/xxx.xml" - awk '/plugin-applicationContext/{gsub(/!*--/,"")}; 1' "$APPLICATION_CONTEXT_FILE" > "$tmpFile" - mv "$tmpFile" "$APPLICATION_CONTEXT_FILE" - awk '/plugin-applicationContext/{gsub(/import/,"!--import") gsub(/>/,"-->")}; 1' "$APPLICATION_CONTEXT_FILE" > "$tmpFile" - mv "$tmpFile" "$APPLICATION_CONTEXT_FILE" - for technology in $ENABLED_TECHNOLOGIES; do - awk -v technology=${technology/,/} 'index($0, technology){gsub(/!*--/,"")}; 1' "$APPLICATION_CONTEXT_FILE" > "$tmpFile" - mv "$tmpFile" "$APPLICATION_CONTEXT_FILE" - done -fi \ No newline at end of file diff --git a/openbis_standard_technologies/source/java/standard-technologies-applicationContext.xml b/openbis_standard_technologies/source/java/standard-technologies-applicationContext.xml index d20faa6fe2f..eb620fa2ad5 100644 --- a/openbis_standard_technologies/source/java/standard-technologies-applicationContext.xml +++ b/openbis_standard_technologies/source/java/standard-technologies-applicationContext.xml @@ -8,14 +8,22 @@ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-2.5.xsd"> + http://www.springframework.org/schema/context/spring-context-2.5.xsd +"> <import resource="applicationContext.xml"/> - <import resource="proteomics-plugin-applicationContext.xml"/> - <import resource="screening-plugin-applicationContext.xml"/> + + <bean name="bootstrap" class="ch.systemsx.cisd.common.spring.Bootstrapper"> + <property name="configLocation" value="classpath:applicationContext.xml" /> + <property name="conditionalConfigLocations"> + <list> + <value>${enabled-technologies}~proteomics:classpath:proteomics-plugin-applicationContext.xml</value> + <value>${enabled-technologies}~screening:classpath:screening-plugin-applicationContext.xml</value> + </list> + </property> + </bean> <plugins:component-scan base-package="ch.systemsx.cisd.openbis" annotation-config="false"> <plugins:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </plugins:component-scan> - </beans> \ No newline at end of file -- GitLab