Search in sources :

Example 1 with Resource

use of org.glassfish.resources.api.Resource in project Payara by payara.

the class ResourceUtilities method resolveResourceDuplicatesConflictsWithinArchive.

/**
 * Resolves all duplicates and conflicts within an archive and returns a set
 * of resources that needs to be created for the archive being deployed. The
 * deployment backend would then use these set of resources to check for
 * conflicts with resources existing in domain.xml and then continue
 * with deployment.
 *
 * All resource duplicates within an archive found are flagged with a
 * WARNING and only one resource is added in the final <code>Resource</code>
 * <code>Set</code> returned.
 *
 * We currently do not handle any resource conflicts found within the archive
 * and the method throws an exception when this condition is detected.
 *
 * @param sunResList a list of <code>SunResourcesXML</code> corresponding to
 * sun-resources.xml found within an archive.
 *
 * @return a Set of <code>Resource</code>s that have been resolved of
 * duplicates and conflicts.
 *
 * @throws org.glassfish.resources.api.ResourceConflictException an exception is thrown when an archive is found to
 * have two or more resources that conflict with each other.
 */
public static Set<org.glassfish.resources.api.Resource> resolveResourceDuplicatesConflictsWithinArchive(List<org.glassfish.resources.admin.cli.SunResourcesXML> sunResList) throws ResourceConflictException {
    boolean conflictExist = false;
    StringBuilder conflictingResources = new StringBuilder();
    Set<org.glassfish.resources.api.Resource> resourceSet = new HashSet<org.glassfish.resources.api.Resource>();
    Iterator<org.glassfish.resources.admin.cli.SunResourcesXML> sunResourcesXMLIter = sunResList.iterator();
    while (sunResourcesXMLIter.hasNext()) {
        // get list of resources from one sun-resources.xml file
        org.glassfish.resources.admin.cli.SunResourcesXML sunResXML = sunResourcesXMLIter.next();
        List<org.glassfish.resources.api.Resource> resources = sunResXML.getResourcesList();
        Iterator<org.glassfish.resources.api.Resource> resourcesIter = resources.iterator();
        // for each resource mentioned
        while (resourcesIter.hasNext()) {
            org.glassfish.resources.api.Resource res = resourcesIter.next();
            Iterator<org.glassfish.resources.api.Resource> resSetIter = resourceSet.iterator();
            boolean addResource = true;
            // check if a duplicate has already been added
            while (resSetIter.hasNext()) {
                Resource existingRes = resSetIter.next();
                if (existingRes.equals(res)) {
                    // duplicate within an archive
                    addResource = false;
                    _logger.warning(localStrings.getString("duplicate.resource.sun.resource.xml", getIdToCompare(res), sunResXML.getXMLPath()));
                    break;
                }
                // resource being added
                if (existingRes.isAConflict(res)) {
                    // conflict within an archive
                    addResource = false;
                    conflictingResources.append("\n");
                    String message = localStrings.getString("conflict.resource.sun.resource.xml", getIdToCompare(res), sunResXML.getXMLPath());
                    conflictingResources.append(message);
                    _logger.warning(message);
                    if (_logger.isLoggable(Level.FINE))
                        logAttributes(res);
                }
            }
            if (addResource)
                resourceSet.add(res);
        }
    }
    if (conflictingResources.toString().length() > 0) {
        throw new ResourceConflictException(conflictingResources.toString());
    }
    return resourceSet;
}
Also used : Resource(org.glassfish.resources.api.Resource) Resource(org.glassfish.resources.api.Resource) ResourceConflictException(org.glassfish.resourcebase.resources.api.ResourceConflictException)

Example 2 with Resource

use of org.glassfish.resources.api.Resource in project Payara by payara.

the class ResourcesDeployer method processArchive.

private void processArchive(DeploymentContext dc) {
    try {
        ReadableArchive archive = dc.getSource();
        if (ResourceUtil.hasGlassfishResourcesXML(archive, locator) || ResourceUtil.hasPayaraResourcesXML(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 3 with Resource

use of org.glassfish.resources.api.Resource in project Payara by payara.

the class ResourcesDeployer method isEmbeddedResource.

private static boolean isEmbeddedResource(org.glassfish.resources.api.Resource resource, Collection<org.glassfish.resources.api.Resource> resources) {
    boolean result = false;
    if (isConnectorResource(resource)) {
        String attributeName = null;
        if (resource.getType().equals(ADMIN_OBJECT_RESOURCE)) {
            attributeName = RES_ADAPTER;
        } else if (resource.getType().equals(CONNECTOR_CONNECTION_POOL)) {
            attributeName = RES_ADAPTER_NAME;
        } else if (resource.getType().equals(CONNECTOR_RESOURCE)) {
            String poolName = (String) resource.getAttributes().get(POOL_NAME);
            if (poolName != null) {
                org.glassfish.resources.api.Resource poolResource = getPoolResource(poolName, resources);
                // point to poolResource
                resource = poolResource;
                attributeName = RES_ADAPTER_NAME;
            }
        } else /* else if(resource.getType().equals(org.glassfish.resources.api.Resource.RESOURCE_ADAPTER_CONFIG)){
                attributeName = ResourceConstants.RES_ADAPTER_NAME;
            } */
        if (resource.getType().equals(CONNECTOR_WORK_SECURITY_MAP)) {
            attributeName = RES_ADAPTER_NAME;
        }
        if (attributeName != null && resource != null) {
            result = isEmbeddedRar(resource, attributeName);
        }
    }
    return result;
}
Also used : Resource(org.glassfish.resources.api.Resource) org.glassfish.resources.api(org.glassfish.resources.api)

Example 4 with Resource

use of org.glassfish.resources.api.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) Module(com.sun.enterprise.config.serverbeans.Module)

Example 5 with Resource

use of org.glassfish.resources.api.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)

Aggregations

Resource (org.glassfish.resources.api.Resource)24 Resource (com.sun.enterprise.config.serverbeans.Resource)5 ResourceConflictException (org.glassfish.resourcebase.resources.api.ResourceConflictException)5 org.glassfish.resources.api (org.glassfish.resources.api)4 IOException (java.io.IOException)3 ResourceException (javax.resource.ResourceException)3 DeploymentException (org.glassfish.deployment.common.DeploymentException)3 File (java.io.File)2 ResourcesXMLParser (org.glassfish.resources.admin.cli.ResourcesXMLParser)2 Module (com.sun.enterprise.config.serverbeans.Module)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Properties (java.util.Properties)1 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)1 DeploymentProperties (org.glassfish.deployment.common.DeploymentProperties)1 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)1 ResourceInfo (org.glassfish.resourcebase.resources.api.ResourceInfo)1 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)1 ResourceManager (org.glassfish.resources.admin.cli.ResourceManager)1