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