Search in sources :

Example 6 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class ResourcesDeployer method createConfig.

private Collection<Resource> createConfig(Resources resources, Collection<org.glassfish.resources.api.Resource> resourcesToRegister, boolean embedded) throws ResourceException {
    List<Resource> resourceConfigs = new ArrayList<Resource>();
    for (org.glassfish.resources.api.Resource resource : resourcesToRegister) {
        final HashMap attrList = resource.getAttributes();
        final Properties props = resource.getProperties();
        String desc = resource.getDescription();
        if (desc != null) {
            attrList.put("description", desc);
        }
        try {
            final ResourceManager rm = resourceFactory.getResourceManager(resource);
            if (embedded && isEmbeddedResource(resource, resourcesToRegister)) {
                Resource configBeanResource = rm.createConfigBean(resources, attrList, props, false);
                resources.getResources().add(configBeanResource);
                resourceConfigs.add(configBeanResource);
            } else if (!embedded && !isEmbeddedResource(resource, resourcesToRegister)) {
                com.sun.enterprise.config.serverbeans.Resource configBeanResource = rm.createConfigBean(resources, attrList, props, true);
                resources.getResources().add(configBeanResource);
                resourceConfigs.add(configBeanResource);
            }
        } catch (Exception e) {
            throw new ResourceException(e);
        }
    }
    return resourceConfigs;
}
Also used : Resource(com.sun.enterprise.config.serverbeans.Resource) Resource(org.glassfish.resources.api.Resource) ResourceManager(org.glassfish.resources.admin.cli.ResourceManager) DeploymentProperties(org.glassfish.deployment.common.DeploymentProperties) ResourceException(javax.resource.ResourceException) IOException(java.io.IOException) DeploymentException(org.glassfish.deployment.common.DeploymentException) ResourceConflictException(org.glassfish.resourcebase.resources.api.ResourceConflictException) Resource(org.glassfish.resources.api.Resource) ResourceException(javax.resource.ResourceException) org.glassfish.resources.api(org.glassfish.resources.api) org.jvnet.hk2.config(org.jvnet.hk2.config)

Example 7 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class ResourcesDeployer method processArchive.

private void processArchive(DeploymentContext dc) {
    try {
        ReadableArchive archive = dc.getSource();
        if (ResourceUtil.hasResourcesXML(archive, locator)) {
            Map<String, Map<String, List>> appScopedResources = new HashMap<>();
            Map<String, List<String>> jndiNames = new HashMap<>();
            List<Map.Entry<String, String>> raNames = new ArrayList<>();
            Map<String, String> fileNames = new HashMap<>();
            String appName = getAppNameFromDeployCmdParams(dc);
            // using appName as it is possible that "deploy --name=APPNAME" will
            // be different than the archive name.
            retrieveAllResourcesXMLs(fileNames, archive, appName);
            for (Map.Entry<String, String> entry : fileNames.entrySet()) {
                String moduleName = entry.getKey();
                String fileName = entry.getValue();
                debug("Sun Resources XML : " + fileName);
                moduleName = org.glassfish.resourcebase.resources.util.ResourceUtil.getActualModuleNameWithExtension(moduleName);
                String scope;
                if (appName.equals(moduleName)) {
                    scope = JAVA_APP_SCOPE_PREFIX;
                } else {
                    scope = JAVA_MODULE_SCOPE_PREFIX;
                }
                File file = new File(fileName);
                ResourcesXMLParser parser = new ResourcesXMLParser(file, scope);
                validateResourcesXML(file, parser);
                List list = parser.getResourcesList();
                Map<String, List> resourcesList = new HashMap<>();
                List<String> jndiNamesList = new ArrayList<>();
                List<org.glassfish.resources.api.Resource> nonConnectorResources = ResourcesXMLParser.getNonConnectorResourcesList(list, false, true);
                resourcesList.put(NON_CONNECTOR_RESOURCES, nonConnectorResources);
                for (org.glassfish.resources.api.Resource resource : nonConnectorResources) {
                    String jndiName = extractJNDIName(resource);
                    if (hasRAName(resource)) {
                        raNames.add(new AbstractMap.SimpleEntry<>(extractRAName(resource), resource.getType()));
                    }
                    if (jndiName != null) {
                        jndiNamesList.add(jndiName);
                    }
                }
                List<org.glassfish.resources.api.Resource> connectorResources = ResourcesXMLParser.getConnectorResourcesList(list, false, true);
                resourcesList.put(CONNECTOR_RESOURCES, connectorResources);
                for (org.glassfish.resources.api.Resource resource : connectorResources) {
                    String jndiName = extractJNDIName(resource);
                    if (hasRAName(resource)) {
                        raNames.add(new AbstractMap.SimpleEntry<>(extractRAName(resource), resource.getType()));
                    }
                    if (jndiName != null) {
                        jndiNamesList.add(jndiName);
                    }
                }
                jndiNames.put(moduleName, jndiNamesList);
                appScopedResources.put(moduleName, resourcesList);
            }
            dc.addTransientAppMetaData(APP_SCOPED_RESOURCES_JNDI_NAMES, jndiNames);
            dc.addTransientAppMetaData(APP_SCOPED_RESOURCES_RA_NAMES, raNames);
            dc.addTransientAppMetaData(APP_SCOPED_RESOURCES_MAP, appScopedResources);
            ApplicationInfo appInfo = appRegistry.get(appName);
            if (appInfo != null) {
                Application app = dc.getTransientAppMetaData(ServerTags.APPLICATION, Application.class);
                appInfo.addTransientAppMetaData(ServerTags.APPLICATION, app);
            }
        }
    } catch (Exception e) {
        // in the event notification infrastructure
        throw new DeploymentException("Failue while processing glassfish-resources.xml(s) in the archive ", e);
    }
}
Also used : ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) org.glassfish.resources.api(org.glassfish.resources.api) ResourcesXMLParser(org.glassfish.resources.admin.cli.ResourcesXMLParser) Resource(com.sun.enterprise.config.serverbeans.Resource) Resource(org.glassfish.resources.api.Resource) ResourceException(javax.resource.ResourceException) IOException(java.io.IOException) DeploymentException(org.glassfish.deployment.common.DeploymentException) ResourceConflictException(org.glassfish.resourcebase.resources.api.ResourceConflictException) Resource(org.glassfish.resources.api.Resource) DeploymentException(org.glassfish.deployment.common.DeploymentException) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File)

