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.nio.intraband.proxy;
016    
017    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
018    import com.liferay.portal.kernel.nio.intraband.proxy.IntrabandProxySkeleton;
019    import com.liferay.portal.kernel.nio.intraband.proxy.IntrabandProxySkeletonRegistryUtil;
020    import com.liferay.portal.kernel.nio.intraband.proxy.TargetLocator;
021    import com.liferay.portal.kernel.nio.intraband.rpc.IntrabandRPCUtil;
022    import com.liferay.portal.kernel.process.ProcessCallable;
023    import com.liferay.portal.kernel.process.ProcessException;
024    import com.liferay.portal.kernel.util.ClassLoaderPool;
025    
026    import java.lang.reflect.Constructor;
027    
028    import java.util.Arrays;
029    import java.util.concurrent.Future;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class IntrabandProxyInstallationUtil {
035    
036            public static void checkProxyMethodSignatures(
037                    String[] skeletonProxyMethodSignatures,
038                    String[] stubProxyMethodSignatures) {
039    
040                    if (Arrays.equals(
041                                    skeletonProxyMethodSignatures, stubProxyMethodSignatures)) {
042    
043                            return;
044                    }
045    
046                    String skeletonProxyMethodSignaturesString = Arrays.toString(
047                            skeletonProxyMethodSignatures);
048                    String stubProxyMethodSignaturesString = Arrays.toString(
049                            stubProxyMethodSignatures);
050    
051                    throw new IllegalStateException(
052                            "Skeleton and stub proxy method signatures do not match. " +
053                                    "Skeleton is " + skeletonProxyMethodSignaturesString +
054                                            ". Stub is " + stubProxyMethodSignaturesString + ".");
055            }
056    
057            public static String[] installSkeleton(
058                    Class<?> clazz, TargetLocator targetLocator) {
059    
060                    return installSkeleton(clazz.getClassLoader(), clazz, targetLocator);
061            }
062    
063            public static String[] installSkeleton(
064                    ClassLoader classLoader, Class<?> clazz, TargetLocator targetLocator) {
065    
066                    try {
067                            Class<?> proxySkeletonClass = IntrabandProxyUtil.getSkeletonClass(
068                                    classLoader, clazz);
069    
070                            Constructor<? extends IntrabandProxySkeleton> constructor =
071                                    (Constructor<? extends IntrabandProxySkeleton>)
072                                            proxySkeletonClass.getConstructor(TargetLocator.class);
073    
074                            IntrabandProxySkeletonRegistryUtil.register(
075                                    clazz.getName(), constructor.newInstance(targetLocator));
076    
077                            return IntrabandProxyUtil.getProxyMethodSignatures(
078                                    proxySkeletonClass);
079                    }
080                    catch (Exception e) {
081                            throw new RuntimeException(e);
082                    }
083            }
084    
085            public static Future<String[]> installSkeleton(
086                    RegistrationReference registrationReference, Class<?> clazz,
087                    TargetLocator targetLocator) {
088    
089                    return installSkeleton(
090                            registrationReference, clazz.getClassLoader(), clazz,
091                            targetLocator);
092            }
093    
094            public static Future<String[]> installSkeleton(
095                    RegistrationReference registrationReference, ClassLoader classLoader,
096                    Class<?> clazz, TargetLocator targetLocator) {
097    
098                    return IntrabandRPCUtil.execute(
099                            registrationReference,
100                            new InstallSkeletonProcessCallable(
101                                    classLoader, clazz, targetLocator));
102            }
103    
104            protected static class InstallSkeletonProcessCallable
105                    implements ProcessCallable<String[]> {
106    
107                    @Override
108                    public String[] call() throws ProcessException {
109                            ClassLoader classLoader = ClassLoaderPool.getClassLoader(
110                                    _servletContextName);
111    
112                            try {
113                                    return installSkeleton(
114                                            classLoader, classLoader.loadClass(_skeletonId),
115                                            _targetLocator);
116                            }
117                            catch (Exception e) {
118                                    throw new ProcessException(e);
119                            }
120                    }
121    
122                    protected InstallSkeletonProcessCallable(
123                            ClassLoader classLoader, Class<?> clazz,
124                            TargetLocator targetLocator) {
125    
126                            _servletContextName = ClassLoaderPool.getContextName(classLoader);
127                            _skeletonId = clazz.getName();
128                            _targetLocator = targetLocator;
129                    }
130    
131                    private static final long serialVersionUID = 1L;
132    
133                    private final String _servletContextName;
134                    private final String _skeletonId;
135                    private final TargetLocator _targetLocator;
136    
137            }
138    
139    }