From ff3d497b86ab3a87defdf7f385b786d761320022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mat=C4=9Bj=C4=8Dek?= Date: Tue, 6 Jul 2021 00:54:36 +0200 Subject: [PATCH] Issue #23507 Several fixes - 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 --- .../admin/servermgmt/DomainConfig.java | 6 +- .../admin/servermgmt/RepositoryConfig.java | 88 +++++++++++-------- .../impl/StringSubstitutionEngine.java | 14 ++- .../impl/StringSubstitutionParser.java | 22 +++-- 4 files changed, 70 insertions(+), 60 deletions(-) diff --git a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/DomainConfig.java b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/DomainConfig.java index 94d3b0ab92e..a20906377fb 100644 --- a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/DomainConfig.java +++ b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/DomainConfig.java @@ -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 @@ -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 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); } diff --git a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/RepositoryConfig.java b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/RepositoryConfig.java index a4dcdff5986..e48d38fdb63 100644 --- a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/RepositoryConfig.java +++ b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/RepositoryConfig.java @@ -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 @@ -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: + *
    + *
  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", * 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", + *
  3. For a server instance (agentRootDirectory, agentName, instanceName, configName) e.g. + * ("/sun/appserver/agents", * "agent1", "server1", "default-config") + *
* - * 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. + *
    + *
  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. + *
* * @author kebbs */ public class RepositoryConfig extends HashMap { + 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 @@ -84,17 +94,16 @@ public RepositoryConfig(String repositoryName, String repositoryRoot, String ins _repositoryRoot = repositoryRoot; _configurationName = configName; final Map 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) { @@ -121,10 +130,13 @@ public RepositoryConfig(String instanceRootString) { _repositoryRoot = FileUtils.makeForwardSlashes(repositoryDir.getParentFile().getAbsolutePath()); _configurationName = null; final Map 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); diff --git a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/stringsubs/impl/StringSubstitutionEngine.java b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/stringsubs/impl/StringSubstitutionEngine.java index cd76f6ca7c4..b024debfdbb 100644 --- a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/stringsubs/impl/StringSubstitutionEngine.java +++ b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/stringsubs/impl/StringSubstitutionEngine.java @@ -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 @@ -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; @@ -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 @@ -106,7 +104,7 @@ public List getDefaultProperties(PropertyType type) { if (type == null) { return defaults.getProperty(); } - List props = new ArrayList(); + List props = new ArrayList<>(); for (Property prop : defaults.getProperty()) { if (prop.getType().equals(type)) { props.add(prop); @@ -199,7 +197,7 @@ private void doSubstitution(Group group) throws StringSubstitutionException { groupMode = modeType.value(); } buildChangePairsMap(); - Map substitutionMap = new HashMap(); + Map substitutionMap = new HashMap<>(); for (ChangePairRef ref : refList) { String name = ref.getName(); String localMode = ref.getMode(); @@ -271,14 +269,14 @@ private void buildChangePairsMap() { if (defaults != null) { List properties = defaults.getProperty(); if (!properties.isEmpty()) { - _defaultProperties = new HashMap(properties.size(), 1); + _defaultProperties = new HashMap<>(properties.size(), 1); for (Property prop : properties) { _defaultProperties.put(prop.getKey(), prop); } } } List changePairList = _root.getChangePair(); - _changePairsMap = new HashMap(changePairList.size()); + _changePairsMap = new HashMap<>(changePairList.size()); for (ChangePair pair : _root.getChangePair()) { String id = pair.getId(); String beforeValue = pair.getBefore(); diff --git a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/stringsubs/impl/StringSubstitutionParser.java b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/stringsubs/impl/StringSubstitutionParser.java index a6b7f09b214..44ff1c5d8a1 100644 --- a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/stringsubs/impl/StringSubstitutionParser.java +++ b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/stringsubs/impl/StringSubstitutionParser.java @@ -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 @@ -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) { @@ -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")); } } }