Search in sources :

Example 1 with StringSubstitutionException

use of com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException in project Payara by payara.

the class StringSubstitutionEngine method substituteGroups.

@Override
public void substituteGroups(List<String> groups) throws StringSubstitutionException {
    if (!isValid(groups)) {
        throw new StringSubstitutionException(_strings.get("missingGroupIdentifiers"));
    }
    for (String groupId : groups) {
        Group group = findGroupById(groupId);
        if (group == null) {
            _logger.log(Level.WARNING, SLogger.MISSING_GROUP, groupId);
            continue;
        }
        doSubstitution(group);
    }
}
Also used : Group(com.sun.enterprise.admin.servermgmt.xml.stringsubs.Group) StringSubstitutionException(com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException)

Example 2 with StringSubstitutionException

use of com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException in project Payara by payara.

the class StringSubstitutionEngine method doSubstitution.

/**
 * Perform's string substitution for a given group.
 *
 * @param groups Groups for which the string substitution
 *  has to be performed.
 * @throws StringSubstitutionException If any error occurs during
 *  substitution.
 */
private void doSubstitution(Group group) throws StringSubstitutionException {
    List<? extends FileEntry> fileList = group.getFileEntry();
    List<? extends Archive> archiveList = group.getArchive();
    if (!isValid(fileList) && !isValid(archiveList)) {
        if (_logger.isLoggable(Level.FINER)) {
            _logger.log(Level.FINER, _strings.get("noSubstitutableGroupEntry", group.getId()));
        }
        return;
    }
    List<? extends ChangePairRef> refList = group.getChangePairRef();
    if (!isValid(refList)) {
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, _strings.get("noChangePairForGroup", group.getId()));
        }
        return;
    }
    String groupMode = null;
    ModeType modeType = group.getMode();
    if (modeType != null) {
        groupMode = modeType.value();
    }
    buildChangePairsMap();
    Map<String, String> substitutionMap = new HashMap<String, String>();
    for (ChangePairRef ref : refList) {
        String name = ref.getName();
        String localMode = ref.getMode();
        // then inherit the mode of the group
        if (localMode == null || localMode.length() == 0) {
            localMode = groupMode;
        }
        Pair pair = _changePairsMap.get(name);
        if (pair == null) {
            _logger.log(Level.INFO, SLogger.MISSING_CHANGE_PAIR, new Object[] { name, group.getId() });
            continue;
        }
        String beforeString = pair.getBefore();
        String afterString = pair.getAfter();
        if (localMode == null || localMode.length() == 0) {
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST, _strings.get("noModeValue", group.getId()));
            }
        } else {
            try {
                afterString = ModeProcessor.processModeType(ModeType.fromValue(localMode), afterString);
            } catch (Exception e) {
                _logger.log(Level.WARNING, SLogger.INVALID_MODE_TYPE, localMode);
            }
        }
        substitutionMap.put(beforeString, afterString);
    }
    SubstitutionAlgorithm algorithm = new SubstitutionAlgorithmFactory().getAlgorithm(substitutionMap);
    for (FileEntry fileEntry : fileList) {
        fileEntry.setName(_attrPreprocessor.substitutePath(fileEntry.getName()));
        List<? extends Substitutable> substituables = _substitutableFactory.getFileEntrySubstituables(fileEntry);
        for (Substitutable substituable : substituables) {
            algorithm.substitute(substituable);
            substituable.finish();
        }
    }
    for (Archive archive : archiveList) {
        if (archive == null || archive.getName().isEmpty()) {
            continue;
        }
        try {
            archive.setName(_attrPreprocessor.substitutePath(archive.getName()));
            List<? extends Substitutable> substituables = _substitutableFactory.getArchiveEntrySubstitutable(archive);
            if (!isValid(substituables)) {
                continue;
            }
            for (Substitutable substituable : substituables) {
                algorithm.substitute(substituable);
                substituable.finish();
            }
        } catch (Exception e) {
            LogHelper.log(_logger, Level.WARNING, SLogger.ERR_ARCHIVE_SUBSTITUTION, e, archive.getName());
        }
    }
}
Also used : Archive(com.sun.enterprise.admin.servermgmt.xml.stringsubs.Archive) HashMap(java.util.HashMap) ChangePairRef(com.sun.enterprise.admin.servermgmt.xml.stringsubs.ChangePairRef) StringSubstitutionException(com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException) SubstitutionAlgorithm(com.sun.enterprise.admin.servermgmt.stringsubs.SubstitutionAlgorithm) ModeType(com.sun.enterprise.admin.servermgmt.xml.stringsubs.ModeType) Substitutable(com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable) FileEntry(com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry) ChangePair(com.sun.enterprise.admin.servermgmt.xml.stringsubs.ChangePair)

