Index: src/test/java/org/glassfish/admin/rest/SystemPropertiesTest.java =================================================================== --- src/test/java/org/glassfish/admin/rest/SystemPropertiesTest.java (revision 46336) +++ src/test/java/org/glassfish/admin/rest/SystemPropertiesTest.java (working copy) @@ -53,12 +53,18 @@ * @author jasonlee */ public class SystemPropertiesTest extends RestTestBase { - public static final String URL_SYSTEM_PROPERTIES = "/domain/servers/server/server/system-properties"; + public static final String URL_CONFIG_SYSTEM_PROPERTIES = "/domain/configs/config/%config%/system-properties"; + public static final String URL_CLUSTER_SYSTEM_PROPERTIES = "/domain/clusters/cluster/%clusterName%/system-properties"; + public static final String URL_INSTANCE_SYSTEM_PROPERTIES = "/domain/servers/server/%instanceName%/system-properties"; + public static final String URL_DAS_SYSTEM_PROPERTIES = URL_INSTANCE_SYSTEM_PROPERTIES.replaceAll("%instanceName%", "server"); + public static final String URL_CREATE_INSTANCE = "/domain/create-instance"; + public static final String PROP_VALUE = "${com.sun.aas.instanceRoot}/foo"; + @Test public void getSystemProperties() { - ClientResponse response = get(URL_SYSTEM_PROPERTIES); + ClientResponse response = get(URL_DAS_SYSTEM_PROPERTIES); checkStatusForSuccess(response); - List> systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); + List> systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); assertNotNull(systemProperties); // This may or may not be empty, depending on whether or not other tests failed } @@ -70,14 +76,14 @@ put(prop1, "value1"); put(prop2, "value2"); }}; - ClientResponse response = post(URL_SYSTEM_PROPERTIES, payload); + ClientResponse response = post(URL_DAS_SYSTEM_PROPERTIES, payload); checkStatusForSuccess(response); - response = get(URL_SYSTEM_PROPERTIES); - List> systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); + response = get(URL_DAS_SYSTEM_PROPERTIES); + List> systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); assertNotNull(systemProperties); // This may or may not be empty, depending on whether or not other tests failed int testPropsFound = 0; - for (Map systemProperty : systemProperties) { + for (Map systemProperty : systemProperties) { String name = (String)systemProperty.get("name"); if (prop1.equals(name) || prop2.equals(name)) { testPropsFound++; @@ -86,9 +92,9 @@ assertEquals(2, testPropsFound); - response = delete(URL_SYSTEM_PROPERTIES+"/"+prop1); + response = delete(URL_DAS_SYSTEM_PROPERTIES+"/"+prop1); checkStatusForSuccess(response); - response = delete(URL_SYSTEM_PROPERTIES+"/"+prop2); + response = delete(URL_DAS_SYSTEM_PROPERTIES+"/"+prop2); checkStatusForSuccess(response); } @@ -98,14 +104,14 @@ Map payload = new HashMap() {{ put(prop1, "http://localhost:4848"); }}; - ClientResponse response = post(URL_SYSTEM_PROPERTIES, payload); + ClientResponse response = post(URL_DAS_SYSTEM_PROPERTIES, payload); checkStatusForSuccess(response); - response = get(URL_SYSTEM_PROPERTIES); - List> systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); + response = get(URL_DAS_SYSTEM_PROPERTIES); + List> systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); assertNotNull(systemProperties); // This may or may not be empty, depending on whether or not other tests failed int testPropsFound = 0; - for (Map systemProperty : systemProperties) { + for (Map systemProperty : systemProperties) { String name = (String)systemProperty.get("name"); if (prop1.equals(name)) { testPropsFound++; @@ -114,12 +120,127 @@ assertEquals(1, testPropsFound); - response = delete(URL_SYSTEM_PROPERTIES+"/"+prop1); + response = delete(URL_DAS_SYSTEM_PROPERTIES+"/"+prop1); checkStatusForSuccess(response); } + + @Test + public void testNotResolvingDasSystemProperties() { + final String prop1 = "property" + generateRandomString(); + Map payload = new HashMap() {{ + put(prop1, PROP_VALUE); + }}; + createAndTestConfigProperty(prop1, PROP_VALUE, "server-config"); + createAndTestInstanceOverride(prop1, PROP_VALUE, PROP_VALUE+"-instace", "server"); + } - private List> getSystemProperties(Map responseMap) { + @Test + public void testNotResolvingDasInstanceProperties() { + final String instanceName = "in" + generateRandomNumber(); + final String propertyName = "property" + generateRandomString(); + + ClientResponse cr = post(URL_CREATE_INSTANCE, new HashMap() {{ + put("id", instanceName); + put("node", "localhost-domain1"); + }}); + checkStatusForSuccess(cr); + + createAndTestConfigProperty(propertyName, PROP_VALUE, instanceName + "-config"); + + createAndTestInstanceOverride(propertyName, PROP_VALUE, PROP_VALUE + "-instance", instanceName); + } + + @Test + public void testNotResolvingClusterProperties() { + final String propertyName = "property" + generateRandomString(); + final String clusterName = "c" + generateRandomNumber(); + final String instanceName = clusterName + "in" + generateRandomNumber(); + ClusterTest ct = new ClusterTest(); + ct.setup(); + ct.createCluster(clusterName); + ct.createClusterInstance(clusterName, instanceName); + + createAndTestConfigProperty(propertyName, PROP_VALUE, clusterName + "-config"); + createAndTestClusterOverride(propertyName, PROP_VALUE, PROP_VALUE + "-cluster", clusterName); + createAndTestInstanceOverride(propertyName, PROP_VALUE+"-cluster", PROP_VALUE + "-instance", instanceName); + + ct.deleteCluster(clusterName); + } + + protected void createAndTestConfigProperty(final String propertyName, final String propertyValue, String configName) { + Map payload = new HashMap() {{ + put(propertyName, propertyValue); + }}; + final String url = URL_CONFIG_SYSTEM_PROPERTIES.replaceAll("%config%", configName); + ClientResponse response = post(url, payload); + checkStatusForSuccess(response); + + // Check config props + response = get(url); + List> systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); + Map sysProp = getSystemProperty(propertyName, systemProperties); + assertNotNull(sysProp); + assertEquals(sysProp.get("value"), propertyValue); + } + + protected void createAndTestClusterOverride(final String propertyName, final String defaultValue, final String propertyValue, final String clusterName) { + final String clusterSysPropsUrl = URL_CLUSTER_SYSTEM_PROPERTIES.replaceAll("%clusterName%", clusterName); + + ClientResponse response = get(clusterSysPropsUrl); + List> systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); + Map sysProp = getSystemProperty(propertyName, systemProperties); + assertNotNull(sysProp); + assertEquals(sysProp.get("defaultValue"), defaultValue); + + response = post(clusterSysPropsUrl, new HashMap() {{ + put(propertyName, propertyValue); + }}); + checkStatusForSuccess(response); + + // Check updated/overriden system property + response = get(clusterSysPropsUrl); + systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); + sysProp = getSystemProperty(propertyName, systemProperties); + assertNotNull(sysProp); + assertEquals(sysProp.get("value"), propertyValue); + assertEquals(sysProp.get("defaultValue"), defaultValue); + } + + protected void createAndTestInstanceOverride(final String propertyName, final String defaultValue, final String propertyValue, final String instanceName) { + final String instanceSysPropsUrl = URL_INSTANCE_SYSTEM_PROPERTIES.replaceAll("%instanceName%", instanceName); + + ClientResponse response = get(instanceSysPropsUrl); + List> systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); + Map sysProp = getSystemProperty(propertyName, systemProperties); + assertNotNull(sysProp); + assertEquals(sysProp.get("defaultValue"), defaultValue); + + response = post(instanceSysPropsUrl, new HashMap() {{ + put(propertyName, propertyValue); + }}); + checkStatusForSuccess(response); + + // Check updated/overriden system property + response = get(instanceSysPropsUrl); + systemProperties = getSystemProperties(MarshallingUtils.buildMapFromDocument(response.getEntity(String.class))); + sysProp = getSystemProperty(propertyName, systemProperties); + assertNotNull(sysProp); + assertEquals(sysProp.get("value"), propertyValue); + assertEquals(sysProp.get("defaultValue"), defaultValue); + } + + private List> getSystemProperties(Map responseMap) { Map extraProperties = (Map)responseMap.get("extraProperties"); - return (List>)extraProperties.get("systemProperties"); + return (List>)extraProperties.get("systemProperties"); } + + private Map getSystemProperty(String propName, List> systemProperties) { + for (Map sysProp : systemProperties) { + if (sysProp.get("name").equals(propName)) { + return sysProp; + } + } + + return null; + } } Index: src/main/java/org/glassfish/admin/rest/resources/custom/SystemPropertiesCliResource.java =================================================================== --- src/main/java/org/glassfish/admin/rest/resources/custom/SystemPropertiesCliResource.java (revision 46336) +++ src/main/java/org/glassfish/admin/rest/resources/custom/SystemPropertiesCliResource.java (working copy) @@ -145,9 +145,9 @@ } } } + + getSystemProperties(properties, getEntity(), false); - getSystemProperties(properties, spb, false); - actionReport.getExtraProperties().put("systemProperties", new ArrayList(properties.values())); if (properties.isEmpty()) { actionReport.getTopMessagePart().setMessage("Nothing to list."); // i18n @@ -187,7 +187,66 @@ return deleteProperty(grandParent, id); } + + protected void getSystemProperties(Map> properties, Dom dom, boolean getDefaults) { + List sysprops = dom.nodeElements("system-property"); + if ((sysprops != null) && (!sysprops.isEmpty())) { + for (Dom sysprop : sysprops) { + String name = sysprop.attribute("name"); + Map currValue = properties.get(name); + if (currValue == null) { + currValue = new HashMap(); + currValue.put("name", name); + currValue.put(getDefaults ? "defaultValue" : "value", sysprop.attribute("value")); + if (sysprop.attribute("description") != null) { + currValue.put("description", sysprop.attribute("description")); + } + properties.put(name, currValue); + } else { + // Only add a default value if there isn't one already + if (currValue.get("defaultValue") == null) { + currValue.put("defaultValue", sysprop.attribute("value")); + } + } + } + } + + // Figure out how to recurse + if (dom.getProxyType().equals(Server.class)) { +// Server server = (Server) spb; + // Clustered instance + if (((Server)dom.createProxy()).getCluster() != null) { + getSystemProperties(properties, getCluster(dom.parent().parent(), ((Server)dom.createProxy()).getCluster().getName()), true); + } else { + // Standalone instance or DAS + getSystemProperties(properties, getConfig(dom.parent().parent(), dom.attribute("config-ref")), true); + } + } else if (dom.getProxyType().equals(Cluster.class)) { + getSystemProperties(properties, getConfig(dom.parent().parent(), dom.attribute("config-ref")), true); + } + } + + protected Dom getCluster(Dom domain, String clusterName) { + List configs = domain.nodeElements("clusters").get(0).nodeElements("cluster"); + for(Dom config : configs) { + if (config.attribute("name").equals(clusterName)) { + return config; + } + } + return null; + } + + protected Dom getConfig(Dom domain, String configName) { + List configs = domain.nodeElements("configs").get(0).nodeElements("config"); + for(Dom config : configs) { + if (config.attribute("name").equals(configName)) { + return config; + } + } + return null; + } + protected void getSystemProperties(Map> properties, SystemPropertyBag spb, boolean getDefaults) { try { List sysProps = spb.getSystemProperty();