Example 8 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class ResourcesDeployer method validatePreservedResources.

/**
 * Validates the old resource configuration against new archive's modules.
 * @param allResources all resources (app scoped, module scoped)
 * @param oldApp Old Application config
 * @param newApp New Applicatoin config
 * @throws org.glassfish.resourcebase.resources.api.ResourceConflictException when it is not possible to map any of the resource(s) to
 * new application/its modules
 */
private void validatePreservedResources(Map<String, Resources> allResources, Application oldApp, Application newApp) throws ResourceConflictException {
    String appName = newApp.getName();
    Resources appScopedResources = allResources.get(appName);
    if (appScopedResources != null) {
        for (Resource resource : appScopedResources.getResources()) {
            getResourceDeployer(resource).validatePreservedResource(oldApp, newApp, resource, appScopedResources);
        }
    }
    List<Module> newModules = newApp.getModule();
    for (Module newModule : newModules) {
        Module oldModule = oldApp.getModule(newModule.getName());
        if (oldModule != null) {
            Resources oldModuleResources = oldModule.getResources();
            if (oldModuleResources != null) {
                for (Resource resource : oldModuleResources.getResources()) {
                    getResourceDeployer(resource).validatePreservedResource(oldApp, newApp, resource, oldModuleResources);
                }
            }
        }
    // else its a new module in the archive being redeployed.
    }
}
Also used : Resource(com.sun.enterprise.config.serverbeans.Resource) Resource(org.glassfish.resources.api.Resource)

Example 9 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class CreateJavaMailResourceTest method testExecuteSuccess.

/**
 * Test of execute method, of class CreateJavaMailResource.
 * asadmin create-javamail-resource --mailuser=test --mailhost=localhost
 * --fromaddress=test@sun.com mail/MyMailSession
 */
@Test
public void testExecuteSuccess() {
    parameters.set("mailhost", "localhost");
    parameters.set("mailuser", "test");
    parameters.set("fromaddress", "test@sun.com");
    parameters.set("jndi_name", "mail/MyMailSession");
    org.glassfish.resources.javamail.admin.cli.CreateJavaMailResource command = habitat.getService(org.glassfish.resources.javamail.admin.cli.CreateJavaMailResource.class);
    assertTrue(command != null);
    cr.getCommandInvocation("create-javamail-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(command);
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    boolean isCreated = false;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof MailResource) {
            MailResource r = (MailResource) resource;
            if (r.getJndiName().equals("mail/MyMailSession")) {
                assertEquals("localhost", r.getHost());
                assertEquals("test", r.getUser());
                assertEquals("test@sun.com", r.getFrom());
                assertEquals("true", r.getEnabled());
                assertEquals("false", r.getDebug());
                assertEquals("imap", r.getStoreProtocol());
                assertEquals("com.sun.mail.imap.IMAPStore", r.getStoreProtocolClass());
                assertEquals("smtp", r.getTransportProtocol());
                assertEquals("com.sun.mail.smtp.SMTPTransport", r.getTransportProtocolClass());
                isCreated = true;
                logger.fine("MailResource config bean mail/MyMailSession is created.");
                break;
            }
        }
    }
    assertTrue(isCreated);
    logger.fine("msg: " + context.getActionReport().getMessage());
    Servers servers = habitat.getService(Servers.class);
    boolean isRefCreated = false;
    for (Server server : servers.getServer()) {
        if (server.getName().equals(SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME)) {
            for (ResourceRef ref : server.getResourceRef()) {
                if (ref.getRef().equals("mail/MyMailSession")) {
                    assertEquals("true", ref.getEnabled());
                    isRefCreated = true;
                    break;
                }
            }
        }
    }
    assertTrue(isRefCreated);
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) MailResource(org.glassfish.resources.javamail.config.MailResource) Resource(com.sun.enterprise.config.serverbeans.Resource) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) Servers(com.sun.enterprise.config.serverbeans.Servers) MailResource(org.glassfish.resources.javamail.config.MailResource) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 10 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class DeleteJavaMailResourceTest method testExecuteSuccessDefaultTarget.

