1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
10
* Sun Industry Standards Source License Version 1.2
11
* =================================================
12
* The contents of this file are subject to the Sun Industry Standards
13
* Source License Version 1.2 (the "License"); You may not use this file
14
* except in compliance with the License. You may obtain a copy of the
15
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
17
* Software provided under this License is provided on an "AS IS" basis,
18
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
19
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
20
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
21
* See the License for the specific provisions governing your rights and
22
* obligations concerning the Software.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
32
package org.ggf.drmaa;
34
import java.io.BufferedReader;
36
import java.io.FileInputStream;
37
import java.io.IOException;
38
import java.io.InputStream;
39
import java.io.InputStreamReader;
40
import java.security.AccessController;
41
import java.security.PrivilegedAction;
42
import java.util.Properties;
45
* This class is used to retrieve a Session instance tailored to the DRM and
46
* DRMAA implementation in use. The factory will use the
47
* org.ggf.drmaa.SessionFactory property to discover the DRM-specific Session
48
* implementation class.
52
* <pre>public static void main(String[] args) throws Exception {
53
* SessionFactory factory = SessionFactory.getFactory();
54
* Session session = factory.getSession();
56
* session.init("");
60
* @author dan.templeton@sun.com
65
public abstract class SessionFactory {
67
* Right now, only one SessionFactory can exist at a time. This is that
70
private static SessionFactory thisFactory = null;
72
* The name of the property used to find the Session implementation
75
private static final String SESSION_PROPERTY =
76
"org.ggf.drmaa.SessionFactory";
79
* Gets a Session instance appropriate for the DRM in use.
80
* @return a Session instance appropriate for the DRM in use
82
public abstract Session getSession();
85
* Gets a SessionFactory instance appropriate for the DRM in use. This
86
* method uses the org.ggf.drmaa.SessionFactory property to find
87
* the appropriate class. It looks first in the system properties. If the
88
* property is not present, the method looks in
89
* $java.home/lib/drmaa.properties. If the property still isn't found, the
90
* method will search the classpath for a
91
* META-INF/services/org.ggf.drmaa.SessionFactory resource. If the
92
* property still has not been found, the method throws an Error.
93
* @return a SessionFactory instance appropriate for the DRM in use
94
* @throws Error if an appropriate SessionFactory implementation could not
95
* be found or instantiated
97
public static SessionFactory getFactory() {
98
synchronized (SessionFactory.class) {
99
if (thisFactory == null) {
100
NewFactoryAction action = new NewFactoryAction();
103
(SessionFactory)AccessController.doPrivileged(action);
111
* Creates a SessionFactory object appropriate for the DRM in use. This
112
* method uses the org.ggf.drmaa.SessionFactory property to find
113
* the appropriate class. It looks first in the system properties. If the
114
* property is not present, the method looks in
115
* $java.home/lib/drmaa.properties. If the property still isn't found, the
116
* method will search the classpath for a
117
* META-INF/services/org.ggf.drmaa.SessionFactory resource. If the
118
* property still has not been found, the method throws an Error.
119
* @return a DRMAASession object appropriate for the DRM in use
120
* @throws ConfigurationError if an appropriate SessionFactory
121
* implementation could not be found or instantiated
123
private static SessionFactory newFactory() throws ConfigurationError {
124
ClassLoader classLoader = findClassLoader();
127
// Use the system property first
129
String systemProp = System.getProperty(SESSION_PROPERTY);
131
if (systemProp != null) {
132
return (SessionFactory)newInstance(systemProp, classLoader);
134
} catch (SecurityException se) {
135
// If we get a security exception, treat it as failure and try the
140
// try to read from $java.home/lib/drmaa.properties
142
String javah = System.getProperty("java.home");
143
String configFile = javah + File.separator + "lib" +
144
File.separator + "drmaa.properties";
145
File f = new File(configFile);
148
Properties props = new Properties();
150
props.load(new FileInputStream(f));
152
String className = props.getProperty(SESSION_PROPERTY);
154
return (SessionFactory)newInstance(className, classLoader);
156
} catch (SecurityException se ) {
157
// If we get a security exception, treat it as failure and try the
160
} catch (IOException ie) {
161
// If we get an I/O exception, treat it as failure and try the next
166
String serviceId = "META-INF/services/" + SESSION_PROPERTY;
167
// try to find services in CLASSPATH
169
InputStream is = null;
171
if (classLoader == null) {
172
is = ClassLoader.getSystemResourceAsStream(serviceId);
174
is = classLoader.getResourceAsStream(serviceId);
179
new BufferedReader(new InputStreamReader(is, "UTF-8"));
181
String className = rd.readLine();
185
if (className != null && ! className.equals("")) {
186
return (SessionFactory)newInstance(className, classLoader);
189
} catch (Exception ex) {
190
//Ignore exceptions here and let the config error be thrown
194
throw new ConfigurationError("Provider for " + SESSION_PROPERTY +
195
" cannot be found", e);
199
* Figure out which ClassLoader to use. For JDK 1.2 and later use the
200
* context ClassLoader if possible. Note: we defer linking the class
201
* that calls an API only in JDK 1.2 until runtime so that we can catch
202
* LinkageError so that this code will run in older non-Sun JVMs such
203
* as the Microsoft JVM in IE.
204
* @throws ConfigurationError thrown if the classloader cannot be found or
206
* @return an appropriate ClassLoader
208
private static ClassLoader findClassLoader() {
209
ClassLoader classLoader = null;
212
// Construct the name of the concrete class to instantiate
213
classLoader = Thread.currentThread().getContextClassLoader();
214
} catch (LinkageError le) {
215
// Assume that we are running JDK 1.1, use the current ClassLoader
216
classLoader = SessionFactory.class.getClassLoader();
217
} catch (Exception ex) {
218
// Something abnormal happened so throw an error
219
throw new ConfigurationError(ex.toString(), ex);
226
* Create an instance of a class using the specified ClassLoader.
227
* @param className The name of the class to be used to create the object
228
* @param classLoader the classloader to use to create the object
229
* @throws ConfigurationError thrown is the class cannot be instantiated
230
* @return an instance of the given class
232
private static Object newInstance(String className, ClassLoader classLoader)
233
throws ConfigurationError {
237
if (classLoader == null) {
238
spiClass = Class.forName(className);
240
spiClass = classLoader.loadClass(className);
243
return spiClass.newInstance();
244
} catch (ClassNotFoundException ex) {
245
throw new ConfigurationError("Provider " + className +
247
} catch (Exception ex) {
248
throw new ConfigurationError("Provider " + className +
249
" could not be instantiated: " + ex,
255
* Error used to indicate trouble loading the needed classes. Note that
256
* this class is private, meaning that it is only catchable as Error outside
257
* of the SessionFactory class.
259
private static class ConfigurationError extends Error {
261
* The Exception which caused this Exception
263
private Exception exception;
266
* Construct a new instance with the specified detail string and
268
* @param msg the error message
269
* @param x the original Exception which caused this Exception
271
ConfigurationError(String msg, Exception ex) {
277
* Get the Exception which caused this Exception
278
* @return the Exception which caused this Exception
280
Exception getException() {
286
* Privileged action used to load a factory implementation. This class
287
* allows the DRMAA library to be granted the required security permissions
288
* without having to grant those permission to the user's application.
290
private static class NewFactoryAction implements PrivilegedAction {
292
* Create a new factory.
293
* @return a new factory
295
public Object run() {