try {
reader = new FileReader(domainXMLFile);
char[] buf = new char[(int)domainXMLFile.length()];
count = reader.read(buf);
if (count < buf.length) {
throw new DomainException(_strings.get("loadingFailure", domainXMLFile.getAbsolutePath()));
}
loadedDomainXML = new String(buf);
} finally {
if (reader != null) {
reader.close();
}
}
// Check presence of token place-holder
if (loadedDomainXML.indexOf(CUSTOM_TOKEN_PLACE_HOLDER) != -1) {
Set<String> existingSysProps = new HashSet<String>();
Pattern sysPropTagPattern = Pattern.compile("<system-property.*?>");
Pattern nameAttributePattern = Pattern.compile("name=[\\\"](.*?)[\\\"]");
Matcher sysPropTagMatcher = sysPropTagPattern.matcher(loadedDomainXML);
while (sysPropTagMatcher.find()) {
String systemTag = sysPropTagMatcher.group();
Matcher nameTagMatcher = nameAttributePattern.matcher(systemTag);
while (nameTagMatcher.find()) {
String strs[] = nameTagMatcher.group().split("\"");
if (strs != null && strs.length == 2) {
existingSysProps.add(strs[1]);
}
}
}
Set<Integer> usedPorts = new HashSet<Integer>();
Properties domainProps = _domainConfig.getDomainProperties();
String portBase = (String)_domainConfig.get(DomainConfig.K_PORTBASE);
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));
int noOfTokens = customTokens.size();
String lineSeparator = System.getProperty("line.separator");
for (ConfigCustomizationToken token : customTokens) {
String key = token.getName();
// Check for valid custom token parameters.
if (isNullOrEmpty(key) || isNullOrEmpty(token.getValue()) || isNullOrEmpty(token.getDescription())) {
throw new IllegalArgumentException(_strings.get("invalidTokenParameters", key, token.getValue(), token.getDescription()));
}
// Skipping the token processing if token already exist in domain.xml
if (existingSysProps.contains(key)) {
_logger.log(Level.FINER, _strings.get("existingCustomToken", key));
continue;
}
switch (token.getCustomizationType()) {
case PORT :
Integer port = null;
if (domainProps.containsKey(token.getName())) {
port = Integer.valueOf(domainProps.getProperty(token.getName()));
if (!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.valueOf(domainProps.getProperty(token.getName())) + Integer.valueOf(portTypeDetails.getBaseOffset());
generatedTokens.put(PORTBASE_PLACE_HOLDER, SystemPropertyTagBuilder.buildSystemTag(
PORTBASE_PLACE_HOLDER, portBase));
} else {
port = Integer.valueOf(token.getValue());
}
while (!NetUtils.isPortFree(port) && !usedPorts.contains(port++));
}
usedPorts.add(port);
newlyCreatedSysProps.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;
}
}
newlyCreatedSysProps.append(SystemPropertyTagBuilder.buildSystemTag(token, path));
break;
case STRING:
break;
}
if (--noOfTokens > 1) {
newlyCreatedSysProps.append(lineSeparator);
}
}
generatedTokens.put(CUSTOM_TOKEN_PLACE_HOLDER, newlyCreatedSysProps.toString());
}
}
} catch (DomainException de) {
throw de;
} catch (Exception ex) {
throw new DomainException(ex);
}
return generatedTokens;
}