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 com.sun.grid.jgdi;
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;
45
* Factory class for {@link JGDI} objects.
47
* @see com.sun.grid.jgdi.JGDI
49
public class JGDIFactory {
51
private final static Logger log = Logger.getLogger(JGDIFactory.class.getName());
52
private static String versionString;
53
private static boolean libNotLoaded = true;
55
private static synchronized void initLib() throws JGDIException {
58
System.loadLibrary("jgdi");
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
69
* Get a new instance of a <code>JGDI</code> object.
71
* @param url JGDI connection url in the form
72
* <code>bootstrap://<SGE_ROOT>@<SGE_CELL>:<SGE_QMASTER_PORT></code>
73
* @throws com.sun.grid.jgdi.JGDIException on any error on the GDI layer
74
* @return the <code>JGDI<code> instance
76
public static JGDI newInstance(String url) throws JGDIException {
77
log.entering(JGDIFactory.class.getName(), "newInstance", url);
79
com.sun.grid.jgdi.jni.JGDIImpl ret = new com.sun.grid.jgdi.jni.JGDIImpl();
81
log.exiting(JGDIFactory.class.getName(), "newInstance", ret);
86
* Get a synchronized instance of a <code>JGDI</code> object.
88
* @param url JGDI connection url in the form
89
* <code>bootstrap://<SGE_ROOT>@<SGE_CELL>:<SGE_QMASTER_PORT></code>
90
* @throws com.sun.grid.jgdi.JGDIException on any error on the GDI layer
91
* @return the <code>JGDI<code> instance
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)));
99
private static class SynchronJGDIInvocationHandler implements InvocationHandler {
101
private final JGDI jgdi;
103
public SynchronJGDIInvocationHandler(JGDI jgdi) {
107
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
109
synchronized (jgdi) {
110
return method.invoke(jgdi, args);
112
} catch (InvocationTargetException ex) {
113
throw ex.getTargetException();
119
* Create a new event client which receives events from a jgdi
122
* @param url JGDI connection url in the form
123
* <code>bootstrap://<SGE_ROOT>@<SGE_CELL>:<SGE_QMASTER_PORT></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
128
public static EventClientImpl createEventClient(String url, int evcId) throws JGDIException {
129
return new EventClientImpl(url, evcId);
133
* return the jgdi shared library version string, e.g. 'GE maintrunk' or
135
* @return the jgdi shared library version string
137
public static String getJGDIVersion() {
140
} catch (JGDIException ex) {
141
throw new IllegalStateException(ex.getCause());
143
if (versionString == null) {
144
versionString = nativeSetJGDIVersion();
146
return versionString;
149
private static native String nativeSetJGDIVersion();
152
* Create a proxy object to the JGDI MBean.
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
159
public static JGDIProxy newJMXInstance(String host, int port, Object credentials) {
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);
168
return new JGDIProxy(url, credentials);