Util.java 16.5 KB
Newer Older
K
kohsuke 已提交
1 2
package hudson;

K
kohsuke 已提交
3 4 5 6 7
import hudson.model.TaskListener;
import hudson.util.IOException2;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Chmod;
import org.apache.tools.ant.taskdefs.Copy;
K
kohsuke 已提交
8 9

import java.io.BufferedReader;
10
import java.io.ByteArrayInputStream;
K
kohsuke 已提交
11 12 13 14 15 16 17 18
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
K
kohsuke 已提交
19 20
import java.io.Reader;
import java.io.Writer;
K
kohsuke 已提交
21 22
import java.net.InetAddress;
import java.net.UnknownHostException;
23 24 25
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
K
kohsuke 已提交
26
import java.text.SimpleDateFormat;
27 28 29
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
30
import java.util.Locale;
K
kohsuke 已提交
31 32 33
import java.util.Map;
import java.util.ResourceBundle;
import java.util.SimpleTimeZone;
K
kohsuke 已提交
34
import java.util.StringTokenizer;
K
kohsuke 已提交
35
import java.util.logging.Level;
K
kohsuke 已提交
36
import java.util.logging.Logger;
K
kohsuke 已提交
37 38 39 40 41 42 43
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Kohsuke Kawaguchi
 */
public class Util {
K
kohsuke 已提交
44

K
kohsuke 已提交
45 46 47 48 49 50 51 52 53 54 55 56
    /**
     * Creates a filtered sublist.
     */
    public static <T> List<T> filter( List<?> base, Class<T> type ) {
        List<T> r = new ArrayList<T>();
        for (Object i : base) {
            if(type.isInstance(i))
                r.add(type.cast(i));
        }
        return r;
    }

57 58 59 60 61
    /**
     * Pattern for capturing variables. Either $xyz or ${xyz}, while ignoring "$$"
      */
    private static final Pattern VARIABLE = Pattern.compile("(?<!\\$)\\$([A-Za-z0-9_]+|\\{[A-Za-z0-9_]+\\})");

K
kohsuke 已提交
62
    /**
K
kohsuke 已提交
63
     * Replaces the occurrence of '$key' by <tt>properties.get('key')</tt>.
K
kohsuke 已提交
64 65
     *
     * <p>
66 67
     * Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.) 
     *
K
kohsuke 已提交
68 69 70
     */
    public static String replaceMacro(String s, Map<String,String> properties) {
        int idx=0;
71 72 73 74 75 76 77
        while(true) {
            Matcher m = VARIABLE.matcher(s);
            if(!m.find(idx))   return s;

            String key = m.group().substring(1);
            if(key.charAt(0)=='{')  key = key.substring(1,key.length()-1);

K
kohsuke 已提交
78
            String value = properties.get(key);
79 80 81 82 83
            if(value==null)
                idx = m.start()+1; // skip this
            else {
                s = s.substring(0,m.start())+value+s.substring(m.end());
                idx = m.start();
K
kohsuke 已提交
84 85 86 87
            }
        }
    }

K
kohsuke 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    /**
     * Loads the contents of a file into a string.
     */
    public static String loadFile(File logfile) throws IOException {
        if(!logfile.exists())
            return "";

        StringBuffer str = new StringBuffer((int)logfile.length());

        BufferedReader r = new BufferedReader(new FileReader(logfile));
        char[] buf = new char[1024];
        int len;
        while((len=r.read(buf,0,buf.length))>0)
           str.append(buf,0,len);
        r.close();

        return str.toString();
    }

    /**
     * Deletes the contents of the given directory (but not the directory itself)
     * recursively.
     *
     * @throws IOException
     *      if the operation fails.
     */
    public static void deleteContentsRecursive(File file) throws IOException {
        File[] files = file.listFiles();
        if(files==null)
            return;     // the directory didn't exist in the first place
118 119
        for (File child : files)
            deleteRecursive(child);
K
kohsuke 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    }

