001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.server.rpc;
017:
018: import com.google.gwt.user.server.rpc.impl.StandardSerializationPolicy;
019:
020: import java.io.BufferedReader;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.text.ParseException;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: /**
029: * API for loading a {@link SerializationPolicy}.
030: */
031: public final class SerializationPolicyLoader {
032: /**
033: * Default encoding for serialization policy files.
034: */
035: public static final String SERIALIZATION_POLICY_FILE_ENCODING = "UTF-8";
036:
037: private static final String FORMAT_ERROR_MESSAGE = "Expected: className, [true | false]";
038:
039: /**
040: * Returns the serialization policy file name from the from the serialization
041: * policy strong name.
042: *
043: * @param serializationPolicyStrongName the serialization policy strong name
044: * @return the serialization policy file name from the from the serialization
045: * policy strong name
046: */
047: public static String getSerializationPolicyFileName(
048: String serializationPolicyStrongName) {
049: return serializationPolicyStrongName + ".gwt.rpc";
050: }
051:
052: /**
053: * Loads a SerializationPolicy from an input stream.
054: *
055: * @param inputStream stream to load from
056: * @return a {@link SerializationPolicy} loaded from the input stream
057: *
058: * @throws IOException if an error occurs while reading the stream
059: * @throws ParseException if the input stream is not properly formatted
060: * @throws ClassNotFoundException if a class specified in the serialization
061: * policy cannot be loaded
062: */
063: public static SerializationPolicy loadFromStream(
064: InputStream inputStream) throws IOException,
065: ParseException, ClassNotFoundException {
066: if (inputStream == null) {
067: throw new NullPointerException("inputStream");
068: }
069:
070: Map<Class<?>, Boolean> whitelist = new HashMap<Class<?>, Boolean>();
071:
072: InputStreamReader isr = new InputStreamReader(inputStream,
073: SERIALIZATION_POLICY_FILE_ENCODING);
074: BufferedReader br = new BufferedReader(isr);
075:
076: String line = br.readLine();
077: int lineNum = 1;
078: while (line != null) {
079: line = line.trim();
080: if (line.length() > 0) {
081: String[] components = line.split(",");
082:
083: if (components.length != 2) {
084: throw new ParseException(FORMAT_ERROR_MESSAGE,
085: lineNum);
086: }
087:
088: String binaryTypeName = components[0].trim();
089: String instantiable = components[1].trim();
090:
091: if (binaryTypeName.length() == 0
092: || instantiable.length() == 0) {
093: throw new ParseException(FORMAT_ERROR_MESSAGE,
094: lineNum);
095: }
096:
097: ClassLoader contextClassLoader = Thread.currentThread()
098: .getContextClassLoader();
099: Class<?> clazz = Class.forName(binaryTypeName, false,
100: contextClassLoader);
101: // TODO: Validate the instantiable string better.
102: whitelist.put(clazz, Boolean.valueOf(instantiable));
103: }
104:
105: line = br.readLine();
106: lineNum++;
107: }
108:
109: return new StandardSerializationPolicy(whitelist);
110: }
111:
112: private SerializationPolicyLoader() {
113: }
114: }
|