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