Example 3 with StringSubstitutionException

use of com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException in project Payara by payara.

the class StringSubstitutionParser method parse.

/**
 * Parse the configuration stream against the string-subs schema.
 *
 * @param configStream InputStream of stringsubs.xml file.
 * @return Parsed Object.
 * @throws StringSubstitutionException If any error occurs in parsing.
 */
@SuppressWarnings("rawtypes")
public static StringsubsDefinition parse(InputStream configStream) throws StringSubstitutionException {
    // If schema information is missing
    if (configStream == null) {
        throw new StringSubstitutionException(_strings.get("invalidStream"));
    }
    try {
        URL schemaUrl = StringSubstitutionParser.class.getClassLoader().getResource(DEFAULT_SCHEMA);
        JAXBContext context = JAXBContext.newInstance(StringsubsDefinition.class.getPackage().getName());
        Unmarshaller unmarshaller = context.createUnmarshaller();
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaUrl);
        unmarshaller.setSchema(schema);
        InputSource is = new InputSource(configStream);
        SAXSource source = new SAXSource(is);
        Object obj = unmarshaller.unmarshal(source);
        return obj instanceof JAXBElement ? (StringsubsDefinition) ((JAXBElement) obj).getValue() : (StringsubsDefinition) obj;
    } catch (SAXException se) {
        throw new StringSubstitutionException(_strings.get("failedToParse", DEFAULT_SCHEMA), se);
    } catch (JAXBException jaxbe) {
        throw new StringSubstitutionException(_strings.get("failedToParse", DEFAULT_SCHEMA), jaxbe);
    } finally {
        if (configStream != null) {
            try {
                configStream.close();
                configStream = null;
            } catch (IOException e) {
                if (_logger.isLoggable(Level.FINER)) {
                    _logger.log(Level.FINER, _strings.get("errorInClosingStream"));
                }
            }
        }
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) StringSubstitutionException(com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) URL(java.net.URL) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 4 with StringSubstitutionException

use of com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException in project Payara by payara.

the class StringReplacementAlgo method substitute.

@Override
public void substitute(Substitutable resolver) throws StringSubstitutionException {
    Reader reader = resolver.getReader();
    Writer writer = resolver.getWriter();
    try {
        String inputLine = null;
        char[] cbuffer = new char[8192];
        int count = 0;
        while ((count = reader.read(cbuffer)) > 0) {
            inputLine = new String(cbuffer, 0, count);
            for (String key : _substitutionMap.keySet()) {
                inputLine = inputLine.replace(key, _substitutionMap.get(key));
            }
            writer.write(inputLine);
        }
        writer.flush();
    } catch (IOException e) {
        throw new StringSubstitutionException("Error occurred while performing the String substitution", e);
    }
}
Also used : StringSubstitutionException(com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException) Reader(java.io.Reader) IOException(java.io.IOException) Writer(java.io.Writer)

Example 5 with StringSubstitutionException

use of com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException in project Payara by payara.

the class StringSubstitutionEngine method substituteComponents.

@Override
public void substituteComponents(List<String> components) throws StringSubstitutionException {
    if (!isValid(components)) {
        throw new StringSubstitutionException(_strings.get("missingComponentIdentifiers"));
    }
    for (String componentId : components) {
        Component component = findComponentById(componentId);
        if (component == null) {
            _logger.log(Level.INFO, SLogger.MISSING_COMPONENT, componentId);
            continue;
        }
        doSubstitution(component);
    }
}
Also used : StringSubstitutionException(com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException) Component(com.sun.enterprise.admin.servermgmt.xml.stringsubs.Component)

Aggregations

StringSubstitutionException (com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException)7 IOException (java.io.IOException)3 Reader (java.io.Reader)2 Writer (java.io.Writer)2 Substitutable (com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable)1 SubstitutionAlgorithm (com.sun.enterprise.admin.servermgmt.stringsubs.SubstitutionAlgorithm)1 Archive (com.sun.enterprise.admin.servermgmt.xml.stringsubs.Archive)1 ChangePair (com.sun.enterprise.admin.servermgmt.xml.stringsubs.ChangePair)1 ChangePairRef (com.sun.enterprise.admin.servermgmt.xml.stringsubs.ChangePairRef)1 Component (com.sun.enterprise.admin.servermgmt.xml.stringsubs.Component)1 FileEntry (com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry)1 Group (com.sun.enterprise.admin.servermgmt.xml.stringsubs.Group)1 ModeType (com.sun.enterprise.admin.servermgmt.xml.stringsubs.ModeType)1 StringsubsDefinition (com.sun.enterprise.admin.servermgmt.xml.stringsubs.StringsubsDefinition)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBElement (javax.xml.bind.JAXBElement)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1