001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.perseus.platform;
028:
029: import java.io.InputStream;
030: import java.io.ByteArrayInputStream;
031: import java.io.IOException;
032:
033: import com.sun.midp.security.SecurityToken;
034: import com.sun.midp.security.SecurityInitializer;
035: import com.sun.midp.security.SecurityInitializerImpl;
036: import com.sun.midp.security.Permissions;
037: import com.sun.midp.security.ImplicitlyTrustedClass;
038:
039: import com.sun.midp.io.j2me.storage.RandomAccessStream;
040: import com.sun.midp.io.j2me.storage.File;
041: import javax.microedition.io.Connector;
042: import com.sun.midp.configurator.Constants;
043:
044: /**
045: * This class provides a way to securely access platform resources.
046: * On some versions of the Java platform, there is a need to specify
047: * a security token to access resources. This allows different
048: * versions of the security features.
049: *
050: * @version $Id: ResourceHandler.java,v 1.5 2006/07/17 00:35:44 st125089 Exp $
051: */
052: public final class ResourceHandler {
053:
054: /**
055: * Location of the default font face resource file
056: */
057: private static final String DEFAULT_FONT_FACE_FILE = "/com/sun/perseus/render/resources/defaultFont.svg";
058:
059: /**
060: * Location of the intial font face resource file
061: */
062: private static final String INITIAL_FONT_FACE_FILE = "/com/sun/perseus/render/resources/initialFont.svg";
063:
064: /**
065: * Inner class to request security token from SecurityInitializer.
066: * SecurityInitializer should be able to check this inner class name.
067: */
068: static private class SecurityTrusted implements
069: ImplicitlyTrustedClass {
070: };
071:
072: private static SecurityToken securityToken;
073:
074: static {
075: try {
076: securityToken = com.sun.midp.security.SecurityInitializer
077: .requestToken(new SecurityTrusted());
078: } catch (SecurityException se) {
079: // Just log the error. This may happen in development context
080: // when running a midlet which bundles the SVG engine.
081: se.printStackTrace();
082: } catch (NoClassDefFoundError e) {
083: // Just log the error. This may happen in development context
084: // when running a midlet which bundles the SVG engine.
085: e.printStackTrace();
086: }
087: }
088:
089: /**
090: * Don't allow instances to be created.
091: */
092: private ResourceHandler() {
093: }
094:
095: final public static InputStream getInitialFontResource() {
096: InputStream is = null;
097:
098: try {
099: is = getSystemResource(INITIAL_FONT_FACE_FILE,
100: securityToken);
101: } catch (SecurityException se) {
102: //log security exceptions
103: se.printStackTrace();
104: }
105:
106: return is;
107: }
108:
109: final public static InputStream getDefaultFontResource() {
110: InputStream is = null;
111:
112: try {
113: is = getSystemResource(DEFAULT_FONT_FACE_FILE,
114: securityToken);
115: } catch (SecurityException se) {
116: //log security exceptions
117: se.printStackTrace();
118: }
119:
120: return is;
121: }
122:
123: /**
124: * @param resourceName the name of the resource to be retrieved. This has the
125: * same semantic and syntax as the java.lang.Class#getrResourceAsStream's name
126: * parameter.
127: * @param securityToken opaque object which the caller must provide to grant
128: * access to the system resource. Note that on some platform, this securityToken
129: * is ignored because the access to the platform resources are secured through other
130: * mechanisms.
131: * @return null if the resource is not found. Otherwise, an stream to the
132: * requested resource.
133: */
134: private static InputStream getSystemResource(
135: final String resourceName, final Object securityToken) {
136: if (securityToken == null) {
137: return ResourceHandler.class
138: .getResourceAsStream(resourceName);
139: } else {
140: InputStream is = null;
141: RandomAccessStream storage = new RandomAccessStream(
142: (SecurityToken) securityToken);
143:
144: try {
145: // extract the file name part of the full resource name
146: int namePartIdx = resourceName.lastIndexOf('/');
147: String namePart = (namePartIdx != -1) ? resourceName
148: .substring(namePartIdx + 1) : resourceName;
149: storage.connect(File
150: .getConfigRoot(Constants.INTERNAL_STORAGE_ID)
151: + namePart, Connector.READ);
152: byte[] data = new byte[storage.getSizeOf()];
153: storage.readBytes(data, 0, data.length);
154: is = new ByteArrayInputStream(data);
155: } catch (IOException e) {
156: System.out.println("Error in getSystemResource");
157: } finally {
158: try {
159: storage.disconnect();
160: } catch (IOException ignored) {
161: }
162: }
163: return is;
164: }
165: }
166: }
|