Skip to content

Commit

Permalink
PAYARA-1904 Add jndi to get set ordinal commands (payara#1833)
Browse files Browse the repository at this point in the history
PAYARA-1904
* add-jndi-to-get-set-ordinal-commands

* add cases to switches

* source should be optional

Since there is a default for the source, it should be optional or the default can't be used.

* source should be optional

Since there is a default for the source, it should be optional or the default can't be used.

* Source should be optional

Since there is a default for the source, it should be optional or the default can't be used.

* Source should be optional

Since there is a default for the source, it should be optional or the default can't be used.

* Clarify error text for ambiguous sourceName property

* Clarify error text for ambiguous sourceName property

* jndi source for set-config-property

* slightly modify JNDI setter and add getter

* removed prefix and added support for targets to getter
  • Loading branch information
mikecroft authored and Pandrex247 committed Sep 4, 2017
1 parent b004683 commit 18f8e85
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 31 deletions.
12 changes: 12 additions & 0 deletions nucleus/payara-modules/nucleus-microprofile/config-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ holder.
<artifactId>kernel</artifactId>
<version>4.1.2.173.0.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.glassfish.main.resources</groupId>
<artifactId>resources-connector</artifactId>
<version>${project.version}</version>
<type>jar</type>
</dependency>
<!-- <dependency>
<groupId>org.glassfish.main.resources</groupId>
<artifactId>resources-connector</artifactId>
<version>4.1.1.171-SNAPSHOT</version>
<type>jar</type>
</dependency>-->
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
})
public class GetConfigOrdinal implements AdminCommand {

@Param(acceptableValues = "domain,config,server,application,module,cluster", defaultValue = "domain")
@Param(optional = true, acceptableValues = "domain,config,server,application,module,cluster,jndi", defaultValue = "domain")
String source;

@Param(optional = true, defaultValue = "server") // if no target is specified it will be the DAS
Expand Down Expand Up @@ -116,6 +116,9 @@ public void execute(AdminCommandContext context) {
case "cluster": {
result = serviceConfig.getClusterOrdinality();
break;
}case "jndi": {
result = serviceConfig.getJNDIOrdinality();
break;
}
}
context.getActionReport().setMessage(result.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*/
package fish.payara.nucleus.microprofile.config.admin;

import com.sun.enterprise.util.SystemPropertyConstants;
import fish.payara.nucleus.microprofile.config.service.MicroprofileConfigConfiguration;
import fish.payara.nucleus.microprofile.config.service.MicroprofileConfigService;
import java.util.logging.Logger;
Expand All @@ -64,18 +65,18 @@
@ExecuteOn()
@TargetType()
@RestEndpoints({ // creates a REST endpoint needed for integration with the admin interface

@RestEndpoint(configBean = MicroprofileConfigConfiguration.class,
opType = RestEndpoint.OpType.POST, // must be POST as it is doing an update
path = "get-config-property",
description = "Gets a configuration property")
})
public class GetConfigProperty implements AdminCommand {

@Param(acceptableValues = "domain,config,server,application,module,cluster", defaultValue = "domain")
@Param(optional = true, acceptableValues = "domain,config,server,application,module,cluster, jndi", defaultValue = "domain")
String source;

@Param(optional = true, defaultValue = "server") // if no target is specified it will be the DAS
@Param(optional = true, defaultValue = SystemPropertyConstants.DAS_SERVER_NAME) // if no target is specified it will be the DAS
String target;

@Param
Expand All @@ -92,7 +93,7 @@ public class GetConfigProperty implements AdminCommand {

@Override
public void execute(AdminCommandContext context) {

String result = null;
switch (source) {
case "domain": {
Expand Down Expand Up @@ -125,7 +126,7 @@ public void execute(AdminCommandContext context) {
}
case "module": {
if (sourceName == null || moduleName == null) {
context.getActionReport().failure(Logger.getLogger(SetConfigProperty.class.getName()), "sourceName and moduleName are required parameters module is the source");
context.getActionReport().failure(Logger.getLogger(SetConfigProperty.class.getName()), "sourceName and moduleName are required parameters if module is the source. The sourceName should be the name of the application where the module is deployed.");
} else {
result = service.getModuleProperty(sourceName, moduleName, propertyName);
}
Expand All @@ -135,6 +136,10 @@ public void execute(AdminCommandContext context) {
result = service.getClusteredProperty(propertyName);
break;
}
case "jndi": {
result = service.getJNDIProperty(propertyName, target);
break;
}
}
if (result != null) {
context.getActionReport().setMessage(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class SetConfigOrdinal implements AdminCommand {
@Param()
int ordinal;

@Param(acceptableValues = "domain,config,server,application,module,cluster", defaultValue = "domain")
@Param(optional = true, acceptableValues = "domain,config,server,application,module,cluster,jndi", defaultValue = "domain")
String source;

@Param (optional = true, defaultValue = "server") // if no target is specified it will be the DAS
Expand Down Expand Up @@ -128,7 +128,10 @@ public Object run(MicroprofileConfigConfiguration config) {
config.setClusterOrdinality(ordinal);
break;
}

case "jndi": {
config.setJNDIOrdinality(ordinal);
break;
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*/
package fish.payara.nucleus.microprofile.config.admin;

import com.sun.enterprise.util.SystemPropertyConstants;
import fish.payara.nucleus.microprofile.config.service.MicroprofileConfigConfiguration;
import fish.payara.nucleus.microprofile.config.service.MicroprofileConfigService;
import java.util.logging.Logger;
Expand All @@ -64,18 +65,18 @@
@ExecuteOn()
@TargetType()
@RestEndpoints({ // creates a REST endpoint needed for integration with the admin interface

@RestEndpoint(configBean = MicroprofileConfigConfiguration.class,
opType = RestEndpoint.OpType.POST, // must be POST as it is doing an update
path = "set-config-property",
description = "Sets a configuration property")
})
public class SetConfigProperty implements AdminCommand {

@Param(acceptableValues = "domain,config,server,application,module,cluster", defaultValue = "domain")
@Param(optional = true, acceptableValues = "domain,config,server,application,module,cluster, jndi", defaultValue = "domain")
String source;

@Param(optional = true, defaultValue = "server") // if no target is specified it will be the DAS
@Param(optional = true, defaultValue = SystemPropertyConstants.DAS_SERVER_NAME) // if no target is specified it will be the DAS
String target;

@Param
Expand Down Expand Up @@ -127,7 +128,7 @@ public void execute(AdminCommandContext context) {
}
case "module": {
if (sourceName == null || moduleName == null) {
context.getActionReport().failure(Logger.getLogger(SetConfigProperty.class.getName()), "sourceName and moduleName are required parameters module is the source");
context.getActionReport().failure(Logger.getLogger(SetConfigProperty.class.getName()), "sourceName and moduleName are required parameters if module is the source. The sourceName should be the name of the application where the module is deployed.");
} else {
service.setModuleProperty(sourceName, moduleName, propertyName, propertyValue);
}
Expand All @@ -138,6 +139,11 @@ public void execute(AdminCommandContext context) {
break;
}

case "jndi": {
service.setJNDIProperty(propertyName, propertyValue, target);
break;
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.Module;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.config.serverbeans.ServerTags;
import fish.payara.nucleus.store.ClusteredStore;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
Expand All @@ -62,6 +64,8 @@
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import static javax.naming.InitialContext.doLookup;
import javax.naming.NamingException;
import org.glassfish.api.StartupRunLevel;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.event.EventListener;
Expand All @@ -71,6 +75,12 @@
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.data.ApplicationRegistry;
import org.glassfish.internal.deployment.Deployment;
import org.glassfish.resources.admin.cli.CustomResourceManager;
import org.glassfish.resources.admin.cli.ResourceConstants;
import static org.glassfish.resources.admin.cli.ResourceConstants.JNDI_NAME;
import org.glassfish.resources.config.CustomResource;
import org.glassfish.resourcebase.resources.util.BindableResourcesHelper;
import org.glassfish.resources.admin.cli.DeleteCustomResource;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.config.ConfigListener;
import org.jvnet.hk2.config.ConfigSupport;
Expand Down Expand Up @@ -120,17 +130,22 @@ public class MicroprofileConfigService implements EventListener, ConfigListener

@Inject
Domain domainConfiguration;


@Inject
private CustomResourceManager customResMgr;

@Inject
private BindableResourcesHelper bindableResourcesHelper;

// This injects the configuration from the domain.xml magically
// and for the correct server configuation
@Inject
@Named(ServerEnvironment.DEFAULT_INSTANCE_NAME)
MicroprofileConfigConfiguration configuration;

// Provides ability to register a configuration listener
@Inject
Transactions transactions;


@PostConstruct
public void postConstruct() {
Expand All @@ -140,8 +155,7 @@ public void postConstruct() {

@Override
public void event(Event event) {
if (event.is(Deployment.APPLICATION_LOADED))
{
if (event.is(Deployment.APPLICATION_LOADED)) {
// TO DO look for microprofile config files in the application classloader
ApplicationInfo info = (ApplicationInfo) event.hook();
LinkedList<Properties> appConfigProperties = new LinkedList<>();
Expand All @@ -159,7 +173,7 @@ public void event(Event event) {
} catch (IOException ex) {
Logger.getLogger(MicroprofileConfigService.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}

@Override
Expand Down Expand Up @@ -346,7 +360,7 @@ public String getSystemProperty(String name) {
}
return result;
}

public List<Properties> getDeployedApplicationProperties(String applicationName) {
ApplicationInfo info = applicationRegistry.get(applicationName);
List<Properties> result = null;
Expand All @@ -355,7 +369,7 @@ public List<Properties> getDeployedApplicationProperties(String applicationName)
}
return result;
}

public String getDeployedApplicationProperty(String applicationName, String name) {
String result = null;
ApplicationInfo info = applicationRegistry.get(applicationName);
Expand All @@ -373,6 +387,39 @@ public String getDeployedApplicationProperty(String applicationName, String name
return result;
}

public void setJNDIProperty(final String name, final String value, final String target) {

HashMap attrList = new HashMap();
attrList.put("factory-class", "org.glassfish.resources.custom.factory.PrimitivesAndStringFactory");
attrList.put("res-type", "java.lang.String");
attrList.put(ResourceConstants.ENABLED, Boolean.TRUE.toString());
attrList.put(JNDI_NAME, name);
attrList.put(ServerTags.DESCRIPTION, "MicroProfile Config property for " + name);

Properties props = new Properties();

props.put("value", value);

try {
customResMgr.create(domainConfiguration.getResources(), attrList, props, target);
} catch (Exception ex) {
Logger.getLogger(MicroprofileConfigService.class.getName()).log(Level.WARNING, "Unable to set MicroProfile Config property " + name, ex);
}
}

public String getJNDIProperty(String name, String target) {
Collection<CustomResource> customResources
= domainConfiguration.getResources().getResources(CustomResource.class);
for (CustomResource customResource : customResources) {
if (bindableResourcesHelper.resourceExists(customResource.getJndiName(), target)) {
if (customResource.getJndiName().equals(name)) {
return customResource.getPropertyValue("value");
}
}
}
return null;
}

public String getEnvironmentVariable(String name) {
return System.getenv(name);
}
Expand All @@ -396,7 +443,7 @@ public Map<String, String> getSystemPropertiesMap() {

public Map<String, String> getDomainProperyMap() {
List<Property> properties = domainConfiguration.getProperty();
HashMap<String,String> result = new HashMap<>(properties.size());
HashMap<String, String> result = new HashMap<>(properties.size());
for (Property property : properties) {
result.put(property.getName().substring(PROPERTY_PREFIX.length()), property.getValue());
}
Expand All @@ -405,7 +452,7 @@ public Map<String, String> getDomainProperyMap() {

public Map<String, String> getConfigPropertyMap(String configName) {
Config config = domainConfiguration.getConfigNamed(configName);
HashMap<String,String> result = new HashMap<>();
HashMap<String, String> result = new HashMap<>();
if (config != null) {
List<Property> properties = config.getProperty();
for (Property property : properties) {
Expand All @@ -414,7 +461,7 @@ public Map<String, String> getConfigPropertyMap(String configName) {
}
return result;
}

public Map<String, String> getServerPropertyMap(String configName) {
Server config = domainConfiguration.getServerNamed(configName);
HashMap<String, String> result = new HashMap<>();
Expand All @@ -426,9 +473,7 @@ public Map<String, String> getServerPropertyMap(String configName) {
}
return result;
}




public Map<String, String> getApplicationPropertyMap(String configName) {
Application config = domainConfiguration.getApplications().getApplication(configName);
HashMap<String, String> result = new HashMap<>();
Expand All @@ -440,7 +485,7 @@ public Map<String, String> getApplicationPropertyMap(String configName) {
}
return result;
}

public Map<String, String> getModulePropertyMap(String configName, String moduleName) {
Application config = domainConfiguration.getApplications().getApplication(configName);
HashMap<String, String> result = new HashMap<>();
Expand All @@ -455,12 +500,12 @@ public Map<String, String> getModulePropertyMap(String configName, String module
}
return result;
}
public Map<String,String> getClusteredPropertyMap() {

public Map<String, String> getClusteredPropertyMap() {
Map<Serializable, Serializable> map = clusterStore.getMap(CLUSTERED_CONFIG_STORE);
HashMap<String,String> result = new HashMap<>();
HashMap<String, String> result = new HashMap<>();
for (Serializable key : map.keySet()) {
result.put((String)key, (String)map.get(key));
result.put((String) key, (String) map.get(key));
}
return result;
}
Expand Down

0 comments on commit 18f8e85

Please sign in to comment.