Search in sources :

Example 11 with Element

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

the class GenericCrudCommand method elementName.

/**
 * Returns the element name used by the parent to store instances of the child
 *
 * @param document the dom document this configuration element lives in.
 * @param parent type of the parent
 * @param child type of the child
 * @return the element name holding child's instances in the parent
 * @throws ClassNotFoundException when subclasses cannot be loaded
 */
public static String elementName(DomDocument document, Class<?> parent, Class<?> child) throws ClassNotFoundException {
    ConfigModel cm = document.buildModel(parent);
    for (String elementName : cm.getElementNames()) {
        ConfigModel.Property prop = cm.getElement(elementName);
        if (prop instanceof ConfigModel.Node) {
            ConfigModel childCM = ((ConfigModel.Node) prop).getModel();
            String childTypeName = childCM.targetTypeName;
            if (childTypeName.equals(child.getName())) {
                return childCM.getTagName();
            }
            // check the inheritance hierarchy
            List<ConfigModel> subChildrenModels = document.getAllModelsImplementing(childCM.classLoaderHolder.loadClass(childTypeName));
            if (subChildrenModels != null) {
                for (ConfigModel subChildModel : subChildrenModels) {
                    if (subChildModel.targetTypeName.equals(child.getName())) {
                        return subChildModel.getTagName();
                    }
                }
            }
        }
    }
    return null;
}
Also used : ConfigModel(org.jvnet.hk2.config.ConfigModel)

Example 12 with Element

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

the class PortUtils method checkInternalConsistency.

/**
 * Make sure all ports that are specified by the user make sense.
 * @param server The new Server element
 * @return null if all went OK.  Otherwise return a String with the error message.
 */
static void checkInternalConsistency(Server server) throws TransactionFailure {
    // Make sure all the system properties for ports have different numbers.
    List<SystemProperty> sysProps = server.getSystemProperty();
    Set<Integer> ports = new TreeSet<Integer>();
    for (SystemProperty sp : sysProps) {
        String name = sp.getName();
        if (PORTSLIST.contains(name)) {
            String val = sp.getValue();
            try {
                boolean wasAdded = ports.add(Integer.parseInt(val));
                if (// TODO unit test
                !wasAdded)
                    throw new TransactionFailure(Strings.get("PortUtils.duplicate_port", val, server.getName()));
            } catch (TransactionFailure tf) {
                // don't re-wrap the same Exception type!
                throw tf;
            } catch (Exception e) {
                // TODO unit test
                throw new TransactionFailure(Strings.get("PortUtils.non_int_port", val, server.getName()));
            }
        }
    }
    checkForLegalPorts(ports, server.getName());
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) SystemProperty(com.sun.enterprise.config.serverbeans.SystemProperty)

Example 13 with Element

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

the class TemplateListOfResource method getElementTypeByName.

public static Class<? extends ConfigBeanProxy> getElementTypeByName(Dom parentDom, String elementName) throws ClassNotFoundException {
    DomDocument document = parentDom.document;
    ConfigModel.Property a = parentDom.model.getElement(elementName);
    if (a != null) {
        if (a.isLeaf()) {
            // : I am not too sure, but that should be a String @Element
            return null;
        } else {
            ConfigModel childModel = ((ConfigModel.Node) a).getModel();
            return (Class<? extends ConfigBeanProxy>) childModel.classLoaderHolder.loadClass(childModel.targetTypeName);
        }
    }
    // global lookup
    ConfigModel model = document.getModelByElementName(elementName);
    if (model != null) {
        return (Class<? extends ConfigBeanProxy>) model.classLoaderHolder.loadClass(model.targetTypeName);
    }
    return null;
}
Also used : ConfigModel(org.jvnet.hk2.config.ConfigModel) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) DomDocument(org.jvnet.hk2.config.DomDocument)

Example 14 with Element

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

the class ApplicationLifecycle method prepareAppConfigChanges.

