Skip to content

Commit

Permalink
Merge pull request #233 from rnc/224
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Jul 1, 2020
2 parents 2b7f73b + 5beff8f commit 1c88d38
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 60 deletions.
10 changes: 9 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
</dependency>
</dependencies>

Expand Down
111 changes: 53 additions & 58 deletions core/src/main/java/org/jacorb/config/JacORBConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

import org.jacorb.orb.ORB;
import org.jacorb.util.ObjectUtil;
import org.jacorb.util.Version;
import org.omg.CORBA.NO_IMPLEMENT;
import org.slf4j.Logger;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import org.jacorb.orb.ORB;
import org.jacorb.util.ObjectUtil;
import org.jacorb.util.Version;
import org.omg.CORBA.NO_IMPLEMENT;
import org.slf4j.Logger;

/**
* The Class JacORBConfiguration.
Expand Down Expand Up @@ -86,14 +88,7 @@ public class JacORBConfiguration implements Configuration
static
{
String clpolicy = System.getProperty ("jacorb.classloaderpolicy", "tccl");
if (clpolicy.equalsIgnoreCase ("forname"))
{
useTCCL = false;
}
else
{
useTCCL = true;
}
useTCCL = !clpolicy.equalsIgnoreCase( "forname" );
}


Expand All @@ -103,17 +98,17 @@ public class JacORBConfiguration implements Configuration
* To speed up the access of frequently requested configuration values a set
* of dedicated hashmaps provide String, Boolean and Number storage.
*/
private ConcurrentHashMap<String,String> stringAttributes = new ConcurrentHashMap<String,String>(16, 0.9f, 1);
private final ConcurrentHashMap<String,String> stringAttributes = new ConcurrentHashMap<>( 16, 0.9f, 1 );

/**
* The boolean attributes.
*/
private ConcurrentHashMap<String,Boolean> booleanAttributes = new ConcurrentHashMap<String,Boolean>(16, 0.9f, 1);
private final ConcurrentHashMap<String,Boolean> booleanAttributes = new ConcurrentHashMap<>( 16, 0.9f, 1 );

/**
* The number attributes.
*/
private ConcurrentHashMap<String, Number> numberAttributes = new ConcurrentHashMap<String, Number> (16, 0.9f, 1);
private final ConcurrentHashMap<String, Number> numberAttributes = new ConcurrentHashMap<>( 16, 0.9f, 1 );

/**
* The orb.
Expand All @@ -123,7 +118,7 @@ public class JacORBConfiguration implements Configuration
/**
* The logger.
*/
private Logger logger;
private final Logger logger;

