test/micro/src/classes/org/openjdk/micro/util/SecurityManagerHelper.java
branchJEP-230-microbenchmarks-branch
changeset 56913 013359fdfeb2
parent 56909 7cf3051d8572
child 56914 796f76953273
equal deleted inserted replaced
56909:7cf3051d8572 56913:013359fdfeb2
     1 /*
       
     2  * Copyright (c) 2015, 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 package org.openjdk.micro.util;
       
    26 
       
    27 import java.io.File;
       
    28 import java.io.IOException;
       
    29 import java.io.PrintStream;
       
    30 import java.net.URI;
       
    31 import java.security.NoSuchAlgorithmException;
       
    32 import java.security.Permission;
       
    33 import java.security.Policy;
       
    34 import java.security.URIParameter;
       
    35 
       
    36 /**
       
    37  * Help class to create and load Security Policy file from a set of Permissions
       
    38  */
       
    39 public class SecurityManagerHelper {
       
    40 
       
    41     /**
       
    42      * Create and load a security manager using the provided permissions.
       
    43      *
       
    44      * @param perms Permissions to add to the file
       
    45      *
       
    46      * @throws IOException If file could not be created or written to.
       
    47      * @throws NoSuchAlgorithmException if no Provider supports a PolicySpi
       
    48      * implementation for the specified type.
       
    49      */
       
    50     public static void setupSecurityManager(Permission... perms)
       
    51             throws IOException, NoSuchAlgorithmException {
       
    52 
       
    53         URI policyURI = createSecurityFile(perms);
       
    54         Policy.setPolicy(Policy.getInstance("JavaPolicy", new URIParameter(policyURI)));
       
    55         System.setSecurityManager(new SecurityManager());
       
    56     }
       
    57 
       
    58     private static URI createSecurityFile(Permission... perms) throws IOException {
       
    59 
       
    60         File policyFile = File.createTempFile("security", ".policy");
       
    61         policyFile.deleteOnExit();
       
    62 
       
    63         try (PrintStream writer = new PrintStream(policyFile)) {
       
    64             writer.println("grant {");
       
    65             for (Permission p : perms) {
       
    66                 appendPermission(writer, p);
       
    67             }
       
    68             // Permissions required by JMH
       
    69             appendPermission(writer, new RuntimePermission("modifyThread"));
       
    70             // Required when running without forking
       
    71 //            appendPermission(writer, new RuntimePermission("accessDeclaredMembers"));
       
    72 //            appendPermission(writer, new RuntimePermission("createSecurityManager"));
       
    73 //            appendPermission(writer, new ReflectPermission("suppressAccessChecks"));
       
    74 //            appendPermission(writer, new ManagementPermission("monitor"));
       
    75 //            appendPermission(writer, new PropertyPermission("jmh.scorePrecision", "read"));
       
    76             writer.println("};");
       
    77         }
       
    78 
       
    79         return policyFile.toURI();
       
    80     }
       
    81 
       
    82     private static void appendPermission(PrintStream writer, Permission p) {
       
    83         writer.printf("\tpermission %s \"%s\", \"%s\";\n",
       
    84                       p.getClass().getName(), p.getName(), p.getActions());
       
    85     }
       
    86 }