// prepare application config change for later registering
// in the domain.xml
@Override
public Transaction prepareAppConfigChanges(final DeploymentContext context) throws TransactionFailure {
    final Properties appProps = context.getAppProps();
    final DeployCommandParameters deployParams = context.getCommandParameters(DeployCommandParameters.class);
    Transaction t = new Transaction();
    try {
        // prepare the application element
        ConfigBean newBean = ((ConfigBean) ConfigBean.unwrap(applications)).allocate(Application.class);
        Application app = newBean.createProxy();
        Application app_w = t.enroll(app);
        setInitialAppAttributes(app_w, deployParams, appProps, context);
        context.addTransientAppMetaData(ServerTags.APPLICATION, app_w);
    } catch (TransactionFailure e) {
        t.rollback();
        throw e;
    } catch (Exception e) {
        t.rollback();
        throw new TransactionFailure(e.getMessage(), e);
    }
    return t;
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Transaction(org.jvnet.hk2.config.Transaction) ConfigBean(org.jvnet.hk2.config.ConfigBean) PropertyVetoException(java.beans.PropertyVetoException) RetryableException(org.jvnet.hk2.config.RetryableException) MultiException(org.glassfish.hk2.api.MultiException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) IOException(java.io.IOException)

Example 15 with Element

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

the class CreateProfilerTest method testExecuteSuccess.

/**
 * Test of execute method, of class CreateProfiler.
 * asadmin create-profiler --nativelibrarypath "myNativeLibraryPath"
 *          --enabled=true --classpath "myProfilerClasspath" testProfiler
 */
@Test
public void testExecuteSuccess() {
    // Set the options and operand to pass to the command
    parameters.set("classpath", "myProfilerClasspath");
    parameters.set("enabled", "true");
    parameters.set("nativelibrarypath", "myNativeLibraryPath");
    parameters.set("property", "a=x:b=y:c=z");
    parameters.set("DEFAULT", "testProfiler");
    // Call CommandRunnerImpl.doCommand(..) to execute the command
    cr.getCommandInvocation("create-profiler", context.getActionReport(), adminSubject()).parameters(parameters).execute(command);
    // Check the exit code is SUCCESS
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    // Check that the profiler is created
    boolean isCreated = false;
    int propertyCount = 0;
    Profiler profiler = javaConfig.getProfiler();
    if (profiler.getName().equals("testProfiler")) {
        assertEquals("myProfilerClasspath", profiler.getClasspath());
        assertEquals("true", profiler.getEnabled());
        assertEquals("myNativeLibraryPath", profiler.getNativeLibraryPath());
        List<Property> properties = profiler.getProperty();
        for (Property property : properties) {
            if (property.getName().equals("a"))
                assertEquals("x", property.getValue());
            if (property.getName().equals("b"))
                assertEquals("y", property.getValue());
            if (property.getName().equals("c"))
                assertEquals("z", property.getValue());
            propertyCount++;
        }
        isCreated = true;
        logger.fine("Profiler element myProfiler is created.");
    }
    assertTrue(isCreated);
    assertEquals(propertyCount, 3);
    // Check the exit code is SUCCESS
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    // Check the success message
    // assertEquals("Command create-profiler executed successfully.", context.getActionReport().getMessage());
    logger.fine("msg: " + context.getActionReport().getMessage());
}
Also used : Profiler(com.sun.enterprise.config.serverbeans.Profiler) Property(org.jvnet.hk2.config.types.Property) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Aggregations

Property (org.jvnet.hk2.config.types.Property)14 PropertyVetoException (java.beans.PropertyVetoException)13 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)13 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 List (java.util.List)4 TreeMap (java.util.TreeMap)4 ActionReport (org.glassfish.api.ActionReport)4 MultiException (org.glassfish.hk2.api.MultiException)4 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)4 ConfigModel (org.jvnet.hk2.config.ConfigModel)4 Dom (org.jvnet.hk2.config.Dom)4 Application (com.sun.enterprise.config.serverbeans.Application)3 Domain (com.sun.enterprise.config.serverbeans.Domain)3 Server (com.sun.enterprise.config.serverbeans.Server)3 SystemProperty (com.sun.enterprise.config.serverbeans.SystemProperty)3 File (java.io.File)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3