Search in sources :

Example 1 with JBossServiceConstructorConfig

use of org.jboss.as.service.descriptor.JBossServiceConstructorConfig in project wildfly by wildfly.

the class ParsedServiceDeploymentProcessor method newInstance.

private static Object newInstance(final JBossServiceConfig serviceConfig, final List<ClassReflectionIndex> mBeanClassHierarchy, final ClassLoader deploymentClassLoader) throws DeploymentUnitProcessingException {
    // set TCCL so that the MBean instantiation happens in the deployment's classloader
    final ClassLoader oldTCCL = WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(deploymentClassLoader);
    try {
        final JBossServiceConstructorConfig constructorConfig = serviceConfig.getConstructorConfig();
        final int paramCount = constructorConfig != null ? constructorConfig.getArguments().length : 0;
        final Class<?>[] types = new Class<?>[paramCount];
        final Object[] params = new Object[paramCount];
        if (constructorConfig != null) {
            final Argument[] arguments = constructorConfig.getArguments();
            for (int i = 0; i < paramCount; i++) {
                final Argument argument = arguments[i];
                types[i] = ReflectionUtils.getClass(argument.getType(), deploymentClassLoader);
                params[i] = newValue(ReflectionUtils.getClass(argument.getType(), deploymentClassLoader), argument.getValue());
            }
        }
        final Constructor<?> constructor = mBeanClassHierarchy.get(0).getConstructor(types);
        if (constructor == null) {
            throw SarLogger.ROOT_LOGGER.defaultConstructorNotFound(mBeanClassHierarchy.get(0).getIndexedClass());
        }
        final Object mBeanInstance = ReflectionUtils.newInstance(constructor, params);
        return mBeanInstance;
    } finally {
        // switch back the TCCL
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTCCL);
    }
}
Also used : JBossServiceConstructorConfig(org.jboss.as.service.descriptor.JBossServiceConstructorConfig) Argument(org.jboss.as.service.descriptor.JBossServiceConstructorConfig.Argument)

Example 2 with JBossServiceConstructorConfig

use of org.jboss.as.service.descriptor.JBossServiceConstructorConfig in project wildfly by wildfly.

the class JBossServiceXmlDescriptorParserTestCase method testParse.

@Test
public void testParse() throws Exception {
    final File testXmlFile = getResourceFile(JBossServiceXmlDescriptorParserTestCase.class, "/test/serviceXmlDeployment.jar/META-INF/jboss-service.xml");
    final ParseResult<JBossServiceXmlDescriptor> jBossServiceXmlDescriptorParseResult = new ParseResult<JBossServiceXmlDescriptor>();
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream(testXmlFile);
        final XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream);
        xmlMapper.parseDocument(jBossServiceXmlDescriptorParseResult, reader);
    } finally {
        if (inputStream != null)
            inputStream.close();
    }
    final JBossServiceXmlDescriptor xmlDescriptor = jBossServiceXmlDescriptorParseResult.getResult();
    assertNotNull(xmlDescriptor);
    final List<JBossServiceConfig> serviceConfigs = xmlDescriptor.getServiceConfigs();
    assertEquals(3, serviceConfigs.size());
    for (JBossServiceConfig jBossServiceConfig : serviceConfigs) {
        assertEquals("org.jboss.as.service.LegacyService", jBossServiceConfig.getCode());
        if (jBossServiceConfig.getName().equals("jboss.test.service")) {
            final JBossServiceConstructorConfig constructorConfig = jBossServiceConfig.getConstructorConfig();
            assertNotNull(constructorConfig);
            final JBossServiceConstructorConfig.Argument[] arguments = constructorConfig.getArguments();
            assertEquals(1, arguments.length);
            assertEquals(String.class.getName(), arguments[0].getType());
            assertEquals("Test Value", arguments[0].getValue());
        } else if (jBossServiceConfig.getName().equals("jboss.test.service.second")) {
            assertNull(jBossServiceConfig.getConstructorConfig());
            final JBossServiceDependencyConfig[] dependencyConfigs = jBossServiceConfig.getDependencyConfigs();
            assertEquals(1, dependencyConfigs.length);
            assertEquals("jboss.test.service", dependencyConfigs[0].getDependencyName());
            assertEquals("other", dependencyConfigs[0].getOptionalAttributeName());
            final JBossServiceAttributeConfig[] attributeConfigs = jBossServiceConfig.getAttributeConfigs();
            assertEquals(1, attributeConfigs.length);
            assertEquals("somethingElse", attributeConfigs[0].getName());
            assertNull(attributeConfigs[0].getInject());
            final JBossServiceAttributeConfig.ValueFactory valueFactory = attributeConfigs[0].getValueFactory();
            assertNotNull(valueFactory);
            assertEquals("jboss.test.service", valueFactory.getBeanName());
            assertEquals("appendSomethingElse", valueFactory.getMethodName());
            final JBossServiceAttributeConfig.ValueFactoryParameter[] parameters = valueFactory.getParameters();
            assertEquals(1, parameters.length);
            assertEquals("java.lang.String", parameters[0].getType());
            assertEquals("more value", parameters[0].getValue());
        } else if (jBossServiceConfig.getName().equals("jboss.test.service.third")) {
            assertNull(jBossServiceConfig.getConstructorConfig());
            final JBossServiceAttributeConfig[] attributeConfigs = jBossServiceConfig.getAttributeConfigs();
            assertEquals(2, attributeConfigs.length);
            assertEquals("other", attributeConfigs[0].getName());
            assertNull(attributeConfigs[0].getValueFactory());
            final JBossServiceAttributeConfig.Inject inject = attributeConfigs[0].getInject();
            assertNotNull(inject);
            assertEquals("jboss.test.service.second", inject.getBeanName());
            assertEquals("other", inject.getPropertyName());
            assertEquals("somethingElse", attributeConfigs[1].getName());
            assertNull(attributeConfigs[1].getValueFactory());
            assertNull(attributeConfigs[1].getInject());
            assertEquals("Another test value", attributeConfigs[1].getValue());
        }
    }
}
Also used : JBossServiceXmlDescriptor(org.jboss.as.service.descriptor.JBossServiceXmlDescriptor) XMLStreamReader(javax.xml.stream.XMLStreamReader) JBossServiceConstructorConfig(org.jboss.as.service.descriptor.JBossServiceConstructorConfig) ParseResult(org.jboss.as.model.ParseResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JBossServiceAttributeConfig(org.jboss.as.service.descriptor.JBossServiceAttributeConfig) JBossServiceConfig(org.jboss.as.service.descriptor.JBossServiceConfig) FileInputStream(java.io.FileInputStream) File(java.io.File) Test(org.junit.Test)

Aggregations

JBossServiceConstructorConfig (org.jboss.as.service.descriptor.JBossServiceConstructorConfig)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 ParseResult (org.jboss.as.model.ParseResult)1 JBossServiceAttributeConfig (org.jboss.as.service.descriptor.JBossServiceAttributeConfig)1 JBossServiceConfig (org.jboss.as.service.descriptor.JBossServiceConfig)1 Argument (org.jboss.as.service.descriptor.JBossServiceConstructorConfig.Argument)1 JBossServiceXmlDescriptor (org.jboss.as.service.descriptor.JBossServiceXmlDescriptor)1 Test (org.junit.Test)1