/**
 * Test of execute method, of class DeleteJavaMailResource.
 * asadmin create-javamail-resource --mailuser=test --mailhost=localhost
 * --fromaddress=test@sun.com mail/MyMailSession
 * delete-javamail-resource mail/MyMailSession
 */
@Test
public void testExecuteSuccessDefaultTarget() {
    parameters.set("mailhost", "localhost");
    parameters.set("mailuser", "test");
    parameters.set("fromaddress", "test@sun.com");
    parameters.set("jndi_name", "mail/MyMailSession");
    org.glassfish.resources.javamail.admin.cli.CreateJavaMailResource createCommand = habitat.getService(org.glassfish.resources.javamail.admin.cli.CreateJavaMailResource.class);
    assertTrue(createCommand != null);
    cr.getCommandInvocation("create-javamail-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(createCommand);
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    parameters = new ParameterMap();
    parameters.set("jndi_name", "mail/MyMailSession");
    org.glassfish.resources.javamail.admin.cli.DeleteJavaMailResource deleteCommand = habitat.getService(org.glassfish.resources.javamail.admin.cli.DeleteJavaMailResource.class);
    assertTrue(deleteCommand != null);
    cr.getCommandInvocation("delete-javamail-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(deleteCommand);
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    boolean isDeleted = true;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof MailResource) {
            MailResource jr = (MailResource) resource;
            if (jr.getJndiName().equals("mail/MyMailSession")) {
                isDeleted = false;
                logger.fine("JavaMailResource config bean mail/MyMailSession is deleted.");
                break;
            }
        }
    }
    assertTrue(isDeleted);
    logger.fine("msg: " + context.getActionReport().getMessage());
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    Servers servers = habitat.getService(Servers.class);
    boolean isRefDeleted = true;
    for (Server server : servers.getServer()) {
        if (server.getName().equals(SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME)) {
            for (ResourceRef ref : server.getResourceRef()) {
                if (ref.getRef().equals("mail/MyMailSession")) {
                    isRefDeleted = false;
                    break;
                }
            }
        }
    }
    assertTrue(isRefDeleted);
}
Also used : MailResource(org.glassfish.resources.javamail.config.MailResource) Resource(com.sun.enterprise.config.serverbeans.Resource) ParameterMap(org.glassfish.api.admin.ParameterMap) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) MailResource(org.glassfish.resources.javamail.config.MailResource) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Aggregations

Resource (com.sun.enterprise.config.serverbeans.Resource)44 Resources (com.sun.enterprise.config.serverbeans.Resources)14 Test (org.junit.Test)14 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)13 ParameterMap (org.glassfish.api.admin.ParameterMap)10 BindableResource (com.sun.enterprise.config.serverbeans.BindableResource)9 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)9 Domain (com.sun.enterprise.config.serverbeans.Domain)6 ResourceRef (com.sun.enterprise.config.serverbeans.ResourceRef)6 JdbcResource (org.glassfish.jdbc.config.JdbcResource)6 MailResource (org.glassfish.resources.javamail.config.MailResource)6 ResourceException (javax.resource.ResourceException)5 AdminObjectResource (org.glassfish.connectors.config.AdminObjectResource)5 ConnectorResource (org.glassfish.connectors.config.ConnectorResource)5 Resource (org.glassfish.resources.api.Resource)5 CustomResource (org.glassfish.resources.config.CustomResource)5 SingleConfigCode (org.jvnet.hk2.config.SingleConfigCode)5 ResourcePool (com.sun.enterprise.config.serverbeans.ResourcePool)4 PropsFileActionReporter (com.sun.enterprise.v3.common.PropsFileActionReporter)4 AdminCommandContextImpl (org.glassfish.api.admin.AdminCommandContextImpl)4