001: /**********************************************************************
002: Copyright (c) 2004 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: 2004 Marcus Mennemeier - contributed with the class loading fix
017: ...
018: **********************************************************************/package org.jpox;
019:
020: import java.io.IOException;
021: import java.net.URL;
022: import java.util.Enumeration;
023:
024: /**
025: * Class to allow resolution and loading of classes in a persistence framework.
026: * Implementations are to define the rules for resolving the classes. For example
027: * JDO (used outside a J2EE container) would likely differ from EJB3 (used within
028: * a J2EE container).
029: *
030: * The search class path order is:
031: * <ul>
032: * <li>the primary classloader argument</li>
033: * <li>the primary classloader (set using setPrimary and kept in ThreadLocal variable).</li>
034: * <li>the current thread context classloader.</li>
035: * <li>the pm classloader.</li>
036: * <li>the registered classloader.</li>
037: * <li>the user registered classloader.</li>
038: * </ul>
039: * @version $Revision: 1.10 $
040: */
041: public interface ClassLoaderResolver {
042: /**
043: * Class loading method, allowing specification of a primary loader. This method does not initialize the class
044: * @param name Name of the Class to be loaded
045: * @param primary the primary ClassLoader to use (or null)
046: * @return The Class given the name, using the specified ClassLoader
047: * @throws ClassNotResolvedException if the class can't be found in the classpath
048: */
049: public Class classForName(String name, ClassLoader primary);
050:
051: /**
052: * Class loading method, allowing specification of a primary loader
053: * and whether the class should be initialised or not.
054: * @param name Name of the Class to be loaded
055: * @param primary the primary ClassLoader to use (or null)
056: * @param initialize whether to initialize the class or not.
057: * @return The Class given the name, using the specified ClassLoader
058: * @throws ClassNotResolvedException if the class can't be found in the classpath
059: */
060: public Class classForName(String name, ClassLoader primary,
061: boolean initialize);
062:
063: /**
064: * Class loading method. This method does not initialize the class
065: * @param name Name of the Class to be loaded
066: * @return The Class given the name, using the specified ClassLoader
067: */
068: public Class classForName(String name);
069:
070: /**
071: * Class loading method, allowing for initialisation of the class.
072: * @param name Name of the Class to be loaded
073: * @param initialize whether to initialize the class or not.
074: * @return The Class given the name, using the specified ClassLoader
075: */
076: public Class classForName(String name, boolean initialize);
077:
078: /**
079: * Method to test whether the type represented by the specified class_2
080: * parameter can be converted to the type represented by class_name_1 parameter.
081: * @param class_name_1 Class name
082: * @param class_2 Class to compare against
083: * @return Whether they are assignable
084: */
085: public boolean isAssignableFrom(String class_name_1, Class class_2);
086:
087: /**
088: * Method to test whether the type represented by the specified class_name_2
089: * parameter can be converted to the type represented by class_1 parameter.
090: * @param class_1 First class
091: * @param class_name_2 Class name to compare against
092: * @return Whether they are assignable
093: */
094: public boolean isAssignableFrom(Class class_1, String class_name_2);
095:
096: /**
097: * Method to test whether the type represented by the specified class_name_2
098: * parameter can be converted to the type represented by class_name_1 parameter.
099: * @param class_name_1 Class name
100: * @param class_name_2 Class name to compare against
101: * @return Whether they are assignable
102: */
103: public boolean isAssignableFrom(String class_name_1,
104: String class_name_2);
105:
106: /**
107: * ClassLoader registered to load classes created at runtime. One ClassLoader can
108: * be registered, and if one ClassLoader is already registered, the registered ClassLoader
109: * is replaced by <code>loader</code>.
110: * @param loader The ClassLoader in which classes are defined
111: */
112: public void registerClassLoader(ClassLoader loader);
113:
114: /**
115: * ClassLoader registered by users to load classes. One ClassLoader can
116: * be registered, and if one ClassLoader is already registered, the registered ClassLoader
117: * is replaced by <code>loader</code>.
118: * @param loader The ClassLoader in which classes are loaded
119: */
120: public void registerUserClassLoader(ClassLoader loader);
121:
122: /**
123: * Finds all the resources with the given name.
124: * @param resourceName the resource name. If <code>resourceName</code> starts with "/", remove it before searching.
125: * @param primary the primary ClassLoader to use (or null)
126: * @return An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty.
127: * Resources that the class loader doesn't have access to will not be in the enumeration.
128: * @throws IOException If I/O errors occur
129: * @see ClassLoader#getResources(java.lang.String)
130: */
131: public Enumeration getResources(String resourceName,
132: ClassLoader primary) throws IOException;
133:
134: /**
135: * Finds the resource with the given name.
136: * @param resourceName the path to resource name relative to the classloader root path. If <code>resourceName</code> starts with "/", remove it.
137: * @param primary the primary ClassLoader to use (or null)
138: * @return A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
139: * @throws IOException If I/O errors occur
140: * @see ClassLoader#getResource(java.lang.String)
141: */
142: public URL getResource(String resourceName, ClassLoader primary);
143:
144: /**
145: * Sets the primary classloader for the current thread.
146: * The primary should be kept in a ThreadLocal variable.
147: * @param primary the primary classloader
148: */
149: void setPrimary(ClassLoader primary);
150:
151: /**
152: * Unsets the primary classloader for the current thread
153: */
154: void unsetPrimary();
155:
156: }
|