View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.mime4j.storage;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * Allows for a default {@link StorageProvider} instance to be configured on an
27   * application level.
28   * <p>
29   * The default instance can be set by either calling
30   * {@link #setInstance(StorageProvider)} when the application starts up or by
31   * setting the system property
32   * <code>org.apache.james.mime4j.defaultStorageProvider</code> to the class
33   * name of a <code>StorageProvider</code> implementation.
34   * <p>
35   * If neither option is used or if the class instantiation fails this class
36   * provides a pre-configured default instance.
37   */
38  public class DefaultStorageProvider {
39  
40      /** Value is <code>org.apache.james.mime4j.defaultStorageProvider</code> */
41      public static final String DEFAULT_STORAGE_PROVIDER_PROPERTY =
42          "org.apache.james.mime4j.defaultStorageProvider";
43  
44      private static Log log = LogFactory.getLog(DefaultStorageProvider.class);
45  
46      private static volatile StorageProvider instance = null;
47  
48      static {
49          initialize();
50      }
51  
52      private DefaultStorageProvider() {
53      }
54  
55      /**
56       * Returns the default {@link StorageProvider} instance.
57       *
58       * @return the default {@link StorageProvider} instance.
59       */
60      public static StorageProvider getInstance() {
61          return instance;
62      }
63  
64      /**
65       * Sets the default {@link StorageProvider} instance.
66       *
67       * @param instance
68       *            the default {@link StorageProvider} instance.
69       */
70      public static void setInstance(StorageProvider instance) {
71          if (instance == null) {
72              throw new IllegalArgumentException();
73          }
74  
75          DefaultStorageProvider.instance = instance;
76      }
77  
78      private static void initialize() {
79          String clazz = System.getProperty(DEFAULT_STORAGE_PROVIDER_PROPERTY);
80          try {
81              if (clazz != null) {
82                  instance = (StorageProvider) Class.forName(clazz).newInstance();
83              }
84          } catch (Exception e) {
85              log.warn("Unable to create or instantiate StorageProvider class '"
86                      + clazz + "'. Using default instead.", e);
87          }
88  
89          if (instance == null) {
90              StorageProvider backend = new TempFileStorageProvider();
91              instance = new ThresholdStorageProvider(backend, 1024);
92          }
93      }
94  
95      // for unit tests only
96      static void reset() {
97          instance = null;
98          initialize();
99      }
100 
101 }