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.kernel.lar.xstream;
016    
017    import com.liferay.registry.Registry;
018    import com.liferay.registry.RegistryUtil;
019    import com.liferay.registry.ServiceReference;
020    import com.liferay.registry.ServiceRegistration;
021    import com.liferay.registry.ServiceTracker;
022    import com.liferay.registry.ServiceTrackerCustomizer;
023    import com.liferay.registry.collections.ServiceRegistrationMap;
024    
025    import java.util.Map;
026    import java.util.concurrent.ConcurrentHashMap;
027    
028    /**
029     * @author Mate Thurzo
030     */
031    public class XStreamAliasRegistryUtil {
032    
033            public static Map<Class<?>, String> getAliases() {
034                    return _instance._getAliases();
035            }
036    
037            public static void register(Class<?> clazz, String name) {
038                    _instance._register(clazz, name);
039            }
040    
041            public static void unregister(Class<?> clazz, String name) {
042                    _instance._unregister(clazz, name);
043            }
044    
045            private XStreamAliasRegistryUtil() {
046                    Registry registry = RegistryUtil.getRegistry();
047    
048                    _serviceTracker = registry.trackServices(
049                            XStreamAlias.class, new XStreamAliasServiceTrackerCustomizer());
050    
051                    _serviceTracker.open();
052            }
053    
054            private Map<Class<?>, String> _getAliases() {
055                    return _xstreamAliases;
056            }
057    
058            private void _register(Class<?> clazz, String name) {
059                    XStreamAlias xStreamAlias = new XStreamAlias(clazz, name);
060    
061                    Registry registry = RegistryUtil.getRegistry();
062    
063                    ServiceRegistration<XStreamAlias> serviceRegistration =
064                            registry.registerService(XStreamAlias.class, xStreamAlias);
065    
066                    _serviceRegistrations.put(xStreamAlias, serviceRegistration);
067            }
068    
069            private void _unregister(Class<?> clazz, String name) {
070                    XStreamAlias xStreamAlias = new XStreamAlias(clazz, name);
071    
072                    ServiceRegistration<XStreamAlias> serviceRegistration =
073                            _serviceRegistrations.remove(xStreamAlias);
074    
075                    if (serviceRegistration != null) {
076                            serviceRegistration.unregister();
077                    }
078            }
079    
080            private static final XStreamAliasRegistryUtil _instance =
081                    new XStreamAliasRegistryUtil();
082    
083            private final ServiceRegistrationMap<XStreamAlias> _serviceRegistrations =
084                    new ServiceRegistrationMap<XStreamAlias>();
085            private final ServiceTracker<XStreamAlias, XStreamAlias> _serviceTracker;
086            private final Map<Class<?>, String> _xstreamAliases =
087                    new ConcurrentHashMap<Class<?>, String>();
088    
089            private class XStreamAlias {
090    
091                    public XStreamAlias(Class<?> clazz, String name) {
092                            _class = clazz;
093                            _name = name;
094                    }
095    
096                    public Class<?> getClazz() {
097                            return _class;
098                    }
099    
100                    public String getName() {
101                            return _name;
102                    }
103    
104                    private final Class<?> _class;
105                    private final String _name;
106    
107            }
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    }