Search in sources :

Example 41 with ConfigBean

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

the class TemplateRestResource method doDelete.

protected ExitCode doDelete(Map<String, String> data) {
    if (data == null) {
        data = new HashMap<String, String>();
    }
    if (entity == null) {
        // wrong resource
        // return Response.status(404).entity(ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, errorMessage, requestHeaders, uriInfo)).build();
        throwError(Status.NOT_FOUND, localStrings.getLocalString("rest.resource.erromessage.noentity", "Resource not found."));
    }
    if (getDeleteCommand() == null) {
        String message = localStrings.getLocalString("rest.resource.delete.forbidden", "DELETE on \"{0}\" is forbidden.", uriInfo.getAbsolutePath());
        throwError(Status.FORBIDDEN, message);
    }
    if (getDeleteCommand().equals("GENERIC-DELETE")) {
        try {
            ConfigBean p = (ConfigBean) parent;
            if (parent == null) {
                p = (ConfigBean) entity.parent();
            }
            ConfigSupport.deleteChild(p, (ConfigBean) entity);
            return ExitCode.SUCCESS;
        } catch (TransactionFailure ex) {
            throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
    // do the delete via the command:
    if (data.containsKey("error")) {
        throwError(Status.BAD_REQUEST, localStrings.getLocalString("rest.request.parsing.error", "Unable to parse the input entity. Please check the syntax."));
    }
    ResourceUtil.addQueryString(uriInfo.getQueryParameters(), data);
    ResourceUtil.purgeEmptyEntries(data);
    ResourceUtil.adjustParameters(data);
    if (data.get("DEFAULT") == null) {
        addDefaultParameter(data);
    } else {
        String resourceName = getResourceName(uriInfo.getAbsolutePath().getPath(), "/");
        if (!data.get("DEFAULT").equals(resourceName)) {
            throwError(Status.FORBIDDEN, localStrings.getLocalString("rest.resource.not.deleted", "Resource not deleted. Value of \"name\" should be the name of this resource."));
        }
    }
    RestActionReporter actionReport = runCommand(getDeleteCommand(), data);
    if (actionReport != null) {
        ActionReport.ExitCode exitCode = actionReport.getActionExitCode();
        if (exitCode != ActionReport.ExitCode.FAILURE) {
            return exitCode;
        }
        throwError(Status.BAD_REQUEST, actionReport.getMessage());
    }
    throw new WebApplicationException(handleError(Status.BAD_REQUEST, localStrings.getLocalString("rest.resource.delete.forbidden", "DELETE on \"{0}\" is forbidden.", uriInfo.getAbsolutePath())));
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) WebApplicationException(javax.ws.rs.WebApplicationException) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) ConfigBean(org.jvnet.hk2.config.ConfigBean) ExitCode(org.glassfish.api.ActionReport.ExitCode) ActionReport(org.glassfish.api.ActionReport)

Example 42 with ConfigBean

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

the class SetCommand method setLeafElement.

public static void setLeafElement(final ConfigBean node, final String elementName, final String values) throws TransactionFailure {
    ConfigBeanProxy readableView = node.getProxy(node.getProxyType());
    ConfigSupport.apply(new SingleConfigCode<ConfigBeanProxy>() {

        /**
         * Runs the following command passing the configuration object. The
         * code will be run within a transaction, returning true will commit
         * the transaction, false will abort it.
         *
         * @param param is the configuration object protected by the
         * transaction
         * @return any object that should be returned from within the
         * transaction code
         * @throws java.beans.PropertyVetoException if the changes cannot be
         * applied to the configuration
         */
        @Override
        public Object run(ConfigBeanProxy param) throws PropertyVetoException, TransactionFailure {
            WriteableView writeableParent = (WriteableView) Proxy.getInvocationHandler(param);
            StringTokenizer st = new StringTokenizer(values, ",");
            List<String> valList = new ArrayList<>();
            while (st.hasMoreTokens()) {
                valList.add(st.nextToken());
            }
            ConfigBean bean = writeableParent.getMasterView();
            for (Method m : writeableParent.getProxyType().getMethods()) {
                // Check to see if the method is a setter for the element
                // An element setter has to have the right name, take a single
                // collection parameter that parameterized with the right type
                Class[] argClasses = m.getParameterTypes();
                Type[] argTypes = m.getGenericParameterTypes();
                ConfigModel.Property prop = bean.model.toProperty(m);
                if (prop == null || !prop.xmlName().equals(elementName) || argClasses.length != 1 || !Collection.class.isAssignableFrom(argClasses[0]) || argTypes.length != 1 || !(argTypes[0] instanceof ParameterizedType) || !Types.erasure(Types.getTypeArgument(argTypes[0], 0)).isAssignableFrom(values.getClass())) {
                    continue;
                }
                // we have the right method.  Now call it
                try {
                    m.invoke(writeableParent.getProxy(writeableParent.<ConfigBeanProxy>getProxyType()), valList);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new TransactionFailure("Exception while setting element", e);
                }
                return node;
            }
            throw new TransactionFailure("No method found for setting element");
        }
    }, readableView);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigBean(org.jvnet.hk2.config.ConfigBean) Method(java.lang.reflect.Method) PropertyVetoException(java.beans.PropertyVetoException) ParameterizedType(java.lang.reflect.ParameterizedType) WriteableView(org.jvnet.hk2.config.WriteableView) StringTokenizer(java.util.StringTokenizer) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) List(java.util.List) ArrayList(java.util.ArrayList) Property(org.jvnet.hk2.config.types.Property)

Example 43 with ConfigBean

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

the class SetCommand method setProperty.

private boolean setProperty(AdminCommandContext context, String pattern, SetOperation op, String targetName, Map<Dom, String> dottedNames) {
    Dom parentNode = null;
    pattern = pattern.substring(0, trueLastIndexOf(pattern, '.'));
    TreeNode[] parentNodes_ = getAliasedParent(domain, pattern);
    pattern = parentNodes_[0].relativeName;
    Map<Dom, String> matchingNodes_ = getMatchingNodes(dottedNames, pattern);
    if (matchingNodes_.isEmpty()) {
        fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
        return false;
    }
    for (Map.Entry<Dom, String> node : matchingNodes_.entrySet()) {
        if (node.getValue().equals(pattern)) {
            parentNode = node.getKey();
        }
    }
    if (parentNode == null) {
        fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
        return false;
    }
    if (op.value == null || op.value.length() == 0) {
        // setting to the empty string means to remove the property, so don't create it
        success(context, targetName, op.value);
        return true;
    }
    // create and set the property
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put("value", op.value);
    attributes.put("name", op.attrName);
    try {
        if (!(parentNode instanceof ConfigBean)) {
            final ClassCastException cce = new ClassCastException(parentNode.getClass().getName());
            fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", cce.getMessage(), cce));
            return false;
        }
        ConfigSupport.createAndSet((ConfigBean) parentNode, Property.class, attributes);
        return replicatePropertyChange(context, op, targetName);
    } catch (TransactionFailure transactionFailure) {
        fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
        return false;
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Dom(org.jvnet.hk2.config.Dom) HashMap(java.util.HashMap) ConfigBean(org.jvnet.hk2.config.ConfigBean) Map(java.util.Map) ParameterMap(org.glassfish.api.admin.ParameterMap) HashMap(java.util.HashMap)

Aggregations

ConfigBean (org.jvnet.hk2.config.ConfigBean)28 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)21 HashMap (java.util.HashMap)18 Map (java.util.Map)15 ConfigSupport (org.jvnet.hk2.config.ConfigSupport)12 Test (org.junit.Test)11 ConfigApiTest (com.sun.enterprise.configapi.tests.ConfigApiTest)8 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)8 Property (org.jvnet.hk2.config.types.Property)8 PropertyVetoException (java.beans.PropertyVetoException)7 Method (java.lang.reflect.Method)5 ConstraintViolationException (javax.validation.ConstraintViolationException)5 Dom (org.jvnet.hk2.config.Dom)5 Cluster (com.sun.enterprise.config.serverbeans.Cluster)4 Domain (com.sun.enterprise.config.serverbeans.Domain)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 RestActionReporter (org.glassfish.admin.rest.utils.xml.RestActionReporter)4 MultiException (org.glassfish.hk2.api.MultiException)4