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.portlet.exportimport.xstream;
016    
017    import com.liferay.portal.kernel.util.AggregateClassLoader;
018    import com.liferay.registry.Registry;
019    import com.liferay.registry.RegistryUtil;
020    import com.liferay.registry.ServiceReference;
021    import com.liferay.registry.ServiceRegistration;
022    import com.liferay.registry.ServiceTracker;
023    import com.liferay.registry.ServiceTrackerCustomizer;
024    import com.liferay.registry.collections.ServiceRegistrationMap;
025    import com.liferay.registry.collections.ServiceRegistrationMapImpl;
026    
027    import java.util.HashSet;
028    import java.util.Map;
029    import java.util.Set;
030    import java.util.concurrent.ConcurrentHashMap;
031    
032    /**
033     * @author Mate Thurzo
034     */
035    public class XStreamAliasRegistryUtil {
036    
037            public static Map<Class<?>, String> getAliases() {
038                    return _instance._getAliases();
039            }
040    
041            public static ClassLoader getAliasesClassLoader(
042                    ClassLoader masterClassLoader) {
043    
044                    Set<ClassLoader> classLoaders = new HashSet<>();
045    
046                    Map<Class<?>, String> aliases = _instance._getAliases();
047    
048                    for (Class<?> clazz : aliases.keySet()) {
049                            classLoaders.add(clazz.getClassLoader());
050                    }
051    
052                    return AggregateClassLoader.getAggregateClassLoader(
053                            masterClassLoader,
054                            classLoaders.toArray(new ClassLoader[classLoaders.size()]));
055            }
056    
057            public static void register(Class<?> clazz, String name) {
058                    _instance._register(clazz, name);
059            }
060    
061            public static void unregister(Class<?> clazz, String name) {
062                    _instance._unregister(clazz, name);
063            }
064    
065            private XStreamAliasRegistryUtil() {
066                    Registry registry = RegistryUtil.getRegistry();
067    
068                    _serviceTracker = registry.trackServices(
069                            XStreamAlias.class, new XStreamAliasServiceTrackerCustomizer());
070    
071                    _serviceTracker.open();
072            }
073    
074            private Map<Class<?>, String> _getAliases() {
075                    return _xstreamAliases;
076            }
077    
078            private void _register(Class<?> clazz, String name) {
079                    XStreamAlias xStreamAlias = new XStreamAlias(clazz, name);
080    
081                    Registry registry = RegistryUtil.getRegistry();
082    
083                    ServiceRegistration<XStreamAlias> serviceRegistration =
084                            registry.registerService(XStreamAlias.class, xStreamAlias);
085    
086                    _serviceRegistrations.put(xStreamAlias, serviceRegistration);
087            }
088    
089            private void _unregister(Class<?> clazz, String name) {
090                    XStreamAlias xStreamAlias = new XStreamAlias(clazz, name);
091    
092                    ServiceRegistration<XStreamAlias> serviceRegistration =
093                            _serviceRegistrations.remove(xStreamAlias);
094    
095                    if (serviceRegistration != null) {
096                            serviceRegistration.unregister();
097                    }
098            }
099    
100            private static final XStreamAliasRegistryUtil _instance =
101                    new XStreamAliasRegistryUtil();
102    
103            private final ServiceRegistrationMap<XStreamAlias> _serviceRegistrations =
104                    new ServiceRegistrationMapImpl<>();
105            private final ServiceTracker<XStreamAlias, XStreamAlias> _serviceTracker;
106            private final Map<Class<?>, String> _xstreamAliases =
107                    new ConcurrentHashMap<>();
108    
109            private class XStreamAliasServiceTrackerCustomizer
110                    implements ServiceTrackerCustomizer<XStreamAlias, XStreamAlias> {
111    
112                    @Override
113                    public XStreamAlias addingService(
114                            ServiceReference<XStreamAlias> serviceReference) {
115    
116                            Registry registry = RegistryUtil.getRegistry();
117    
118                            XStreamAlias xStreamAlias = registry.getService(serviceReference);
119    
120                            _xstreamAliases.put(
121                                    xStreamAlias.getClazz(), xStreamAlias.getName());
122    
123                            return xStreamAlias;
124                    }
125    
126                    @Override
127                    public void modifiedService(
128                            ServiceReference<XStreamAlias> serviceReference,
129                            XStreamAlias xStreamAlias) {
130                    }
131    
132                    @Override
133                    public void removedService(
134                            ServiceReference<XStreamAlias> serviceReference,
135                            XStreamAlias xStreamAlias) {
136    
137                            Registry registry = RegistryUtil.getRegistry();
138    
139                            registry.ungetService(serviceReference);
140    
141                            _xstreamAliases.remove(xStreamAlias.getClazz());
142                    }
143    
144            }
145    
146    }