/**
* The li.
Expand Down Expand Up @@ -226,7 +221,7 @@ private JacORBConfiguration(String name,
{
super();
this.orb = orb;
LinkedHashMap<Level,String> delayedLogging = new LinkedHashMap<Level,String> ();
LinkedHashMap<Level,String> delayedLogging = new LinkedHashMap<>();
if (isApplet)
{
initApplet(delayedLogging, name, orbProperties);
Expand All @@ -238,29 +233,41 @@ private JacORBConfiguration(String name,

initLogging();

logger = getLogger ("org.jacorb.config");
logger = getLogger( "org.jacorb.config" );

// This delays logging out any information about the loading of the properties
// or configuration until any logging subsystem has been setup.
for (Entry<Level, String> e : delayedLogging.entrySet())
{
if (e.getKey () == Level.INFO)
{
logger.info (e.getValue ());
logger.info ( e.getValue ());
}
else if (e.getKey () == Level.WARNING)
{
logger.warn (e.getValue ());
logger.warn ( e.getValue ());
}
else if (e.getKey () == Level.FINE)
{
logger.debug (e.getValue ());
logger.debug ( e.getValue ());
}
else
{
throw new NO_IMPLEMENT("Only info/warn delayed logging implemented.");
}
}
delayedLogging.clear();

if ( orbProperties != null )
{
Set<String> propertyNames = orbProperties.stringPropertyNames();
orbProperties.keySet().forEach( k -> {
if ( !propertyNames.contains( String.valueOf( k ) ) )
{
logger.warn( "Property {} does not map to String object ({})", k, orbProperties.get( k ) );
}
} );
}
}


Expand Down Expand Up @@ -408,7 +415,6 @@ private void init(LinkedHashMap<Level, String> delayedLogging, String name, Prop
// settings in config files or system properties!
if (orbProperties != null)
{
loaded = true;
setAttributes(orbProperties);

// This is in case ORBClass/ORBSingleton are not in system properties
Expand Down Expand Up @@ -570,15 +576,14 @@ public void setAttributes(Properties properties)
{
// Some lunatics illegally put non String objects into System props
// as keys / values - we ignore them.
Iterator<String> keyIt = properties.stringPropertyNames().iterator();
while (keyIt.hasNext())
for ( String key : properties.stringPropertyNames() )
{
String key = keyIt.next();
setAttribute(key, properties.getProperty(key));
setAttribute( key, properties.getProperty( key ) );
}
}



/**
* Loads properties from a file.
*
Expand All @@ -590,17 +595,12 @@ private Properties loadPropertiesFromFile(String fileName)
{
try
{
InputStream stream = new FileInputStream(fileName);
try
try (InputStream stream = new FileInputStream( fileName ))
{
Properties result = new Properties();
result.load(stream);
result.load( stream );
return result;
}
finally
{
stream.close();
}
}
catch (java.io.FileNotFoundException e)
{
Expand Down Expand Up @@ -646,14 +646,9 @@ private Properties loadPropertiesFromClassPath(String name)
if (url!=null)
{
result = new Properties();
final InputStream stream = url.openStream();
try
{
result.load(stream);
}
finally
try (InputStream stream = url.openStream())
{
stream.close();
result.load( stream );
}
}
}
Expand Down Expand Up @@ -776,7 +771,7 @@ else if (name.startsWith (LoggingInitializer.ATTR_LOG_NAME + '.'))
}

@Override
public boolean isAttributeSet(String key) throws ConfigurationException
public boolean isAttributeSet(String key)
{
return stringAttributes.containsKey(key);
}
Expand Down Expand Up @@ -866,12 +861,12 @@ public int getAttributeAsInteger(String key, int defaultValue, int radix) throws

if (value == null)
{
result = Integer.valueOf (defaultValue);
result = defaultValue;
}
else if (value.trim().length() < 1)
{
// treat empty values as non-defined (null)
result = Integer.valueOf (defaultValue);
result = defaultValue;
}
else
{
Expand Down Expand Up @@ -939,12 +934,12 @@ public long getAttributeAsLong(String key, long defaultValue) throws Configurati

if (value == null)
{
result = Long.valueOf (defaultValue);
result = defaultValue;
}
else if (value.trim().length() < 1)
{
// treat empty values as non-defined (null)
result = Long.valueOf (defaultValue);
result = defaultValue;
}
else
{
Expand Down Expand Up @@ -977,7 +972,7 @@ else if (value.trim().length() < 1)
@Override
public List<String> getAttributeList(String key)
{
List<String> result = new ArrayList<String>();
List<String> result = new ArrayList<>();
String value = null;

try
Expand Down Expand Up @@ -1027,7 +1022,7 @@ public String[] getAttributeAsStringsArray(String key)
return null;
}

return values.toArray (new String[values.size ()]);
return values.toArray ( new String[0] );
}


Expand Down Expand Up @@ -1124,17 +1119,17 @@ public boolean getAttributeAsBoolean(String key, boolean defaultValue)

if (value == null)
{
result = Boolean.valueOf (defaultValue);
result = defaultValue;
}
else if (value.trim().length() < 1)
{
// treat empty values as non-defined (null)
result = Boolean.valueOf (defaultValue);
result = defaultValue;
}
else
{
value = value.trim().toLowerCase();
result = Boolean.valueOf ((ON.equals(value) || TRUE.equals(value)));
result = ( ON.equals( value ) || TRUE.equals( value ) );
}
booleanAttributes.put (key, result);
}
Expand Down Expand Up @@ -1163,15 +1158,15 @@ private String[] getAttributeNames()
@Override
public List<String> getAttributeNamesWithPrefix(String prefix)
{
final ArrayList<String> attributesWithPrefix = new ArrayList<String>();
final ArrayList<String> attributesWithPrefix = new ArrayList<>();

final String[] allAttributes = getAttributeNames();

for (int x = 0; x < allAttributes.length; ++x)
for ( String allAttribute : allAttributes )
{
if (allAttributes[x].startsWith(prefix))
if ( allAttribute.startsWith( prefix ) )
{
attributesWithPrefix.add(allAttributes[x]);
attributesWithPrefix.add( allAttribute );
}
}

Expand All @@ -1196,12 +1191,12 @@ public double getAttributeAsFloat (String key, double defaultValue) throws Confi

if (value == null)
{
result = Double.valueOf (defaultValue);
result = defaultValue;
}
else if (value.trim().length() < 1)
{
// treat empty values as non-defined (null)
result = Double.valueOf (defaultValue);
result = defaultValue;
}
else
{
Expand Down
34 changes: 34 additions & 0 deletions core/src/test/java/org/jacorb/config/JacORBConfigurationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jacorb.config;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.omg.CORBA.ORB;

import java.util.Properties;

import static org.junit.Assert.*;

public class JacORBConfigurationTest
{
@Rule
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();

@Test
public void setAttributes() throws ConfigurationException
{
Properties orbProps = new Properties();
orbProps.setProperty("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");
orbProps.setProperty("org.omg.CORBA.ORBSingletonClass", "org.jacorb.orb.ORBSingleton");
ORB orb = ORB.init( new String[] {}, orbProps);
Properties props = new Properties();
props.put ("jacorb.config.log.verbosity", "4");
props.put("jacorb.connection.request.write_timeout", 60000);
props.put ("custom.props", "applet-special.properties");

JacORBConfiguration.getConfiguration(props, orb, true);
orb.destroy();

assertTrue( systemErrRule.getLog().contains( "Property jacorb.connection.request.write_timeout does not map to String object" ) );
}
}
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.7.0</version>
<version>1.18.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mx4j</groupId>
Expand Down
1 change: 1 addition & 0 deletions test/regression/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
Expand Down

0 comments on commit 1c88d38

Please sign in to comment.