test/jdk/javax/management/remote/rest/RunRestAdapter.java
author hb
Wed, 27 Dec 2017 14:44:48 +0530
branchjmx-rest-api
changeset 55995 a798bdd52997
parent 55994 9721e36abeb0
child 55998 54779691e11f
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:
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     1
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     2
import java.io.BufferedWriter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     3
import java.io.File;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     4
import java.io.FileInputStream;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     5
import java.io.FileWriter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     6
import java.io.IOException;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     7
import java.util.Properties;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     8
import java.util.Scanner;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     9
import javax.management.remote.rest.PlatformRestAdapter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    10
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    11
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    12
/**
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    13
 * @test
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    14
 * @modules java.logging
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    15
 *          java.management.rest
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    16
 * @run main RunRestAdapter
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    17
 */
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    18
public class RunRestAdapter {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    19
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    20
    private static String sslAgentConfig;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    21
    private static String sslClientConfig;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    22
    private static String configFile;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    23
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    24
    public static void main(String[] args) throws Exception {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    25
        RunRestAdapter rr = new RunRestAdapter();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    26
        rr.run();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    27
        while (true)
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    28
        {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    29
            Thread.sleep(1000);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    30
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    31
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    32
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    33
    private void createAgentSslConfigFile(String fileName) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    34
        File f = new File(fileName);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    35
        if (f.exists()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    36
            return;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    37
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    38
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    39
        String testDir = System.getProperty("test.src");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    40
        props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreAgent");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    41
        props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    42
        props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreAgent");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    43
        props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    44
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    45
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    46
            props.store(writer, "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    47
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    48
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    49
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    50
    private void createClientSslConfigFile(String fileName) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    51
        File f = new File(fileName);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    52
        if (f.exists()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    53
            return;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    54
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    55
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    56
        String testDir = System.getProperty("test.src");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    57
        props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreClient");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    58
        props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    59
        props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreClient");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    60
        props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    61
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    62
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    63
            props.store(writer, "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    64
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    65
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    66
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    67
    private void setupMgmtConfig(String fileName) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    68
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    69
        props.setProperty("com.sun.management.jmxremote.ssl", "true");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    70
        props.setProperty("com.sun.management.jmxremote.ssl.config.file", sslAgentConfig);
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    71
        props.setProperty("com.sun.management.jmxremote.authenticate", "true");
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    72
        props.setProperty("com.sun.management.jmxremote.rest.port", "8686");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    73
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    74
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    75
            props.store(writer, "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    76
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    77
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    78
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    79
    private void setupConfig() throws Exception {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    80
        String testSrcRoot = System.getProperty("test.src") + File.separator;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    81
        sslAgentConfig = testSrcRoot + "sslConfigAgent";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    82
        sslClientConfig = testSrcRoot + "sslConfigClient";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    83
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    84
        configFile = testSrcRoot + "mgmt1.properties";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    85
        createAgentSslConfigFile(sslAgentConfig);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    86
        createClientSslConfigFile(sslClientConfig);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    87
        setupMgmtConfig(configFile);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    88
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    89
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    90
    public void run() throws Exception {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    91
        setupConfig();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    92
        File file = new File(configFile);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    93
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    94
        props.load(new FileInputStream(file));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    95
        if (props.get("com.sun.management.jmxremote.rest.port") != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    96
            PlatformRestAdapter.init((String) props.get("com.sun.management.jmxremote.rest.port"), props);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    97
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    98
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    99
}