001: /*
002: * @(#)SecurityUtils.java
003: *
004: * Copyright 2002 - 2004 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.utils;
007:
008: import javax.swing.plaf.FontUIResource;
009: import java.awt.*;
010: import java.io.IOException;
011: import java.security.AccessControlException;
012: import java.util.Hashtable;
013: import java.util.Locale;
014: import java.util.MissingResourceException;
015:
016: /**
017: * A class that keeps all the security stuff so that an application can safely run in applet or webstart environment.
018: * Please refer to JIDE_Developer_Guide_for_Webstart_Applet.pdf in doc folder for more information.
019: */
020: public class SecurityUtils {
021: public static FontUIResource createFontUIResource(String name,
022: int style, int size) {
023: Font font = createFont(name, style, size);
024: if (font != null) {
025: return new FontUIResource(font);
026: } else {
027: return null;
028: }
029: }
030:
031: static class FontStruct {
032: String font;
033: int style;
034: }
035:
036: public static final String BOLD = "Bold";
037: public static final String ITALIC = "Italic";
038: public static final String BOLD_ITALIC = "Bold Italic";
039:
040: private static String createFontStrings(String font, int style) {
041: String fontString;
042: switch (style) {
043: case Font.BOLD:
044: fontString = font + " " + BOLD;
045: break;
046: case Font.ITALIC:
047: fontString = font + " " + ITALIC;
048: break;
049: case Font.BOLD | Font.ITALIC:
050: fontString = font + " " + BOLD_ITALIC;
051: break;
052: case Font.PLAIN:
053: default:
054: fontString = font;
055: break;
056: }
057: return fontString.replace(' ', '_');
058: }
059:
060: private static FontStruct getFontStruct(String text) {
061: if (text == null || text.length() == 0) {
062: return null;
063: }
064: text = text.replace('_', ' ');
065: FontStruct fontStruct = new FontStruct();
066: if (text.endsWith(BOLD)) {
067: fontStruct.style = Font.BOLD;
068: fontStruct.font = text.substring(0, text.length()
069: - BOLD.length());
070: } else if (text.endsWith(ITALIC)) {
071: fontStruct.style = Font.ITALIC;
072: fontStruct.font = text.substring(0, text.length()
073: - ITALIC.length());
074: } else if (text.endsWith(BOLD)) {
075: fontStruct.style = Font.BOLD | Font.ITALIC;
076: fontStruct.font = text.substring(0, text.length()
077: - BOLD_ITALIC.length());
078: } else {
079: fontStruct.style = Font.PLAIN;
080: fontStruct.font = text;
081: }
082: return fontStruct;
083:
084: }
085:
086: /**
087: * Creates font. If there is no permission to access font file, it will try to create the font
088: * directly from font file that is bundled as part of jar.
089: *
090: * @param name
091: * @param style
092: * @param size
093: * @return the font.
094: */
095: public static Font createFont(String name, int style, int size) {
096: try {
097: // System.out.println("new Font");
098: return new Font(name, style, size);
099: } catch (AccessControlException e) {
100: // System.out.println("new Font failed " + createFontStrings(name, style));
101: ClassLoader cl = SecurityUtils.class.getClassLoader();
102: try {
103: String value = null;
104: try {
105: value = FontFilesResource.getResourceBundle(
106: Locale.getDefault()).getString(
107: createFontStrings(name, style));
108: } catch (MissingResourceException me1) {
109: try {
110: value = FontFilesResource.getResourceBundle(
111: Locale.getDefault()).getString(name);
112: } catch (MissingResourceException me2) {
113: }
114: }
115: if (value == null) {
116: return null;
117: } else {
118: // System.out.print("createFont " + value);
119: Font font = Font.createFont(Font.TRUETYPE_FONT, cl
120: .getResourceAsStream(value));
121: // System.out.println("successful " + font);
122: if (font != null) {
123: return font.deriveFont(style, size);
124: }
125: }
126: } catch (FontFormatException e1) {
127: e1.printStackTrace();
128: throw e;
129: } catch (IOException e1) {
130: e1.printStackTrace();
131: throw e;
132: }
133: }
134: return null;
135: }
136:
137: private static Hashtable<String, String> _safeProperties = null;
138:
139: private static Hashtable getSafeProperties() {
140: if (_safeProperties == null) {
141: _safeProperties = new Hashtable<String, String>(13);
142: _safeProperties.put("java.version", "");
143: _safeProperties.put("java.vendor", "");
144: _safeProperties.put("java.vendor.url", "");
145: _safeProperties.put("java.class.version", "");
146: _safeProperties.put("os.name", "");
147: _safeProperties.put("os.version", "");
148: _safeProperties.put("os.arch", "");
149: _safeProperties.put("file.separator", "");
150: _safeProperties.put("path.separator", "");
151: _safeProperties.put("line.separator", "");
152: _safeProperties.put("java.specification.version", "");
153: _safeProperties.put("java.specification.vendor", "");
154: _safeProperties.put("java.specification.name", "");
155: _safeProperties.put("java.vm.specification.vendor", "");
156: _safeProperties.put("java.vm.specification.name", "");
157: _safeProperties.put("java.vm.version", "");
158: _safeProperties.put("java.vm.vendor", "");
159: _safeProperties.put("java.vm.name", "");
160: }
161: return _safeProperties;
162: }
163:
164: /**
165: * Gets the system property.
166: *
167: * @param key
168: * @param defaultValue
169: * @return the system property.
170: */
171: public static String getProperty(String key, String defaultValue) {
172: try {
173: return System.getProperty(key, defaultValue);
174: } catch (AccessControlException e) {
175: return defaultValue;
176: }
177: }
178:
179: private static boolean _AWTEventListenerDisabled = false;
180:
181: /**
182: * Checks if AWTEventListener is disabled. This flag can be set by user. If false, JIDE code will read the value and
183: * not use AWTEventListener. The reason we need this flag is because AWTEventListener needs a special security permission.
184: * If applet, it will throw security if the user policy doesn't have the correct permission.
185: *
186: * @return true if AWTEventListener is disabled.
187: */
188: public static boolean isAWTEventListenerDisabled() {
189: return _AWTEventListenerDisabled;
190: }
191:
192: /**
193: * Enables or disables the usage of AWTEventListener. If you wantto change it, you should change the value at
194: * the beginning of your main method.
195: *
196: * @param AWTEventListenerDisabled
197: */
198: public static void setAWTEventListenerDisabled(
199: boolean AWTEventListenerDisabled) {
200: _AWTEventListenerDisabled = AWTEventListenerDisabled;
201: }
202: }
|