001
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
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 }