Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.pgm.init.api; 16 : 17 : import static com.google.gerrit.common.FileUtil.modified; 18 : 19 : import com.google.common.io.ByteStreams; 20 : import com.google.gerrit.common.Die; 21 : import com.google.gerrit.common.Nullable; 22 : import java.io.ByteArrayInputStream; 23 : import java.io.File; 24 : import java.io.FileNotFoundException; 25 : import java.io.IOException; 26 : import java.io.InputStream; 27 : import java.io.OutputStream; 28 : import java.net.InetAddress; 29 : import java.net.URI; 30 : import java.net.URISyntaxException; 31 : import java.net.UnknownHostException; 32 : import java.nio.file.Files; 33 : import java.nio.file.NoSuchFileException; 34 : import java.nio.file.Path; 35 : import java.util.Arrays; 36 : import org.eclipse.jgit.internal.storage.file.LockFile; 37 : import org.eclipse.jgit.storage.file.FileBasedConfig; 38 : import org.eclipse.jgit.util.SystemReader; 39 : 40 : /** Utility functions to help initialize a site. */ 41 : public class InitUtil { 42 : public static Die die(String why) { 43 0 : return new Die(why); 44 : } 45 : 46 : public static Die die(String why, Throwable cause) { 47 0 : return new Die(why, cause); 48 : } 49 : 50 : public static void savePublic(FileBasedConfig sec) throws IOException { 51 15 : if (modified(sec)) { 52 15 : sec.save(); 53 : } 54 15 : } 55 : 56 : public static void mkdir(File file) { 57 0 : mkdir(file.toPath()); 58 0 : } 59 : 60 : public static void mkdir(Path path) { 61 15 : if (Files.isDirectory(path)) { 62 15 : return; 63 : } 64 : try { 65 15 : Files.createDirectory(path); 66 0 : } catch (IOException e) { 67 0 : throw die("Cannot make directory " + path, e); 68 15 : } 69 15 : } 70 : 71 : public static String version() { 72 15 : return com.google.gerrit.common.Version.getVersion(); 73 : } 74 : 75 : public static String username() { 76 15 : return System.getProperty("user.name"); 77 : } 78 : 79 : public static String hostname() { 80 8 : return SystemReader.getInstance().getHostname(); 81 : } 82 : 83 : public static boolean isLocal(String hostname) { 84 : try { 85 0 : return InetAddress.getByName(hostname).isLoopbackAddress(); 86 0 : } catch (UnknownHostException e) { 87 0 : return false; 88 : } 89 : } 90 : 91 : public static String dnOf(String name) { 92 0 : if (name != null) { 93 0 : int p = name.indexOf("://"); 94 0 : if (0 < p) { 95 0 : name = name.substring(p + 3); 96 : } 97 : 98 0 : p = name.indexOf("."); 99 0 : if (0 < p) { 100 0 : name = name.substring(p + 1); 101 0 : name = "DC=" + name.replace(".", ",DC="); 102 : } else { 103 0 : name = null; 104 : } 105 : } 106 0 : return name; 107 : } 108 : 109 : public static String domainOf(String name) { 110 0 : if (name != null) { 111 0 : int p = name.indexOf("://"); 112 0 : if (0 < p) { 113 0 : name = name.substring(p + 3); 114 : } 115 0 : p = name.indexOf("."); 116 0 : if (0 < p) { 117 0 : name = name.substring(p + 1); 118 : } 119 : } 120 0 : return name; 121 : } 122 : 123 : public static void extract(Path dst, Class<?> sibling, String name) throws IOException { 124 15 : try (InputStream in = open(sibling, name)) { 125 15 : if (in != null) { 126 15 : copy(dst, ByteStreams.toByteArray(in)); 127 : } 128 : } 129 15 : } 130 : 131 : @Nullable 132 : private static InputStream open(Class<?> sibling, String name) { 133 15 : final InputStream in = sibling.getResourceAsStream(name); 134 15 : if (in == null) { 135 0 : String pkg = sibling.getName(); 136 0 : int end = pkg.lastIndexOf('.'); 137 0 : if (0 < end) { 138 0 : pkg = pkg.substring(0, end + 1); 139 0 : pkg = pkg.replace('.', '/'); 140 : } else { 141 0 : pkg = ""; 142 : } 143 0 : System.err.println("warn: Cannot read " + pkg + name); 144 0 : return null; 145 : } 146 15 : return in; 147 : } 148 : 149 : public static void copy(Path dst, byte[] buf) throws FileNotFoundException, IOException { 150 : // If the file already has the content we want to put there, 151 : // don't attempt to overwrite the file. 152 : // 153 1 : try (InputStream in = Files.newInputStream(dst)) { 154 1 : if (Arrays.equals(buf, ByteStreams.toByteArray(in))) { 155 1 : return; 156 : } 157 15 : } catch (NoSuchFileException notFound) { 158 : // Fall through and write the file. 159 0 : } 160 : 161 15 : Files.createDirectories(dst.getParent()); 162 15 : LockFile lf = new LockFile(dst.toFile()); 163 15 : if (!lf.lock()) { 164 0 : throw new IOException("Cannot lock " + dst); 165 : } 166 : try { 167 15 : try (InputStream in = new ByteArrayInputStream(buf); 168 15 : OutputStream out = lf.getOutputStream()) { 169 15 : ByteStreams.copy(in, out); 170 : } 171 15 : if (!lf.commit()) { 172 0 : throw new IOException("Cannot commit " + dst); 173 : } 174 : } finally { 175 15 : lf.unlock(); 176 : } 177 15 : } 178 : 179 : public static URI toURI(String url) throws URISyntaxException { 180 15 : final URI u = new URI(url); 181 15 : if (isAnyAddress(u)) { 182 : // If the URL uses * it means all addresses on this system, use the 183 : // current hostname instead in the returned URI. 184 : // 185 0 : final int s = url.indexOf('*'); 186 0 : url = url.substring(0, s) + hostname() + url.substring(s + 1); 187 : } 188 15 : return new URI(url); 189 : } 190 : 191 : public static boolean isAnyAddress(URI u) { 192 15 : return u.getHost() == null 193 15 : && (u.getAuthority().equals("*") || u.getAuthority().startsWith("*:")); 194 : } 195 : 196 : public static int portOf(URI uri) { 197 0 : int port = uri.getPort(); 198 0 : if (port < 0) { 199 0 : port = "https".equals(uri.getScheme()) ? 443 : 80; 200 : } 201 0 : return port; 202 : } 203 : 204 : private InitUtil() {} 205 : }