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

« back to all changes in this revision

Viewing changes to source/libs/jgdi/src/com/sun/grid/jgdi/JGDIFactory.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 com.sun.grid.jgdi;
 
33
 
 
34
import com.sun.grid.jgdi.jni.EventClientImpl;
 
35
import com.sun.grid.jgdi.management.JGDIProxy;
 
36
import java.lang.reflect.InvocationHandler;
 
37
import java.lang.reflect.InvocationTargetException;
 
38
import java.lang.reflect.Method;
 
39
import java.lang.reflect.Proxy;
 
40
import java.net.MalformedURLException;
 
41
import java.util.logging.Logger;
 
42
import javax.management.remote.JMXServiceURL;
 
43
 
 
44
/**
 
45
 * Factory class for {@link JGDI} objects.
 
46
 *
 
47
 * @see com.sun.grid.jgdi.JGDI
 
48
 */
 
49
public class JGDIFactory {
 
50
 
 
51
    private final static Logger log = Logger.getLogger(JGDIFactory.class.getName());
 
52
    private static String versionString;
 
53
    private static boolean libNotLoaded = true;
 
54
 
 
55
    private static synchronized void initLib() throws JGDIException {
 
56
        if (libNotLoaded) {
 
57
            try {
 
58
                System.loadLibrary("jgdi");
 
59
                libNotLoaded = false;
 
60
            } catch (Throwable e) {
 
61
                JGDIException ex = new JGDIException("Can not load native jgdi lib: " + e.getMessage());
 
62
                // ex.initCause(e);  does not work for whatever reason
 
63
                throw ex;
 
64
            }
 
65
        }
 
66
    }
 
67
 
 
68
    /**
 
69
     * Get a new instance of a <code>JGDI</code> object.
 
70
     *
 
71
     * @param url  JGDI connection url in the form
 
72
     *             <code>bootstrap://&lt;SGE_ROOT&gt;@&lt;SGE_CELL&gt;:&lt;SGE_QMASTER_PORT&gt;</code>
 
73
     * @throws com.sun.grid.jgdi.JGDIException on any error on the GDI layer
 
74
     * @return the <code>JGDI<code> instance
 
75
     */
 
76
    public static JGDI newInstance(String url) throws JGDIException {
 
77
        log.entering(JGDIFactory.class.getName(), "newInstance", url);
 
78
        initLib();
 
79
        com.sun.grid.jgdi.jni.JGDIImpl ret = new com.sun.grid.jgdi.jni.JGDIImpl();
 
80
        ret.init(url);
 
81
        log.exiting(JGDIFactory.class.getName(), "newInstance", ret);
 
82
        return ret;
 
83
    }
 
84
 
 
85
    /**
 
86
     * Get a synchronized instance of a <code>JGDI</code> object.
 
87
     *
 
88
     * @param url  JGDI connection url in the form
 
89
     *             <code>bootstrap://&lt;SGE_ROOT&gt;@&lt;SGE_CELL&gt;:&lt;SGE_QMASTER_PORT&gt;</code>
 
90
     * @throws com.sun.grid.jgdi.JGDIException on any error on the GDI layer
 
91
     * @return the <code>JGDI<code> instance
 
92
     * @since  0.91
 
93
     */
 
94
    public static JGDI newSynchronizedInstance(String url) throws JGDIException {
 
95
        return (JGDI) Proxy.newProxyInstance(JGDIFactory.class.getClassLoader(), new Class[]{JGDI.class},
 
96
                new SynchronJGDIInvocationHandler(newInstance(url)));
 
97
    }
 
98
 
 
99
    private static class SynchronJGDIInvocationHandler implements InvocationHandler {
 
100
 
 
101
        private final JGDI jgdi;
 
102
 
 
103
        public SynchronJGDIInvocationHandler(JGDI jgdi) {
 
104
            this.jgdi = jgdi;
 
105
        }
 
106
 
 
107
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 
108
            try {
 
109
                synchronized (jgdi) {
 
110
                    return method.invoke(jgdi, args);
 
111
                }
 
112
            } catch (InvocationTargetException ex) {
 
113
                throw ex.getTargetException();
 
114
            }
 
115
        }
 
116
    }
 
117
 
 
118
    /**
 
119
     * Create a new event client which receives events from a jgdi
 
120
     * connection.
 
121
     *
 
122
     * @param url  JGDI connection url in the form
 
123
     *             <code>bootstrap://&lt;SGE_ROOT&gt;@&lt;SGE_CELL&gt;:&lt;SGE_QMASTER_PORT&gt;</code>
 
124
     * @param evcId  id of the event client (0 mean dynamically assigned)
 
125
     * @throws com.sun.grid.jgdi.JGDIException
 
126
     * @return the new event client
 
127
     */
 
128
    public static EventClientImpl createEventClient(String url, int evcId) throws JGDIException {
 
129
        return new EventClientImpl(url, evcId);
 
130
    }
 
131
 
 
132
    /**
 
133
     * return the jgdi shared library version string, e.g. 'GE maintrunk' or
 
134
     * 'GE 6.1u1'
 
135
     * @return the jgdi shared library version string
 
136
     */
 
137
    public static String getJGDIVersion() {
 
138
        try {
 
139
            initLib();
 
140
        } catch (JGDIException ex) {
 
141
            throw new IllegalStateException(ex.getCause());
 
142
        }
 
143
        if (versionString == null) {
 
144
            versionString = nativeSetJGDIVersion();
 
145
        }
 
146
        return versionString;
 
147
    }
 
148
 
 
149
    private static native String nativeSetJGDIVersion();
 
150
 
 
151
    /**
 
152
     *   Create a proxy object to the JGDI MBean.
 
153
     *
 
154
     *   @param  host  the qmaster host
 
155
     *   @param  port  port where qmasters MBeanServer is listening
 
156
     *   @param  credentials credentials for authentication
 
157
     *   @return the JGDI Proxy object
 
158
     */
 
159
    public static JGDIProxy newJMXInstance(String host, int port, Object credentials) {
 
160
 
 
161
        JMXServiceURL url;
 
162
        try {
 
163
            url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", host, port));
 
164
        } catch (MalformedURLException ex) {
 
165
            throw new IllegalStateException("Invalid JMX url", ex);
 
166
        }
 
167
 
 
168
        return new JGDIProxy(url, credentials);
 
169
    }
 
170
}