    private static void deleteFile(File f) throws IOException {
        if (!f.delete()) {
            if(!f.exists())
                // we are trying to delete a file that no longer exists, so this is not an error
                return;

            // perhaps this file is read-only?
            // try chmod. this becomes no-op if this is not Unix.
            try {
                Chmod chmod = new Chmod();
                chmod.setProject(new org.apache.tools.ant.Project());
                chmod.setFile(f);
                chmod.setPerm("u+w");
                chmod.execute();
            } catch (BuildException e) {
                LOGGER.log(Level.INFO,"Failed to chmod "+f,e);
            }

140 141
            if(!f.delete() && f.exists())
                throw new IOException("Unable to delete " + f.getPath());
K
kohsuke 已提交
142 143 144 145
        }
    }

    public static void deleteRecursive(File dir) throws IOException {
146 147
        if(!isSymlink(dir))
            deleteContentsRecursive(dir);
K
kohsuke 已提交
148 149 150
        deleteFile(dir);
    }

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    /*
     * Copyright 2001-2004 The Apache Software Foundation.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    /**
     * Checks if the given file represents a symlink.
     */
    //Taken from http://svn.apache.org/viewvc/maven/shared/trunk/file-management/src/main/java/org/apache/maven/shared/model/fileset/util/FileSetManager.java?view=markup
170
    public static boolean isSymlink(File file) throws IOException {
171 172 173 174 175 176 177 178 179
        File parent = file.getParentFile();
        File canonicalFile = file.getCanonicalFile();

        return parent != null
            && (!canonicalFile.getName().equals(file.getName()) || !canonicalFile.getPath().startsWith(
            parent.getCanonicalPath()));
    }


K
kohsuke 已提交
180 181 182 183 184 185 186 187 188 189 190 191
    /**
     * Creates a new temporary directory.
     */
    public static File createTempDir() throws IOException {
        File tmp = File.createTempFile("hudson", "tmp");
        if(!tmp.delete())
            throw new IOException("Failed to delete "+tmp);
        if(!tmp.mkdirs())
            throw new IOException("Failed to create a new directory "+tmp);
        return tmp;
    }

192
    private static final Pattern errorCodeParser = Pattern.compile(".*CreateProcess.*error=([0-9]+).*");
K
kohsuke 已提交
193 194 195 196 197

    /**
     * On Windows, error messages for IOException aren't very helpful.
     * This method generates additional user-friendly error message to the listener
     */
K
kohsuke 已提交
198 199 200 201 202 203
    public static void displayIOException( IOException e, TaskListener listener ) {
        String msg = getWin32ErrorMessage(e);
        if(msg!=null)
            listener.getLogger().println(msg);
    }

204 205 206 207
    public static String getWin32ErrorMessage(IOException e) {
        return getWin32ErrorMessage((Throwable)e);
    }

K
kohsuke 已提交
208
    /**
209
     * Extracts the Win32 error message from {@link Throwable} if possible.
K
kohsuke 已提交
210 211 212 213
     *
     * @return
     *      null if there seems to be no error code or if the platform is not Win32.
     */
214
    public static String getWin32ErrorMessage(Throwable e) {
K
kohsuke 已提交
215
        String msg = e.getMessage();
216 217 218 219 220 221 222 223 224 225
        if(msg!=null) {
            Matcher m = errorCodeParser.matcher(msg);
            if(m.matches()) {
                try {
                    ResourceBundle rb = ResourceBundle.getBundle("/hudson/win32errors");
                    return rb.getString("error"+m.group(1));
                } catch (Exception _) {
                    // silently recover from resource related failures
                }
            }
226
        }
K
kohsuke 已提交
227

228 229 230
        if(e.getCause()!=null)
            return getWin32ErrorMessage(e.getCause());
        return null; // no message
K
kohsuke 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    }

    /**
     * Guesses the current host name.
     */
    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            return "localhost";
        }
    }

    public static void copyStream(InputStream in,OutputStream out) throws IOException {
        byte[] buf = new byte[8192];
        int len;
K
kohsuke 已提交
247 248 249 250 251 252 253
        while((len=in.read(buf))>0)
            out.write(buf,0,len);
    }

    public static void copyStream(Reader in, Writer out) throws IOException {
        char[] buf = new char[8192];
        int len;
K
kohsuke 已提交
254 255 256 257
        while((len=in.read(buf))>0)
            out.write(buf,0,len);
    }

