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 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import java.security.AccessControlContext;
  29 import java.security.Permission;
  30 import java.security.Permissions;
  31 import java.security.ProtectionDomain;
  32 import java.util.stream.Stream;
  33 
  34 /**
  35  * Utility class for creating permission-restricting {@link AccessControlContext}s.
  36  */
  37 public final class AccessControlContextFactory {
  38     private AccessControlContextFactory () {
  39     }
  40 
  41     /**
  42      * Creates an access control context with no permissions.
  43      * @return an access control context with no permissions.
  44      */
  45     public static AccessControlContext createAccessControlContext() {
  46         return createAccessControlContext(new Permission[0]);
  47     }
  48 
  49     /**
  50      * Creates an access control context limited to only the specified permissions.
  51      * @param permissions the permissions for the newly created access control context.
  52      * @return a new access control context limited to only the specified permissions.
  53      */
  54     public static AccessControlContext createAccessControlContext(final Permission... permissions) {
  55         final Permissions perms = new Permissions();
  56         for(final Permission permission: permissions) {
  57             perms.add(permission);
  58         }
  59         return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
  60     }
  61 
  62     /**
  63      * Creates an access control context limited to only the {@link RuntimePermission}s
  64      * of the given names.
  65      * @param runtimePermissionNames the names of runtime permissions for the
  66      * newly created access control context.
  67      * @return a new access control context limited to only the runtime
  68      * permissions with the specified names.
  69      */
  70     public static AccessControlContext createAccessControlContext(final String... runtimePermissionNames) {
  71         return createAccessControlContext(makeRuntimePermissions(runtimePermissionNames));
  72     }
  73 
  74     private static Permission[] makeRuntimePermissions(final String... runtimePermissionNames) {
  75         return Stream.of(runtimePermissionNames).map(RuntimePermission::new).toArray(Permission[]::new);
  76     }
  77 }