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.key.SimpleCacheKeyGenerator;
018    import com.liferay.portal.json.JSONFactoryImpl;
019    import com.liferay.portal.kernel.cache.MultiVMPool;
020    import com.liferay.portal.kernel.cache.PortalCache;
021    import com.liferay.portal.kernel.cache.PortalCacheListener;
022    import com.liferay.portal.kernel.cache.PortalCacheListenerScope;
023    import com.liferay.portal.kernel.cache.PortalCacheManager;
024    import com.liferay.portal.kernel.cache.SingleVMPool;
025    import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
026    import com.liferay.portal.kernel.json.JSONFactoryUtil;
027    import com.liferay.portal.kernel.microsofttranslator.MicrosoftTranslatorFactoryUtil;
028    import com.liferay.portal.kernel.util.DigesterUtil;
029    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
030    import com.liferay.portal.kernel.util.FileUtil;
031    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
032    import com.liferay.portal.kernel.util.HtmlUtil;
033    import com.liferay.portal.kernel.util.HttpUtil;
034    import com.liferay.portal.kernel.xml.SAXReaderUtil;
035    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
036    import com.liferay.portal.microsofttranslator.MicrosoftTranslatorFactoryImpl;
037    import com.liferay.portal.model.DefaultModelHintsImpl;
038    import com.liferay.portal.model.ModelHintsUtil;
039    import com.liferay.portal.security.auth.DefaultFullNameGenerator;
040    import com.liferay.portal.security.auth.FullNameGenerator;
041    import com.liferay.portal.security.permission.ResourceActionsImpl;
042    import com.liferay.portal.security.permission.ResourceActionsUtil;
043    import com.liferay.portal.security.xml.SecureXMLFactoryProviderImpl;
044    import com.liferay.portal.security.xml.SecureXMLFactoryProviderUtil;
045    import com.liferay.portal.service.permission.PortletPermissionImpl;
046    import com.liferay.portal.service.permission.PortletPermissionUtil;
047    import com.liferay.portal.util.DigesterImpl;
048    import com.liferay.portal.util.FastDateFormatFactoryImpl;
049    import com.liferay.portal.util.FileImpl;
050    import com.liferay.portal.util.FriendlyURLNormalizerImpl;
051    import com.liferay.portal.util.HtmlImpl;
052    import com.liferay.portal.util.HttpImpl;
053    import com.liferay.portal.util.InitUtil;
054    import com.liferay.portal.util.PortalImpl;
055    import com.liferay.portal.util.PortalUtil;
056    import com.liferay.portal.xml.SAXReaderImpl;
057    import com.liferay.registry.BasicRegistryImpl;
058    import com.liferay.registry.Registry;
059    import com.liferay.registry.RegistryUtil;
060    
061    import java.io.Serializable;
062    
063    import java.util.ArrayList;
064    import java.util.List;
065    import java.util.Map;
066    import java.util.concurrent.ConcurrentHashMap;
067    import java.util.concurrent.ConcurrentMap;
068    
069    /**
070     * @author Raymond Aug??
071     */
072    public class ToolDependencies {
073    
074            public static void wireBasic() {
075                    InitUtil.init();
076    
077                    wireCaches();
078    
079                    Registry registry = RegistryUtil.getRegistry();
080    
081                    registry.registerService(
082                            FullNameGenerator.class, new DefaultFullNameGenerator());
083    
084                    CacheKeyGeneratorUtil cacheKeyGeneratorUtil =
085                            new CacheKeyGeneratorUtil();
086    
087                    cacheKeyGeneratorUtil.setDefaultCacheKeyGenerator(
088                            new SimpleCacheKeyGenerator());
089    
090                    DigesterUtil digesterUtil = new DigesterUtil();
091    
092                    digesterUtil.setDigester(new DigesterImpl());
093    
094                    FastDateFormatFactoryUtil fastDateFormatFactoryUtil =
095                            new FastDateFormatFactoryUtil();
096    
097                    fastDateFormatFactoryUtil.setFastDateFormatFactory(
098                            new FastDateFormatFactoryImpl());
099    
100                    FileUtil fileUtil = new FileUtil();
101    
102                    fileUtil.setFile(new FileImpl());
103    
104                    FriendlyURLNormalizerUtil friendlyURLNormalizerUtil =
105                            new FriendlyURLNormalizerUtil();
106    
107                    friendlyURLNormalizerUtil.setFriendlyURLNormalizer(
108                            new FriendlyURLNormalizerImpl());
109    
110                    HtmlUtil htmlUtil = new HtmlUtil();
111    
112                    htmlUtil.setHtml(new HtmlImpl());
113    
114                    HttpUtil httpUtil = new HttpUtil();
115    
116                    httpUtil.setHttp(new HttpImpl());
117    
118                    JSONFactoryUtil jsonFactoryUtil = new JSONFactoryUtil();
119    
120                    jsonFactoryUtil.setJSONFactory(new JSONFactoryImpl());
121    
122                    MicrosoftTranslatorFactoryUtil microsoftTranslatorFactoryUtil =
123                            new MicrosoftTranslatorFactoryUtil();
124    
125                    microsoftTranslatorFactoryUtil.setMicrosoftTranslatorFactory(
126                            new MicrosoftTranslatorFactoryImpl());
127    
128                    PortletPermissionUtil portletPermissionUtil =
129                            new PortletPermissionUtil();
130    
131                    portletPermissionUtil.setPortletPermission(new PortletPermissionImpl());
132    
133                    SAXReaderUtil saxReaderUtil = new SAXReaderUtil();
134    
135                    SAXReaderImpl secureSAXReader = new SAXReaderImpl();
136    
137                    secureSAXReader.setSecure(true);
138    
139                    saxReaderUtil.setSAXReader(secureSAXReader);
140    
141                    SecureXMLFactoryProviderUtil secureXMLFactoryProviderUtil =
142                            new SecureXMLFactoryProviderUtil();
143    
144                    secureXMLFactoryProviderUtil.setSecureXMLFactoryProvider(
145                            new SecureXMLFactoryProviderImpl());
146    
147                    UnsecureSAXReaderUtil unsecureSAXReaderUtil =
148                            new UnsecureSAXReaderUtil();
149    
150                    SAXReaderImpl unsecureSAXReader = new SAXReaderImpl();
151    
152                    unsecureSAXReaderUtil.setSAXReader(unsecureSAXReader);
153    
154                    // DefaultModelHintsImpl requires SecureXMLFactoryProviderUtil
155    
156                    ModelHintsUtil modelHintsUtil = new ModelHintsUtil();
157    
158                    DefaultModelHintsImpl defaultModelHintsImpl =
159                            new DefaultModelHintsImpl();
160    
161                    defaultModelHintsImpl.afterPropertiesSet();
162    
163                    modelHintsUtil.setModelHints(defaultModelHintsImpl);
164            }
165    
166            public static void wireCaches() {
167                    RegistryUtil.setRegistry(new BasicRegistryImpl());
168    
169                    Registry registry = RegistryUtil.getRegistry();
170    
171                    registry.registerService(MultiVMPool.class, new TestMultiVMPool());
172                    registry.registerService(SingleVMPool.class, new TestSingleVMPool());
173            }
174    
175            public static void wireDeployers() {
176                    wireBasic();
177    
178                    PortalUtil portalUtil = new PortalUtil();
179    
180                    portalUtil.setPortal(new PortalImpl());
181            }
182    
183            public static void wireServiceBuilder() {
184                    wireDeployers();
185    
186                    ResourceActionsUtil resourceActionsUtil = new ResourceActionsUtil();
187    
188                    ResourceActionsImpl resourceActionsImpl = new ResourceActionsImpl();
189    
190                    resourceActionsImpl.afterPropertiesSet();
191    
192                    resourceActionsUtil.setResourceActions(resourceActionsImpl);
193            }
194    
195            private static class TestMultiVMPool implements MultiVMPool {
196    
197                    @Override
198                    public void clear() {
199                            _portalCaches.clear();
200                    }
201    
202                    /**
203                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCache(String)}
204                     */
205                    @Deprecated
206                    @Override
207                    public PortalCache<? extends Serializable, ? extends Serializable>
208                            getCache(String portalCacheName) {
209    
210                            return getPortalCache(portalCacheName);
211                    }
212    
213                    /**
214                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCache(String,
215                     * boolean)}
216                     */
217                    @Deprecated
218                    @Override
219                    public PortalCache<? extends Serializable, ? extends Serializable>
220                            getCache(String portalCacheName, boolean blocking) {
221    
222                            return getPortalCache(portalCacheName, blocking);
223                    }
224    
225                    /**
226                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCacheManager()}
227                     */
228                    @Deprecated
229                    @Override
230                    public PortalCacheManager
231                            <? extends Serializable, ? extends Serializable>
232                                    getCacheManager() {
233    
234                            return getPortalCacheManager();
235                    }
236    
237                    @Override
238                    public PortalCache<? extends Serializable, ? extends Serializable>
239                            getPortalCache(String portalCacheName) {
240    
241                            PortalCache<? extends Serializable, ? extends Serializable>
242                                    portalCache = _portalCaches.get(portalCacheName);
243    
244                            if (portalCache != null) {
245                                    return portalCache;
246                            }
247    
248                            portalCache = new TestPortalCache<>(portalCacheName);
249    
250                            _portalCaches.putIfAbsent(portalCacheName, portalCache);
251    
252                            return _portalCaches.get(portalCacheName);
253                    }
254    
255                    @Override
256                    public PortalCache<? extends Serializable, ? extends Serializable>
257                            getPortalCache(String portalCacheName, boolean blocking) {
258    
259                            return getPortalCache(portalCacheName);
260                    }
261    
262                    @Override
263                    public PortalCacheManager
264                            <? extends Serializable, ? extends Serializable>
265                                    getPortalCacheManager() {
266    
267                            return null;
268                    }
269    
270                    /**
271                     * @deprecated As of 7.0.0, replaced by {@link #removePortalCache(
272                     * String)}
273                     */
274                    @Deprecated
275                    @Override
276                    public void removeCache(String portalCacheName) {
277                            removePortalCache(portalCacheName);
278                    }
279    
280                    @Override
281                    public void removePortalCache(String portalCacheName) {
282                            _portalCaches.remove(portalCacheName);
283                    }
284    
285                    private final ConcurrentMap
286                            <String,
287                                    PortalCache<? extends Serializable, ? extends Serializable>>
288                                            _portalCaches = new ConcurrentHashMap<>();
289    
290            }
291    
292            private static class TestPortalCache<K extends Serializable, V>
293                    implements PortalCache<K, V> {
294    
295                    public TestPortalCache(String portalCacheName) {
296                            _portalCacheName = portalCacheName;
297                    }
298    
299                    @Override
300                    public V get(K key) {
301                            return _map.get(key);
302                    }
303    
304                    @Override
305                    public List<K> getKeys() {
306                            return new ArrayList<>(_map.keySet());
307                    }
308    
309                    /**
310                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCacheName()}
311                     */
312                    @Deprecated
313                    @Override
314                    public String getName() {
315                            return getPortalCacheName();
316                    }
317    
318                    @Override
319                    public PortalCacheManager<K, V> getPortalCacheManager() {
320                            return null;
321                    }
322    
323                    @Override
324                    public String getPortalCacheName() {
325                            return _portalCacheName;
326                    }
327    
328                    @Override
329                    public void put(K key, V value) {
330                            put(key, value, DEFAULT_TIME_TO_LIVE);
331                    }
332    
333                    @Override
334                    public void put(K key, V value, int timeToLive) {
335                            V oldValue = _map.put(key, value);
336    
337                            for (PortalCacheListener<K, V> portalCacheListener :
338                                            _portalCacheListeners) {
339    
340                                    if (oldValue != null) {
341                                            portalCacheListener.notifyEntryUpdated(
342                                                    this, key, value, timeToLive);
343                                    }
344                                    else {
345                                            portalCacheListener.notifyEntryPut(
346                                                    this, key, value, timeToLive);
347                                    }
348                            }
349                    }
350    
351                    @Override
352                    public void registerPortalCacheListener(
353                            PortalCacheListener<K, V> portalCacheListener) {
354    
355                            _portalCacheListeners.add(portalCacheListener);
356                    }
357    
358                    @Override
359                    public void registerPortalCacheListener(
360                            PortalCacheListener<K, V> portalCacheListener,
361                            PortalCacheListenerScope portalCacheListenerScope) {
362    
363                            _portalCacheListeners.add(portalCacheListener);
364                    }
365    
366                    @Override
367                    public void remove(K key) {
368                            _map.remove(key);
369    
370                            for (PortalCacheListener<K, V> portalCacheListener :
371                                            _portalCacheListeners) {
372    
373                                    portalCacheListener.notifyEntryRemoved(
374                                            this, key, null, DEFAULT_TIME_TO_LIVE);
375                            }
376                    }
377    
378                    @Override
379                    public void removeAll() {
380                            _map.clear();
381    
382                            for (PortalCacheListener<K, V> portalCacheListener :
383                                            _portalCacheListeners) {
384    
385                                    portalCacheListener.notifyRemoveAll(this);
386                            }
387                    }
388    
389                    @Override
390                    public void unregisterPortalCacheListener(
391                            PortalCacheListener<K, V> portalCacheListener) {
392    
393                            portalCacheListener.dispose();
394    
395                            _portalCacheListeners.remove(portalCacheListener);
396                    }
397    
398                    @Override
399                    public void unregisterPortalCacheListeners() {
400                            for (PortalCacheListener<K, V> portalCacheListener :
401                                            _portalCacheListeners) {
402    
403                                    portalCacheListener.dispose();
404                            }
405    
406                            _portalCacheListeners.clear();
407                    }
408    
409                    private final Map<K, V> _map = new ConcurrentHashMap<>();
410                    private final List<PortalCacheListener<K, V>> _portalCacheListeners =
411                            new ArrayList<>();
412                    private final String _portalCacheName;
413    
414            }
415    
416            private static class TestSingleVMPool implements SingleVMPool {
417    
418                    public TestSingleVMPool() {
419                    }
420    
421                    @Override
422                    public void clear() {
423                    }
424    
425                    /**
426                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCache(String)}
427                     */
428                    @Deprecated
429                    @Override
430                    public PortalCache<? extends Serializable, ?> getCache(
431                            String portalCacheName) {
432    
433                            return getPortalCache(portalCacheName);
434                    }
435    
436                    /**
437                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCache(String,
438                     * boolean)}
439                     */
440                    @Deprecated
441                    @Override
442                    public PortalCache<? extends Serializable, ?> getCache(
443                            String portalCacheName, boolean blocking) {
444    
445                            return getPortalCache(portalCacheName, blocking);
446                    }
447    
448                    /**
449                     * @deprecated As of 7.0.0, replaced by {@link #getPortalCacheManager()}
450                     */
451                    @Deprecated
452                    @Override
453                    public PortalCacheManager<? extends Serializable, ?> getCacheManager() {
454                            return getPortalCacheManager();
455                    }
456    
457                    @Override
458                    public PortalCache<? extends Serializable, ?> getPortalCache(
459                            String portalCacheName) {
460    
461                            PortalCache<? extends Serializable, ?> portalCache =
462                                    _portalCaches.get(portalCacheName);
463    
464                            if (portalCache != null) {
465                                    return portalCache;
466                            }
467    
468                            portalCache = new TestPortalCache<>(portalCacheName);
469    
470                            _portalCaches.putIfAbsent(portalCacheName, portalCache);
471    
472                            return _portalCaches.get(portalCacheName);
473                    }
474    
475                    @Override
476                    public PortalCache<? extends Serializable, ?> getPortalCache(
477                            String portalCacheName, boolean blocking) {
478    
479                            return getPortalCache(portalCacheName);
480                    }
481    
482                    @Override
483                    public PortalCacheManager<? extends Serializable, ?>
484                            getPortalCacheManager() {
485    
486                            return null;
487                    }
488    
489                    /**
490                     * @deprecated As of 7.0.0, replaced by {@link #removePortalCache(
491                     * String)}
492                     */
493                    @Deprecated
494                    @Override
495                    public void removeCache(String portalCacheName) {
496                            removePortalCache(portalCacheName);
497                    }
498    
499                    @Override
500                    public void removePortalCache(String portalCacheName) {
501                            _portalCaches.remove(portalCacheName);
502                    }
503    
504                    private final
505                            ConcurrentMap<String, PortalCache<? extends Serializable, ?>>
506                                    _portalCaches = new ConcurrentHashMap<>();
507    
508            }
509    
510    }