Search in sources :

Example 1 with CustomizationTokensProvider

use of com.sun.enterprise.config.modularity.customization.CustomizationTokensProvider in project Payara by payara.

the class CustomTokenClient method getSubstitutableTokens.

/**
 * Gets the substitutable custom tokens.
 *
 * @return {@link Map} of substitutable tokens, or empty Map
 *   if no custom token found.
 * @throws DomainException If error occurred in retrieving the
 *   custom tokens.
 */
@SuppressWarnings("empty-statement")
public Map<String, String> getSubstitutableTokens() throws DomainException {
    CustomizationTokensProvider provider = CustomizationTokensProviderFactory.createCustomizationTokensProvider();
    Map<String, String> generatedTokens = new HashMap<String, String>();
    String lineSeparator = System.getProperty("line.separator");
    int noOfTokens = 0;
    try {
        List<ConfigCustomizationToken> customTokens = provider.getPresentConfigCustomizationTokens();
        if (!customTokens.isEmpty()) {
            StringBuilder generatedSysTags = new StringBuilder();
            // Check presence of token place-holder
            Set<Integer> usedPorts = new HashSet<Integer>();
            Properties domainProps = domainConfig.getDomainProperties();
            String portBase = (String) domainConfig.get(DomainConfig.K_PORTBASE);
            boolean checkPorts = (Boolean) domainConfig.getOrDefault(DomainConfig.K_VALIDATE_PORTS, Boolean.TRUE);
            Map<String, String> filePaths = new HashMap<String, String>(3, 1);
            filePaths.put(SystemPropertyConstants.INSTALL_ROOT_PROPERTY, System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY));
            filePaths.put(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY));
            filePaths.put(SystemPropertyConstants.JAVA_ROOT_PROPERTY, System.getProperty(SystemPropertyConstants.JAVA_ROOT_PROPERTY));
            noOfTokens = customTokens.size();
            for (ConfigCustomizationToken token : customTokens) {
                String name = token.getName();
                // Check for valid custom token parameters.
                if (isNullOrEmpty(name) || isNullOrEmpty(token.getValue()) || isNullOrEmpty(token.getDescription())) {
                    throw new IllegalArgumentException(_strings.get("invalidTokenParameters", name, token.getValue(), token.getDescription()));
                }
                switch(token.getCustomizationType()) {
                    case PORT:
                        Integer port = null;
                        if (domainProps.containsKey(name)) {
                            port = Integer.valueOf(domainProps.getProperty(token.getName()));
                            if (checkPorts && !NetUtils.isPortFree(port)) {
                                throw new DomainException(_strings.get("unavailablePort", port));
                            }
                        } else {
                            if (portBase != null && token.getTokenTypeDetails() instanceof PortTypeDetails) {
                                PortTypeDetails portTypeDetails = (PortTypeDetails) token.getTokenTypeDetails();
                                port = Integer.parseInt(portBase) + Integer.parseInt(portTypeDetails.getBaseOffset());
                                if (!generatedTokens.containsKey(PORTBASE_PLACE_HOLDER)) {
                                    // Adding a token to persist port base value as a system tag
                                    generatedTokens.put(PORTBASE_PLACE_HOLDER, SystemPropertyTagBuilder.buildSystemTag(PORTBASE_PLACE_HOLDER, portBase));
                                }
                            } else {
                                port = Integer.valueOf(token.getValue());
                            }
                            // Find next available unused port by incrementing the port value by 1
                            while (!NetUtils.isPortFree(port) && !usedPorts.contains(port++)) ;
                        }
                        usedPorts.add(port);
                        generatedSysTags.append(SystemPropertyTagBuilder.buildSystemTag(token, port.toString()));
                        break;
                    case FILE:
                        String path = token.getValue();
                        for (Map.Entry<String, String> entry : filePaths.entrySet()) {
                            if (path.contains(entry.getKey())) {
                                path = path.replace(entry.getKey(), entry.getValue());
                                break;
                            }
                        }
                        if (token.getTokenTypeDetails() instanceof FileTypeDetails) {
                            FileTypeDetails details = (FileTypeDetails) token.getTokenTypeDetails();
                            File file = new File(path);
                            switch(details.getExistCondition()) {
                                case MUST_EXIST:
                                    if (!file.exists()) {
                                        throw new DomainException(_strings.get("missingFile", file.getAbsolutePath()));
                                    }
                                    break;
                                case MUST_NOT_EXIST:
                                    if (file.exists()) {
                                        throw new DomainException(_strings.get("filePresenceNotDesired", file.getAbsolutePath()));
                                    }
                                    break;
                                case NO_OP:
                                    break;
                            }
                        }
                        generatedSysTags.append(SystemPropertyTagBuilder.buildSystemTag(token, path));
                        break;
                    case STRING:
                        generatedSysTags.append(SystemPropertyTagBuilder.buildSystemTag(token));
                        break;
                }
                if (--noOfTokens > 0) {
                    generatedSysTags.append(lineSeparator);
                }
            }
            String tags = generatedSysTags.toString();
            if (!isNullOrEmpty(tags)) {
                generatedTokens.put(CUSTOM_TOKEN_PLACE_HOLDER, tags);
            }
        }
        List<ConfigCustomizationToken> defaultTokens = provider.getPresentDefaultConfigCustomizationTokens();
        if (!defaultTokens.isEmpty()) {
            StringBuilder defaultSysTags = new StringBuilder();
            noOfTokens = defaultTokens.size();
            for (ConfigCustomizationToken token : defaultTokens) {
                defaultSysTags.append(SystemPropertyTagBuilder.buildSystemTag(token));
                if (--noOfTokens > 0) {
                    defaultSysTags.append(lineSeparator);
                }
            }
            generatedTokens.put(DEFAULT_TOKEN_PLACE_HOLDER, defaultSysTags.toString());
        }
    } catch (DomainException de) {
        throw de;
    } catch (Exception ex) {
        throw new DomainException(ex);
    }
    return generatedTokens;
}
Also used : FileTypeDetails(com.sun.enterprise.config.modularity.customization.FileTypeDetails) DomainException(com.sun.enterprise.admin.servermgmt.DomainException) HashMap(java.util.HashMap) CustomizationTokensProvider(com.sun.enterprise.config.modularity.customization.CustomizationTokensProvider) PortTypeDetails(com.sun.enterprise.config.modularity.customization.PortTypeDetails) Properties(java.util.Properties) DomainException(com.sun.enterprise.admin.servermgmt.DomainException) ConfigCustomizationToken(com.sun.enterprise.config.modularity.customization.ConfigCustomizationToken) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

DomainException (com.sun.enterprise.admin.servermgmt.DomainException)1 ConfigCustomizationToken (com.sun.enterprise.config.modularity.customization.ConfigCustomizationToken)1 CustomizationTokensProvider (com.sun.enterprise.config.modularity.customization.CustomizationTokensProvider)1 FileTypeDetails (com.sun.enterprise.config.modularity.customization.FileTypeDetails)1 PortTypeDetails (com.sun.enterprise.config.modularity.customization.PortTypeDetails)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Properties (java.util.Properties)1