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