src/java.management.rest/share/classes/javax/management/remote/rest/PlatformRestAdapter.java
author hb
Wed, 27 Dec 2017 14:44:48 +0530
branchjmx-rest-api
changeset 55995 a798bdd52997
parent 55994 9721e36abeb0
child 55997 f881344569d9
permissions -rw-r--r--
POST : Attribute update - working POST : Bulk operation for MBean - working Exception to HTTP error Mapping
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     1
/*
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     2
 * To change this license header, choose License Headers in Project Properties.
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     3
 * To change this template file, choose Tools | Templates
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     4
 * and open the template in the editor.
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     5
 */
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     6
package javax.management.remote.rest;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     7
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
     8
import javax.management.MBeanServerFactoryListener;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
     9
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    10
import com.oracle.jmx.remote.rest.http.JmxRestAdapter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    11
import com.oracle.jmx.remote.rest.http.MBeanServerCollectionResource;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    12
import com.sun.net.httpserver.HttpServer;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    13
import com.sun.net.httpserver.HttpsConfigurator;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    14
import com.sun.net.httpserver.HttpsServer;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    15
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    16
import javax.management.MBeanServer;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    17
import javax.net.ssl.KeyManagerFactory;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    18
import javax.net.ssl.SSLContext;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    19
import javax.net.ssl.TrustManagerFactory;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    20
import java.io.BufferedInputStream;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    21
import java.io.FileInputStream;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    22
import java.io.IOException;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    23
import java.lang.management.ManagementFactory;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    24
import java.net.InetAddress;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    25
import java.net.InetSocketAddress;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    26
import java.net.UnknownHostException;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    27
import java.security.KeyStore;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    28
import java.util.*;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    29
import java.util.concurrent.CopyOnWriteArrayList;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    30
import java.util.concurrent.Executors;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    31
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    32
/**
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    33
 * @author harsha
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    34
 */
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    35
public class PlatformRestAdapter implements MBeanServerFactoryListener {
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    36
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    37
    /*
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    38
     * Initializes HTTPServer with settings from config file
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    39
     * acts as container for platform rest adapter
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    40
     */
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    41
    private static HttpServer httpServer = null;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    42
    private static List<JmxRestAdapter> restAdapters = new CopyOnWriteArrayList<>();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    43
    private static Map<String, Object> env;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    44
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    45
    private static String portStr;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    46
    private static Properties props;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    47
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    48
    private PlatformRestAdapter() {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    49
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    50
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    51
    public static synchronized void init(String portStr, Properties props) throws IOException {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    52
        PlatformRestAdapter.portStr = portStr;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    53
        PlatformRestAdapter.props = props;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    54
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    55
        if (httpServer == null) {
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    56
            final int port;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    57
            try {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    58
                port = Integer.parseInt(portStr);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    59
            } catch (NumberFormatException x) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    60
                throw new IllegalArgumentException("Invalid string for port");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    61
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    62
            if (port < 0) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    63
                throw new IllegalArgumentException("Invalid string for port");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    64
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    65
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    66
            boolean useSSL = Boolean.parseBoolean((String) props.get("com.sun.management.jmxremote.ssl"));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    67
            if (useSSL) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    68
                final String sslConfigFileName
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    69
                        = props.getProperty(PropertyNames.SSL_CONFIG_FILE_NAME);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    70
                SSLContext ctx = getSSlContext(sslConfigFileName);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    71
                if (ctx != null) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    72
                    HttpsServer server = HttpsServer.create(new InetSocketAddress("0.0.0.0", port), 0);
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    73
                    server.setHttpsConfigurator(new HttpsConfigurator(ctx));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    74
                    httpServer = server;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    75
                } else {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    76
                    httpServer = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0);
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    77
                }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    78
            } else {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    79
                httpServer = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0);
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    80
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    81
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    82
            // Initialize rest adapter
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    83
			env = new HashMap<>();
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    84
            // Do we use authentication?
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    85
            final String useAuthenticationStr
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    86
                    = props.getProperty(PropertyNames.USE_AUTHENTICATION,
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    87
                            DefaultValues.USE_AUTHENTICATION);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    88
            final boolean useAuthentication
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    89
                    = Boolean.valueOf(useAuthenticationStr);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    90
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    91
            String loginConfigName;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    92
            String passwordFileName;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    93
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    94
            if (useAuthentication) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    95
                env.put("jmx.remote.x.authentication", Boolean.TRUE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    96
                // Get non-default login configuration
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    97
                loginConfigName
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    98
                        = props.getProperty(PropertyNames.LOGIN_CONFIG_NAME);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    99
                env.put("jmx.remote.x.login.config", loginConfigName);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   100
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   101
                if (loginConfigName == null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   102
                    // Get password file
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   103
                    passwordFileName
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   104
                            = props.getProperty(PropertyNames.PASSWORD_FILE_NAME);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   105
                    env.put("jmx.remote.x.password.file", passwordFileName);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   106
                }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   107
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   108
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   109
            restAdapters.add(new JmxRestAdapter(httpServer, "platform", env, ManagementFactory.getPlatformMBeanServer()));
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   110
            new MBeanServerCollectionResource(restAdapters, httpServer);
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   111
            httpServer.setExecutor(Executors.newCachedThreadPool());
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   112
            httpServer.start();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   113
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   114
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   115
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   116
    public synchronized static void stop() {
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   117
        if (httpServer != null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   118
            httpServer.stop(0);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   119
            httpServer = null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   120
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   121
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   122
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   123
    public synchronized static void start() throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   124
        if(httpServer == null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   125
            PlatformRestAdapter.init(portStr,props);
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   126
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   127
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   128
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   129
    @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   130
    public void onMBeanServerCreated(MBeanServer mBeanServer) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   131
        JmxRestAdapter restAdapter = new JmxRestAdapter(httpServer, "", env, mBeanServer);
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   132
        restAdapters.add(restAdapter);
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   133
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   134
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   135
    @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   136
    public void onMBeanServerRemoved(MBeanServer mBeanServer) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   137
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   138
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   139
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   140
    public static synchronized String getAuthority() {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   141
        if(httpServer == null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   142
            throw new IllegalStateException("Platform rest adapter not initialized");
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   143
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   144
        try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   145
            if (httpServer instanceof HttpsServer) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   146
                return "https://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   147
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   148
            return "http://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   149
        } catch (UnknownHostException ex) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   150
            return "http://localhost" + ":" + httpServer.getAddress().getPort();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   151
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   152
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   153
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   154
    public static synchronized String getBaseURL() {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   155
        if(httpServer == null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   156
            throw new IllegalStateException("Platform rest adapter not initialized");
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   157
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   158
        try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   159
            if (httpServer instanceof HttpsServer) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   160
                return "https://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort() + "/jmx/servers";
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   161
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   162
            return "http://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort() + "/jmx/servers";
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   163
        } catch (UnknownHostException ex) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   164
            return "http://localhost" + ":" + httpServer.getAddress().getPort() + "/jmx/servers";
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   165
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   166
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   167
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   168
    private static SSLContext getSSlContext(String sslConfigFileName) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   169
        final String keyStore, keyStorePassword, trustStore, trustStorePassword;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   170
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   171
        try {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   172
            if (sslConfigFileName == null || sslConfigFileName.isEmpty()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   173
                keyStore = System.getProperty(PropertyNames.SSL_KEYSTORE_FILE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   174
                keyStorePassword = System.getProperty(PropertyNames.SSL_KEYSTORE_PASSWORD);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   175
                trustStore = System.getProperty(PropertyNames.SSL_TRUSTSTORE_FILE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   176
                trustStorePassword = System.getProperty(PropertyNames.SSL_TRUSTSTORE_PASSWORD);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   177
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   178
                Properties p = new Properties();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   179
                BufferedInputStream bin = new BufferedInputStream(new FileInputStream(sslConfigFileName));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   180
                p.load(bin);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   181
                keyStore = p.getProperty(PropertyNames.SSL_KEYSTORE_FILE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   182
                keyStorePassword = p.getProperty(PropertyNames.SSL_KEYSTORE_PASSWORD);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   183
                trustStore = p.getProperty(PropertyNames.SSL_TRUSTSTORE_FILE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   184
                trustStorePassword = p.getProperty(PropertyNames.SSL_TRUSTSTORE_PASSWORD);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   185
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   186
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   187
            char[] keyStorePasswd = null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   188
            if (keyStorePassword.length() != 0) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   189
                keyStorePasswd = keyStorePassword.toCharArray();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   190
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   191
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   192
            char[] trustStorePasswd = null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   193
            if (trustStorePassword.length() != 0) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   194
                trustStorePasswd = trustStorePassword.toCharArray();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   195
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   196
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   197
            KeyStore ks = null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   198
            if (keyStore != null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   199
                ks = KeyStore.getInstance(KeyStore.getDefaultType());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   200
                FileInputStream ksfis = new FileInputStream(keyStore);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   201
                ks.load(ksfis, keyStorePasswd);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   202
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   203
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   204
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   205
                    KeyManagerFactory.getDefaultAlgorithm());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   206
            kmf.init(ks, keyStorePasswd);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   207
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   208
            KeyStore ts = null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   209
            if (trustStore != null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   210
                ts = KeyStore.getInstance(KeyStore.getDefaultType());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   211
                FileInputStream tsfis = new FileInputStream(trustStore);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   212
                ts.load(tsfis, trustStorePasswd);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   213
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   214
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   215
                    TrustManagerFactory.getDefaultAlgorithm());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   216
            tmf.init(ts);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   217
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   218
            SSLContext ctx = SSLContext.getInstance("SSL");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   219
            ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   220
            return ctx;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   221
        } catch (Exception ex) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   222
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   223
        return null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   224
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   225
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   226
    /**
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   227
     * Default values for JMX configuration properties.
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   228
     */
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   229
    static interface DefaultValues {
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   230
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   231
        public static final String PORT = "0";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   232
        public static final String CONFIG_FILE_NAME = "management.properties";
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   233
        public static final String USE_SSL = "false";
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   234
        public static final String USE_LOCAL_ONLY = "true";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   235
        public static final String USE_REGISTRY_SSL = "false";
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   236
        public static final String USE_AUTHENTICATION = "false";
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   237
        public static final String PASSWORD_FILE_NAME = "jmxremote.password";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   238
        public static final String ACCESS_FILE_NAME = "jmxremote.access";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   239
        public static final String SSL_NEED_CLIENT_AUTH = "false";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   240
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   241
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   242
    /**
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   243
     * Names of JMX configuration properties.
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   244
     */
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   245
    public static interface PropertyNames {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   246
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   247
        String PORT
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   248
                = "com.sun.management.jmxremote.rest.port";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   249
        public static final String HOST
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   250
                = "com.sun.management.jmxremote.host";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   251
        public static final String USE_SSL
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   252
                = "com.sun.management.jmxremote.ssl";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   253
        public static final String USE_AUTHENTICATION
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   254
                = "com.sun.management.jmxremote.authenticate";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   255
        public static final String PASSWORD_FILE_NAME
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   256
                = "com.sun.management.jmxremote.password.file";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   257
        public static final String LOGIN_CONFIG_NAME
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   258
                = "com.sun.management.jmxremote.login.config";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   259
        public static final String SSL_NEED_CLIENT_AUTH
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   260
                = "com.sun.management.jmxremote.ssl.need.client.auth";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   261
        public static final String SSL_CONFIG_FILE_NAME
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   262
                = "com.sun.management.jmxremote.ssl.config.file";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   263
        public static final String SSL_KEYSTORE_FILE
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   264
                = "javax.net.ssl.keyStore";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   265
        public static final String SSL_TRUSTSTORE_FILE
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   266
                = "javax.net.ssl.trustStore";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   267
        public static final String SSL_KEYSTORE_PASSWORD
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   268
                = "javax.net.ssl.keyStorePassword";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   269
        public static final String SSL_TRUSTSTORE_PASSWORD
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   270
                = "javax.net.ssl.trustStorePassword";
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   271
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   272
}