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.module.framework.service;
016    
017    import com.liferay.portal.kernel.util.ClassLoaderPool;
018    import com.liferay.portal.kernel.util.ClassLoaderUtil;
019    import com.liferay.portal.kernel.util.MethodHandler;
020    import com.liferay.portal.kernel.util.MethodKey;
021    
022    import java.lang.reflect.Method;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class IdentifiableOSGiServiceInvokerUtil {
028    
029            public static MethodHandler createMethodHandler(
030                    Object targetObject, Method method, Object[] args) {
031    
032                    MethodHandler methodHandler = new MethodHandler(method, args);
033    
034                    String threadContextServletContextName = ClassLoaderPool.getContextName(
035                            ClassLoaderUtil.getContextClassLoader());
036    
037                    IdentifiableOSGiService identifiableOSGiService =
038                            (IdentifiableOSGiService)targetObject;
039    
040                    return new MethodHandler(
041                            _invokeMethodKey, methodHandler, threadContextServletContextName,
042                            identifiableOSGiService.getOSGiServiceIdentifier());
043            }
044    
045            @SuppressWarnings("unused")
046            private static Object _invoke(
047                            MethodHandler methodHandler, String threadContextServletContextName,
048                            String osgiServiceIdentifier)
049                    throws Exception {
050    
051                    Object osgiService =
052                            IdentifiableOSGiServiceUtil.getIdentifiableOSGiService(
053                                    osgiServiceIdentifier);
054    
055                    if (osgiService == null) {
056                            throw new Exception(
057                                    "Unable to load OSGi service " + osgiServiceIdentifier);
058                    }
059    
060                    ClassLoader contextClassLoader =
061                            ClassLoaderUtil.getContextClassLoader();
062    
063                    ClassLoader classLoader = ClassLoaderPool.getClassLoader(
064                            threadContextServletContextName);
065    
066                    ClassLoaderUtil.setContextClassLoader(classLoader);
067    
068                    try {
069                            return methodHandler.invoke(osgiService);
070                    }
071                    finally {
072                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
073                    }
074            }
075    
076            private static final MethodKey _invokeMethodKey = new MethodKey(
077                    IdentifiableOSGiServiceInvokerUtil.class, "_invoke",
078                    MethodHandler.class, String.class, String.class);
079    
080    }