001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.PropsKeys;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
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 =
083                                    PACLClassLoaderUtil.getPortalClassLoader();
084    
085                            try {
086                                    _store = (Store)classLoader.loadClass(
087                                            PropsValues.DL_STORE_IMPL).newInstance();
088                            }
089                            catch (Exception e) {
090                                    _log.error(e, e);
091                            }
092                    }
093    
094                    if (_log.isDebugEnabled()) {
095                            _log.debug("Return " + _store.getClass().getName());
096                    }
097    
098                    return _store;
099            }
100    
101            public static void setInstance(Store store) {
102                    if (_log.isDebugEnabled()) {
103                            _log.debug("Set " + store.getClass().getName());
104                    }
105    
106                    _store = store;
107            }
108    
109            private static final String[][] _DL_HOOK_STORES = new String[][] {
110                    new String[] {
111                            "com.liferay.documentlibrary.util.AdvancedFileSystemHook",
112                            AdvancedFileSystemStore.class.getName()
113                    },
114                    new String[] {
115                            "com.liferay.documentlibrary.util.CMISHook",
116                            CMISStore.class.getName()
117                    },
118                    new String[] {
119                            "com.liferay.documentlibrary.util.FileSystemHook",
120                            FileSystemStore.class.getName()
121                    },
122                    new String[] {
123                            "com.liferay.documentlibrary.util.JCRHook", JCRStore.class.getName()
124                    },
125                    new String[] {
126                            "com.liferay.documentlibrary.util.S3Hook", S3Store.class.getName()
127                    }
128            };
129    
130            private static Log _log = LogFactoryUtil.getLog(StoreFactory.class);
131    
132            private static Store _store;
133            private static boolean _warned;
134    
135    }