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