001    /**
002     * Copyright (c) 2000-2013 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.bean.PortalBeanLocatorUtil;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ClassUtil;
023    import com.liferay.portal.kernel.util.InstanceFactory;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.ProxyUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.spring.aop.MethodInterceptorInvocationHandler;
029    import com.liferay.portal.util.ClassLoaderUtil;
030    import com.liferay.portal.util.PropsUtil;
031    import com.liferay.portal.util.PropsValues;
032    
033    import java.util.Arrays;
034    import java.util.List;
035    
036    import org.aopalliance.intercept.MethodInterceptor;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     * @author Shuyang Zhou
041     */
042    public class StoreFactory {
043    
044            public static void checkProperties() {
045                    if (_warned) {
046                            return;
047                    }
048    
049                    String dlHookImpl = PropsUtil.get("dl.hook.impl");
050    
051                    if (Validator.isNotNull(dlHookImpl)) {
052                            boolean found = false;
053    
054                            for (String[] dlHookStoreParts : _DL_HOOK_STORES) {
055                                    if (dlHookImpl.equals(dlHookStoreParts[0])) {
056                                            PropsValues.DL_STORE_IMPL = dlHookStoreParts[1];
057    
058                                            found = true;
059    
060                                            break;
061                                    }
062                            }
063    
064                            if (!found) {
065                                    PropsValues.DL_STORE_IMPL = dlHookImpl;
066                            }
067    
068                            if (_log.isWarnEnabled()) {
069                                    StringBundler sb = new StringBundler(8);
070    
071                                    sb.append("Liferay is configured with the legacy ");
072                                    sb.append("property \"dl.hook.impl=" + dlHookImpl + "\" ");
073                                    sb.append("in portal-ext.properties. Please reconfigure ");
074                                    sb.append("to use the new property \"");
075                                    sb.append(PropsKeys.DL_STORE_IMPL + "\". Liferay will ");
076                                    sb.append("attempt to temporarily set \"");
077                                    sb.append(PropsKeys.DL_STORE_IMPL + "=");
078                                    sb.append(PropsValues.DL_STORE_IMPL + "\".");
079    
080                                    _log.warn(sb.toString());
081                            }
082                    }
083    
084                    _warned = true;
085            }
086    
087            public static Store getInstance() {
088                    if (_store == null) {
089                            checkProperties();
090    
091                            if (_log.isDebugEnabled()) {
092                                    _log.debug("Instantiate " + PropsValues.DL_STORE_IMPL);
093                            }
094    
095                            try {
096                                    _store = _getInstance();
097                            }
098                            catch (Exception e) {
099                                    _log.error(e, e);
100                            }
101                    }
102    
103                    if ((_store != null) && _log.isDebugEnabled()) {
104                            Class<?> clazz = _store.getClass();
105    
106                            _log.debug("Return " + clazz.getName());
107                    }
108    
109                    return _store;
110            }
111    
112            public static void setInstance(Store store) {
113                    if (_log.isDebugEnabled()) {
114                            _log.debug("Set " + ClassUtil.getClassName(store));
115                    }
116    
117                    _store = store;
118            }
119    
120            private static Store _getInstance() throws Exception {
121                    ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
122    
123                    Store store = (Store)InstanceFactory.newInstance(
124                            classLoader, PropsValues.DL_STORE_IMPL);
125    
126                    if (store instanceof DBStore) {
127                            DB db = DBFactoryUtil.getDB();
128    
129                            String dbType = db.getType();
130    
131                            if (dbType.equals(DB.TYPE_POSTGRESQL)) {
132                                    MethodInterceptor transactionAdviceMethodInterceptor =
133                                            (MethodInterceptor)PortalBeanLocatorUtil.locate(
134                                                    "transactionAdvice");
135    
136                                    MethodInterceptor tempFileMethodInterceptor =
137                                            new TempFileMethodInterceptor();
138    
139                                    List<MethodInterceptor> methodInterceptors =
140                                            Arrays.asList(
141                                                    transactionAdviceMethodInterceptor,
142                                                    tempFileMethodInterceptor);
143    
144                                    store = (Store)ProxyUtil.newProxyInstance(
145                                            classLoader, new Class<?>[] {Store.class},
146                                            new MethodInterceptorInvocationHandler(
147                                                    store, methodInterceptors));
148                            }
149                    }
150    
151                    return store;
152            }
153    
154            private static final String[][] _DL_HOOK_STORES = new String[][] {
155                    new String[] {
156                            "com.liferay.documentlibrary.util.AdvancedFileSystemHook",
157                            AdvancedFileSystemStore.class.getName()
158                    },
159                    new String[] {
160                            "com.liferay.documentlibrary.util.CMISHook",
161                            CMISStore.class.getName()
162                    },
163                    new String[] {
164                            "com.liferay.documentlibrary.util.FileSystemHook",
165                            FileSystemStore.class.getName()
166                    },
167                    new String[] {
168                            "com.liferay.documentlibrary.util.JCRHook", JCRStore.class.getName()
169                    },
170                    new String[] {
171                            "com.liferay.documentlibrary.util.S3Hook", S3Store.class.getName()
172                    }
173            };
174    
175            private static Log _log = LogFactoryUtil.getLog(StoreFactory.class);
176    
177            private static Store _store;
178            private static boolean _warned;
179    
180    }