001    /**
002     * Copyright (c) 2000-present 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.portal.tools;
016    
017    import com.liferay.portal.cache.DummyPortalCache;
018    import com.liferay.portal.cache.key.SimpleCacheKeyGenerator;
019    import com.liferay.portal.json.JSONFactoryImpl;
020    import com.liferay.portal.kernel.cache.MultiVMPool;
021    import com.liferay.portal.kernel.cache.PortalCache;
022    import com.liferay.portal.kernel.cache.PortalCacheManager;
023    import com.liferay.portal.kernel.cache.SingleVMPool;
024    import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
025    import com.liferay.portal.kernel.json.JSONFactoryUtil;
026    import com.liferay.portal.kernel.microsofttranslator.MicrosoftTranslatorFactoryUtil;
027    import com.liferay.portal.kernel.util.DigesterUtil;
028    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
029    import com.liferay.portal.kernel.util.FileUtil;
030    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
031    import com.liferay.portal.kernel.util.HtmlUtil;
032    import com.liferay.portal.kernel.util.HttpUtil;
033    import com.liferay.portal.kernel.xml.SAXReaderUtil;
034    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
035    import com.liferay.portal.microsofttranslator.MicrosoftTranslatorFactoryImpl;
036    import com.liferay.portal.model.DefaultModelHintsImpl;
037    import com.liferay.portal.model.ModelHintsUtil;
038    import com.liferay.portal.security.auth.DefaultFullNameGenerator;
039    import com.liferay.portal.security.auth.FullNameGenerator;
040    import com.liferay.portal.security.permission.ResourceActionsImpl;
041    import com.liferay.portal.security.permission.ResourceActionsUtil;
042    import com.liferay.portal.security.xml.SecureXMLFactoryProviderImpl;
043    import com.liferay.portal.security.xml.SecureXMLFactoryProviderUtil;
044    import com.liferay.portal.service.permission.PortletPermissionImpl;
045    import com.liferay.portal.service.permission.PortletPermissionUtil;
046    import com.liferay.portal.util.DigesterImpl;
047    import com.liferay.portal.util.FastDateFormatFactoryImpl;
048    import com.liferay.portal.util.FileImpl;
049    import com.liferay.portal.util.FriendlyURLNormalizerImpl;
050    import com.liferay.portal.util.HtmlImpl;
051    import com.liferay.portal.util.HttpImpl;
052    import com.liferay.portal.util.InitUtil;
053    import com.liferay.portal.util.PortalImpl;
054    import com.liferay.portal.util.PortalUtil;
055    import com.liferay.portal.xml.SAXReaderImpl;
056    import com.liferay.registry.BasicRegistryImpl;
057    import com.liferay.registry.Registry;
058    import com.liferay.registry.RegistryUtil;
059    
060    import java.io.Serializable;
061    
062    import java.util.concurrent.ConcurrentHashMap;
063    import java.util.concurrent.ConcurrentMap;
064    
065    /**
066     * @author Raymond Aug??
067     */
068    public class ToolDependencies {
069    
070            public static void wireBasic() {
071                    InitUtil.init();
072    
073                    wireCaches();
074    
075                    Registry registry = RegistryUtil.getRegistry();
076    
077                    registry.registerService(
078                            FullNameGenerator.class, new DefaultFullNameGenerator());
079    
080                    CacheKeyGeneratorUtil cacheKeyGeneratorUtil =
081                            new CacheKeyGeneratorUtil();
082    
083                    cacheKeyGeneratorUtil.setDefaultCacheKeyGenerator(
084                            new SimpleCacheKeyGenerator());
085    
086                    DigesterUtil digesterUtil = new DigesterUtil();
087    
088                    digesterUtil.setDigester(new DigesterImpl());
089    
090                    FastDateFormatFactoryUtil fastDateFormatFactoryUtil =
091                            new FastDateFormatFactoryUtil();
092    
093                    fastDateFormatFactoryUtil.setFastDateFormatFactory(
094                            new FastDateFormatFactoryImpl());
095    
096                    FileUtil fileUtil = new FileUtil();
097    
098                    fileUtil.setFile(new FileImpl());
099    
100                    FriendlyURLNormalizerUtil friendlyURLNormalizerUtil =
101                            new FriendlyURLNormalizerUtil();
102    
103                    friendlyURLNormalizerUtil.setFriendlyURLNormalizer(
104                            new FriendlyURLNormalizerImpl());
105    
106                    HtmlUtil htmlUtil = new HtmlUtil();
107    
108                    htmlUtil.setHtml(new HtmlImpl());
109    
110                    HttpUtil httpUtil = new HttpUtil();
111    
112                    httpUtil.setHttp(new HttpImpl());
113    
114                    JSONFactoryUtil jsonFactoryUtil = new JSONFactoryUtil();
115    
116                    jsonFactoryUtil.setJSONFactory(new JSONFactoryImpl());
117    
118                    MicrosoftTranslatorFactoryUtil microsoftTranslatorFactoryUtil =
119                            new MicrosoftTranslatorFactoryUtil();
120    
121                    microsoftTranslatorFactoryUtil.setMicrosoftTranslatorFactory(
122                            new MicrosoftTranslatorFactoryImpl());
123    
124                    PortletPermissionUtil portletPermissionUtil =
125                            new PortletPermissionUtil();
126    
127                    portletPermissionUtil.setPortletPermission(new PortletPermissionImpl());
128    
129                    SAXReaderUtil saxReaderUtil = new SAXReaderUtil();
130    
131                    SAXReaderImpl secureSAXReader = new SAXReaderImpl();
132    
133                    secureSAXReader.setSecure(true);
134    
135                    saxReaderUtil.setSAXReader(secureSAXReader);
136    
137                    SecureXMLFactoryProviderUtil secureXMLFactoryProviderUtil =
138                            new SecureXMLFactoryProviderUtil();
139    
140                    secureXMLFactoryProviderUtil.setSecureXMLFactoryProvider(
141                            new SecureXMLFactoryProviderImpl());
142    
143                    UnsecureSAXReaderUtil unsecureSAXReaderUtil =
144                            new UnsecureSAXReaderUtil();
145    
146                    SAXReaderImpl unsecureSAXReader = new SAXReaderImpl();
147    
148                    unsecureSAXReaderUtil.setSAXReader(unsecureSAXReader);
149    
150                    // DefaultModelHintsImpl requires SecureXMLFactoryProviderUtil
151    
152                    ModelHintsUtil modelHintsUtil = new ModelHintsUtil();
153    
154                    DefaultModelHintsImpl defaultModelHintsImpl =
155                            new DefaultModelHintsImpl();
156    
157                    defaultModelHintsImpl.afterPropertiesSet();
158    
159                    modelHintsUtil.setModelHints(defaultModelHintsImpl);
160            }
161    
162            public static void wireCaches() {
163                    RegistryUtil.setRegistry(new BasicRegistryImpl());
164    
165                    Registry registry = RegistryUtil.getRegistry();
166    
167                    registry.registerService(MultiVMPool.class, new TestMultiVMPool());
168                    registry.registerService(SingleVMPool.class, new TestSingleVMPool());
169            }
170    
171            public static void wireDeployers() {
172                    wireBasic();
173    
174                    PortalUtil portalUtil = new PortalUtil();
175    
176                    portalUtil.setPortal(new PortalImpl());
177            }
178    
179            public static void wireServiceBuilder() {
180                    wireDeployers();
181    
182                    ResourceActionsUtil resourceActionsUtil = new ResourceActionsUtil();
183    
184                    ResourceActionsImpl resourceActionsImpl = new ResourceActionsImpl();
185    
186                    resourceActionsImpl.afterPropertiesSet();
187    
188                    resourceActionsUtil.setResourceActions(resourceActionsImpl);
189            }
190    
191            private static class TestMultiVMPool implements MultiVMPool {
192    
193                    @Override
194                    public void clear() {
195                    }
196    
197                    /**
198                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCache(String)}
199                     */
200                    @Deprecated
201                    @Override
202                    public PortalCache<? extends Serializable, ? extends Serializable>
203                            getCache(String portalCacheName) {
204    
205                            return getPortalCache(portalCacheName);
206                    }
207    
208                    /**
209                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCache(String,
210                     * boolean)}
211                     */
212                    @Deprecated
213                    @Override
214                    public PortalCache<? extends Serializable, ? extends Serializable>
215                            getCache(String portalCacheName, boolean blocking) {
216    
217                            return getPortalCache(portalCacheName, blocking);
218                    }
219    
220                    /**
221                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCacheManager()}
222                     */
223                    @Deprecated
224                    @Override
225                    public PortalCacheManager
226                                    <? extends Serializable, ? extends Serializable>
227                            getCacheManager() {
228    
229                            return getPortalCacheManager();
230                    }
231    
232                    @Override
233                    public PortalCache<? extends Serializable, ? extends Serializable>
234                            getPortalCache(String portalCacheName) {
235    
236                            PortalCache<? extends Serializable, ? extends Serializable>
237                                    portalCache = _portalCaches.get(portalCacheName);
238    
239                            if (portalCache != null) {
240                                    return portalCache;
241                            }
242    
243                            portalCache = new DummyPortalCache<>(portalCacheName);
244    
245                            _portalCaches.putIfAbsent(portalCacheName, portalCache);
246    
247                            return _portalCaches.get(portalCacheName);
248                    }
249    
250                    @Override
251                    public PortalCache<? extends Serializable, ? extends Serializable>
252                            getPortalCache(String portalCacheName, boolean blocking) {
253    
254                            return getPortalCache(portalCacheName);
255                    }
256    
257                    @Override
258                    public PortalCacheManager
259                            <? extends Serializable, ? extends Serializable>
260                             getPortalCacheManager() {
261    
262                            return null;
263                    }
264    
265                    /**
266                     * @deprecated As of 7.0.0, replaced by {@link #removePortalCache(
267                     * String)}
268                     */
269                    @Deprecated
270                    @Override
271                    public void removeCache(String portalCacheName) {
272                            removePortalCache(portalCacheName);
273                    }
274    
275                    @Override
276                    public void removePortalCache(String portalCacheName) {
277                            _portalCaches.remove(portalCacheName);
278                    }
279    
280                    private final ConcurrentMap
281                            <String,
282                                    PortalCache<? extends Serializable, ? extends Serializable>>
283                                            _portalCaches = new ConcurrentHashMap<>();
284    
285            }
286    
287            private static class TestSingleVMPool implements SingleVMPool {
288    
289                    public TestSingleVMPool() {
290                    }
291    
292                    @Override
293                    public void clear() {
294                    }
295    
296                    /**
297                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCache(String)}
298                     */
299                    @Deprecated
300                    @Override
301                    public PortalCache<? extends Serializable, ?> getCache(
302                            String portalCacheName) {
303    
304                            return getPortalCache(portalCacheName);
305                    }
306    
307                    /**
308                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCache(String,
309                     * boolean)}
310                     */
311                    @Deprecated
312                    @Override
313                    public PortalCache<? extends Serializable, ?> getCache(
314                            String portalCacheName, boolean blocking) {
315    
316                            return getPortalCache(portalCacheName, blocking);
317                    }
318    
319                    /**
320                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCacheManager()}
321                     */
322                    @Deprecated
323                    @Override
324                    public PortalCacheManager<? extends Serializable, ?> getCacheManager() {
325                            return getPortalCacheManager();
326                    }
327    
328                    @Override
329                    public PortalCache<? extends Serializable, ?> getPortalCache(
330                            String portalCacheName) {
331    
332                            PortalCache<? extends Serializable, ?> portalCache =
333                                    _portalCaches.get(portalCacheName);
334    
335                            if (portalCache != null) {
336                                    return portalCache;
337                            }
338    
339                            portalCache = new DummyPortalCache<>(portalCacheName);
340    
341                            _portalCaches.putIfAbsent(portalCacheName, portalCache);
342    
343                            return _portalCaches.get(portalCacheName);
344                    }
345    
346                    @Override
347                    public PortalCache<? extends Serializable, ?> getPortalCache(
348                            String portalCacheName, boolean blocking) {
349    
350                            return getPortalCache(portalCacheName);
351                    }
352    
353                    @Override
354                    public PortalCacheManager<? extends Serializable, ?>
355                            getPortalCacheManager() {
356    
357                            return null;
358                    }
359    
360                    /**
361                     * @deprecated As of 7.0.0, replaced by {@link #removePortalCache(
362                     * String)}
363                     */
364                    @Deprecated
365                    @Override
366                    public void removeCache(String portalCacheName) {
367                            removePortalCache(portalCacheName);
368                    }
369    
370                    @Override
371                    public void removePortalCache(String portalCacheName) {
372                            _portalCaches.remove(portalCacheName);
373                    }
374    
375                    private final
376                            ConcurrentMap<String, PortalCache<? extends Serializable, ?>>
377                                    _portalCaches = new ConcurrentHashMap<>();
378    
379            }
380    
381    }