1 /*
   2  * Copyright (c) 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
  23  * questions.
  24  */
  25 
  26 package build.tools.addtorestrictedpkgs;
  27 
  28 import java.io.*;
  29 
  30 /**
  31  * Adds additional packages to the package.access and package.definition
  32  * security properties.
  33  */
  34 public class AddToRestrictedPkgs {
  35 
  36     private static final String PKG_ACC = "package.access";
  37     private static final String PKG_DEF = "package.definition";
  38     private static final int PKG_ACC_INDENT = 15;
  39     private static final int PKG_DEF_INDENT = 19;
  40 
  41     public static void main(String[] args) throws Exception {
  42 
  43         if (args.length < 3) {
  44             System.err.println("Usage: java AddToRestrictedPkgs " +
  45                                "[input java.security file name] " +
  46                                "[output java.security file name] " +
  47                                "[packages ...]");
  48             System.exit(1);
  49         }
  50 
  51         try (FileReader fr = new FileReader(args[0]);
  52              BufferedReader br = new BufferedReader(fr);
  53              FileWriter fw = new FileWriter(args[1]);
  54              BufferedWriter bw = new BufferedWriter(fw))
  55         {
  56             // parse the file line-by-line, looking for pkg access properties
  57             String line = br.readLine();
  58             while (line != null) {
  59                 if (line.startsWith(PKG_ACC)) {
  60                     writePackages(br, bw, line, PKG_ACC_INDENT, args);
  61                 } else if (line.startsWith(PKG_DEF)) {
  62                     writePackages(br, bw, line, PKG_DEF_INDENT, args);
  63                 } else {
  64                     writeLine(bw, line);
  65                 }
  66                 line = br.readLine();
  67             }
  68             bw.flush();
  69         }
  70     }
  71 
  72     private static void writePackages(BufferedReader br, BufferedWriter bw,
  73                                       String line, int numSpaces,
  74                                       String[] args) throws IOException {
  75         // parse property until EOL, not including line breaks
  76         while (line.endsWith("\\")) {
  77             writeLine(bw, line);
  78             line = br.readLine();
  79         }
  80         // append comma and line-break to last package
  81         writeLine(bw, line + ",\\");
  82         // add new packages, one per line
  83         for (int i = 2; i < args.length - 1; i++) {
  84             indent(bw, numSpaces);
  85             writeLine(bw, args[i] + ",\\");
  86         }
  87         indent(bw, numSpaces);
  88         writeLine(bw, args[args.length - 1]);
  89     }
  90 
  91     private static void writeLine(BufferedWriter bw, String line)
  92         throws IOException
  93     {
  94         bw.write(line);
  95         bw.newLine();
  96     }
  97 
  98     private static void indent(BufferedWriter bw, int numSpaces)
  99         throws IOException
 100     {
 101         for (int i = 0; i < numSpaces; i++) {
 102             bw.append(' ');
 103         }
 104     }
 105 }