1 /*
2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
599 throw (SecurityException) ex;
600 } else {
601 throw new IOException("Exception: " + ex);
602 }
603 }
604
605 // Install the normal default ErrorManager.
606 setErrorManager(new ErrorManager());
607 }
608
609 /**
610 * Generate a file based on a user-supplied pattern, generation number,
611 * and an integer uniqueness suffix
612 * @param pattern the pattern for naming the output file
613 * @param generation the generation number to distinguish rotated logs
614 * @param unique a unique number to resolve conflicts
615 * @return the generated File
616 * @throws IOException
617 */
618 private File generate(String pattern, int generation, int unique)
619 throws IOException {
620 File file = null;
621 String word = "";
622 int ix = 0;
623 boolean sawg = false;
624 boolean sawu = false;
625 while (ix < pattern.length()) {
626 char ch = pattern.charAt(ix);
627 ix++;
628 char ch2 = 0;
629 if (ix < pattern.length()) {
630 ch2 = Character.toLowerCase(pattern.charAt(ix));
631 }
632 if (ch == '/') {
633 if (file == null) {
634 file = new File(word);
635 } else {
636 file = new File(file, word);
637 }
638 word = "";
639 continue;
640 } else if (ch == '%') {
641 if (ch2 == 't') {
642 String tmpDir = System.getProperty("java.io.tmpdir");
643 if (tmpDir == null) {
644 tmpDir = System.getProperty("user.home");
645 }
646 file = new File(tmpDir);
647 ix++;
648 word = "";
649 continue;
650 } else if (ch2 == 'h') {
651 file = new File(System.getProperty("user.home"));
652 if (jdk.internal.misc.VM.isSetUID()) {
653 // Ok, we are in a set UID program. For safety's sake
654 // we disallow attempts to open files relative to %h.
655 throw new IOException("can't use %h in set UID program");
656 }
657 ix++;
658 word = "";
659 continue;
660 } else if (ch2 == 'g') {
661 word = word + generation;
662 sawg = true;
663 ix++;
664 continue;
665 } else if (ch2 == 'u') {
666 word = word + unique;
667 sawu = true;
668 ix++;
669 continue;
670 } else if (ch2 == '%') {
671 word = word + "%";
672 ix++;
673 continue;
674 }
675 }
676 word = word + ch;
677 }
678 if (count > 1 && !sawg) {
679 word = word + "." + generation;
680 }
681 if (unique > 0 && !sawu) {
682 word = word + "." + unique;
683 }
684 if (word.length() > 0) {
685 if (file == null) {
686 file = new File(word);
687 } else {
688 file = new File(file, word);
689 }
690 }
691 return file;
692 }
693
694 /**
695 * Rotate the set of output files
696 */
697 private synchronized void rotate() {
698 Level oldLevel = getLevel();
699 setLevel(Level.OFF);
700
701 super.close();
702 for (int i = count-2; i >= 0; i--) {
703 File f1 = files[i];
704 File f2 = files[i+1];
705 if (f1.exists()) {
706 if (f2.exists()) {
707 f2.delete();
708 }
709 f1.renameTo(f2);
710 }
711 }
|
1 /*
2 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
599 throw (SecurityException) ex;
600 } else {
601 throw new IOException("Exception: " + ex);
602 }
603 }
604
605 // Install the normal default ErrorManager.
606 setErrorManager(new ErrorManager());
607 }
608
609 /**
610 * Generate a file based on a user-supplied pattern, generation number,
611 * and an integer uniqueness suffix
612 * @param pattern the pattern for naming the output file
613 * @param generation the generation number to distinguish rotated logs
614 * @param unique a unique number to resolve conflicts
615 * @return the generated File
616 * @throws IOException
617 */
618 private File generate(String pattern, int generation, int unique)
619 throws IOException
620 {
621 return generate(pattern, count, generation, unique);
622 }
623
624 // The static method here is provided for whitebox testing of the algorithm.
625 static File generate(String pat, int count, int generation, int unique)
626 throws IOException
627 {
628 Path path = Paths.get(pat);
629 Path result = null;
630 boolean sawg = false;
631 boolean sawu = false;
632 StringBuilder word = new StringBuilder();
633 Path prev = null;
634 for (Path elem : path) {
635 if (prev != null) {
636 prev = prev.resolveSibling(word.toString());
637 if (result == null) result = prev;
638 else result = result.resolve(prev);
639 }
640 String pattern = elem.toString();
641 int ix = 0;
642 word.setLength(0);
643 while (ix < pattern.length()) {
644 char ch = pattern.charAt(ix);
645 ix++;
646 char ch2 = 0;
647 if (ix < pattern.length()) {
648 ch2 = Character.toLowerCase(pattern.charAt(ix));
649 }
650 if (ch == '%') {
651 if (ch2 == 't') {
652 String tmpDir = System.getProperty("java.io.tmpdir");
653 if (tmpDir == null) {
654 tmpDir = System.getProperty("user.home");
655 }
656 result = Paths.get(tmpDir);
657 ix++;
658 word.setLength(0);
659 continue;
660 } else if (ch2 == 'h') {
661 result = Paths.get(System.getProperty("user.home"));
662 if (jdk.internal.misc.VM.isSetUID()) {
663 // Ok, we are in a set UID program. For safety's sake
664 // we disallow attempts to open files relative to %h.
665 throw new IOException("can't use %h in set UID program");
666 }
667 ix++;
668 word.setLength(0);
669 continue;
670 } else if (ch2 == 'g') {
671 word = word.append(generation);
672 sawg = true;
673 ix++;
674 continue;
675 } else if (ch2 == 'u') {
676 word = word.append(unique);
677 sawu = true;
678 ix++;
679 continue;
680 } else if (ch2 == '%') {
681 word = word.append('%');
682 ix++;
683 continue;
684 }
685 }
686 word = word.append(ch);
687 }
688 prev = elem;
689 }
690
691 if (count > 1 && !sawg) {
692 word = word.append('.').append(generation);
693 }
694 if (unique > 0 && !sawu) {
695 word = word.append('.').append(unique);
696 }
697 if (word.length() > 0) {
698 String n = word.toString();
699 Path p = prev == null ? Paths.get(n) : prev.resolveSibling(n);
700 result = result == null ? p : result.resolve(p);
701 } else if (result == null) {
702 result = Paths.get("");
703 }
704
705 if (path.getRoot() == null) {
706 return result.toFile();
707 } else {
708 return path.getRoot().resolve(result).toFile();
709 }
710 }
711
712 /**
713 * Rotate the set of output files
714 */
715 private synchronized void rotate() {
716 Level oldLevel = getLevel();
717 setLevel(Level.OFF);
718
719 super.close();
720 for (int i = count-2; i >= 0; i--) {
721 File f1 = files[i];
722 File f2 = files[i+1];
723 if (f1.exists()) {
724 if (f2.exists()) {
725 f2.delete();
726 }
727 f1.renameTo(f2);
728 }
729 }
|