Search in sources :

Example 6 with ConfigBean

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

the class TemplateRestResource method buildActionReportResult.

protected ActionReportResult buildActionReportResult(boolean showEntityValues) {
    RestActionReporter ar = new RestActionReporter();
    ar.setExtraProperties(new Properties());
    ConfigBean entity = (ConfigBean) getEntity();
    if (childID != null) {
        ar.setActionDescription(childID);
    } else if (childModel != null) {
        ar.setActionDescription(childModel.getTagName());
    }
    if (showEntityValues && entity != null) {
        ar.getExtraProperties().put("entity", getAttributes(entity));
    }
    OptionsResult optionsResult = new OptionsResult(Util.getResourceName(uriInfo));
    Map<String, MethodMetaData> mmd = getMethodMetaData();
    optionsResult.putMethodMetaData("GET", mmd.get("GET"));
    optionsResult.putMethodMetaData("POST", mmd.get("POST"));
    optionsResult.putMethodMetaData("DELETE", mmd.get("DELETE"));
    ResourceUtil.addMethodMetaData(ar, mmd);
    if (entity != null) {
        ar.getExtraProperties().put("childResources", ResourceUtil.getResourceLinks(entity, uriInfo, ResourceUtil.canShowDeprecatedItems(locatorBridge.getRemoteLocator())));
    }
    ar.getExtraProperties().put("commands", ResourceUtil.getCommandLinks(getCommandResourcesPaths()));
    return new ActionReportResult(ar, entity, optionsResult);
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) ConfigBean(org.jvnet.hk2.config.ConfigBean) MethodMetaData(org.glassfish.admin.rest.provider.MethodMetaData) Properties(java.util.Properties) OptionsResult(org.glassfish.admin.rest.results.OptionsResult)

Example 7 with ConfigBean

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

the class ActionReportResultHtmlProvider method getContent.

