001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package org.apache.james.mime4j.storage;
021
022 /**
023 * Allows for a default {@link StorageProvider} instance to be configured on an
024 * application level.
025 * <p>
026 * The default instance can be set by either calling
027 * {@link #setInstance(StorageProvider)} when the application starts up or by
028 * setting the system property
029 * <code>org.apache.james.mime4j.defaultStorageProvider</code> to the class
030 * name of a <code>StorageProvider</code> implementation.
031 * <p>
032 * If neither option is used or if the class instantiation fails this class
033 * provides a pre-configured default instance.
034 */
035 public class DefaultStorageProvider {
036
037 /** Value is <code>org.apache.james.mime4j.defaultStorageProvider</code> */
038 public static final String DEFAULT_STORAGE_PROVIDER_PROPERTY =
039 "org.apache.james.mime4j.defaultStorageProvider";
040
041 private static volatile StorageProvider instance = null;
042
043 static {
044 initialize();
045 }
046
047 private DefaultStorageProvider() {
048 }
049
050 /**
051 * Returns the default {@link StorageProvider} instance.
052 *
053 * @return the default {@link StorageProvider} instance.
054 */
055 public static StorageProvider getInstance() {
056 return instance;
057 }
058
059 /**
060 * Sets the default {@link StorageProvider} instance.
061 *
062 * @param instance
063 * the default {@link StorageProvider} instance.
064 */
065 public static void setInstance(StorageProvider instance) {
066 if (instance == null) {
067 throw new IllegalArgumentException();
068 }
069
070 DefaultStorageProvider.instance = instance;
071 }
072
073 private static void initialize() {
074 String clazz = System.getProperty(DEFAULT_STORAGE_PROVIDER_PROPERTY);
075 try {
076 if (clazz != null) {
077 instance = (StorageProvider) Class.forName(clazz).newInstance();
078 }
079 } catch (Exception e) {
080 }
081 if (instance == null) {
082 StorageProvider backend = new TempFileStorageProvider();
083 instance = new ThresholdStorageProvider(backend, 1024);
084 }
085 }
086
087 // for unit tests only
088 static void reset() {
089 instance = null;
090 initialize();
091 }
092
093 }