Search in sources :

Example 16 with org.jvnet.hk2.config

use of org.jvnet.hk2.config in project Payara by payara.

the class SetMonitoringLevel method execute.

@Override
public void execute(AdminCommandContext context) {
    final ActionReport actionReport = context.getActionReport();
    Config config = targetUtil.getConfig(target);
    if (config != null) {
        monitoringService = config.getMonitoringService();
    } else {
        actionReport.setMessage("Cound not find target: " + target);
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    }
    if (moduleNames != null && moduleMonitoringLevels != null) {
        String modifiedModuleNames = moduleNames.replace(":", ",");
        String modifiedModuleMonitoringLevels = moduleMonitoringLevels.replace(":", ",");
        List<String> moduleNameList = Arrays.asList(modifiedModuleNames.split(","));
        List<String> moduleLevelList = Arrays.asList(modifiedModuleMonitoringLevels.split(","));
        if (moduleNameList.size() == moduleLevelList.size()) {
            for (int i = 0; i < moduleLevelList.size(); i++) {
                boolean isValidMoudle = false;
                List<String> validModuleList = new ArrayList<>(Arrays.asList(Constants.validModuleNames));
                String moduleName = moduleNameList.get(i).trim().toLowerCase();
                for (String module : validModuleList) {
                    if (module.trim().equalsIgnoreCase(moduleName)) {
                        String moduleLevel = moduleLevelList.get(i).trim().toUpperCase();
                        try {
                            ConfigSupport.apply(new SingleConfigCode<MonitoringService>() {

                                @Override
                                public Object run(final MonitoringService monitoringServiceProxy) throws PropertyVetoException, TransactionFailure {
                                    monitoringServiceProxy.setMonitoringLevel(moduleName, moduleLevel);
                                    return monitoringServiceProxy;
                                }
                            }, monitoringService);
                        } catch (TransactionFailure ex) {
                            logger.log(Level.WARNING, "Failed to execute the command set-monitoring-level: {0}", ex.getCause().getMessage());
                            actionReport.setMessage(ex.getCause().getMessage());
                            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
                        }
                        isValidMoudle = true;
                        break;
                    }
                }
                if (!isValidMoudle) {
                    actionReport.setMessage(moduleNameList.get(i) + " isn't a valid Module name");
                    actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
            }
        } else {
            actionReport.setMessage("Number of Module and Level entered doesn't match");
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Config(com.sun.enterprise.config.serverbeans.Config) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) RestEndpoint(org.glassfish.api.admin.RestEndpoint) PropertyVetoException(java.beans.PropertyVetoException) MonitoringService(com.sun.enterprise.config.serverbeans.MonitoringService)

Example 17 with org.jvnet.hk2.config

use of org.jvnet.hk2.config in project Payara by payara.

the class ReferenceConstrainTest method serverConfigRefInvalid.

@Test
public void serverConfigRefInvalid() throws TransactionFailure {
    Server server = habitat.getService(Server.class, "server");
    assertNotNull(server);
    ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(server);
    Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
    Map<String, String> configChanges = new HashMap<String, String>();
    configChanges.put("config-ref", "server-config-nonexist");
    changes.put(serverConfig, configChanges);
    try {
        ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
        cs.apply(changes);
        fail("Can not reach this point");
    } catch (TransactionFailure tf) {
        ConstraintViolationException cv = findConstrViolation(tf);
        assertNotNull(cv);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigSupport(org.jvnet.hk2.config.ConfigSupport) Server(com.sun.enterprise.config.serverbeans.Server) HashMap(java.util.HashMap) ConstraintViolationException(javax.validation.ConstraintViolationException) ConfigBean(org.jvnet.hk2.config.ConfigBean) HashMap(java.util.HashMap) Map(java.util.Map) ConfigApiTest(com.sun.enterprise.configapi.tests.ConfigApiTest) Test(org.junit.Test)

Example 18 with org.jvnet.hk2.config

use of org.jvnet.hk2.config in project Payara by payara.

the class UpdateResourceRef method execute.

/**
 * Execution method for updating the configuration of a ResourceRef. Will be
 * replicated if the target is a cluster.
 *
 * @param context context for the command.
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // Make a list of all ResourceRefs that need to change
    List<ResourceRef> resourceRefsToChange = new ArrayList<>();
    // Add the ResourceRef from a named server if the target is a server
    Server server = domain.getServerNamed(target);
    // if the target is a server
    if (server != null) {
        ResourceRef serverResourceRef = server.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (serverResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(serverResourceRef);
    }
    // Add the ResourceRef from a named config if the target is a config
    Config config = domain.getConfigNamed(target);
    // if the target is a config
    if (config != null) {
        ResourceRef configResourceRef = config.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (configResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(configResourceRef);
    }
    // Add the ResourceRefs from a named cluster if the target is a cluster
    Cluster cluster = domain.getClusterNamed(target);
    // if the target is a cluster
    if (cluster != null) {
        ResourceRef clusterResourceRef = cluster.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (clusterResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(clusterResourceRef);
        for (Server instance : cluster.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the cluster contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Add the ResourceRefs from a named Deployment Group if the target is a Deployment Group
    DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
    if (dg != null) {
        ResourceRef ref = dg.getResourceRef(name);
        if (ref == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(ref);
        for (Server instance : dg.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the dg contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Apply the configuration to the listed ResourceRefs
    try {
        ConfigSupport.apply(new ConfigCode() {

            @Override
            public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
                for (ConfigBeanProxy proxy : params) {
                    if (proxy instanceof ResourceRef) {
                        ResourceRef resourceRefProxy = (ResourceRef) proxy;
                        if (enabled != null) {
                            resourceRefProxy.setEnabled(enabled.toString());
                        }
                    }
                }
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return true;
            }
        }, resourceRefsToChange.toArray(new ResourceRef[] {}));
    } catch (TransactionFailure ex) {
        report.failure(logger, ex.getLocalizedMessage());
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) Config(com.sun.enterprise.config.serverbeans.Config) ArrayList(java.util.ArrayList) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) PropertyVetoException(java.beans.PropertyVetoException) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 19 with org.jvnet.hk2.config

use of org.jvnet.hk2.config in project Payara by payara.

the class DeleteConfigCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    report.setActionExitCode(SUCCESS);
    // Do not delete default-config
    if (destConfig.equals("default-config")) {
        report.setMessage(LOCAL_STRINGS.getLocalString("Config.defaultConfig", "The default configuration template " + "named default-config cannot be deleted."));
        report.setActionExitCode(FAILURE);
        return;
    }
    // Get the config from the domain. Does the config exist?
    // if not return
    Config config = domain.getConfigNamed(destConfig);
    if (config == null) {
        report.setMessage(LOCAL_STRINGS.getLocalString("Config.noSuchConfig", "Config {0} does not exist.", destConfig));
        report.setActionExitCode(FAILURE);
        return;
    }
    // Check if the config in use by some other
    // ReferenceContainer -- if so just return...
    List<ReferenceContainer> referenceContainers = domain.getReferenceContainersOf(config);
    if (!referenceContainers.isEmpty()) {
        StringBuilder namesOfContainers = new StringBuilder();
        for (ReferenceContainer referenceContainer : referenceContainers) {
            namesOfContainers.append(referenceContainer.getReference()).append(',');
        }
        report.setMessage(LOCAL_STRINGS.getLocalString("Config.inUseConfig", "Config {0} is in use " + "and must be referenced by no server instances or clusters", destConfig, namesOfContainers));
        report.setActionExitCode(FAILURE);
        return;
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<Configs>() {

            @Override
            public Object run(Configs configs) throws PropertyVetoException, TransactionFailure {
                configs.getConfig().remove(config);
                return null;
            }
        }, configs);
    } catch (TransactionFailure ex) {
        report.setMessage(LOCAL_STRINGS.getLocalString("Config.deleteConfigFailed", "Unable to remove config {0} ", config) + " " + ex.getLocalizedMessage());
        report.setActionExitCode(FAILURE);
        report.setFailureCause(ex);
    }
}
Also used : ReferenceContainer(org.glassfish.api.admin.config.ReferenceContainer) PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Config(com.sun.enterprise.config.serverbeans.Config) Configs(com.sun.enterprise.config.serverbeans.Configs) ActionReport(org.glassfish.api.ActionReport)

Example 20 with org.jvnet.hk2.config

use of org.jvnet.hk2.config in project Payara by payara.

the class ResourceAdapterConfigManager method create.

public ResourceStatus create(Resources resources, HashMap attributes, final Properties properties, String target) throws Exception {
    setParams(attributes);
    ResourceStatus validationStatus = isValid(resources);
    if (validationStatus.getStatus() == ResourceStatus.FAILURE) {
        return validationStatus;
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<Resources>() {

            public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
                ResourceAdapterConfig newResource = createConfigBean(param, properties);
                param.getResources().add(newResource);
                return newResource;
            }
        }, resources);
    } catch (TransactionFailure tfe) {
        Logger.getLogger(ResourceAdapterConfigManager.class.getName()).log(Level.SEVERE, "TransactionFailure: create-resource-adapter-config", tfe);
        String msg = localStrings.getLocalString("create.resource.adapter.config.fail", "Unable to create resource adapter config", raName) + " " + tfe.getLocalizedMessage();
        return new ResourceStatus(ResourceStatus.FAILURE, msg);
    }
    String msg = localStrings.getLocalString("create.resource.adapter.config.success", "Resource adapter config {0} created successfully", raName);
    return new ResourceStatus(ResourceStatus.SUCCESS, msg);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ResourceAdapterConfig(org.glassfish.connectors.config.ResourceAdapterConfig) ResourceStatus(org.glassfish.resourcebase.resources.api.ResourceStatus) Resources(com.sun.enterprise.config.serverbeans.Resources)

Aggregations

TransactionFailure (org.jvnet.hk2.config.TransactionFailure)100 Config (com.sun.enterprise.config.serverbeans.Config)81 ActionReport (org.glassfish.api.ActionReport)79 PropertyVetoException (java.beans.PropertyVetoException)69 Property (org.jvnet.hk2.config.types.Property)53 Properties (java.util.Properties)24 CommandTarget (org.glassfish.config.support.CommandTarget)24 Target (org.glassfish.internal.api.Target)23 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)16 List (java.util.List)16 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)16 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)16 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)15 Protocol (org.glassfish.grizzly.config.dom.Protocol)15 PropertyChangeEvent (java.beans.PropertyChangeEvent)14 Server (com.sun.enterprise.config.serverbeans.Server)13 IOException (java.io.IOException)12 Protocols (org.glassfish.grizzly.config.dom.Protocols)12 File (java.io.File)10