@Override
public String getContent(ActionReportResult proxy) {
    RestActionReporter ar = (RestActionReporter) proxy.getActionReport();
    StringBuilder result = new StringBuilder(ProviderUtil.getHtmlHeader(getBaseUri()));
    final String message = ResourceUtil.encodeString(ar.getCombinedMessage());
    if (!message.isEmpty()) {
        result.append("<h3>").append(message).append(HEADING_END);
    }
    if (proxy.isError()) {
        result.append("<h2>").append(ar.getActionDescription()).append(" Error:</h2>").append(proxy.getErrorMessage());
    } else {
        final Map<String, String> childResources = (Map<String, String>) ar.getExtraProperties().get("childResources");
        final List<Map<String, String>> commands = (List<Map<String, String>>) ar.getExtraProperties().get("commands");
        final MethodMetaData postMetaData = proxy.getMetaData().getMethodMetaData("POST");
        final MethodMetaData deleteMetaData = proxy.getMetaData().getMethodMetaData(DELETE);
        final MethodMetaData getMetaData = proxy.getMetaData().getMethodMetaData("GET");
        final ConfigBean entity = proxy.getEntity();
        if ((proxy.getCommandDisplayName() != null) && (getMetaData != null)) {
            // for commands, we want the output of the command before the form
            if (entity == null) {
                // show extra properties only for non entity pages
                result.append(processReport(ar));
            }
        }
        if ((postMetaData != null) && (entity == null)) {
            String postCommand = getHtmlRespresentationsForCommand(postMetaData, "POST", (proxy.getCommandDisplayName() == null) ? "Create" : proxy.getCommandDisplayName(), uriInfo.get());
            result.append(getHtmlForComponent(postCommand, "Create " + ar.getActionDescription(), ""));
        }
        if ((deleteMetaData != null) && (entity == null)) {
            String deleteCommand = getHtmlRespresentationsForCommand(deleteMetaData, DELETE, (proxy.getCommandDisplayName() == null) ? "Delete" : proxy.getCommandDisplayName(), uriInfo.get());
            result.append(getHtmlForComponent(deleteCommand, "Delete " + ar.getActionDescription(), ""));
        }
        if ((getMetaData != null) && (entity == null) && (proxy.getCommandDisplayName() != null)) {
            String getCommand = getHtmlRespresentationsForCommand(getMetaData, "GET", (proxy.getCommandDisplayName() == null) ? "Get" : proxy.getCommandDisplayName(), uriInfo.get());
            result.append(getHtmlForComponent(getCommand, "Get " + ar.getActionDescription(), ""));
        }
        if (entity != null) {
            String attributes = ProviderUtil.getHtmlRepresentationForAttributes(proxy.getEntity(), uriInfo.get());
            result.append(ProviderUtil.getHtmlForComponent(attributes, ar.getActionDescription() + " Attributes", ""));
            String deleteCommand = ProviderUtil.getHtmlRespresentationsForCommand(proxy.getMetaData().getMethodMetaData(DELETE), DELETE, (proxy.getCommandDisplayName() == null) ? "Delete" : proxy.getCommandDisplayName(), uriInfo.get());
            result.append(ProviderUtil.getHtmlForComponent(deleteCommand, "Delete " + entity.model.getTagName(), ""));
        } else if (proxy.getLeafContent() != null) {
            // it is a single leaf @Element
            String content = "<form action=\"" + uriInfo.get().getAbsolutePath().toString() + "\" method=\"post\">" + "<dl><dt>" + "<label for=\"" + proxy.getLeafContent().name + "\">" + proxy.getLeafContent().name + ":&nbsp;</label>" + "</dt><dd>" + "<input name=\"" + proxy.getLeafContent().name + "\" value =\"" + proxy.getLeafContent().value + "\" type=\"text\" >" + "</dd><dt class=\"button\"></dt><dd class=\"button\"><input value=\"Update\" type=\"submit\"></dd></dl>" + "</form><br><hr class=\"separator\"/";
            result.append(content);
        } else {
            // This is a monitoring result!!!
            final Map vals = (Map) ar.getExtraProperties().get("entity");
            if ((vals != null) && (!vals.isEmpty())) {
                result.append("<ul>");
                for (Map.Entry entry : (Set<Map.Entry>) vals.entrySet()) {
                    Object object = entry.getValue();
                    if (object == null) {
                    // do nothing
                    } else if (object instanceof Collection) {
                        if (!((Collection) object).isEmpty()) {
                            Collection c = ((Collection) object);
                            Iterator i = c.iterator();
                            result.append("<li>").append(entry.getKey());
                            result.append("<ul>");
                            while (i.hasNext()) {
                                result.append("<li>").append(getHtmlRepresentation(i.next())).append(LIST_ITEM_END);
                            }
                            result.append(LIST_END);
                            result.append(LIST_ITEM_END);
                        }
                    } else if (object instanceof Map) {
                        if (!((Map) object).isEmpty()) {
                            Map m = (Map) object;
                            if (vals.size() != 1) {
                                // add a link if more than 1 child
                                result.append("<li>").append(ANCHOR_OPEN).append(uriInfo.get().getAbsolutePath().toString()).append("/").append(entry.getKey()).append("\">").append(entry.getKey()).append("</a>");
                            } else {
                                result.append("<li>").append(entry.getKey());
                            }
                            result.append("<ul>");
                            for (Map.Entry anEntry : (Set<Map.Entry>) m.entrySet()) {
                                final String htmlRepresentation = getHtmlRepresentation(anEntry.getValue());
                                if (htmlRepresentation != null) {
                                    result.append("<li>").append(anEntry.getKey()).append(" : ").append(htmlRepresentation).append(LIST_ITEM_END);
                                }
                            }
                            result.append(LIST_END);
                            result.append(LIST_ITEM_END);
                        }
                    } else {
                        result.append("<li>").append(entry.getKey()).append(" : ").append(object.toString()).append(LIST_ITEM_END);
                    }
                }
                result.append(LIST_END);
            } else {
                // no values to show... give an hint
                if ((childResources == null) || (childResources.isEmpty())) {
                    if ((uriInfo != null) && (uriInfo.get().getPath().equalsIgnoreCase("domain"))) {
                        result.append(getHint(uriInfo.get(), MediaType.TEXT_HTML));
                    }
                }
            }
        }
        if ((childResources != null) && (!childResources.isEmpty())) {
            String childResourceLinks = getResourcesLinks(childResources);
            result.append(ProviderUtil.getHtmlForComponent(childResourceLinks, "Child Resources", ""));
        }
        if ((commands != null) && (!commands.isEmpty())) {
            String commandLinks = getCommandLinks(commands);
            result.append(ProviderUtil.getHtmlForComponent(commandLinks, "Commands", ""));
        }
    }
    return result.append("</div></body></html>").toString();
}
Also used : ConfigBean(org.jvnet.hk2.config.ConfigBean) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) Iterator(java.util.Iterator) Collection(java.util.Collection) List(java.util.List) Map(java.util.Map)

Example 8 with ConfigBean

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

the class SetCommand method applyToNodes.

