1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.config;
20
21 import org.apache.shiro.io.ResourceUtils;
22 import org.apache.shiro.util.AbstractFactory;
23 import org.apache.shiro.util.CollectionUtils;
24 import org.apache.shiro.util.Factory;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31
32
33
34
35
36
37
38 @Deprecated
39 public abstract class IniFactorySupport<T> extends AbstractFactory<T> {
40
41 public static final String DEFAULT_INI_RESOURCE_PATH = "classpath:shiro.ini";
42
43 private static transient final Logger log = LoggerFactory.getLogger(IniFactorySupport.class);
44
45 private Ini ini;
46
47 private Map<String, ?> defaultBeans;
48
49 protected IniFactorySupport() {
50 }
51
52 protected IniFactorySupport(Ini ini) {
53 this.ini = ini;
54 }
55
56 public Ini getIni() {
57 return ini;
58 }
59
60 public void setIni(Ini ini) {
61 this.ini = ini;
62 }
63
64
65
66
67
68
69
70 protected Map<String, ?> getDefaults() {
71 return defaultBeans;
72 }
73
74
75
76
77
78
79
80 public void setDefaults(Map<String, ?> defaultBeans) {
81 this.defaultBeans = defaultBeans;
82 }
83
84
85
86
87
88
89
90
91 public static Ini loadDefaultClassPathIni() {
92 Ini ini = null;
93 if (ResourceUtils.resourceExists(DEFAULT_INI_RESOURCE_PATH)) {
94 log.debug("Found shiro.ini at the root of the classpath.");
95 ini = new Ini();
96 ini.loadFromPath(DEFAULT_INI_RESOURCE_PATH);
97 if (CollectionUtils.isEmpty(ini)) {
98 log.warn("shiro.ini found at the root of the classpath, but it did not contain any data.");
99 }
100 }
101 return ini;
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115 protected Ini resolveIni() {
116 Ini ini = getIni();
117 if (CollectionUtils.isEmpty(ini)) {
118 log.debug("Null or empty Ini instance. Falling back to the default {} file.", DEFAULT_INI_RESOURCE_PATH);
119 ini = loadDefaultClassPathIni();
120 }
121 return ini;
122 }
123
124
125
126
127
128
129
130
131
132
133
134 public T createInstance() {
135 Ini ini = resolveIni();
136
137 T instance;
138
139 if (CollectionUtils.isEmpty(ini)) {
140 log.debug("No populated Ini available. Creating a default instance.");
141 instance = createDefaultInstance();
142 if (instance == null) {
143 String msg = getClass().getName() + " implementation did not return a default instance in " +
144 "the event of a null/empty Ini configuration. This is required to support the " +
145 "Factory interface. Please check your implementation.";
146 throw new IllegalStateException(msg);
147 }
148 } else {
149 log.debug("Creating instance from Ini [" + ini + "]");
150 instance = createInstance(ini);
151 if (instance == null) {
152 String msg = getClass().getName() + " implementation did not return a constructed instance from " +
153 "the createInstance(Ini) method implementation.";
154 throw new IllegalStateException(msg);
155 }
156 }
157
158 return instance;
159 }
160
161 protected abstract T createInstance(Ini ini);
162
163 protected abstract T createDefaultInstance();
164 }