001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
037     * @author Brian Wing Shun Chan
038     * @author Shuyang Zhou
039     * @author Manuel de la Pe??a
040     * @author Edward C. Han
041     */
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(11);
089    
090                            sb.append("Liferay is configured with the legacy property ");
091                            sb.append("\"dl.hook.impl=");
092                            sb.append(dlHookImpl);
093                            sb.append("\" in portal-ext.properties. Please reconfigure to ");
094                            sb.append("use the new property \"");
095                            sb.append(PropsKeys.DL_STORE_IMPL);
096                            sb.append("\". Liferay will 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    }