private boolean applyToNodes(AdminCommandContext context, SetOperation op, String targetName, String prefix, String pattern, List<Map.Entry> mNodes) {
    final String value = op.value;
    final boolean isProperty = op.isProperty;
    final String attrName = op.isProperty ? "value" : op.attrName;
    final boolean delProperty = isProperty && (value == null || value.isEmpty());
    final String target = op.target;
    Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
    Map<String, String> attrChanges = new HashMap<String, String>();
    if (!delProperty) {
        attrChanges.put(attrName, value);
    }
    for (Map.Entry<Dom, String> node : mNodes) {
        final Dom targetNode = node.getKey();
        for (String name : targetNode.model.getAttributeNames()) {
            String finalDottedName = node.getValue() + "." + name;
            if (matches(finalDottedName, pattern)) {
                if (attrName.equals(name) || attrName.replace('_', '-').equals(name.replace('_', '-'))) {
                    if (isDeprecatedAttr(targetNode, name)) {
                        warning(context, localStrings.getLocalString("admin.set.deprecated", "Warning: The attribute {0} is deprecated.", finalDottedName));
                    }
                    if (!isProperty) {
                        targetName = prefix + finalDottedName;
                        if (value != null && value.length() > 0) {
                            attrChanges.put(name, value);
                        } else {
                            attrChanges.put(name, null);
                        }
                    } else {
                        targetName = prefix + node.getValue();
                    }
                    if (delProperty) {
                        // delete property element
                        String str = node.getValue();
                        if (trueLastIndexOf(str, '.') != -1) {
                            str = str.substring(trueLastIndexOf(str, '.') + 1);
                        }
                        if (str != null) {
                            return deleteProperty(context, op, targetName, targetNode);
                        }
                    } else {
                        changes.put((ConfigBean) node.getKey(), attrChanges);
                    }
                }
            }
        }
        for (String name : targetNode.model.getLeafElementNames()) {
            String finalDottedName = node.getValue() + "." + name;
            if (matches(finalDottedName, pattern)) {
                if (attrName.equals(name) || attrName.replace('_', '-').equals(name.replace('_', '-'))) {
                    if (isDeprecatedAttr(targetNode, name)) {
                        warning(context, localStrings.getLocalString("admin.set.elementdeprecated", "Warning: The element {0} is deprecated.", finalDottedName));
                    }
                    return setAttribute(context, op, targetName, (ConfigBean) targetNode, name);
                }
            }
        }
    }
    if (!changes.isEmpty()) {
        try {
            config.apply(changes);
            success(context, targetName, value);
            runLegacyChecks(context);
        } catch (TransactionFailure transactionFailure) {
            // fail(context, "Could not change the attributes: " +
            // transactionFailure.getMessage(), transactionFailure);
            fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
            return false;
        }
    } else {
        fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
        return false;
    }
    if (targetService.isThisDAS() && !replicateSetCommand(context, target, value)) {
        return false;
    }
    return true;
}
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)

Example 9 with ConfigBean

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

the class ReferenceConstrainClusterTest method clusterServerRefValid.

@Test
public void clusterServerRefValid() throws TransactionFailure {
    Cluster cluster = habitat.getService(Cluster.class, "clusterA");
    assertNotNull(cluster);
    ServerRef sref = cluster.getServerRef().get(0);
    ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(sref);
    Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
    Map<String, String> configChanges = new HashMap<String, String>();
    configChanges.put("ref", "server");
    changes.put(serverConfig, configChanges);
    try {
        ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
        cs.apply(changes);
    } catch (TransactionFailure tf) {
        fail("Can not reach this point");
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigSupport(org.jvnet.hk2.config.ConfigSupport) HashMap(java.util.HashMap) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ConfigBean(org.jvnet.hk2.config.ConfigBean) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef) HashMap(java.util.HashMap) Map(java.util.Map) ConfigApiTest(com.sun.enterprise.configapi.tests.ConfigApiTest) Test(org.junit.Test)

Example 10 with ConfigBean

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

the class DirectCreationTest method doTest.

public void doTest() throws TransactionFailure {
    AdminService service = habitat.getService(AdminService.class);
    ConfigBean serviceBean = (ConfigBean) ConfigBean.unwrap(service);
    Class<?>[] subTypes = null;
    try {
        subTypes = ConfigSupport.getSubElementsTypes(serviceBean);
    } catch (ClassNotFoundException e) {
        // To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    ConfigSupport support = getBaseServiceLocator().getService(ConfigSupport.class);
    assertNotNull("ConfigSupport not found", support);
    for (Class<?> subType : subTypes) {
        // TODO:  JL force compilation error to mark this probably edit point for grizzly config
        if (subType.getName().endsWith("DasConfig")) {
            Map<String, String> configChanges = new HashMap<String, String>();
            configChanges.put("dynamic-reload-enabled", "true");
            configChanges.put("autodeploy-dir", "funky-dir");
            support.createAndSet(serviceBean, (Class<? extends ConfigBeanProxy>) subType, configChanges);
            break;
        }
    }
    support.createAndSet(serviceBean, DasConfig.class, (List) null);
    List<AttributeChanges> profilerChanges = new ArrayList<AttributeChanges>();
    String[] values = { "-Xmx512m", "-RFtrq", "-Xmw24" };
    ConfigSupport.MultipleAttributeChanges multipleChanges = new ConfigSupport.MultipleAttributeChanges("jvm-options", values);
    String[] values1 = { "profile" };
    ConfigSupport.MultipleAttributeChanges multipleChanges1 = new ConfigSupport.MultipleAttributeChanges("name", values1);
    profilerChanges.add(multipleChanges);
    profilerChanges.add(multipleChanges1);
    support.createAndSet((ConfigBean) ConfigBean.unwrap(habitat.<JavaConfig>getService(JavaConfig.class)), Profiler.class, profilerChanges);
}
Also used : AdminService(com.sun.enterprise.config.serverbeans.AdminService) ConfigSupport(org.jvnet.hk2.config.ConfigSupport) AttributeChanges(org.jvnet.hk2.config.AttributeChanges) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConfigBean(org.jvnet.hk2.config.ConfigBean) JavaConfig(com.sun.enterprise.config.serverbeans.JavaConfig)

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