Skip to content

Commit

Permalink
Issue eclipse-ee4j#23507 Several fixes
Browse files Browse the repository at this point in the history
- with these fixes tests passed also in Eclipse editor, not just on command line
- envProperties can be null (no asenv.conf used)
- RepositoryConfig - contains suspicious code, I'm not changing the behavior
  despite it looks like a bug until I will have tests for clusters
- StringSubstitutionEngine - inputStream field was unused except constructor
- StringSubstitutionParser - javadoc now mentions that stream is closed after
  parsing
  • Loading branch information
dmatej committed Aug 1, 2021
1 parent 782ef00 commit e92b6ac
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2018-2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -81,7 +81,9 @@ public DomainConfig(String domainName, String domainRoot) throws DomainException
// net to get fully qualified host, not just hostname
ASenvPropertyReader pr = new ASenvPropertyReader();
Map<String, String> envProperties = pr.getProps();
put(K_HOST_NAME, envProperties.get(SystemPropertyConstants.HOST_NAME_PROPERTY));
if (envProperties != null) {
put(K_HOST_NAME, envProperties.get(SystemPropertyConstants.HOST_NAME_PROPERTY));
}
} catch (Exception ex) {
throw new DomainException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2018-2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -31,43 +31,53 @@
import com.sun.enterprise.universal.glassfish.ASenvPropertyReader;

/**
* This class represents a repository configuration. A repository can be either a domain, a node agent, or a server
* instance. Configuration specific to each (DomainConfig, AgentConfig, InstanceConfig) is derived from this class. A
* repository config consists of the following attributes:
*
* 1)repositoryName -- domain or node agent name (e.g. domain1 or agent1)
*
* 2)repositoryRoot -- the parent directory of the repository (e.g. $installDir/domains or $installDir/agents)
*
* 3)instanceName -- the optional server instance name (e.g. server1)
*
* 4)configurationName -- the optional configuration name of the server instance (e.g. default-config).
*
* Using (repositoryName, repositoryRoot, instanceName, configurationName) syntax. Here are the following permutations:
*
* 1)For a domain: (domainRootDirectory, domainName, null, null) e.g. ("/sun/appserver/domains", "domain1", null, null)
*
* 2)For a node agent: (agentRootDirectory, agentName, "agent", null) e.g ("/sun/appserver/agents", "agent1", "agent",
* This class represents a repository configuration. A repository can be either a domain, a node
* agent, or a server
* instance. Configuration specific to each (DomainConfig, AgentConfig, InstanceConfig) is derived
* from this class.
*
* A repository config consists of the following attributes:
* <ol>
* <li>repositoryName -- domain or node agent name (e.g. domain1 or agent1)
* <li>repositoryRoot -- the parent directory of the repository (e.g. $installDir/domains or
* $installDir/agents)
* <li>instanceName -- the optional server instance name (e.g. server1)
* <li>configurationName -- the optional configuration name of the server instance (e.g.
* default-config).
* </ol>
*
* Using (repositoryName, repositoryRoot, instanceName, configurationName) syntax. Here are the
* following permutations:
* <ol>
* <li>For a domain: (domainRootDirectory, domainName, null, null) e.g. ("/sun/appserver/domains",
* "domain1", null, null)
* <li>For a node agent: (agentRootDirectory, agentName, "agent", null) e.g
* ("/sun/appserver/agents", "agent1", "agent",
* null). Note that the instance name of a node agent is always the literal string "agent".
*
* 3)For a server instance (agentRootDirectory, agentName, instanceName, configName) e.g. ("/sun/appserver/agents",
* <li>For a server instance (agentRootDirectory, agentName, instanceName, configName) e.g.
* ("/sun/appserver/agents",
* "agent1", "server1", "default-config")
* </ol>
*
* The RepositoryConfig class is an extensible HashMap that can contain any attributes, but also relies on two system
* The RepositoryConfig class is an extensible HashMap that can contain any attributes, but also
* relies on two system
* properties being set:
*
* 1)com.sun.aas.installRoot -- installation root directory stored under the K_INSTALL_ROOT key.
*
* 2)com.sun.aas.configRoot -- configuration root (for locating asenv.conf) stored under the K_CONFIG_ROOT key.
* <ol>
* <li>com.sun.aas.installRoot -- installation root directory stored under the K_INSTALL_ROOT key.
* <li>com.sun.aas.configRoot -- configuration root (for locating asenv.conf) stored under the
* K_CONFIG_ROOT key.
* </ol>
*
* @author kebbs
*/
public class RepositoryConfig extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;

public static final String K_INSTALL_ROOT = "install.root";
public static final String K_CONFIG_ROOT = "config.root";
public static final String K_REFRESH_CONFIG_CONTEXT = "refresh.cc";
//Name of the domain or node agent. Cannot be null.
private String _repositoryName;
private final String _repositoryName;
//Root directory where the domain or node agent resides. Cannot be null
private String _repositoryRoot;
//Name of the server instance. May be null
Expand All @@ -84,17 +94,16 @@ public RepositoryConfig(String repositoryName, String repositoryRoot, String ins
_repositoryRoot = repositoryRoot;
_configurationName = configName;
final Map<String, String> envProperties = getEnvProps();
put(K_INSTALL_ROOT, getFilePath(envProperties.get(SystemPropertyConstants.INSTALL_ROOT_PROPERTY)));
//SystemPropertyConstants.INSTALL_ROOT_PROPERTY));
put(K_CONFIG_ROOT, getFilePath(envProperties.get(SystemPropertyConstants.INSTALL_ROOT_PROPERTY)));
//SystemPropertyConstants.CONFIG_ROOT_PROPERTY));

// Since the changes for the startup, we have the problem of refreshing
// config context. So, by default, I am making a change to refresh the
// config context. If some processes (e.g. start-domain) have already
// created a config context, then they should explicitly say so.
put(K_REFRESH_CONFIG_CONTEXT, true);
/*
* Since the changes for the startup, we have the problem of refreshing
* config context. So, by default, I am making a change to refresh the
* config context. If some processes (e.g. start-domain) have already
* created a config context, then they should explicitly say so.
*/
if (envProperties != null) {
put(K_INSTALL_ROOT, getFilePath(envProperties.get(SystemPropertyConstants.INSTALL_ROOT_PROPERTY)));
put(K_CONFIG_ROOT, getFilePath(envProperties.get(SystemPropertyConstants.INSTALL_ROOT_PROPERTY)));
}
}

public RepositoryConfig(String repositoryName, String repositoryRoot, String instanceName) {
Expand All @@ -121,10 +130,13 @@ public RepositoryConfig(String instanceRootString) {
_repositoryRoot = FileUtils.makeForwardSlashes(repositoryDir.getParentFile().getAbsolutePath());
_configurationName = null;
final Map<String, String> envProperties = getEnvProps();
put(K_INSTALL_ROOT, envProperties.get(SystemPropertyConstants.INSTALL_ROOT_PROPERTY));
put(K_CONFIG_ROOT, getFilePath(envProperties.get(SystemPropertyConstants.CONFIG_ROOT_PROPERTY)));
if (envProperties != null) {
put(K_INSTALL_ROOT, envProperties.get(SystemPropertyConstants.INSTALL_ROOT_PROPERTY));
put(K_CONFIG_ROOT, getFilePath(envProperties.get(SystemPropertyConstants.CONFIG_ROOT_PROPERTY)));
}
}

@Override
public String toString() {
return ("repositoryRoot " + _repositoryRoot + " repositoryName " + _repositoryName + " instanceName " + _instanceName
+ " configurationName " + _configurationName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2018-2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -58,7 +58,6 @@ public class StringSubstitutionEngine implements StringSubstitutor {
private static final Logger _logger = SLogger.getLogger();

private static final LocalStringsImpl _strings = new LocalStringsImpl(StringSubstitutionEngine.class);
private InputStream _configInputStream = null;

//Root of JAXB parsed string-subs configuration
private StringsubsDefinition _root = null;
Expand All @@ -78,8 +77,7 @@ public StringSubstitutionEngine(InputStream inputStream) throws StringSubstituti
if (inputStream == null) {
throw new StringSubstitutionException("InputStream is null");
}
_configInputStream = inputStream;
_root = StringSubstitutionParser.parse(_configInputStream);
_root = StringSubstitutionParser.parse(inputStream);
}

@Override
Expand All @@ -106,7 +104,7 @@ public List<Property> getDefaultProperties(PropertyType type) {
if (type == null) {
return defaults.getProperty();
}
List<Property> props = new ArrayList<Property>();
List<Property> props = new ArrayList<>();
for (Property prop : defaults.getProperty()) {
if (prop.getType().equals(type)) {
props.add(prop);
Expand Down Expand Up @@ -199,7 +197,7 @@ private void doSubstitution(Group group) throws StringSubstitutionException {
groupMode = modeType.value();
}
buildChangePairsMap();
Map<String, String> substitutionMap = new HashMap<String, String>();
Map<String, String> substitutionMap = new HashMap<>();
for (ChangePairRef ref : refList) {
String name = ref.getName();
String localMode = ref.getMode();
Expand Down Expand Up @@ -271,14 +269,14 @@ private void buildChangePairsMap() {
if (defaults != null) {
List<Property> properties = defaults.getProperty();
if (!properties.isEmpty()) {
_defaultProperties = new HashMap<String, Property>(properties.size(), 1);
_defaultProperties = new HashMap<>(properties.size(), 1);
for (Property prop : properties) {
_defaultProperties.put(prop.getKey(), prop);
}
}
}
List<? extends ChangePair> changePairList = _root.getChangePair();
_changePairsMap = new HashMap<String, Pair>(changePairList.size());
_changePairsMap = new HashMap<>(changePairList.size());
for (ChangePair pair : _root.getChangePair()) {
String id = pair.getId();
String beforeValue = pair.getBefore();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2018-2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -51,13 +51,12 @@ public class StringSubstitutionParser {
private final static String DEFAULT_SCHEMA = "xsd/schema/stringsubs.xsd";

/**
* Parse the configuration stream against the string-subs schema.
* Parse the configuration stream against the string-subs schema and then closes the stream.
*
* @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) {
Expand All @@ -73,20 +72,19 @@ public static StringsubsDefinition parse(InputStream configStream) throws String
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;
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"));
}
try {
configStream.close();
} catch (IOException e) {
if (_logger.isLoggable(Level.FINER)) {
_logger.log(Level.FINER, _strings.get("errorInClosingStream"));
}
}
}
Expand Down

0 comments on commit e92b6ac

Please sign in to comment.