001    /**
002     * Copyright (c) 2000-2012 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.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.PropsUtil;
024    import com.liferay.portal.util.PropsValues;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class StoreFactory {
030    
031            public static void checkProperties() {
032                    if (_warned) {
033                            return;
034                    }
035    
036                    String dlHookImpl = PropsUtil.get("dl.hook.impl");
037    
038                    if (Validator.isNotNull(dlHookImpl)) {
039                            boolean found = false;
040    
041                            for (String[] dlHookStoreParts : _DL_HOOK_STORES) {
042                                    if (dlHookImpl.equals(dlHookStoreParts[0])) {
043                                            PropsValues.DL_STORE_IMPL = dlHookStoreParts[1];
044    
045                                            found = true;
046    
047                                            break;
048                                    }
049                            }
050    
051                            if (!found) {
052                                    PropsValues.DL_STORE_IMPL = dlHookImpl;
053                            }
054    
055                            if (_log.isWarnEnabled()) {
056                                    StringBundler sb = new StringBundler(8);
057    
058                                    sb.append("Liferay is configured with the legacy ");
059                                    sb.append("property \"dl.hook.impl=" + dlHookImpl + "\" ");
060                                    sb.append("in portal-ext.properties. Please reconfigure ");
061                                    sb.append("to use the new property \"");
062                                    sb.append(PropsKeys.DL_STORE_IMPL + "\". Liferay will ");
063                                    sb.append("attempt to temporarily set \"");
064                                    sb.append(PropsKeys.DL_STORE_IMPL + "=");
065                                    sb.append(PropsValues.DL_STORE_IMPL + "\".");
066    
067                                    _log.warn(sb.toString());
068                            }
069                    }
070    
071                    _warned = true;
072            }
073    
074            public static Store getInstance() {
075                    if (_store == null) {
076                            checkProperties();
077    
078                            if (_log.isDebugEnabled()) {
079                                    _log.debug("Instantiate " + PropsValues.DL_STORE_IMPL);
080                            }
081    
082                            ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
083    
084                            try {
085                                    _store = (Store)classLoader.loadClass(
086                                            PropsValues.DL_STORE_IMPL).newInstance();
087                            }
088                            catch (Exception e) {
089                                    _log.error(e, e);
090                            }
091                    }
092    
093                    if (_log.isDebugEnabled()) {
094                            _log.debug("Return " + _store.getClass().getName());
095                    }
096    
097                    return _store;
098            }
099    
100            public static void setInstance(Store store) {
101                    if (_log.isDebugEnabled()) {
102                            _log.debug("Set " + store.getClass().getName());
103                    }
104    
105                    _store = store;
106            }
107    
108            private static final String[][] _DL_HOOK_STORES = new String[][] {
109                    new String[] {
110                            "com.liferay.documentlibrary.util.AdvancedFileSystemHook",
111                            AdvancedFileSystemStore.class.getName()
112                    },
113                    new String[] {
114                            "com.liferay.documentlibrary.util.CMISHook",
115                            CMISStore.class.getName()
116                    },
117                    new String[] {
118                            "com.liferay.documentlibrary.util.FileSystemHook",
119                            FileSystemStore.class.getName()
120                    },
121                    new String[] {
122                            "com.liferay.documentlibrary.util.JCRHook", JCRStore.class.getName()
123                    },
124                    new String[] {
125                            "com.liferay.documentlibrary.util.S3Hook", S3Store.class.getName()
126                    }
127            };
128    
129            private static Log _log = LogFactoryUtil.getLog(StoreFactory.class);
130    
131            private static Store _store;
132            private static boolean _warned;
133    
134    }