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.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.Http;
020    import com.liferay.portal.kernel.util.PortalRunMode;
021    import com.liferay.portal.kernel.util.ReflectionUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.io.InputStream;
025    
026    import java.lang.reflect.Method;
027    
028    import java.net.InetAddress;
029    import java.net.URI;
030    import java.net.URL;
031    import java.net.URLClassLoader;
032    import java.net.UnknownHostException;
033    
034    import java.nio.file.Files;
035    import java.nio.file.Path;
036    import java.nio.file.Paths;
037    import java.nio.file.StandardCopyOption;
038    
039    /**
040     * @author Shuyang Zhou
041     */
042    public class JarUtil {
043    
044            public static Path downloadAndInstallJar(
045                            URL url, String libPath, String name)
046                    throws Exception {
047    
048                    String protocol = url.getProtocol();
049    
050                    if (PortalRunMode.isTestMode() &&
051                            (protocol.equals(Http.HTTP) || protocol.equals(Http.HTTPS))) {
052    
053                            try {
054                                    InetAddress.getAllByName("mirrors");
055    
056                                    String urlString = url.toExternalForm();
057    
058                                    String newURLString = StringUtil.replace(
059                                            urlString, "://", "://mirrors/");
060    
061                                    url = new URL(newURLString);
062    
063                                    if (_log.isDebugEnabled()) {
064                                            _log.debug(
065                                                    "Swapping URL from " + urlString + " to " +
066                                                            newURLString);
067                                    }
068                            }
069                            catch (UnknownHostException uhe) {
070                                    if (_log.isDebugEnabled()) {
071                                            _log.debug("Unable to resolve \"mirrors\"");
072                                    }
073                            }
074                    }
075    
076                    Path path = Paths.get(libPath, name);
077    
078                    if (_log.isInfoEnabled()) {
079                            _log.info("Downloading " + url + " to " + path);
080                    }
081    
082                    try (InputStream inputStream = url.openStream()) {
083                            Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
084                    }
085    
086                    if (_log.isInfoEnabled()) {
087                            _log.info("Downloaded " + url + " to " + path);
088                    }
089    
090                    return path;
091            }
092    
093            public static void downloadAndInstallJar(
094                            URL url, String libPath, String name, URLClassLoader urlClassLoader)
095                    throws Exception {
096    
097                    Path path = downloadAndInstallJar(url, libPath, name);
098    
099                    URI uri = path.toUri();
100    
101                    if (_log.isInfoEnabled()) {
102                            _log.info("Installing " + path + " to " + urlClassLoader);
103                    }
104    
105                    _addURLMethod.invoke(urlClassLoader, uri.toURL());
106    
107                    if (_log.isInfoEnabled()) {
108                            _log.info("Installed " + path + " to " + urlClassLoader);
109                    }
110            }
111    
112            private static final Log _log = LogFactoryUtil.getLog(JarUtil.class);
113    
114            private static final Method _addURLMethod;
115    
116            static {
117                    try {
118                            _addURLMethod = ReflectionUtil.getDeclaredMethod(
119                                    URLClassLoader.class, "addURL", URL.class);
120                    }
121                    catch (Exception e) {
122                            throw new ExceptionInInitializerError(e);
123                    }
124            }
125    
126    }