K
kohsuke 已提交
258 259 260 261 262
    /**
     * @since 1.145
     */
    public static String[] tokenize(String s,String delimiter) {
        StringTokenizer st = new StringTokenizer(s,delimiter);
K
kohsuke 已提交
263 264 265 266 267 268
        String[] a = new String[st.countTokens()];
        for (int i = 0; st.hasMoreTokens(); i++)
            a[i] = st.nextToken();
        return a;
    }

K
kohsuke 已提交
269 270 271 272
    public static String[] tokenize(String s) {
        return tokenize(s," \t\n\r\f");
    }

J
jglick 已提交
273
    public static String[] mapToEnv(Map<String,String> m) {
K
kohsuke 已提交
274 275 276
        String[] r = new String[m.size()];
        int idx=0;

J
jglick 已提交
277 278
        for (final Map.Entry<String,String> e : m.entrySet()) {
            r[idx++] = e.getKey() + '=' + e.getValue();
K
kohsuke 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        }
        return r;
    }

    public static int min(int x, int... values) {
        for (int i : values) {
            if(i<x)
                x=i;
        }
        return x;
    }

    public static String nullify(String v) {
        if(v!=null && v.length()==0)    v=null;
        return v;
    }

K
kohsuke 已提交
296 297 298 299 300
    public static String removeTrailingSlash(String s) {
        if(s.endsWith("/")) return s.substring(0,s.length()-1);
        else                return s;
    }

K
kohsuke 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    /**
     * Write-only buffer.
     */
    private static final byte[] garbage = new byte[8192];

    /**
     * Computes MD5 digest of the given input stream.
     *
     * @param source
     *      The stream will be closed by this method at the end of this method.
     */
    public static String getDigestOf(InputStream source) throws IOException {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");

            DigestInputStream in =new DigestInputStream(source,md5);
            try {
                while(in.read(garbage)>0)
                    ; // simply discard the input
            } finally {
                in.close();
            }
            return toHexString(md5.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new IOException2("MD5 not installed",e);    // impossible
        }
    }
328 329 330 331 332 333 334 335
    
    public static String getDigestOf(String text) {
        try {
            return getDigestOf(new ByteArrayInputStream(text.getBytes()));
        } catch (IOException e) {
            throw new Error(e);
        }
    }
K
kohsuke 已提交
336

K
kohsuke 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    public static String toHexString(byte[] data, int start, int len) {
        StringBuffer buf = new StringBuffer();
        for( int i=0; i<len; i++ ) {
            int b = data[start+i]&0xFF;
            if(b<16)    buf.append('0');
            buf.append(Integer.toHexString(b));
        }
        return buf.toString();
    }

    public static String toHexString(byte[] bytes) {
        return toHexString(bytes,0,bytes.length);
    }

    public static String getTimeSpanString(long duration) {
352 353 354
        duration /= 1000;
        if(duration<60)
            return combine(duration,"second");
K
kohsuke 已提交
355
        duration /= 60;
356 357
        if(duration<60)
            return combine(duration,"minute");
K
kohsuke 已提交
358
        duration /= 60;
359 360
        if(duration<24)
            return combine(duration,"hour");
K
kohsuke 已提交
361
        duration /= 24;
362 363
        if(duration<30)
            return combine(duration,"day");
K
kohsuke 已提交
364
        duration /= 30;
365 366
        if(duration<12)
            return combine(duration,"month");
K
kohsuke 已提交
367
        duration /= 12;
368
        return combine(duration,"year");
K
kohsuke 已提交
369 370 371
    }

    /**
K
kohsuke 已提交
372
     * Combines number and unit, with a plural suffix if needed.
K
kohsuke 已提交
373 374 375 376 377 378 379 380
     */
    public static String combine(long n, String suffix) {
        String s = Long.toString(n)+' '+suffix;
        if(n!=1)
            s += 's';
        return s;
    }

K
kohsuke 已提交
381 382 383 384 385 386 387 388 389 390 391 392
    /**
     * Create a sub-list by only picking up instances of the specified type.
     */
    public static <T> List<T> createSubList( Collection<?> source, Class<T> type ) {
        List<T> r = new ArrayList<T>();
        for (Object item : source) {
            if(type.isInstance(item))
                r.add(type.cast(item));
        }
        return r;
    }

