|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.client.common; |
|
27 |
|
28 import java.io.*; |
|
29 import java.lang.reflect.InvocationTargetException; |
|
30 import java.net.*; |
|
31 import java.security.*; |
|
32 import java.util.*; |
|
33 import java.util.regex.*; |
|
34 |
|
35 public class PanelClassLoader extends URLClassLoader { |
|
36 // |
|
37 // Static data |
|
38 // |
|
39 |
|
40 private static Set<Permission> permSet = new HashSet<Permission>(); |
|
41 private static Class[] ARGS_NAME = { String.class }; |
|
42 private static Class[] ARGS_NAME_ACTIONS = { String.class, String.class }; |
|
43 |
|
44 // |
|
45 // Constructors |
|
46 // |
|
47 |
|
48 public PanelClassLoader(URL[] urls) { |
|
49 super(urls); |
|
50 } |
|
51 |
|
52 // |
|
53 // SecureClassLoader methods |
|
54 // |
|
55 |
|
56 @Override |
|
57 protected PermissionCollection getPermissions(CodeSource codeSource) { |
|
58 PermissionCollection perms = super.getPermissions(codeSource); |
|
59 for (Permission perm : permSet) { |
|
60 perms.add(perm); |
|
61 } |
|
62 return perms; |
|
63 } |
|
64 |
|
65 // |
|
66 // Static methods |
|
67 // |
|
68 |
|
69 public static void loadPermissions(URL url) throws IOException, |
|
70 PermissionParseException { |
|
71 |
|
72 BufferedReader in = new BufferedReader(new InputStreamReader( |
|
73 url.openStream())); |
|
74 String line; |
|
75 Pattern ignore = Pattern.compile("^\\s*(//.*)?$"); |
|
76 Pattern valid = Pattern.compile("^\\s*permission\\s+(\\S+)" + |
|
77 "(\\s+\"([^\"]*)\"(,\\s+\"([^\"]*)\")?)?;$"); |
|
78 |
|
79 Set<Permission> perms = new HashSet<Permission>(); |
|
80 |
|
81 while ((line = in.readLine()) != null) { |
|
82 if (ignore.matcher(line).matches()) { |
|
83 continue; |
|
84 } |
|
85 |
|
86 Matcher matcher = valid.matcher(line); |
|
87 if (!matcher.matches()) { |
|
88 throw new PermissionParseException("invalid syntax: " + line); |
|
89 } |
|
90 |
|
91 int nGroups = matcher.groupCount(); |
|
92 String type = matcher.group(1); |
|
93 String name = expand(nGroups >= 3 ? matcher.group(3) : null); |
|
94 String actions = expand(nGroups >= 5 ? matcher.group(5) : null); |
|
95 |
|
96 try { |
|
97 Permission perm = getPermission(type, name, actions); |
|
98 perms.add(perm); |
|
99 } catch (Throwable e) { |
|
100 String message = String.format( |
|
101 "could not instantiate permission: " + |
|
102 "type=%s name=%s actions=", type, name, actions); |
|
103 throw new PermissionParseException(message, e); |
|
104 } |
|
105 } |
|
106 |
|
107 in.close(); |
|
108 |
|
109 permSet.addAll(perms); |
|
110 } |
|
111 |
|
112 // |
|
113 // Private static methods |
|
114 // |
|
115 |
|
116 private static String expand(String str) { |
|
117 if (str == null) { |
|
118 return null; |
|
119 } |
|
120 |
|
121 StringBuilder result = new StringBuilder(); |
|
122 Pattern re = Pattern.compile("^(.*?)\\$\\{([^}]*)\\}(.*)"); |
|
123 while (true) { |
|
124 Matcher matcher = re.matcher(str); |
|
125 if (matcher.matches()) { |
|
126 result.append(matcher.group(1)); |
|
127 String property = matcher.group(2); |
|
128 if (property.equals("/")) { |
|
129 property = "file.separator"; |
|
130 } |
|
131 String value = System.getProperty(property); |
|
132 if (value != null) { |
|
133 result.append(value); |
|
134 } |
|
135 str = matcher.group(3); |
|
136 } else { |
|
137 result.append(str); |
|
138 break; |
|
139 } |
|
140 } |
|
141 return result.toString(); |
|
142 } |
|
143 |
|
144 @SuppressWarnings({"fallthrough"}) |
|
145 private static Permission getPermission(String type, String name, |
|
146 String actions) throws ClassNotFoundException, InstantiationException, |
|
147 IllegalAccessException, NoSuchMethodException, InvocationTargetException |
|
148 { |
|
149 Class<?> clazz = Class.forName(type); |
|
150 int nArgs = actions != null ? 2 : name != null ? 1 : 0; |
|
151 |
|
152 switch (nArgs) { |
|
153 case 0: |
|
154 try { |
|
155 return (Permission)clazz.getConstructor().newInstance(); |
|
156 } catch (NoSuchMethodException e) { |
|
157 } |
|
158 |
|
159 case 1: |
|
160 try { |
|
161 return (Permission)clazz.getConstructor(ARGS_NAME). |
|
162 newInstance(name); |
|
163 } catch (NoSuchMethodException e) { |
|
164 } |
|
165 |
|
166 case 2: |
|
167 return (Permission)clazz.getConstructor(ARGS_NAME_ACTIONS). |
|
168 newInstance(name, actions); |
|
169 } |
|
170 |
|
171 assert false; |
|
172 return null; |
|
173 } |
|
174 } |