test/jdk/javax/management/remote/rest/RunRestAdapter.java
author hb
Tue, 02 Jan 2018 13:20:18 +0530
branchjmx-rest-api
changeset 56003 4f7f76f6be2f
parent 56002 60ab3b595a8e
child 56026 bd531f08d7c7
permissions -rw-r--r--
* Start Rest adapter via command line arguments * Rest adapter config via management.properties
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 javax.management.remote.rest.PlatformRestAdapter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     9
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
 * @test
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    13
 * @modules java.logging
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    14
 *          java.management.rest
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    15
 * @run main RunRestAdapter
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    16
 */
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    17
public class RunRestAdapter {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    18
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    19
    private static String sslAgentConfig;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    20
    private static String sslClientConfig;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    21
    private static String configFile;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    22
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    23
    public static void main(String[] args) throws Exception {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    24
        RunRestAdapter rr = new RunRestAdapter();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    25
        rr.run();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    26
        while (true)
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    27
        {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    28
            Thread.sleep(1000);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    29
        }
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
    private void createAgentSslConfigFile(String fileName) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    33
        File f = new File(fileName);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    34
        if (f.exists()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    35
            return;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    36
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    37
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    38
        String testDir = System.getProperty("test.src");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    39
        props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreAgent");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    40
        props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    41
        props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreAgent");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    42
        props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    43
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    44
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    45
            props.store(writer, "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    46
        }
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
    private void createClientSslConfigFile(String fileName) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    50
        File f = new File(fileName);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    51
        if (f.exists()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    52
            return;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    53
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    54
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    55
        String testDir = System.getProperty("test.src");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    56
        props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreClient");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    57
        props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    58
        props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreClient");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    59
        props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    60
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    61
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    62
            props.store(writer, "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    63
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    64
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    65
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55998
diff changeset
    66
    private void setupManagementConfig(String fileName) throws IOException {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    67
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    68
        props.setProperty("com.sun.management.jmxremote.ssl", "true");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    69
        props.setProperty("com.sun.management.jmxremote.ssl.config.file", sslAgentConfig);
55998
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55995
diff changeset
    70
        props.setProperty("com.sun.management.jmxremote.authenticate", "false");
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    71
        props.setProperty("com.sun.management.jmxremote.rest.port", "8686");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    72
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    73
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    74
            props.store(writer, "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    75
        }
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
    private void setupConfig() throws Exception {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    79
        String testSrcRoot = System.getProperty("test.src") + File.separator;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    80
        sslAgentConfig = testSrcRoot + "sslConfigAgent";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    81
        sslClientConfig = testSrcRoot + "sslConfigClient";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    82
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    83
        configFile = testSrcRoot + "mgmt1.properties";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    84
        createAgentSslConfigFile(sslAgentConfig);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    85
        createClientSslConfigFile(sslClientConfig);
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55998
diff changeset
    86
        setupManagementConfig(configFile);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    87
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    88
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    89
    public void run() throws Exception {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    90
        setupConfig();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    91
        File file = new File(configFile);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    92
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    93
        props.load(new FileInputStream(file));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    94
        if (props.get("com.sun.management.jmxremote.rest.port") != null) {
55998
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55995
diff changeset
    95
            PlatformRestAdapter.init(props);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    96
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    97
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    98
}