use of com.sun.enterprise.config.modularity.customization.ConfigCustomizationToken in project Payara by payara.
the class ModuleXMLConfigurationFileParser method parseServiceConfiguration.
public List<ConfigBeanDefaultValue> parseServiceConfiguration(InputStream xmlDocumentStream) throws XMLStreamException {
List<ConfigBeanDefaultValue> configBeans = new ArrayList<ConfigBeanDefaultValue>();
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
XMLEventReader eventReader = inputFactory.createXMLEventReader(xmlDocumentStream);
ConfigBeanDefaultValue configValue = null;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
// If we have a item element we create a new item
if (startElement.getName().getLocalPart().equalsIgnoreCase(CONFIG_BUNDLE)) {
configValue = new ConfigBeanDefaultValue();
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
if (attribute.getName().toString().equals(LOCATION)) {
configValue.setLocation(attribute.getValue());
} else if (attribute.getName().toString().equals(REPLACE_IF_EXISTS)) {
configValue.setReplaceCurrentIfExists(Boolean.parseBoolean(attribute.getValue()));
}
}
// attributes
continue;
}
if (startElement.getName().getLocalPart().equalsIgnoreCase(CUSTOMIZATION_TOKEN)) {
ConfigCustomizationToken token;
String value = null;
String description = null;
String name = null;
String title = null;
String validationExpression = null;
ConfigCustomizationToken.CustomizationType type = ConfigCustomizationToken.CustomizationType.STRING;
TokenTypeDetails tokenTypeDetails = null;
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
switch(attribute.getName().toString()) {
case DEFAULT_VALUE:
value = attribute.getValue();
break;
case DESCRIPTION:
description = getLocalizedValue(attribute.getValue());
break;
case NAME:
name = attribute.getValue();
break;
case TITLE:
title = getLocalizedValue(attribute.getValue());
break;
case VALIDATION_EXPRESSION:
validationExpression = getLocalizedValue(attribute.getValue());
break;
default:
break;
}
}
// attributes
event = eventReader.nextEvent();
while (!event.isStartElement() && !event.isEndElement()) {
event = eventReader.nextEvent();
}
if (event.isStartElement()) {
startElement = event.asStartElement();
// If we have a item element we create a new item
if (startElement.getName().getLocalPart().equalsIgnoreCase(FILE)) {
type = ConfigCustomizationToken.CustomizationType.FILE;
String tokVal = startElement.getAttributeByName(QName.valueOf(MUST_EXIST)).getValue();
FileTypeDetails.FileExistCondition cond = FileTypeDetails.FileExistCondition.NO_OP;
if (tokVal.equalsIgnoreCase("true")) {
cond = FileTypeDetails.FileExistCondition.MUST_EXIST;
} else if (tokVal.equalsIgnoreCase("false")) {
cond = FileTypeDetails.FileExistCondition.MUST_NOT_EXIST;
}
tokenTypeDetails = new FileTypeDetails(cond);
} else if (startElement.getName().getLocalPart().equalsIgnoreCase(PORT)) {
type = ConfigCustomizationToken.CustomizationType.PORT;
tokenTypeDetails = new PortTypeDetails(startElement.getAttributeByName(QName.valueOf(BASE_OFFSET)).getValue());
}
}
token = new ConfigCustomizationToken(name, title, description, value, validationExpression, tokenTypeDetails, type);
// TODO check that ConfigValue is not null
configValue.addCustomizationToken(token);
continue;
}
if (startElement.getName().getLocalPart().equalsIgnoreCase(CONFIGURATION_ELEMENT)) {
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
if (attribute.getName().toString().equals(CONFIG_BEAN_CLASS_NAME)) {
configValue.setConfigBeanClassName(attribute.getValue());
}
}
// attributes
event = eventReader.nextEvent();
if (event.isCharacters()) {
String str = event.asCharacters().getData();
configValue.setXmlConfiguration(str);
}
continue;
}
}
// isStartElement
if (event.isEndElement()) {
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart().equalsIgnoreCase(CONFIG_BUNDLE)) {
configBeans.add(configValue);
}
}
}
// eventReader
return configBeans;
}
use of com.sun.enterprise.config.modularity.customization.ConfigCustomizationToken in project Payara by payara.
the class BasicModularityTest method moduleConfigurationXmlParserTest.
@Test
public void moduleConfigurationXmlParserTest() {
List<com.sun.enterprise.config.modularity.customization.ConfigBeanDefaultValue> values = configModularityUtils.getDefaultConfigurations(SimpleExtensionTypeOne.class, "admin-");
assertEquals("Incorrect number of config bean configuration read ", 2, values.size());
ConfigCustomizationToken token = configModularityUtils.getDefaultConfigurations(SimpleExtensionTypeOne.class, "embedded-").get(0).getCustomizationTokens().get(0);
assertEquals("Customization Token reading broken ", "CUSTOM_TOKEN", token.getName());
assertEquals("Customization Token reading broken ", "token-default-value", token.getValue());
}
use of com.sun.enterprise.config.modularity.customization.ConfigCustomizationToken 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;
}
Aggregations