001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.upload.services;
016:
017: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
018: import org.apache.commons.io.FileCleaner;
019: import org.apache.tapestry.ioc.Configuration;
020: import org.apache.tapestry.ioc.MappedConfiguration;
021: import org.apache.tapestry.ioc.OrderedConfiguration;
022: import org.apache.tapestry.ioc.annotations.Inject;
023: import org.apache.tapestry.ioc.annotations.Scope;
024: import org.apache.tapestry.ioc.annotations.Symbol;
025: import org.apache.tapestry.ioc.services.RegistryShutdownHub;
026: import org.apache.tapestry.ioc.services.RegistryShutdownListener;
027: import org.apache.tapestry.ioc.services.SymbolSource;
028: import org.apache.tapestry.ioc.services.ThreadCleanupHub;
029: import org.apache.tapestry.services.HttpServletRequestFilter;
030: import org.apache.tapestry.services.LibraryMapping;
031:
032: public class UploadModule {
033: private static boolean _shutdownListenerSet;
034:
035: public static void contributeComponentClassResolver(
036: Configuration<LibraryMapping> configuration) {
037: // Add the component to the "core" library.
038:
039: configuration.add(new LibraryMapping("core",
040: "org.apache.tapestry.upload"));
041: }
042:
043: @Scope("perthread")
044: public synchronized static MultipartDecoder buildMultipartDecoder(
045: ThreadCleanupHub threadCleanupHub,
046:
047: RegistryShutdownHub shutdownHub,
048:
049: @Inject
050: @Symbol(UploadSymbols.REPOSITORY_LOCATION)
051: String repositoryPath,
052:
053: @Symbol(UploadSymbols.REPOSITORY_THRESHOLD)
054: int repositoryThreshold,
055:
056: @Symbol(UploadSymbols.REQUESTSIZE_MAX)
057: long maxRequestSize,
058:
059: @Symbol(UploadSymbols.FILESIZE_MAX)
060: long maxFileSize,
061:
062: SymbolSource symbolSource) {
063: MultipartDecoderImpl multipartDecoder = new MultipartDecoderImpl(
064: repositoryPath, repositoryThreshold, maxRequestSize,
065: maxFileSize);
066:
067: // This is proabably overkill since the FileCleaner should catch temporary files, but lets
068: // be safe.
069: threadCleanupHub.addThreadCleanupListener(multipartDecoder);
070:
071: if (_shutdownListenerSet) {
072: shutdownHub
073: .addRegistryShutdownListener(new RegistryShutdownListener() {
074: public void registryDidShutdown() {
075: FileCleaner.exitWhenFinished();
076: }
077: });
078:
079: _shutdownListenerSet = true;
080: }
081:
082: return multipartDecoder;
083: }
084:
085: public static void contributeHttpServletRequestHandler(
086: OrderedConfiguration<HttpServletRequestFilter> configuration,
087: MultipartDecoder multipartDecoder) {
088: configuration.add("MultipartFilter",
089: new MultipartServletRequestFilter(multipartDecoder));
090: }
091:
092: public static void contributeFactoryDefaults(
093: MappedConfiguration<String, String> configuration) {
094: configuration.add(UploadSymbols.REPOSITORY_THRESHOLD, Integer
095: .toString(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD));
096: configuration.add(UploadSymbols.REPOSITORY_LOCATION, System
097: .getProperty("java.io.tmpdir"));
098: // No limit
099: configuration.add(UploadSymbols.REQUESTSIZE_MAX, "-1");
100: // No limit
101: configuration.add(UploadSymbols.FILESIZE_MAX, "-1");
102: }
103: }
|