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