K
kohsuke 已提交
393
    /**
K
kohsuke 已提交
394
     * Escapes non-ASCII characters in URL.
K
kohsuke 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
     */
    public static String encode(String s) {
        try {
            boolean escaped = false;

            StringBuffer out = new StringBuffer(s.length());

            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            OutputStreamWriter w = new OutputStreamWriter(buf,"UTF-8");

            for (int i = 0; i < s.length(); i++) {
                int c = (int) s.charAt(i);
                if (c<128 && c!=' ') {
                    out.append((char) c);
                } else {
                    // 1 char -> UTF8
                    w.write(c);
                    w.flush();
                    for (byte b : buf.toByteArray()) {
                        out.append('%');
                        out.append(toDigit((b >> 4) & 0xF));
                        out.append(toDigit(b & 0xF));
                    }
                    buf.reset();
                    escaped = true;
                }
            }

            return escaped ? out.toString() : s;
        } catch (IOException e) {
            throw new Error(e); // impossible
        }
    }

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    /**
     * Escapes HTML unsafe characters like &lt;, &amp;to the respective character entities.
     */
    public static String escape(String text) {
        StringBuffer buf = new StringBuffer(text.length()+64);
        for( int i=0; i<text.length(); i++ ) {
            char ch = text.charAt(i);
            if(ch=='\n')
                buf.append("<br>");
            else
            if(ch=='<')
                buf.append("&lt;");
            else
            if(ch=='&')
                buf.append("&amp;");
            else
            if(ch==' ')
                buf.append("&nbsp;");
            else
                buf.append(ch);
        }
        return buf.toString();
    }

K
kohsuke 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    public static String xmlEscape(String text) {
        StringBuffer buf = new StringBuffer(text.length()+64);
        for( int i=0; i<text.length(); i++ ) {
            char ch = text.charAt(i);
            if(ch=='<')
                buf.append("&lt;");
            else
            if(ch=='&')
                buf.append("&amp;");
            else
                buf.append(ch);
        }
        return buf.toString();
    }

K
kohsuke 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
    private static char toDigit(int n) {
        char ch = Character.forDigit(n,16);
        if(ch>='a')     ch = (char)(ch-'a'+'A');
        return ch;
    }

    /**
     * Creates an empty file.
     */
    public static void touch(File file) throws IOException {
        new FileOutputStream(file).close();
    }

    /**
     * Copies a single file by using Ant.
     */
    public static void copyFile(File src, File dst) throws BuildException {
        Copy cp = new Copy();
        cp.setProject(new org.apache.tools.ant.Project());
        cp.setTofile(dst);
        cp.setFile(src);
        cp.setOverwrite(true);
        cp.execute();
    }

    /**
     * Convert null to "".
     */
    public static String fixNull(String s) {
        if(s==null)     return "";
        else            return s;
    }

    /**
     * Convert empty string to null.
     */
    public static String fixEmpty(String s) {
        if(s==null || s.length()==0)    return null;
        return s;
    }

    /**
     * Cuts all the leading path portion and get just the file name.
     */
    public static String getFileName(String filePath) {
        int idx = filePath.lastIndexOf('\\');
        if(idx>=0)
            return getFileName(filePath.substring(idx+1));
        idx = filePath.lastIndexOf('/');
        if(idx>=0)
            return getFileName(filePath.substring(idx+1));
        return filePath;
    }

K
kohsuke 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    /**
     * Concatenate multiple strings by inserting a separator.
     */
    public static String join(Collection<String> strings, String seprator) {
        StringBuilder buf = new StringBuilder();
        boolean first=true;
        for (String s : strings) {
            if(first)   first=false;
            else        buf.append(seprator);
            buf.append(s);
        }
        return buf.toString();
    }


K
kohsuke 已提交
537 538
    public static final SimpleDateFormat XS_DATETIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

539 540 541 542
    // Note: RFC822 dates must not be localized!
    public static final SimpleDateFormat RFC822_DATETIME_FORMATTER
            = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);

K
kohsuke 已提交
543 544 545 546 547 548 549 550
    static {
        XS_DATETIME_FORMATTER.setTimeZone(new SimpleTimeZone(0,"GMT"));
    }



    private static final Logger LOGGER = Logger.getLogger(Util.class.getName());
}