001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.misc.accessors;
019:
020: import org.apache.harmony.misc.internal.nls.Messages;
021:
022: /**
023: * Factory for all accessors. This class is used to get the instances of various
024: * accessor classes methods.
025: * Each factory method checks security permission before it returns
026: * an appropriate accessor singleton.
027: */
028: public class AccessorFactory {
029:
030: /*
031: * Used for security checking before returning an instance of accessors.
032: * <code>classLoader</code> is bootstrap class loader.
033: */
034: private static ClassLoader classLoader = AccessorFactory.class
035: .getClassLoader();
036:
037: private static void checkPermissions() throws SecurityException {
038: Class callerClass = ThreadStackAccessor.getInstance()
039: .getCallerClass(5);
040: ClassLoader callingClassLoader = callerClass.getClassLoader();
041: if (!(classLoader == null ? callingClassLoader == null
042: : classLoader.equals(callingClassLoader))) {
043: // misc.5=User code is not allowed to get accessors, caller class: {0}
044: throw new SecurityException(Messages.getString(
045: "misc.5", callerClass)); //$NON-NLS-1$
046: }
047: }
048:
049: private AccessorFactory() {
050: }
051:
052: /**
053: * Returns the {@link MemoryAccessor}.
054: *
055: * @throws SecurityException if a requester class is loaded by different
056: * class loader from the one used for loading the accessors (i.e. boot class loader).
057: */
058: public static MemoryAccessor getMemoryAccessor() {
059: checkPermissions();
060: return MemoryAccessor.getInstance();
061: }
062:
063: /**
064: * Returns the {@link ObjectAccessor}.
065: *
066: * @throws SecurityException if a requester class is loaded by different
067: * class loader from the one used for loading the accessors (i.e. boot class loader).
068: */
069: public static ObjectAccessor getObjectAccessor() {
070: checkPermissions();
071: return ObjectAccessor.getInstance();
072: }
073:
074: /**
075: * Returns the {@link ArrayAccessor}.
076: *
077: * @throws SecurityException if a requester class is loaded by different
078: * class loader from the one used for loading the accessors (i.e. boot class loader).
079: */
080: public static ArrayAccessor getArrayAccessor() {
081: checkPermissions();
082: return ArrayAccessor.getInstance();
083: }
084:
085: /**
086: * Returns the {@link StringAccessor}.
087: *
088: * @throws SecurityException if a requester class is loaded by different
089: * class loader from the one used for loading the accessors (i.e. boot class loader).
090: */
091: public static StringAccessor getStringAccessor() {
092: checkPermissions();
093: return StringAccessor.getInstance();
094: }
095:
096: /**
097: * Returns the {@link ThreadStackAccessor}.
098: *
099: * @throws SecurityException if a requester class is loaded by different
100: * class loader from the one used for loading the accessors (i.e. boot class loader).
101: */
102: public static ThreadStackAccessor getThreadStackAccessor() {
103: checkPermissions();
104: return ThreadStackAccessor.getInstance();
105: }
106: }
|