~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/jdrmaa/src/org/ggf/drmaa/SessionFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
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
 
16
 *
 
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.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
package org.ggf.drmaa;
 
33
 
 
34
import java.io.BufferedReader;
 
35
import java.io.File;
 
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;
 
43
 
 
44
/**
 
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.
 
49
 *
 
50
 * <p>Example:</p>
 
51
 *
 
52
 * <pre>public static void main(String[] args) throws Exception {
 
53
 *   SessionFactory factory = SessionFactory.getFactory();
 
54
 *   Session session = factory.getSession();
 
55
 *
 
56
 *   session.init(&quot;&quot;);
 
57
 *   session.exit();
 
58
 * }
 
59
 * </pre>
 
60
 * @author dan.templeton@sun.com
 
61
 * @see Session
 
62
 * @since 0.5
 
63
 * @version 1.0
 
64
 */
 
65
public abstract class SessionFactory {
 
66
    /**
 
67
     * Right now, only one SessionFactory can exist at a time.  This is that
 
68
     * session factory.
 
69
     */
 
70
    private static SessionFactory thisFactory = null;
 
71
    /**
 
72
     * The name of the property used to find the Session implementation
 
73
     * class name.
 
74
     */
 
75
    private static final String SESSION_PROPERTY =
 
76
            "org.ggf.drmaa.SessionFactory";
 
77
    
 
78
    /**
 
79
     * Gets a Session instance appropriate for the DRM in use.
 
80
     * @return a Session instance appropriate for the DRM in use
 
81
     */
 
82
    public abstract Session getSession();
 
83
    
 
84
    /**
 
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
 
96
     */
 
97
    public static SessionFactory getFactory() {
 
98
        synchronized (SessionFactory.class) {
 
99
            if (thisFactory == null) {
 
100
                NewFactoryAction action = new NewFactoryAction();
 
101
                
 
102
                thisFactory =
 
103
                        (SessionFactory)AccessController.doPrivileged(action);
 
104
            }
 
105
        }
 
106
        
 
107
        return thisFactory;
 
108
    }
 
109
    
 
110
    /**
 
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
 
122
     */
 
123
    private static SessionFactory newFactory() throws ConfigurationError {
 
124
        ClassLoader classLoader = findClassLoader();
 
125
        Exception e = null;
 
126
        
 
127
        // Use the system property first
 
128
        try {
 
129
            String systemProp = System.getProperty(SESSION_PROPERTY);
 
130
            
 
131
            if (systemProp != null) {
 
132
                return (SessionFactory)newInstance(systemProp, classLoader);
 
133
            }
 
134
        } catch (SecurityException se) {
 
135
            // If we get a security exception, treat it as failure and try the
 
136
            // next method
 
137
            e = se;
 
138
        }
 
139
        
 
140
        // try to read from $java.home/lib/drmaa.properties
 
141
        try {
 
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);
 
146
            
 
147
            if (f.exists()) {
 
148
                Properties props = new Properties();
 
149
                
 
150
                props.load(new FileInputStream(f));
 
151
                
 
152
                String className = props.getProperty(SESSION_PROPERTY);
 
153
                
 
154
                return (SessionFactory)newInstance(className, classLoader);
 
155
            }
 
156
        } catch (SecurityException se ) {
 
157
            // If we get a security exception, treat it as failure and try the
 
158
            // next method
 
159
            e = se;
 
160
        } catch (IOException ie) {
 
161
            // If we get an I/O exception, treat it as failure and try the next
 
162
            // method
 
163
            e = ie;
 
164
        }
 
165
        
 
166
        String serviceId = "META-INF/services/" + SESSION_PROPERTY;
 
167
        // try to find services in CLASSPATH
 
168
        try {
 
169
            InputStream is = null;
 
170
            
 
171
            if (classLoader == null) {
 
172
                is = ClassLoader.getSystemResourceAsStream(serviceId);
 
173
            } else {
 
174
                is = classLoader.getResourceAsStream(serviceId);
 
175
            }
 
176
            
 
177
            if (is != null) {
 
178
                BufferedReader rd =
 
179
                        new BufferedReader(new InputStreamReader(is, "UTF-8"));
 
180
                
 
181
                String className = rd.readLine();
 
182
                
 
183
                rd.close();
 
184
                
 
185
                if (className != null && ! className.equals("")) {
 
186
                    return (SessionFactory)newInstance(className, classLoader);
 
187
                }
 
188
            }
 
189
        } catch (Exception ex) {
 
190
            //Ignore exceptions here and let the config error be thrown
 
191
            e = ex;
 
192
        }
 
193
        
 
194
        throw new ConfigurationError("Provider for " + SESSION_PROPERTY +
 
195
                " cannot be found", e);
 
196
    }
 
197
    
 
198
    /**
 
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
 
205
     * loaded
 
206
     * @return an appropriate ClassLoader
 
207
     */
 
208
    private static ClassLoader findClassLoader() {
 
209
        ClassLoader classLoader = null;
 
210
        
 
211
        try {
 
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);
 
220
        }
 
221
        
 
222
        return classLoader;
 
223
    }
 
224
    
 
225
    /**
 
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
 
231
     */
 
232
    private static Object newInstance(String className, ClassLoader classLoader)
 
233
            throws ConfigurationError {
 
234
        try {
 
235
            Class spiClass;
 
236
            
 
237
            if (classLoader == null) {
 
238
                spiClass = Class.forName(className);
 
239
            } else {
 
240
                spiClass = classLoader.loadClass(className);
 
241
            }
 
242
            
 
243
            return spiClass.newInstance();
 
244
        } catch (ClassNotFoundException ex) {
 
245
            throw new ConfigurationError("Provider " + className +
 
246
                    " not found", ex);
 
247
        } catch (Exception ex) {
 
248
            throw new ConfigurationError("Provider " + className +
 
249
                    " could not be instantiated: " + ex,
 
250
                    ex);
 
251
        }
 
252
    }
 
253
    
 
254
    /**
 
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.
 
258
     */
 
259
    private static class ConfigurationError extends Error {
 
260
        /**
 
261
         * The Exception which caused this Exception
 
262
         */
 
263
        private Exception exception;
 
264
        
 
265
        /**
 
266
         * Construct a new instance with the specified detail string and
 
267
         * exception.
 
268
         * @param msg the error message
 
269
         * @param x the original Exception which caused this Exception
 
270
         */
 
271
        ConfigurationError(String msg, Exception ex) {
 
272
            super(msg);
 
273
            this.exception = ex;
 
274
        }
 
275
        
 
276
        /**
 
277
         * Get the Exception which caused this Exception
 
278
         * @return the Exception which caused this Exception
 
279
         */
 
280
        Exception getException() {
 
281
            return exception;
 
282
        }
 
283
    }
 
284
    
 
285
    /**
 
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.
 
289
     */
 
290
    private static class NewFactoryAction implements PrivilegedAction {
 
291
        /**
 
292
         * Create a new factory.
 
293
         * @return a new factory
 
294
         */
 
295
        public Object run() {
 
296
            return newFactory();
 
297
        }
 
298
    }
 
299
}