Author: jvermillard
Date: Fri Feb 22 23:34:07 2008
New Revision: 630408
URL: http://svn.apache.org/viewvc?rev=630408&view=rev
Log:
plugable file loading strategies for asyncweb file serving
Added:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java
mina/asyncweb/trunk/examples/src/test/
mina/asyncweb/trunk/examples/src/test/java/
mina/asyncweb/trunk/examples/src/test/java/org/
mina/asyncweb/trunk/examples/src/test/java/org/apache/
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoaderTest.java
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoaderTest.java
Modified:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
Modified:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java?rev=630408&r1=630407&r2=630408&view=diff
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
(original)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/FileHttpService.java
Fri Feb 22 23:34:07 2008
@@ -35,6 +35,9 @@
import org.apache.asyncweb.common.MutableHttpResponse;
import org.apache.asyncweb.examples.file.cache.CachingPolicy;
import org.apache.asyncweb.examples.file.cache.SimpleCachingPolicy;
+import org.apache.asyncweb.examples.file.fileloader.FileLoader;
+import org.apache.asyncweb.examples.file.fileloader.MmapFileLoaderTest;
+import org.apache.asyncweb.examples.file.fileloader.SimpleFileLoader;
import org.apache.asyncweb.examples.file.index.DefaultDirectoryIndexGenerator;
import org.apache.asyncweb.examples.file.index.DirectoryIndexGenerator;
import org.apache.asyncweb.examples.file.mimetype.MimeMap;
@@ -46,8 +49,8 @@
/**
* An HTTP service, serving files from the filesystem.
- * @author The Apache MINA Project ([EMAIL PROTECTED])
*
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
*/
public class FileHttpService implements HttpService {
private static final Logger LOG = LoggerFactory
@@ -57,13 +60,15 @@
private String basePath;
- private CachingPolicy cachingPolicy = null;
+ private CachingPolicy cachingPolicy = new SimpleCachingPolicy();
private MimeMap mimeMap = new MimeMap();
private FilenameFilter indexFileFilter;
private DirectoryIndexGenerator indexGenerator = new
DefaultDirectoryIndexGenerator();
+
+ private FileLoader fileLoader = new SimpleFileLoader();
public FileHttpService(String baseUrl, String basePath,
String directoryIndexPattern) {
@@ -83,8 +88,6 @@
throw new InvalidParameterException("The base path [ " + basePath
+ " ] is not a valid path.");
}
-
- cachingPolicy = new SimpleCachingPolicy();
}
public FileHttpService(String baseUrl, String basePath) {
@@ -170,12 +173,10 @@
response.setStatus(HttpResponseStatus.OK);
- RandomAccessFile raf = new RandomAccessFile(f, "r");
- FileChannel fc = raf.getChannel();
- MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0,
fc.size());
- response.setContent(IoBuffer.wrap(buffer));
- fc.close();
-
+ IoBuffer buffer=fileLoader.loadFile(f);
+
+ response.setContent(buffer);
+
} else {
// the file is not found, we send the famous 404 error
response.setStatus(HttpResponseStatus.NOT_FOUND);
@@ -214,4 +215,12 @@
}
}
+
+ public FileLoader getFileLoader() {
+ return fileLoader;
+ }
+
+ public void setFileLoader(FileLoader fileLoader) {
+ this.fileLoader = fileLoader;
+ }
}
Added:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java?rev=630408&view=auto
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java
(added)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/FileLoader.java
Fri Feb 22 23:34:07 2008
@@ -0,0 +1,40 @@
+/*
+ * 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.asyncweb.examples.file.fileloader;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.mina.common.IoBuffer;
+
+/**
+ * Interface for providing IoBuffer to File serving services.
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
+public interface FileLoader {
+
+ /**
+ * Provide an IoBuffer from a given File
+ * @param file the file to provide
+ * @return a buffer representing the file
+ */
+ IoBuffer loadFile(File file) throws IOException;
+}
Added:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java?rev=630408&view=auto
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java
(added)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoader.java
Fri Feb 22 23:34:07 2008
@@ -0,0 +1,51 @@
+/*
+ * 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.asyncweb.examples.file.fileloader;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+
+import org.apache.mina.common.IoBuffer;
+
+/**
+ * A file loader, mammping files to memory, supposed to be efficient
+ * on relativly large files.
+ *
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
+public class MmapFileLoader implements FileLoader {
+
+ public IoBuffer loadFile(File file) throws IOException {
+ RandomAccessFile raf = new RandomAccessFile(file, "r");
+
+ FileChannel fc = raf.getChannel();
+
+ MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0,
fc.size());
+
+ // it's mmaped we can close the file descriptor
+ fc.close();
+
+ // the mapped byte buffer will be garbage collected
+ return IoBuffer.wrap(buffer);
+ }
+}
\ No newline at end of file
Added:
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java?rev=630408&view=auto
==============================================================================
---
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java
(added)
+++
mina/asyncweb/trunk/examples/src/main/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoader.java
Fri Feb 22 23:34:07 2008
@@ -0,0 +1,52 @@
+/*
+ * 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.asyncweb.examples.file.fileloader;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.security.InvalidParameterException;
+
+import org.apache.mina.common.IoBuffer;
+
+/**
+ * A simple file loader, supposed to be efficient on relativly small
+ * files.
+ *
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ */
+public class SimpleFileLoader implements FileLoader {
+
+ public IoBuffer loadFile(File file) throws IOException {
+ if(file.length()> Integer.MAX_VALUE)
+ throw new InvalidParameterException("File
"+file+" is too big for SimpleFileLoader try MmapFileLoader");
+ IoBuffer buffer=IoBuffer.allocate((int)file.length());
+ RandomAccessFile raf = new RandomAccessFile(file, "r");
+ FileChannel fc = raf.getChannel();
+ fc.read(buffer.buf());
+ buffer.flip();
+ fc.close();
+ raf.close();
+ return buffer;
+ }
+
+
+}
Added:
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoaderTest.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoaderTest.java?rev=630408&view=auto
==============================================================================
---
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoaderTest.java
(added)
+++
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/MmapFileLoaderTest.java
Fri Feb 22 23:34:07 2008
@@ -0,0 +1,46 @@
+package org.apache.asyncweb.examples.file.fileloader;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.Random;
+
+import org.apache.mina.common.IoBuffer;
+import org.junit.Test;
+
+public class MmapFileLoaderTest {
+
+ private static final int TEMP_SIZE=1024*100+3; // ~100K file
+
+ @Test
+ public void testLoadFile() throws Exception {
+ // generate temp file
+ File tempFile=File.createTempFile("dummy",null);
+ tempFile.deleteOnExit();
+ FileOutputStream fos=new FileOutputStream(tempFile);
+ Random rng=new Random();
+
+ byte[] data = new byte[TEMP_SIZE];
+ rng.nextBytes(data);
+ fos.write(data);
+ fos.close();
+
+ FileLoader sfl=new MmapFileLoader();
+ IoBuffer buffer=sfl.loadFile(tempFile);
+
+ assertTrue(buffer.remaining()==TEMP_SIZE);
+
+ for(int i=0;i<TEMP_SIZE;i++) {
+ assertTrue(data[i]==buffer.get());
+ }
+
+ buffer=sfl.loadFile(tempFile);
+
+ assertTrue(buffer.remaining()==TEMP_SIZE);
+
+ for(int i=0;i<TEMP_SIZE;i++) {
+ assertTrue(data[i]==buffer.get());
+ }
+ }
+}
Added:
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoaderTest.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoaderTest.java?rev=630408&view=auto
==============================================================================
---
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoaderTest.java
(added)
+++
mina/asyncweb/trunk/examples/src/test/java/org/apache/asyncweb/examples/file/fileloader/SimpleFileLoaderTest.java
Fri Feb 22 23:34:07 2008
@@ -0,0 +1,46 @@
+package org.apache.asyncweb.examples.file.fileloader;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.Random;
+
+import org.apache.mina.common.IoBuffer;
+import org.junit.Test;
+
+public class SimpleFileLoaderTest {
+
+ private static final int TEMP_SIZE=1024*100+3; // ~100K file
+
+ @Test
+ public void testLoadFile() throws Exception {
+ // generate temp file
+ File tempFile=File.createTempFile("dummy",null);
+ tempFile.deleteOnExit();
+ FileOutputStream fos=new FileOutputStream(tempFile);
+ Random rng=new Random();
+
+ byte[] data = new byte[TEMP_SIZE];
+ rng.nextBytes(data);
+ fos.write(data);
+ fos.close();
+
+ SimpleFileLoader sfl=new SimpleFileLoader();
+ IoBuffer buffer=sfl.loadFile(tempFile);
+
+ assertTrue(buffer.remaining()==TEMP_SIZE);
+
+ for(int i=0;i<TEMP_SIZE;i++) {
+ assertTrue(data[i]==buffer.get());
+ }
+
+ buffer=sfl.loadFile(tempFile);
+
+ assertTrue(buffer.remaining()==TEMP_SIZE);
+
+ for(int i=0;i<TEMP_SIZE;i++) {
+ assertTrue(data[i]==buffer.get());
+ }
+ }
+}