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.xmlrpc;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.xmlrpc.Method;
020    import com.liferay.registry.Registry;
021    import com.liferay.registry.RegistryUtil;
022    import com.liferay.registry.ServiceReference;
023    import com.liferay.registry.ServiceTracker;
024    import com.liferay.registry.ServiceTrackerCustomizer;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.concurrent.ConcurrentHashMap;
029    
030    /**
031     * @author Raymond Aug??
032     */
033    public class XmlRpcMethodUtil {
034    
035            public static Method getMethod(String token, String methodName) {
036                    return _instance._getMethod(token, methodName);
037            }
038    
039            protected Method _getMethod(String token, String methodName) {
040                    Method method = null;
041    
042                    Map<String, Method> methods = _methodRegistry.get(token);
043    
044                    if (methods != null) {
045                            method = methods.get(methodName);
046                    }
047    
048                    return method;
049            }
050    
051            private XmlRpcMethodUtil() {
052                    Registry registry = RegistryUtil.getRegistry();
053    
054                    _serviceTracker = registry.trackServices(
055                            Method.class, new MethodServiceTrackerCustomizer());
056    
057                    _serviceTracker.open();
058            }
059    
060            private static final Log _log = LogFactoryUtil.getLog(
061                    XmlRpcMethodUtil.class);
062    
063            private static final XmlRpcMethodUtil _instance = new XmlRpcMethodUtil();
064    
065            private final Map<String, Map<String, Method>> _methodRegistry =
066                    new ConcurrentHashMap<>();
067            private final ServiceTracker<Method, Method> _serviceTracker;
068    
069            private class MethodServiceTrackerCustomizer
070                    implements ServiceTrackerCustomizer<Method, Method> {
071    
072                    @Override
073                    public Method addingService(ServiceReference<Method> serviceReference) {
074                            Registry registry = RegistryUtil.getRegistry();
075    
076                            Method method = registry.getService(serviceReference);
077    
078                            String token = method.getToken();
079    
080                            Map<String, Method> methods = _methodRegistry.get(token);
081    
082                            if (methods == null) {
083                                    methods = new HashMap<>();
084    
085                                    _methodRegistry.put(token, methods);
086                            }
087    
088                            String methodName = method.getMethodName();
089    
090                            Method registeredMethod = methods.get(methodName);
091    
092                            if (registeredMethod != null) {
093                                    if (_log.isWarnEnabled()) {
094                                            _log.warn(
095                                                    "There is already an XML-RPC method registered with " +
096                                                            "name " + methodName + " at " + token);
097                                    }
098                            }
099                            else {
100                                    methods.put(methodName, method);
101                            }
102    
103                            return method;
104                    }
105    
106                    @Override
107                    public void modifiedService(
108                            ServiceReference<Method> serviceReference, Method method) {
109                    }
110    
111                    @Override
112                    public void removedService(
113                            ServiceReference<Method> serviceReference, Method method) {
114    
115                            Registry registry = RegistryUtil.getRegistry();
116    
117                            registry.ungetService(serviceReference);
118    
119                            String token = method.getToken();
120    
121                            Map<String, Method> methods = _methodRegistry.get(token);
122    
123                            if (methods == null) {
124                                    return;
125                            }
126    
127                            String methodName = method.getMethodName();
128    
129                            methods.remove(methodName);
130    
131                            if (methods.isEmpty()) {
132                                    _methodRegistry.remove(token);
133                            }
134                    }
135    
136            }
137    
138    }