📄 policyfile.java
字号:
} if (tok != StreamTokenizer.TT_WORD) { error(url, in, "expecting word token"); } // keystore "<keystore-path>" [',' "<keystore-type>"] ';' if (in.sval.equalsIgnoreCase("keystore")) { String alg = KeyStore.getDefaultType(); tok = in.nextToken(); if (tok != '"' && tok != '\'') error(url, in, "expecting key store URL"); String store = in.sval; tok = in.nextToken(); if (tok == ',') { tok = in.nextToken(); if (tok != '"' && tok != '\'') error(url, in, "expecting key store type"); alg = in.sval; tok = in.nextToken(); } if (tok != ';') error(url, in, "expecting semicolon"); try { KeyStore keystore = KeyStore.getInstance(alg); keystore.load(new URL(url, store).openStream(), null); keystores.add(keystore); } catch (Exception x) { error(url, in, x.toString()); } } else if (in.sval.equalsIgnoreCase("grant")) { if (state != STATE_BEGIN) error(url, in, "extraneous grant keyword"); state = STATE_GRANT; } else if (in.sval.equalsIgnoreCase("signedBy")) { if (state != STATE_GRANT && state != STATE_PERMS) error(url, in, "spurious 'signedBy'"); if (keystores.isEmpty()) error(url, in, "'signedBy' with no keystores"); tok = in.nextToken(); if (tok != '"' && tok != '\'') error(url, in, "expecting signedBy name"); StringTokenizer st = new StringTokenizer(in.sval, ","); while (st.hasMoreTokens()) { String alias = st.nextToken(); for (Iterator it = keystores.iterator(); it.hasNext(); ) { KeyStore keystore = (KeyStore) it.next(); try { if (keystore.isCertificateEntry(alias)) currentCerts.add(keystore.getCertificate(alias)); } catch (KeyStoreException kse) { error(url, in, kse.toString()); } } } tok = in.nextToken(); if (tok != ',') { if (state != STATE_GRANT) error(url, in, "spurious ','"); in.pushBack(); } } else if (in.sval.equalsIgnoreCase("codeBase")) { if (state != STATE_GRANT) error(url, in, "spurious 'codeBase'"); tok = in.nextToken(); if (tok != '"' && tok != '\'') error(url, in, "expecting code base URL"); String base = expand(in.sval); if (File.separatorChar != '/') base = base.replace(File.separatorChar, '/'); try { currentBase = new URL(base); } catch (MalformedURLException mue) { error(url, in, mue.toString()); } tok = in.nextToken(); if (tok != ',') in.pushBack(); } else if (in.sval.equalsIgnoreCase("principal")) { if (state != STATE_GRANT) error(url, in, "spurious 'principal'"); tok = in.nextToken(); if (tok == StreamTokenizer.TT_WORD) { tok = in.nextToken(); if (tok != '"' && tok != '\'') error(url, in, "expecting principal name"); String name = in.sval; Principal p = null; try { Class pclass = Class.forName(in.sval); Constructor c = pclass.getConstructor(new Class[] { String.class }); p = (Principal) c.newInstance(new Object[] { name }); } catch (Exception x) { error(url, in, x.toString()); } for (Iterator it = keystores.iterator(); it.hasNext(); ) { KeyStore ks = (KeyStore) it.next(); try { for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) { String alias = (String) e.nextElement(); if (ks.isCertificateEntry(alias)) { Certificate cert = ks.getCertificate(alias); if (!(cert instanceof X509Certificate)) continue; if (p.equals(((X509Certificate) cert).getSubjectDN()) || p.equals(((X509Certificate) cert).getSubjectX500Principal())) currentCerts.add(cert); } } } catch (KeyStoreException kse) { error(url, in, kse.toString()); } } } else if (tok == '"' || tok == '\'') { String alias = in.sval; for (Iterator it = keystores.iterator(); it.hasNext(); ) { KeyStore ks = (KeyStore) it.next(); try { if (ks.isCertificateEntry(alias)) currentCerts.add(ks.getCertificate(alias)); } catch (KeyStoreException kse) { error(url, in, kse.toString()); } } } else error(url, in, "expecting principal"); tok = in.nextToken(); if (tok != ',') in.pushBack(); } else if (in.sval.equalsIgnoreCase("permission")) { if (state != STATE_PERMS) error(url, in, "spurious 'permission'"); tok = in.nextToken(); if (tok != StreamTokenizer.TT_WORD) error(url, in, "expecting permission class name"); String className = in.sval; Class clazz = null; try { clazz = Class.forName(className); } catch (ClassNotFoundException cnfe) { } tok = in.nextToken(); if (tok == ';') { if (clazz == null) { currentPerms.add(new UnresolvedPermission(className, null, null, (Certificate[]) currentCerts.toArray(new Certificate[currentCerts.size()]))); continue; } try { currentPerms.add((Permission) clazz.newInstance()); } catch (Exception x) { error(url, in, x.toString()); } continue; } if (tok != '"' && tok != '\'') error(url, in, "expecting permission target"); String target = expand(in.sval); tok = in.nextToken(); if (tok == ';') { if (clazz == null) { currentPerms.add(new UnresolvedPermission(className, target, null, (Certificate[]) currentCerts.toArray(new Certificate[currentCerts.size()]))); continue; } try { Constructor c = clazz.getConstructor(new Class[] { String.class }); currentPerms.add((Permission) c.newInstance( new Object[] { target })); } catch (Exception x) { error(url, in, x.toString()); } continue; } if (tok != ',') error(url, in, "expecting ','"); tok = in.nextToken(); if (tok == StreamTokenizer.TT_WORD) { if (!in.sval.equalsIgnoreCase("signedBy")) error(url, in, "expecting 'signedBy'"); try { Constructor c = clazz.getConstructor(new Class[] { String.class }); currentPerms.add((Permission) c.newInstance( new Object[] { target })); } catch (Exception x) { error(url, in, x.toString()); } in.pushBack(); continue; } if (tok != '"' && tok != '\'') error(url, in, "expecting permission action"); String action = in.sval; if (clazz == null) { currentPerms.add(new UnresolvedPermission(className, target, action, (Certificate[]) currentCerts.toArray(new Certificate[currentCerts.size()]))); continue; } else { try { Constructor c = clazz.getConstructor( new Class[] { String.class, String.class }); currentPerms.add((Permission) c.newInstance( new Object[] { target, action })); } catch (Exception x) { error(url, in, x.toString()); } } tok = in.nextToken(); if (tok != ';' && tok != ',') error(url, in, "expecting ';' or ','"); } } } /** * Expand all instances of <code>"${property-name}"</code> into * <code>System.getProperty("property-name")</code>. */ private static String expand(final String s) { final StringBuffer result = new StringBuffer(); final StringBuffer prop = new StringBuffer(); int state = 0; for (int i = 0; i < s.length(); i++) { switch (state) { case 0: if (s.charAt(i) == '$') state = 1; else result.append(s.charAt(i)); break; case 1: if (s.charAt(i) == '{') state = 2; else { state = 0; result.append('$').append(s.charAt(i)); } break; case 2: if (s.charAt(i) == '}') { String p = prop.toString(); if (p.equals("/")) p = "file.separator"; p = System.getProperty(p); if (p == null) p = ""; result.append(p); prop.setLength(0); state = 0; } else prop.append(s.charAt(i)); break; } } if (state != 0) result.append('$').append('{').append(prop); return result.toString(); } /** * I miss macros. */ private static void error(URL base, StreamTokenizer in, String msg) throws IOException { throw new IOException(base+":"+in.lineno()+": "+msg); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -