81 * @since 1.2 |
81 * @since 1.2 |
82 */ |
82 */ |
83 |
83 |
84 public class PolicyParser { |
84 public class PolicyParser { |
85 |
85 |
86 private static final String EXTDIRS_PROPERTY = "java.ext.dirs"; |
|
87 private static final String OLD_EXTDIRS_EXPANSION = |
|
88 "${" + EXTDIRS_PROPERTY + "}"; |
|
89 |
|
90 // package-private: used by PolicyFile for static policy |
|
91 static final String EXTDIRS_EXPANSION = "${{" + EXTDIRS_PROPERTY + "}}"; |
|
92 |
|
93 |
|
94 private Vector<GrantEntry> grantEntries; |
86 private Vector<GrantEntry> grantEntries; |
95 private Map<String, DomainEntry> domainEntries; |
87 private Map<String, DomainEntry> domainEntries; |
96 |
88 |
97 // Convenience variables for parsing |
89 // Convenience variables for parsing |
98 private static final Debug debug = Debug.getInstance("parser", |
90 private static final Debug debug = Debug.getInstance("parser", |
574 match("}"); |
566 match("}"); |
575 |
567 |
576 try { |
568 try { |
577 if (e.signedBy != null) e.signedBy = expand(e.signedBy); |
569 if (e.signedBy != null) e.signedBy = expand(e.signedBy); |
578 if (e.codeBase != null) { |
570 if (e.codeBase != null) { |
579 |
571 e.codeBase = expand(e.codeBase, true).replace |
580 // For backward compatibility with 1.4 |
572 (File.separatorChar, '/'); |
581 if (e.codeBase.equals(OLD_EXTDIRS_EXPANSION)) { |
|
582 e.codeBase = EXTDIRS_EXPANSION; |
|
583 } |
|
584 int es; |
|
585 if ((es=e.codeBase.indexOf(EXTDIRS_EXPANSION)) < 0) { |
|
586 e.codeBase = expand(e.codeBase, true).replace |
|
587 (File.separatorChar, '/'); |
|
588 } else { |
|
589 // expand the system property "java.ext.dirs", |
|
590 // parse it into its path components, |
|
591 // and then create a grant entry for each component |
|
592 String[] extDirs = parseExtDirs(e.codeBase, es); |
|
593 if (extDirs != null && extDirs.length > 0) { |
|
594 for (int i = 0; i < extDirs.length; i++) { |
|
595 GrantEntry newGe = (GrantEntry)e.clone(); |
|
596 newGe.codeBase = extDirs[i]; |
|
597 add(newGe); |
|
598 |
|
599 if (debug != null) { |
|
600 debug.println("creating policy entry for " + |
|
601 "expanded java.ext.dirs path:\n\t\t" + |
|
602 extDirs[i]); |
|
603 } |
|
604 } |
|
605 } |
|
606 ignoreEntry = true; |
|
607 } |
|
608 } |
573 } |
609 } catch (PropertyExpander.ExpandException peee) { |
574 } catch (PropertyExpander.ExpandException peee) { |
610 if (debug != null) { |
575 if (debug != null) { |
611 debug.println(peee.toString()); |
576 debug.println(peee.toString()); |
612 } |
577 } |
709 } |
674 } |
710 properties.put(key.toLowerCase(Locale.ENGLISH), value); |
675 properties.put(key.toLowerCase(Locale.ENGLISH), value); |
711 } |
676 } |
712 |
677 |
713 return properties; |
678 return properties; |
714 } |
|
715 |
|
716 // package-private: used by PolicyFile for static policy |
|
717 static String[] parseExtDirs(String codebase, int start) { |
|
718 |
|
719 String s = System.getProperty(EXTDIRS_PROPERTY); |
|
720 String globalPrefix = (start > 0 ? codebase.substring(0, start) : "file:"); |
|
721 int end = start + EXTDIRS_EXPANSION.length(); |
|
722 String globalSuffix = (end < codebase.length() ? codebase.substring(end) : |
|
723 (String) null); |
|
724 |
|
725 String[] dirs = null; |
|
726 String localSuffix; |
|
727 if (s != null) { |
|
728 StringTokenizer st = |
|
729 new StringTokenizer(s, File.pathSeparator); |
|
730 int count = st.countTokens(); |
|
731 dirs = new String[count]; |
|
732 for (int i = 0; i < count; i++) { |
|
733 File file = new File(st.nextToken()); |
|
734 dirs[i] = sun.net.www.ParseUtil.encodePath |
|
735 (file.getAbsolutePath()); |
|
736 |
|
737 if (!dirs[i].startsWith("/")) { |
|
738 dirs[i] = "/" + dirs[i]; |
|
739 } |
|
740 |
|
741 localSuffix = (globalSuffix == null ? |
|
742 (dirs[i].endsWith("/") ? "*" : "/*") : |
|
743 globalSuffix); |
|
744 |
|
745 dirs[i] = globalPrefix + dirs[i] + localSuffix; |
|
746 } |
|
747 } |
|
748 return dirs; |
|
749 } |
679 } |
750 |
680 |
751 private boolean peekAndMatch(String expect) |
681 private boolean peekAndMatch(String expect) |
752 throws ParsingException, IOException |
682 throws ParsingException, IOException |
753 { |
683 { |