Author: bago
Date: Sun Jun 29 10:00:48 2008
New Revision: 672640
URL: http://svn.apache.org/viewvc?rev=672640&view=rev
Log:
Refactored FileSystemBridge to better integrate Spring Resources mechanisms
(JAMES-842)
Feature based on a patch kindly submitted by David Jencks.
Added:
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/JamesResourceLoader.java
(with props)
Modified:
james/server/trunk/spring-deployment/src/main/config/james/spring-beans.xml
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/AvalonConfigurationFileProvider.java
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/FileSystemBridge.java
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/beanfactory/AvalonBeanDefinitionReader.java
Modified:
james/server/trunk/spring-deployment/src/main/config/james/spring-beans.xml
URL:
http://svn.apache.org/viewvc/james/server/trunk/spring-deployment/src/main/config/james/spring-beans.xml?rev=672640&r1=672639&r2=672640&view=diff
==============================================================================
--- james/server/trunk/spring-deployment/src/main/config/james/spring-beans.xml
(original)
+++ james/server/trunk/spring-deployment/src/main/config/james/spring-beans.xml
Sun Jun 29 10:00:48 2008
@@ -26,8 +26,9 @@
<!--
beans which replace Avalon specific container logic
-->
-
- <bean id="fileSystemOverride"
class="org.apache.james.container.spring.adaptor.FileSystemBridge"/>
+ <bean id="fileSystemOverride"
class="org.apache.james.container.spring.adaptor.FileSystemBridge">
+ <property name="resourceLoader" ref="JamesResourceLoader" />
+ </bean>
<bean id="serviceManager"
class="org.apache.james.container.spring.adaptor.DefaultServiceManagerFactory" >
<property name="replacements">
@@ -85,8 +86,10 @@
<ref bean="stringRemapperConfigurationInterceptor" />
<!--<ref bean="privilegedPortsConfigurationInterceptor" />-->
</list>
- </property>
- </bean>
+ </property>
+ </bean>
+
+ <bean id="JamesResourceLoader"
class="org.apache.james.container.spring.adaptor.JamesResourceLoader" />
<bean id="avalonContext"
class="org.apache.james.container.spring.adaptor.AvalonContext" >
<property name="applicationHome" value="data" />
@@ -161,8 +164,4 @@
<bean id="initializationManager"
class="org.apache.james.container.spring.lifecycle.InitializationPropagator" />
-</beans>
-
-
-
-
+</beans>
\ No newline at end of file
Modified:
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/AvalonConfigurationFileProvider.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/AvalonConfigurationFileProvider.java?rev=672640&r1=672639&r2=672640&view=diff
==============================================================================
---
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/AvalonConfigurationFileProvider.java
(original)
+++
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/AvalonConfigurationFileProvider.java
Sun Jun 29 10:00:48 2008
@@ -21,47 +21,46 @@
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.phoenix.tools.configuration.ConfigurationBuilder;
import
org.apache.james.container.spring.configuration.ConfigurationInterceptor;
+import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
-import org.springframework.context.ApplicationContextAware;
-import org.springframework.context.ApplicationContext;
-import org.springframework.beans.BeansException;
+import org.springframework.core.io.ResourceLoader;
import org.xml.sax.InputSource;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
-import java.io.InputStream;
-import java.io.IOException;
/**
* loads the well-known classic James configuration file
*
* TODO make this thing be based on Resource class and inject
resource.getInputStream() into InputSource
*/
-public class AvalonConfigurationFileProvider implements ConfigurationProvider,
ApplicationContextAware {
+public class AvalonConfigurationFileProvider implements ConfigurationProvider,
ResourceLoaderAware {
- private String absoluteFilePath;
private List configurationInterceptors;
- private ApplicationContext applicationContext;
private String configuration;
-
- public void setConfigurationResource(String configuration) {
- this.configuration = configuration;
- }
-
-
+ private ResourceLoader resourceLoader;
+
public Configuration getConfiguration() {
- Resource resource = applicationContext.getResource(configuration);
- InputStream inputStream;
+ InputStream inputStream = null;
+ String systemId = null;
+
+ Resource resource = resourceLoader.getResource(configuration);
+ if (!resource.exists()) {
+ throw new RuntimeException("could not locate configuration file "
+ configuration);
+ }
try {
inputStream = resource.getInputStream();
- } catch (IOException e) {
- throw new RuntimeException("could not locate configuration file "
+ configuration, e);
+ systemId = resource.getURL().toString();
+ } catch (IOException e1) {
+ throw new RuntimeException("could not open configuration file " +
configuration, e1);
}
InputSource inputSource = new InputSource(inputStream);
Configuration configuration;
try
{
- inputSource.setSystemId(resource.getURL().toString());
+ inputSource.setSystemId(systemId);
configuration = ConfigurationBuilder.build(inputSource, null,
null);
}
catch( final Exception e )
@@ -81,12 +80,15 @@
return configuration;
}
+ public void setConfigurationResource(String configuration) {
+ this.configuration = configuration;
+ }
public void setConfigurationInterceptors(List configurationInterceptors) {
this.configurationInterceptors = configurationInterceptors;
}
- public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
- this.applicationContext = applicationContext;
+ public synchronized void setResourceLoader(ResourceLoader resourceLoader) {
+ this.resourceLoader = resourceLoader;
}
}
Modified:
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/FileSystemBridge.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/FileSystemBridge.java?rev=672640&r1=672639&r2=672640&view=diff
==============================================================================
---
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/FileSystemBridge.java
(original)
+++
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/FileSystemBridge.java
Sun Jun 29 10:00:48 2008
@@ -18,58 +18,46 @@
****************************************************************/
package org.apache.james.container.spring.adaptor;
+import org.apache.james.services.FileSystem;
+import org.springframework.core.io.ResourceLoader;
+
import java.io.File;
import java.io.FileNotFoundException;
-import java.io.InputStream;
import java.io.IOException;
-import java.io.FileInputStream;
-
-import org.apache.james.services.FileSystem;
+import java.io.InputStream;
public class FileSystemBridge implements FileSystem {
- private static final String FILE_PROTOCOL = "file://";
- private static final String FILE_PROTOCOL_AND_CONF = "file://conf/";
- private static final String FILE_PROTOCOL_AND_VAR = "file://var/";
public File getBasedir() throws FileNotFoundException {
return new File(".");
}
+
+ private ResourceLoader resourceLoader = null;
/**
* loads resources from classpath or file system
*/
public InputStream getResource(String url) throws IOException {
- if (url.startsWith("classpath:")) {
- String resourceName = url.substring("classpath:".length());
- InputStream resourceAsStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
- if (resourceAsStream==null) {
- throw new IOException("Resource '" + resourceName + "' not
found in the classpath!");
- }
- return resourceAsStream;
- }
- return new FileInputStream(getFile(url));
+ return resourceLoader.getResource(url).getInputStream();
}
/**
* @see org.apache.james.services.FileSystem#getFile(String filURL)
*/
public File getFile(String fileURL) throws FileNotFoundException {
- if (fileURL.startsWith(FILE_PROTOCOL)) {
- File file = null;
- if (fileURL.startsWith(FILE_PROTOCOL_AND_CONF)) {
- file = new File("../conf/" +
fileURL.substring(FILE_PROTOCOL_AND_CONF.length()));
- } else if (fileURL.startsWith(FILE_PROTOCOL_AND_VAR)) {
- file = new File("../var/" +
fileURL.substring(FILE_PROTOCOL_AND_VAR.length()));
- } else {
- file = new File("./" +
fileURL.substring(FILE_PROTOCOL.length()));
- }
- if (!file.exists()) {
- throw new FileNotFoundException("cannot access file " +
file.toString());
- }
- return file;
- } else {
- throw new UnsupportedOperationException("getFile: " + fileURL);
+ try {
+ return resourceLoader.getResource(fileURL).getFile();
+ } catch (IOException e) {
+ throw new FileNotFoundException(e.getMessage());
}
}
+ protected synchronized ResourceLoader getResourceLoader() {
+ return resourceLoader;
+ }
+
+ public synchronized void setResourceLoader(ResourceLoader provider) {
+ this.resourceLoader = provider;
+ }
+
}
Added:
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/JamesResourceLoader.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/JamesResourceLoader.java?rev=672640&view=auto
==============================================================================
---
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/JamesResourceLoader.java
(added)
+++
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/JamesResourceLoader.java
Sun Jun 29 10:00:48 2008
@@ -0,0 +1,72 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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 org.apache.james.container.spring.adaptor;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+
+import java.io.File;
+
+/**
+ * A resource loader supporting JAMES' conf/var specific behaviours and
+ * the "classpath:" prefix.
+ */
+public class JamesResourceLoader implements ResourceLoader,
ApplicationContextAware {
+
+ private ApplicationContext applicationContext;
+
+ private static final String FILE_PROTOCOL = "file://";
+ private static final String FILE_PROTOCOL_AND_CONF = "file://conf/";
+ private static final String FILE_PROTOCOL_AND_VAR = "file://var/";
+
+ public ClassLoader getClassLoader() {
+ return Thread.currentThread().getContextClassLoader();
+ }
+
+ public Resource getResource(String fileURL) {
+ Resource r = null;
+ if (fileURL.startsWith("classpath:")) {
+ String resourceName = fileURL.substring("classpath:".length());
+ r = new ClassPathResource(resourceName);
+ } else if (fileURL.startsWith(FILE_PROTOCOL)) {
+ File file = null;
+ if (fileURL.startsWith(FILE_PROTOCOL_AND_CONF)) {
+ file = new File("../conf/" +
fileURL.substring(FILE_PROTOCOL_AND_CONF.length()));
+ } else if (fileURL.startsWith(FILE_PROTOCOL_AND_VAR)) {
+ file = new File("../var/" +
fileURL.substring(FILE_PROTOCOL_AND_VAR.length()));
+ } else {
+ file = new File("./" +
fileURL.substring(FILE_PROTOCOL.length()));
+ }
+ r = new FileSystemResource(file);
+ } else {
+ r = applicationContext.getResource(fileURL);
+ }
+ return r;
+ }
+
+ public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
+ this.applicationContext = applicationContext;
+ }
+
+}
Propchange:
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/JamesResourceLoader.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/adaptor/JamesResourceLoader.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/beanfactory/AvalonBeanDefinitionReader.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/beanfactory/AvalonBeanDefinitionReader.java?rev=672640&r1=672639&r2=672640&view=diff
==============================================================================
---
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/beanfactory/AvalonBeanDefinitionReader.java
(original)
+++
james/server/trunk/spring-deployment/src/main/java/org/apache/james/container/spring/beanfactory/AvalonBeanDefinitionReader.java
Sun Jun 29 10:00:48 2008
@@ -21,13 +21,11 @@
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.phoenix.tools.configuration.ConfigurationBuilder;
-import org.apache.james.container.spring.adaptor.ConfigurationProvider;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.io.Resource;
-import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;
import org.xml.sax.InputSource;
@@ -39,10 +37,6 @@
*/
public class AvalonBeanDefinitionReader extends AbstractBeanDefinitionReader {
- private ConfigurationProvider configurationProvider;
- private ResourceLoader resourceLoader;
- private ClassLoader beanClassLoader;
-
public AvalonBeanDefinitionReader(BeanDefinitionRegistry
beanDefinitionRegistry) {
super(beanDefinitionRegistry);
}
@@ -138,6 +132,7 @@
private Configuration getConfiguration(Resource resource) throws
IOException {
InputSource inputSource = new InputSource(resource.getInputStream());
+ inputSource.setSystemId(resource.getURL().toString());
try
{
Configuration configuration =
ConfigurationBuilder.build(inputSource, null, null);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]