From: <fg...@us...> - 2008-02-03 21:20:03
|
Revision: 600 http://openutils.svn.sourceforge.net/openutils/?rev=600&view=rev Author: fgiust Date: 2008-02-03 13:20:09 -0800 (Sun, 03 Feb 2008) Log Message: ----------- [maven-release-plugin] copy for tag openutils-deployment-2.0 Added Paths: ----------- tags/openutils-deployment-2.0/ tags/openutils-deployment-2.0/pom.xml tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/DeploymentResolver.java tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/EnvironmentLog4jConfigListener.java tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java tags/openutils-deployment-2.0/src/site/apt/index.apt Removed Paths: ------------- tags/openutils-deployment-2.0/pom.xml tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/DeploymentResolver.java tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/EnvironmentLog4jConfigListener.java tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java tags/openutils-deployment-2.0/src/site/apt/index.apt Copied: tags/openutils-deployment-2.0 (from rev 594, trunk/openutils-deployment) Deleted: tags/openutils-deployment-2.0/pom.xml =================================================================== --- trunk/openutils-deployment/pom.xml 2008-02-03 21:02:50 UTC (rev 594) +++ tags/openutils-deployment-2.0/pom.xml 2008-02-03 21:20:09 UTC (rev 600) @@ -1,71 +0,0 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils</artifactId> - <version>7</version> - <relativePath>..</relativePath> - </parent> - <artifactId>openutils-deployment</artifactId> - <name>openutils deployment tools</name> - <version>2.0-SNAPSHOT</version> - <description /> - <dependencies> - <dependency> - <groupId>commons-lang</groupId> - <artifactId>commons-lang</artifactId> - <version>2.3</version> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-web</artifactId> - <version>${spring.version}</version> - <optional>true</optional> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-jdbc</artifactId> - <version>${spring.version}</version> - <optional>true</optional> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.4.3</version> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>jcl104-over-slf4j</artifactId> - <version>1.4.3</version> - </dependency> - <dependency> - <groupId>javax.servlet</groupId> - <artifactId>servlet-api</artifactId> - <version>2.4</version> - <scope>provided</scope> - <optional>true</optional> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.0</version> - <scope>test</scope> - </dependency> - </dependencies> - <properties> - <spring.version>2.5.1</spring.version> - </properties> -</project> \ No newline at end of file Copied: tags/openutils-deployment-2.0/pom.xml (from rev 599, trunk/openutils-deployment/pom.xml) =================================================================== --- tags/openutils-deployment-2.0/pom.xml (rev 0) +++ tags/openutils-deployment-2.0/pom.xml 2008-02-03 21:20:09 UTC (rev 600) @@ -0,0 +1,76 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils</artifactId> + <version>7</version> + <relativePath>..</relativePath> + </parent> + <artifactId>openutils-deployment</artifactId> + <name>openutils deployment tools</name> + <version>2.0</version> + <description /> + <dependencies> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.3</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + <version>${spring.version}</version> + <optional>true</optional> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-jdbc</artifactId> + <version>${spring.version}</version> + <optional>true</optional> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.4.3</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jcl104-over-slf4j</artifactId> + <version>1.4.3</version> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.4</version> + <scope>provided</scope> + <optional>true</optional> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.0</version> + <scope>test</scope> + </dependency> + </dependencies> + <properties> + <spring.version>2.5.1</spring.version> + </properties> + + <scm> + <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-deployment-2.0</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-deployment-2.0</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-deployment-2.0</url> + </scm> +</project> \ No newline at end of file Deleted: tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/DeploymentResolver.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/log4j/DeploymentResolver.java 2008-02-03 21:02:50 UTC (rev 594) +++ tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/DeploymentResolver.java 2008-02-03 21:20:09 UTC (rev 600) @@ -1,80 +0,0 @@ -package it.openutils.deployment.log4j; - -import java.io.File; -import java.io.FileNotFoundException; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.text.MessageFormat; - -import javax.servlet.ServletContext; - -import org.apache.commons.lang.ArrayUtils; -import org.apache.commons.lang.StringUtils; - - -/** - * @author fgiust - * @version $Id$ - */ -public class DeploymentResolver -{ - - public static File resolveServerRelativeLocation(ServletContext context, String commaSeparatedListOfPaths) - throws FileNotFoundException - { - String[] propertiesLocation = StringUtils.split(commaSeparatedListOfPaths, ','); - - String servername = resolveServerName(); - - String rootPath = StringUtils.replace(context.getRealPath("/"), "\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$ - String webapp = StringUtils.substringAfterLast(rootPath, "/"); //$NON-NLS-1$ - - for (int j = 0; j < propertiesLocation.length; j++) - { - String location = StringUtils.trim(propertiesLocation[j]); - location = StringUtils.replace(location, "${servername}", servername); //$NON-NLS-1$ - location = StringUtils.replace(location, "${webapp}", webapp); //$NON-NLS-1$ - - File initFile = new File(rootPath, location); - - if (!initFile.exists() || initFile.isDirectory()) - { - continue; - } - - return initFile; - - } - - throw new FileNotFoundException( - MessageFormat - .format( - "No configuration found using location list {0}. [servername] is [{1}], [webapp] is [{2}] and base path is [{3}]", //$NON-NLS-1$ - new Object[]{ArrayUtils.toString(propertiesLocation), servername, webapp, rootPath })); - - } - - /** - * Resolve the current server name. - * @return server name, all lowercase, without domain - */ - public static String resolveServerName() - { - String servername = null; - - try - { - servername = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()); - if (StringUtils.contains(servername, ".")) - { - servername = StringUtils.substringBefore(servername, "."); - } - - } - catch (UnknownHostException e) - { - System.err.println(e.getMessage()); - } - return servername; - } -} Copied: tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/DeploymentResolver.java (from rev 598, trunk/openutils-deployment/src/main/java/it/openutils/deployment/log4j/DeploymentResolver.java) =================================================================== --- tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/DeploymentResolver.java (rev 0) +++ tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/DeploymentResolver.java 2008-02-03 21:20:09 UTC (rev 600) @@ -0,0 +1,95 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.deployment.log4j; + +import java.io.File; +import java.io.FileNotFoundException; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.text.MessageFormat; + +import javax.servlet.ServletContext; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; + + +/** + * @author fgiust + * @version $Id$ + */ +public class DeploymentResolver +{ + + public static File resolveServerRelativeLocation(ServletContext context, String commaSeparatedListOfPaths) + throws FileNotFoundException + { + String[] propertiesLocation = StringUtils.split(commaSeparatedListOfPaths, ','); + + String servername = resolveServerName(); + + String rootPath = StringUtils.replace(context.getRealPath("/"), "\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$ + String webapp = StringUtils.substringAfterLast(rootPath, "/"); //$NON-NLS-1$ + + for (int j = 0; j < propertiesLocation.length; j++) + { + String location = StringUtils.trim(propertiesLocation[j]); + location = StringUtils.replace(location, "${servername}", servername); //$NON-NLS-1$ + location = StringUtils.replace(location, "${webapp}", webapp); //$NON-NLS-1$ + + File initFile = new File(rootPath, location); + + if (!initFile.exists() || initFile.isDirectory()) + { + continue; + } + + return initFile; + + } + + throw new FileNotFoundException( + MessageFormat + .format( + "No configuration found using location list {0}. [servername] is [{1}], [webapp] is [{2}] and base path is [{3}]", //$NON-NLS-1$ + new Object[]{ArrayUtils.toString(propertiesLocation), servername, webapp, rootPath})); + + } + + /** + * Resolve the current server name. + * @return server name, all lowercase, without domain + */ + public static String resolveServerName() + { + String servername = null; + + try + { + servername = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()); + if (StringUtils.contains(servername, ".")) + { + servername = StringUtils.substringBefore(servername, "."); + } + + } + catch (UnknownHostException e) + { + System.err.println(e.getMessage()); + } + return servername; + } +} Deleted: tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/EnvironmentLog4jConfigListener.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/log4j/EnvironmentLog4jConfigListener.java 2008-02-03 21:02:50 UTC (rev 594) +++ tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/EnvironmentLog4jConfigListener.java 2008-02-03 21:20:09 UTC (rev 600) @@ -1,108 +0,0 @@ -package it.openutils.deployment.log4j; - -import java.io.FileNotFoundException; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -import org.springframework.util.Log4jConfigurer; -import org.springframework.web.util.Log4jConfigListener; -import org.springframework.web.util.WebUtils; - - -/** - * @author Fabrizio Giustina - * @version $Id$ - */ -public class EnvironmentLog4jConfigListener extends Log4jConfigListener -{ - - /** - * Default value for the DEFAULT_INITIALIZATION_PARAMETER parameter. - */ - public static final String DEFAULT_INITIALIZATION_PARAMETER = // - "WEB-INF/config/${servername}/${webapp}/log4j.xml," //$NON-NLS-1$ - + "WEB-INF/config/${servername}/log4j.xml," //$NON-NLS-1$ - + "WEB-INF/config/${webapp}/log4j.xml," //$NON-NLS-1$ - + "WEB-INF/config/default/log4j.xml," //$NON-NLS-1$ - + "WEB-INF/config/log4j.xml"; //$NON-NLS-1$ - - /** - * {@inheritDoc} - */ - @Override - public void contextInitialized(ServletContextEvent event) - { - initLogging(event.getServletContext()); - } - - public static void initLogging(ServletContext servletContext) - { - if (exposeWebAppRoot(servletContext)) - { - WebUtils.setWebAppRootSystemProperty(servletContext); - - String servername = DeploymentResolver.resolveServerName(); - if (servername != null) - { - System.setProperty("server.name", servername); - System.setProperty("server", servername); - } - } - - String locationList = servletContext.getInitParameter("log4jConfigLocation"); - - if (locationList == null) - { - locationList = DEFAULT_INITIALIZATION_PARAMETER; - } - - String location; - - try - { - location = DeploymentResolver.resolveServerRelativeLocation(servletContext, locationList).getAbsolutePath(); - } - catch (FileNotFoundException ex) - { - throw new IllegalArgumentException("Invalid 'log4jConfigLocation' parameter: " + ex.getMessage()); - } - if (location != null) - { - servletContext.log("Initializing Log4J from [" + location + "]"); - try - { - - String intervalString = servletContext.getInitParameter("log4jRefreshInterval"); - if (intervalString != null) - { - try - { - long refreshInterval = Long.parseLong(intervalString); - Log4jConfigurer.initLogging(location, refreshInterval); - } - catch (NumberFormatException ex) - { - throw new IllegalArgumentException("Invalid 'log4jRefreshInterval' parameter: " - + ex.getMessage()); - } - } - else - { - Log4jConfigurer.initLogging(location); - } - } - catch (FileNotFoundException ex) - { - throw new IllegalArgumentException("Invalid 'log4jConfigLocation' parameter: " + ex.getMessage()); - } - } - } - - private static boolean exposeWebAppRoot(ServletContext servletContext) - { - String exposeWebAppRootParam = servletContext.getInitParameter("log4jExposeWebAppRoot"); - return exposeWebAppRootParam == null || Boolean.valueOf(exposeWebAppRootParam).booleanValue(); - } - -} Copied: tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/EnvironmentLog4jConfigListener.java (from rev 598, trunk/openutils-deployment/src/main/java/it/openutils/deployment/log4j/EnvironmentLog4jConfigListener.java) =================================================================== --- tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/EnvironmentLog4jConfigListener.java (rev 0) +++ tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/log4j/EnvironmentLog4jConfigListener.java 2008-02-03 21:20:09 UTC (rev 600) @@ -0,0 +1,123 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.deployment.log4j; + +import java.io.FileNotFoundException; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; + +import org.springframework.util.Log4jConfigurer; +import org.springframework.web.util.Log4jConfigListener; +import org.springframework.web.util.WebUtils; + + +/** + * @author Fabrizio Giustina + * @version $Id$ + */ +public class EnvironmentLog4jConfigListener extends Log4jConfigListener +{ + + /** + * Default value for the DEFAULT_INITIALIZATION_PARAMETER parameter. + */ + public static final String DEFAULT_INITIALIZATION_PARAMETER = // + "WEB-INF/config/${servername}/${webapp}/log4j.xml," //$NON-NLS-1$ + + "WEB-INF/config/${servername}/log4j.xml," //$NON-NLS-1$ + + "WEB-INF/config/${webapp}/log4j.xml," //$NON-NLS-1$ + + "WEB-INF/config/default/log4j.xml," //$NON-NLS-1$ + + "WEB-INF/config/log4j.xml"; //$NON-NLS-1$ + + /** + * {@inheritDoc} + */ + @Override + public void contextInitialized(ServletContextEvent event) + { + initLogging(event.getServletContext()); + } + + public static void initLogging(ServletContext servletContext) + { + if (exposeWebAppRoot(servletContext)) + { + WebUtils.setWebAppRootSystemProperty(servletContext); + + String servername = DeploymentResolver.resolveServerName(); + if (servername != null) + { + System.setProperty("server.name", servername); + System.setProperty("server", servername); + } + } + + String locationList = servletContext.getInitParameter("log4jConfigLocation"); + + if (locationList == null) + { + locationList = DEFAULT_INITIALIZATION_PARAMETER; + } + + String location; + + try + { + location = DeploymentResolver.resolveServerRelativeLocation(servletContext, locationList).getAbsolutePath(); + } + catch (FileNotFoundException ex) + { + throw new IllegalArgumentException("Invalid 'log4jConfigLocation' parameter: " + ex.getMessage()); + } + if (location != null) + { + servletContext.log("Initializing Log4J from [" + location + "]"); + try + { + + String intervalString = servletContext.getInitParameter("log4jRefreshInterval"); + if (intervalString != null) + { + try + { + long refreshInterval = Long.parseLong(intervalString); + Log4jConfigurer.initLogging(location, refreshInterval); + } + catch (NumberFormatException ex) + { + throw new IllegalArgumentException("Invalid 'log4jRefreshInterval' parameter: " + + ex.getMessage()); + } + } + else + { + Log4jConfigurer.initLogging(location); + } + } + catch (FileNotFoundException ex) + { + throw new IllegalArgumentException("Invalid 'log4jConfigLocation' parameter: " + ex.getMessage()); + } + } + } + + private static boolean exposeWebAppRoot(ServletContext servletContext) + { + String exposeWebAppRootParam = servletContext.getInitParameter("log4jExposeWebAppRoot"); + return exposeWebAppRootParam == null || Boolean.valueOf(exposeWebAppRootParam).booleanValue(); + } + +} Deleted: tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java 2008-02-03 21:02:50 UTC (rev 594) +++ tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java 2008-02-03 21:20:09 UTC (rev 600) @@ -1,175 +0,0 @@ -package it.openutils.deployment.spring; - -import java.io.IOException; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Properties; - -import javax.sql.DataSource; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanInitializationException; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.context.ApplicationEvent; -import org.springframework.context.ApplicationListener; -import org.springframework.dao.DataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowCallbackHandler; - - -/** - * @author fgiust - * @version $Id$ - */ -public class DatabaseEnvironmentPropertyConfigurer extends EnvironmentPropertyConfigurer - implements - ApplicationContextAware, - ApplicationListener -{ - - private static Logger log = LoggerFactory.getLogger(DatabaseEnvironmentPropertyConfigurer.class); - - private String sqlQuery; - - private String dataSourceName; - - private DataSource dataSource; - - private ApplicationContext applicationContext; - - public void setSqlQuery(String sqlQuery) - { - this.sqlQuery = sqlQuery; - } - - public void setDataSourceName(String dataSourceName) - { - this.dataSourceName = dataSourceName; - } - - /** - * {@inheritDoc} - */ - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException - { - this.applicationContext = applicationContext; - } - - /** - * Get Spring Context proprieties. - * @return A properties object containing all spring properties. - */ - @Override - public Properties getProperties() - { - // loadAndRefresh(); @todo is this needed anymore? - return properties; - } - - /** - * @throws IOException - */ - private void loadAndRefresh() throws IOException - { - properties = mergeProperties(); - refresh(); - } - - /** - * used to reload configuration code from db - */ - public void refresh() - { - manuallyLoadDatasource(); - - /** - * inner utility class to create properties from rows extracted by the query - * @author diego - * @version $Id$ - */ - class RowHandler implements RowCallbackHandler - { - - /** - * {@inheritDoc} - */ - public void processRow(final ResultSet rs) throws SQLException - { - String parmName = rs.getString(1); - String parmValue = rs.getString(2); - - log.debug("Configuring property {}={}", parmName, parmValue); - properties.put(parmName, parmValue); - - } - } - - JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - try - { - jdbcTemplate.query(sqlQuery, new RowHandler()); - } - catch (DataAccessException e) - { - log.error(e.getMessage()); - } - } - - /** - * {@inheritDoc} - */ - public void onApplicationEvent(ApplicationEvent event) - { - manuallyLoadDatasource(); - } - - /** - * - */ - private void manuallyLoadDatasource() - { - if (dataSource == null) - { - dataSource = (DataSource) applicationContext.getBean(dataSourceName); - try - { - properties = mergeProperties(); - } - catch (IOException e) - { - log.debug("Exception while loading environment properties from file."); - } - } - } - - /** - * {@inheritDoc} - */ - @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException - { - - super.postProcessBeanFactory(beanFactory); - - try - { - loadAndRefresh(); - Properties mergedProps = properties; - - // Convert the merged properties, if necessary. - convertProperties(mergedProps); - - // Let the subclass process the properties. - processProperties(beanFactory, mergedProps); - } - catch (IOException ex) - { - throw new BeanInitializationException("Could not load properties", ex); - } - - } -} Copied: tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java (from rev 598, trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java) =================================================================== --- tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java (rev 0) +++ tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/DatabaseEnvironmentPropertyConfigurer.java 2008-02-03 21:20:09 UTC (rev 600) @@ -0,0 +1,190 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.deployment.spring; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import javax.sql.DataSource; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanInitializationException; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowCallbackHandler; + + +/** + * @author fgiust + * @version $Id$ + */ +public class DatabaseEnvironmentPropertyConfigurer extends EnvironmentPropertyConfigurer + implements + ApplicationContextAware, + ApplicationListener +{ + + private static Logger log = LoggerFactory.getLogger(DatabaseEnvironmentPropertyConfigurer.class); + + private String sqlQuery; + + private String dataSourceName; + + private DataSource dataSource; + + private ApplicationContext applicationContext; + + public void setSqlQuery(String sqlQuery) + { + this.sqlQuery = sqlQuery; + } + + public void setDataSourceName(String dataSourceName) + { + this.dataSourceName = dataSourceName; + } + + /** + * {@inheritDoc} + */ + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException + { + this.applicationContext = applicationContext; + } + + /** + * Get Spring Context proprieties. + * @return A properties object containing all spring properties. + */ + @Override + public Properties getProperties() + { + // loadAndRefresh(); @todo is this needed anymore? + return properties; + } + + /** + * @throws IOException + */ + private void loadAndRefresh() throws IOException + { + properties = mergeProperties(); + refresh(); + } + + /** + * used to reload configuration code from db + */ + public void refresh() + { + manuallyLoadDatasource(); + + /** + * inner utility class to create properties from rows extracted by the query + * @author diego + * @version $Id$ + */ + class RowHandler implements RowCallbackHandler + { + + /** + * {@inheritDoc} + */ + public void processRow(final ResultSet rs) throws SQLException + { + String parmName = rs.getString(1); + String parmValue = rs.getString(2); + + log.debug("Configuring property {}={}", parmName, parmValue); + properties.put(parmName, parmValue); + + } + } + + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + try + { + jdbcTemplate.query(sqlQuery, new RowHandler()); + } + catch (DataAccessException e) + { + log.error(e.getMessage()); + } + } + + /** + * {@inheritDoc} + */ + public void onApplicationEvent(ApplicationEvent event) + { + manuallyLoadDatasource(); + } + + /** + * + */ + private void manuallyLoadDatasource() + { + if (dataSource == null) + { + dataSource = (DataSource) applicationContext.getBean(dataSourceName); + try + { + properties = mergeProperties(); + } + catch (IOException e) + { + log.debug("Exception while loading environment properties from file."); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException + { + + super.postProcessBeanFactory(beanFactory); + + try + { + loadAndRefresh(); + Properties mergedProps = properties; + + // Convert the merged properties, if necessary. + convertProperties(mergedProps); + + // Let the subclass process the properties. + processProperties(beanFactory, mergedProps); + } + catch (IOException ex) + { + throw new BeanInitializationException("Could not load properties", ex); + } + + } +} Deleted: tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2008-02-03 21:02:50 UTC (rev 594) +++ tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2008-02-03 21:20:09 UTC (rev 600) @@ -1,323 +0,0 @@ -package it.openutils.deployment.spring; - -import java.io.IOException; -import java.io.InputStream; -import java.net.InetAddress; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.UnknownHostException; -import java.util.Properties; - -import javax.servlet.ServletContext; - -import org.apache.commons.lang.ArrayUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.core.io.Resource; -import org.springframework.core.io.UrlResource; -import org.springframework.util.ResourceUtils; -import org.springframework.web.context.WebApplicationContext; - - -/** - * @author fgiust - * @version $Id: $ - */ -public class EnvironmentPropertyConfigurer extends PropertyPlaceholderConfigurer implements ApplicationContextAware -{ - - /** - * Application name (webapp name) variable. - */ - private static final String PROPERTY_APPL = "${appl}"; - - /** - * Environment (server name) variable. - */ - private static final String PROPERTY_ENV = "${env}"; - - /** - * Logger. - */ - private static Logger log = LoggerFactory.getLogger(EnvironmentPropertyConfigurer.class); - - /** - * @deprecated use defaultLocation - */ - @Deprecated - private String defaultEnvironment; - - private ServletContext servletContext; - - private String fileLocation; - - /** - * Cached properties (super field is private). - */ - protected Properties properties; - - /** - * Are properties inherited from default configuration? default is true, - */ - private boolean inherit = true; - - /** - * Setter for <code>fileLocation</code>. - * @param fileLocation The fileLocation to set. - */ - public void setFileLocation(String fileLocation) - { - this.fileLocation = fileLocation; - } - - /** - * Sets the inherit. - * @param inherit the inherit to set - */ - public void setInherit(boolean inherit) - { - this.inherit = inherit; - } - - /** - * Setter for <code>defaultEnvironment</code>. - * @param defaultEnvironment The defaultEnvironment to set. - * @deprecated use defaultLocation - */ - @Deprecated - public void setDefaultEnvironment(String defaultEnvironment) - { - this.defaultEnvironment = defaultEnvironment; - } - - /** - * {@inheritDoc} - */ - @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException - { - if (fileLocation != null) - { - - String hostname = null; - try - { - hostname = StringUtils.substringBefore( - StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()), - "."); - } - catch (UnknownHostException e) - { - log.error(e.getMessage()); // should not happen - } - - if (hostname != null) - { - System.setProperty("env", hostname); - } - - String applName = getApplicationName(); - if (applName != null) - { - System.setProperty("appl", applName); - } - - URL propertyUrl = null; - - String fileLocationFull = fileLocation; - - if (defaultEnvironment != null) - { - log.warn("Usage of \"defaultEnvironment\" is deprecated, please specify the fallback location " - + "as the last comma separated value in \"fileLocation\""); - fileLocationFull = fileLocationFull - + "," - + StringUtils.replace(fileLocationFull, PROPERTY_ENV, this.defaultEnvironment); - } - - String replacedLocations = StringUtils.replace(fileLocationFull, PROPERTY_ENV, hostname); - replacedLocations = StringUtils.replace(replacedLocations, PROPERTY_APPL, applName); - - String[] locations = StringUtils.split(replacedLocations, ","); - - if (inherit) - { - ArrayUtils.reverse(locations); - } - - Properties props = new Properties(); - boolean found = false; - - for (String loc : locations) - { - propertyUrl = getResource(StringUtils.strip(loc)); - if (propertyUrl != null) - { - found = true; - log.debug("Loading property file at {}", loc); - - Resource resource = new UrlResource(propertyUrl); - InputStream is = null; - - try - { - is = resource.getInputStream(); - props.load(is); - } - catch (IOException e) - { - log.error("Error loading " + propertyUrl.toString(), e); - } - finally - { - if (is != null) - { - try - { - is.close(); - } - catch (IOException e) - { - // ignore - } - } - } - - if (!inherit) - { - break; - } - } - log.debug("Property file not found at {}", loc); - - } - - if (!found) - { - log.error("No properties found at {}", replacedLocations); - } - - this.properties = props; - super.setProperties(props); - - } - - super.postProcessBeanFactory(beanFactory); - } - - private URL getResource(String resource) - { - URL url = null; - - if (servletContext != null) - { - try - { - if (resource != null && !resource.startsWith("/")) - { - url = servletContext.getResource("/" + resource); - } - else - { - url = servletContext.getResource(resource); - } - - if (url != null) - { - // check needed for servletUnit - // we need to check for a connection because getResource always returns a URL, also if the resource - // doesn't exists - url.openConnection().connect(); - } - - } - catch (MalformedURLException e) - { - log.error(e.getMessage(), e); - } - catch (IOException e) - { - // ignore, URL is not a valid resource - url = null; - } - } - else - { - try - { - url = ResourceUtils.getURL(resource); - url.openStream().close(); // test if the resource actually exists - } - catch (IOException e) - { - // ignore, can be normal - url = null; - } - } - return url; - } - - private String getApplicationName() - { - if (servletContext != null) - { - String url = servletContext.getRealPath("/"); - url = StringUtils.replace(url, "\\", "/"); - if (url.endsWith("/")) - { - url = StringUtils.substringBeforeLast(url, "/"); - } - - return StringUtils.substringAfterLast(url, "/"); - } - return StringUtils.EMPTY; - } - - /** - * Returns the Properties loaded by this configurer. - * @return Properties - */ - public Properties getProperties() - { - return properties; - } - - /** - * Returns a single property. - * @param key Property key - * @return property value or <code>null</code> if not found. - */ - public String getProperty(String key) - { - // better be safe, it doesn't hurt - if (properties == null) - { - return null; - } - return properties.getProperty(key); - } - - /** - * {@inheritDoc} - */ - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException - { - try - { - if (applicationContext instanceof WebApplicationContext) - { - this.servletContext = ((WebApplicationContext) applicationContext).getServletContext(); - } - } - catch (NoClassDefFoundError e) - { - // ignore, we are not in a web project or spring web is not available - } - } -} Copied: tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java (from rev 598, trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java) =================================================================== --- tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java (rev 0) +++ tags/openutils-deployment-2.0/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2008-02-03 21:20:09 UTC (rev 600) @@ -0,0 +1,338 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.deployment.spring; + +import java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.UnknownHostException; +import java.util.Properties; + +import javax.servlet.ServletContext; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.core.io.Resource; +import org.springframework.core.io.UrlResource; +import org.springframework.util.ResourceUtils; +import org.springframework.web.context.WebApplicationContext; + + +/** + * @author fgiust + * @version $Id: $ + */ +public class EnvironmentPropertyConfigurer extends PropertyPlaceholderConfigurer implements ApplicationContextAware +{ + + /** + * Application name (webapp name) variable. + */ + private static final String PROPERTY_APPL = "${appl}"; + + /** + * Environment (server name) variable. + */ + private static final String PROPERTY_ENV = "${env}"; + + /** + * Logger. + */ + private static Logger log = LoggerFactory.getLogger(EnvironmentPropertyConfigurer.class); + + /** + * @deprecated use defaultLocation + */ + @Deprecated + private String defaultEnvironment; + + private ServletContext servletContext; + + private String fileLocation; + + /** + * Cached properties (super field is private). + */ + protected Properties properties; + + /** + * Are properties inherited from default configuration? default is true, + */ + private boolean inherit = true; + + /** + * Setter for <code>fileLocation</code>. + * @param fileLocation The fileLocation to set. + */ + public void setFileLocation(String fileLocation) + { + this.fileLocation = fileLocation; + } + + /** + * Sets the inherit. + * @param inherit the inherit to set + */ + public void setInherit(boolean inherit) + { + this.inherit = inherit; + } + + /** + * Setter for <code>defaultEnvironment</code>. + * @param defaultEnvironment The defaultEnvironment to set. + * @deprecated use defaultLocation + */ + @Deprecated + public void setDefaultEnvironment(String defaultEnvironment) + { + this.defaultEnvironment = defaultEnvironment; + } + + /** + * {@inheritDoc} + */ + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException + { + if (fileLocation != null) + { + + String hostname = null; + try + { + hostname = StringUtils.substringBefore( + StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()), + "."); + } + catch (UnknownHostException e) + { + log.error(e.getMessage()); // should not happen + } + + if (hostname != null) + { + System.setProperty("env", hostname); + } + + String applName = getApplicationName(); + if (applName != null) + { + System.setProperty("appl", applName); + } + + URL propertyUrl = null; + + String fileLocationFull = fileLocation; + + if (defaultEnvironment != null) + { + log.warn("Usage of \"defaultEnvironment\" is deprecated, please specify the fallback location " + + "as the last comma separated value in \"fileLocation\""); + fileLocationFull = fileLocationFull + + "," + + StringUtils.replace(fileLocationFull, PROPERTY_ENV, this.defaultEnvironment); + } + + String replacedLocations = StringUtils.replace(fileLocationFull, PROPERTY_ENV, hostname); + replacedLocations = StringUtils.replace(replacedLocations, PROPERTY_APPL, applName); + + String[] locations = StringUtils.split(replacedLocations, ","); + + if (inherit) + { + ArrayUtils.reverse(locations); + } + + Properties props = new Properties(); + boolean found = false; + + for (String loc : locations) + { + propertyUrl = getResource(StringUtils.strip(loc)); + if (propertyUrl != null) + { + found = true; + log.debug("Loading property file at {}", loc); + + Resource resource = new UrlResource(propertyUrl); + InputStream is = null; + + try + { + is = resource.getInputStream(); + props.load(is); + } + catch (IOException e) + { + log.error("Error loading " + propertyUrl.toString(), e); + } + finally + { + if (is != null) + { + try + { + is.close(); + } + catch (IOException e) + { + // ignore + } + } + } + + if (!inherit) + { + break; + } + } + log.debug("Property file not found at {}", loc); + + } + + if (!found) + { + log.error("No properties found at {}", replacedLocations); + } + + this.properties = props; + super.setProperties(props); + + } + + super.postProcessBeanFactory(beanFactory); + } + + private URL getResource(String resource) + { + URL url = null; + + if (servletContext != null) + { + try + { + if (resource != null && !resource.startsWith("/")) + { + url = servletContext.getResource("/" + resource); + } + else + { + url = servletContext.getResource(resource); + } + + if (url != null) + { + // check needed for servletUnit + // we need to check for a connection because getResource always returns a URL, also if the resource + // doesn't exists + url.openConnection().connect(); + } + + } + catch (MalformedURLException e) + { + log.error(e.getMessage(), e); + } + catch (IOException e) + { + // ignore, URL is not a valid resource + url = null; + } + } + else + { + try + { + url = ResourceUtils.getURL(resource); + url.openStream().close(); // test if the resource actually exists + } + catch (IOException e) + { + // ignore, can be normal + url = null; + } + } + return url; + } + + private String getApplicationName() + { + if (servletContext != null) + { + String url = servletContext.getRealPath("/"); + url = StringUtils.replace(url, "\\", "/"); + if (url.endsWith("/")) + { + url = StringUtils.substringBeforeLast(url, "/"); + } + + return StringUtils.substringAfterLast(url, "/"); + } + return StringUtils.EMPTY; + } + + /** + * Returns the Properties loaded by this configurer. + * @return Properties + */ + public Properties getProperties() + { + return properties; + } + + /** + * Returns a single property. + * @param key Property key + * @return property value or <code>null</code> if not found. + */ + public String getProperty(String key) + { + // better be safe, it doesn't hurt + if (properties == null) + { + return null; + } + return properties.getProperty(key); + } + + /** + * {@inheritDoc} + */ + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException + { + try + { + if (applicationContext instanceof WebApplicationContext) + { + this.servletContext = ((WebApplicationContext) applicationContext).getServletContext(); + } + } + catch (NoClassDefFoundError e) + { + // ignore, we are not in a web project or spring web is not available + } + } +} Deleted: tags/openutils-deployment-2.0/src/site/apt/index.apt =================================================================== --- trunk/openutils-deployment/src/site/apt/index.apt 2008-02-03 21:02:50 UTC (rev 594) +++ tags/openutils-deployment-2.0/src/site/apt/index.apt 2008-02-03 21:20:09 UTC (rev 600) @@ -1,15 +0,0 @@ - -------------------------- - O p e n u t i l s - -------------------------- - Fabrizio Giustina - -------------------------- - -openutils-deployment - - - - -Released versions - - Check it at {{{http://www.mvnrepository.com/artifact/net.sourceforge.openutils/openutils-deployment}http://www.mvnrepository.com/artifact/net.sourceforge.openutils/openutils-deployment}} - Copied: tags/openutils-deployment-2.0/src/site/apt/index.apt (from rev 598, trunk/openutils-deployment/src/site/apt/index.apt) =================================================================== --- tags/openutils-deployment-2.0/src/site/apt/index.apt (rev 0) +++ tags/openutils-deployment-2.0/src/site/apt/index.apt 2008-02-03 21:20:09 UTC (rev 600) @@ -0,0 +1,42 @@ + -------------------------- + O p e n u t i l s + -------------------------- + Fabrizio Giustina + -------------------------- + +openutils-deployment + + Openutils-deployment contains a few utility classes that allow switching between different configurations in an application. + + Openutils-deployment works by embedding configurations for different environments in the final application, and by + choosing the correct configuration at runtime dependending on: + + * the server name + + * the webapp name (for web projects) + + [] + + You can find a spring propertyconfigurer and a log4j servlet listener that follow this pattern. + + The following example is a configuration snippet for <<<it.openutils.deployment.spring.EnvironmentPropertyConfigurer>>>: + The ${env} variable will be replaced at runtime with the name of the current server and any existing configuration file + wil be loaded. + + ++--------------------------------------------+ + <bean id="environmentProperties" class="it.openutils.deployment.spring.EnvironmentPropertyConfigurer"> + <property name="fileLocation" + value="WEB-INF/config/${env}/environment.properties, + WEB-INF/config/default/environment.properties, + classpath:environment-${env}.properties, + classpath:environment.properties" /> + </bean> ++--------------------------------------------+ + + + +Released versions + + Check it at {{{http://www.mvnrepository.com/artifact/net.sourceforge.openutils/openutils-deployment}http://www.mvnrepository.com/artifact/net.sourceforge.openutils/openutils-deployment}} + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |