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