001
014
015 package com.liferay.portlet.documentlibrary.store;
016
017 import com.liferay.document.library.kernel.store.Store;
018 import com.liferay.document.library.kernel.store.StoreWrapper;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.PropsKeys;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.util.PropsUtil;
025 import com.liferay.portal.util.PropsValues;
026 import com.liferay.registry.Registry;
027 import com.liferay.registry.RegistryUtil;
028 import com.liferay.registry.ServiceReference;
029 import com.liferay.registry.ServiceTrackerCustomizer;
030 import com.liferay.registry.collections.ServiceTrackerCollections;
031 import com.liferay.registry.collections.ServiceTrackerMap;
032
033 import java.util.List;
034 import java.util.Set;
035
036
042 public class StoreFactory {
043
044 public static StoreFactory getInstance() {
045 if (_storeFactory == null) {
046 _storeFactory = new StoreFactory();
047 }
048
049 return _storeFactory;
050 }
051
052 public void checkProperties() {
053 if (_warned) {
054 return;
055 }
056
057 String dlHookImpl = PropsUtil.get("dl.hook.impl");
058
059 if (Validator.isNull(dlHookImpl)) {
060 _warned = true;
061
062 return;
063 }
064
065 boolean found = false;
066
067 for (String key : _storeServiceTrackerMap.keySet()) {
068 Store store = getStore(key);
069
070 Class<?> clazz = store.getClass();
071
072 String className = clazz.getName();
073
074 if (dlHookImpl.equals(className)) {
075 PropsValues.DL_STORE_IMPL = className;
076
077 found = true;
078
079 break;
080 }
081 }
082
083 if (!found) {
084 PropsValues.DL_STORE_IMPL = dlHookImpl;
085 }
086
087 if (_log.isWarnEnabled()) {
088 StringBundler sb = new StringBundler(13);
089
090 sb.append("Liferay is configured with the legacy ");
091 sb.append("property \"dl.hook.impl=");
092 sb.append(dlHookImpl);
093 sb.append("\" ");
094 sb.append("in portal-ext.properties. Please reconfigure ");
095 sb.append("to use the new property \"");
096 sb.append(PropsKeys.DL_STORE_IMPL);
097 sb.append("\". Liferay will ");
098 sb.append("attempt to temporarily set \"");
099 sb.append(PropsKeys.DL_STORE_IMPL);
100 sb.append("=");
101 sb.append(PropsValues.DL_STORE_IMPL);
102 sb.append("\".");
103
104 _log.warn(sb.toString());
105 }
106
107 _warned = true;
108 }
109
110 public void destroy() {
111 _storeServiceTrackerMap.close();
112
113 _storeWrapperServiceTrackerMap.close();
114 }
115
116 public Store getStore() {
117 if (_store == null) {
118 if (Validator.isNull(_storeType)) {
119 setStore(PropsValues.DL_STORE_IMPL);
120 }
121 else {
122 setStore(_storeType);
123 }
124 }
125
126 if (_store == null) {
127 throw new IllegalStateException("Store is not available");
128 }
129
130 return _store;
131 }
132
133 public Store getStore(String key) {
134 Store store = _storeServiceTrackerMap.getService(key);
135
136 List<StoreWrapper> storeWrappers =
137 _storeWrapperServiceTrackerMap.getService(key);
138
139 if (storeWrappers == null) {
140 return store;
141 }
142
143 for (StoreWrapper storeWrapper : storeWrappers) {
144 store = storeWrapper.wrap(store);
145 }
146
147 return store;
148 }
149
150 public String[] getStoreTypes() {
151 Set<String> storeTypes = _storeServiceTrackerMap.keySet();
152
153 return storeTypes.toArray(new String[storeTypes.size()]);
154 }
155
156 public void setStore(String key) {
157 if (key == null) {
158 _store = null;
159 _storeType = null;
160
161 return;
162 }
163
164 if (_log.isDebugEnabled()) {
165 _log.debug("Set " + key);
166 }
167
168 _store = getStore(key);
169 _storeType = key;
170 }
171
172 private static final Log _log = LogFactoryUtil.getLog(StoreFactory.class);
173
174 private static StoreFactory _storeFactory;
175 private static boolean _warned;
176
177 private volatile Store _store;
178 private final ServiceTrackerMap<String, Store> _storeServiceTrackerMap =
179 ServiceTrackerCollections.openSingleValueMap(
180 Store.class, "store.type", new StoreServiceTrackerCustomizer());
181 private String _storeType;
182 private final ServiceTrackerMap<String, List<StoreWrapper>>
183 _storeWrapperServiceTrackerMap =
184 ServiceTrackerCollections.openMultiValueMap(
185 StoreWrapper.class, "store.type");
186
187 private class StoreServiceTrackerCustomizer
188 implements ServiceTrackerCustomizer<Store, Store> {
189
190 @Override
191 public Store addingService(ServiceReference<Store> serviceReference) {
192 cleanUp(serviceReference);
193
194 Registry registry = RegistryUtil.getRegistry();
195
196 return registry.getService(serviceReference);
197 }
198
199 @Override
200 public void modifiedService(
201 ServiceReference<Store> serviceReference, Store service) {
202
203 cleanUp(serviceReference);
204 }
205
206 @Override
207 public void removedService(
208 ServiceReference<Store> serviceReference, Store service) {
209
210 cleanUp(serviceReference);
211
212 Registry registry = RegistryUtil.getRegistry();
213
214 registry.ungetService(serviceReference);
215 }
216
217 protected void cleanUp(ServiceReference<Store> serviceReference) {
218 String storeType = (String)serviceReference.getProperty(
219 "store.type");
220
221 if (Validator.isNotNull(_storeType) &&
222 _storeType.equals(storeType)) {
223
224 _store = null;
225 }
226 }
227
228 }
229
230 }