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 | /** |
---|
23 | * Allows for a default {@link StorageProvider} instance to be configured on an |
---|
24 | * application level. |
---|
25 | * <p> |
---|
26 | * The default instance can be set by either calling |
---|
27 | * {@link #setInstance(StorageProvider)} when the application starts up or by |
---|
28 | * setting the system property |
---|
29 | * <code>org.apache.james.mime4j.defaultStorageProvider</code> to the class |
---|
30 | * name of a <code>StorageProvider</code> implementation. |
---|
31 | * <p> |
---|
32 | * If neither option is used or if the class instantiation fails this class |
---|
33 | * provides a pre-configured default instance. |
---|
34 | */ |
---|
35 | public class DefaultStorageProvider { |
---|
36 | |
---|
37 | /** Value is <code>org.apache.james.mime4j.defaultStorageProvider</code> */ |
---|
38 | public static final String DEFAULT_STORAGE_PROVIDER_PROPERTY = |
---|
39 | "org.apache.james.mime4j.defaultStorageProvider"; |
---|
40 | |
---|
41 | private static volatile StorageProvider instance = null; |
---|
42 | |
---|
43 | static { |
---|
44 | initialize(); |
---|
45 | } |
---|
46 | |
---|
47 | private DefaultStorageProvider() { |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Returns the default {@link StorageProvider} instance. |
---|
52 | * |
---|
53 | * @return the default {@link StorageProvider} instance. |
---|
54 | */ |
---|
55 | public static StorageProvider getInstance() { |
---|
56 | return instance; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Sets the default {@link StorageProvider} instance. |
---|
61 | * |
---|
62 | * @param instance |
---|
63 | * the default {@link StorageProvider} instance. |
---|
64 | */ |
---|
65 | public static void setInstance(StorageProvider instance) { |
---|
66 | if (instance == null) { |
---|
67 | throw new IllegalArgumentException(); |
---|
68 | } |
---|
69 | |
---|
70 | DefaultStorageProvider.instance = instance; |
---|
71 | } |
---|
72 | |
---|
73 | private static void initialize() { |
---|
74 | String clazz = System.getProperty(DEFAULT_STORAGE_PROVIDER_PROPERTY); |
---|
75 | try { |
---|
76 | if (clazz != null) { |
---|
77 | instance = (StorageProvider) Class.forName(clazz).newInstance(); |
---|
78 | } |
---|
79 | } catch (Exception e) { |
---|
80 | } |
---|
81 | if (instance == null) { |
---|
82 | StorageProvider backend = new TempFileStorageProvider(); |
---|
83 | instance = new ThresholdStorageProvider(backend, 1024); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | // for unit tests only |
---|
88 | static void reset() { |
---|
89 | instance = null; |
---|
90 | initialize(); |
---|
91 | } |
---|
92 | |
---|
93 | } |
---|