diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aeda79..e2c055c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,24 @@ # Change Log for javapos-config-loader +## 4.0.0 + +- jpos.config.DefaultCompositeRegPopulator.load() is throwing more specific IllegalArgument exception instead of RuntimeException +- added Javax XML parser based XML registry populator implementation (contributed by @mjpcger) +- removed Xerces based XML registry populator implementations, mainly + - `jpos.config.simple.xml.XercesRegPopulator` + - `jpos.config.simple.xml.Xerces2RegPopulator` +- removed Xerces dependency at all, thus, it does not appear as transitive dependency in the Maven POM anymore +- added JavaPOS XML node name, attribute name, and fixed file names as constants to `jpos.config.simple.xml.JavaxRegPopulator` making them available for all (proprietary) XML implementations +- restricted XML parser's DTD and XSD resources access for security reasons +- renamed `jpos.profile.XercesProfileFactory` to `jpos.profile.DefaultProfileFactory` +- ensure all resources are well closed by stringent use of try-with-resource clauses +- fixing a NPE in case multi-propo definition is missing +- removed deprecated constructors at + - `jpos.config.simple.SimpleEntryRegistry`, and + - `jpos.loader.simple.SimpleServiceManager` +- removed deprecated class `jpos.util.Tracer` +- ensure compatibility to webstart environments by using standard class-loader instead of the system class-loader (solves issue #1) + ## 3.1.0 - Added missing devices to XML schema/DTD files and as DevCat interfaces (contribution by [@dougberkland](https://github.com/dougberkland)) diff --git a/build.gradle b/build.gradle index cc353bb..baf8e6f 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ wrapper { def artifactName = 'javapos-config-loader' group='org.javapos' -version='3.1.0' // if version is going to be changed, first add "-SNAPSHOT" to test publishing to MavenCentral's Snapshot repo +version='4.0.0-SNAPSHOT' // if version is going to be changed, first add "-SNAPSHOT" to test publishing to MavenCentral's Snapshot repo /////////////////////////////////////////////////////////////////////////////// @@ -64,7 +64,6 @@ if (System.getenv('CI') && null == findProperty('github.event.action')) def testResourceDir = file("${System.properties['java.io.tmpdir']}/javapos-config-loader-test/resources") dependencies { - api 'xerces:xerces:1.2.3' api 'org.javapos:javapos-contracts:1.6.0' testImplementation("junit:junit:4.13.2") diff --git a/src/main/java/jpos/config/CompositeRegPopulator.java b/src/main/java/jpos/config/CompositeRegPopulator.java index 1a40be2..4932c86 100644 --- a/src/main/java/jpos/config/CompositeRegPopulator.java +++ b/src/main/java/jpos/config/CompositeRegPopulator.java @@ -49,6 +49,7 @@ public interface CompositeRegPopulator extends JposRegPopulator public void remove( JposRegPopulator populator ); /** @return an iterator over all populators in this composite */ + @SuppressWarnings("rawtypes") public Iterator getPopulators(); /** diff --git a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java index 70026a6..96096b9 100644 --- a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java +++ b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java @@ -32,8 +32,7 @@ * @since 1.3 (Washington DC 2001 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class DefaultCompositeRegPopulator extends Object - implements CompositeRegPopulator +public class DefaultCompositeRegPopulator implements CompositeRegPopulator { //------------------------------------------------------------------------- // Ctor(s) @@ -73,19 +72,19 @@ private JposRegPopulator createPopulator( String popName, String className ) try { - Class popClass = Class.forName( className ); + Class popClass = Class.forName( className ); try { - Class[] ctorParamTypes = { String.class }; - Constructor ctor = popClass.getConstructor( ctorParamTypes ); + Class[] ctorParamTypes = { String.class }; + Constructor ctor = popClass.getConstructor( ctorParamTypes ); Object[] args = { popName }; populator = (JposRegPopulator)ctor.newInstance( args ); } catch( NoSuchMethodException nsme ) { - Constructor ctor = popClass.getConstructor( new Class[ 0 ] ); + Constructor ctor = popClass.getConstructor( new Class[ 0 ] ); populator = (JposRegPopulator)ctor.newInstance( new Object[ 0 ] ); } @@ -164,34 +163,28 @@ public String getClassName() * @param entries an enumeration of JposEntry objects * @throws java.lang.Exception if any error occurs while saving */ - public void save( Enumeration entries ) throws Exception + public void save( @SuppressWarnings("rawtypes") Enumeration entries ) throws Exception { - HashMap popEntriesMap = new HashMap(); + HashMap>popEntriesMap = new HashMap<>(); - Iterator popIterator = popMap.values().iterator(); + Iterator popIterator = popMap.values().iterator(); while( popIterator.hasNext() ) - popEntriesMap.put( ( (JposRegPopulator)popIterator.next() ). - getUniqueId(), - new ArrayList() ); + popEntriesMap.put( ( popIterator.next() ).getUniqueId(), new ArrayList<>() ); while( entries.hasMoreElements() ) { JposEntry entry = (JposEntry)entries.nextElement(); - JposRegPopulator populator = (JposRegPopulator)entry. - getRegPopulator(); + JposRegPopulator populator = entry.getRegPopulator(); if( populator == null ) { - Collection defaultEntryList = (Collection)popEntriesMap. - get( getDefaultPopulator(). - getUniqueId() ); + List defaultEntryList = popEntriesMap.get( getDefaultPopulator().getUniqueId() ); defaultEntryList.add( entry ); } else { - Collection entryList = (Collection)popEntriesMap. - get( populator.getUniqueId() ); + List entryList = popEntriesMap.get( populator.getUniqueId() ); if( entryList == null ) tracer.println( "Trying to save entry with logicalName = " + @@ -206,17 +199,17 @@ public void save( Enumeration entries ) throws Exception popIterator = popMap.values().iterator(); while( popIterator.hasNext() ) { - JposRegPopulator populator = (JposRegPopulator)popIterator.next(); + JposRegPopulator populator = popIterator.next(); String popUniqueId = populator.getUniqueId(); - Collection entryList = (Collection)popEntriesMap.get( popUniqueId ); + List entryList = popEntriesMap.get( popUniqueId ); try { if( popFileMap.get( populator.getUniqueId() ) != null ) - populator.save( new Vector( entryList ).elements(), - (String)popFileMap.get( populator.getUniqueId() ) ); + populator.save( Collections.enumeration(entryList), + popFileMap.get( populator.getUniqueId() ) ); else - populator.save( new Vector( entryList ).elements() ); + populator.save( Collections.enumeration(entryList) ); } catch( Exception e ) { @@ -236,7 +229,7 @@ public void save( Enumeration entries ) throws Exception * @param fileName the file name to save entries * @throws java.lang.Exception if any error occurs while saving */ - public void save( Enumeration entries, String fileName ) throws Exception + public void save( @SuppressWarnings("rawtypes") Enumeration entries, String fileName ) throws Exception { getDefaultPopulator().save( entries, fileName ); } @@ -254,57 +247,60 @@ public void load() JposProperties jposProperties = JposServiceLoader.getManager().getProperties(); JposProperties.MultiProperty populatorClassMultiProp = - jposProperties.getMultiProperty( JposProperties.JPOS_CONFIG_POPULATOR_CLASS_MULTIPROP_NAME ); + jposProperties.getMultiProperty( JposPropertiesConst.JPOS_CONFIG_POPULATOR_CLASS_MULTIPROP_NAME ); JposProperties.MultiProperty populatorFileMultiProp = - jposProperties.getMultiProperty( JposProperties.JPOS_CONFIG_POPULATOR_FILE_MULTIPROP_NAME ); + jposProperties.getMultiProperty( JposPropertiesConst.JPOS_CONFIG_POPULATOR_FILE_MULTIPROP_NAME ); - if( populatorClassMultiProp.getNumberOfProperties() == 0 ) + if( populatorClassMultiProp == null || populatorClassMultiProp.getNumberOfProperties() == 0 ) { tracer.println( "CompositeRegPopulator.load() w/o any defined multi-property" ); - throw new RuntimeException( "CompositeRegPopulator.load() w/o any defined multi-property" ); + throw new IllegalArgumentException( "CompositeRegPopulator.load() w/o any defined multi-property" ); } - Iterator popClasses = populatorClassMultiProp.getSortedPropertyNames(); + @SuppressWarnings("unchecked") + Iterator popClasses = populatorClassMultiProp.getSortedPropertyNames(); - String defaultPopName = (String)popClasses.next(); + String defaultPopName = popClasses.next(); String defaultPopClass = populatorClassMultiProp.getPropertyString( defaultPopName ); int defaultPopClassNumber = populatorClassMultiProp.propertyNumber( defaultPopName ); JposRegPopulator defaultPopulator = createPopulator( defaultPopName, defaultPopClass ); - if( populatorFileMultiProp != null && populatorFileMultiProp.getNumberOfProperties() > 0 ) - { - String defaultPopFile = (String)populatorFileMultiProp.getPropertyString( defaultPopClassNumber ); - - if( defaultPopFile != null ) + if( defaultPopulator != null ) { + + if( populatorFileMultiProp != null && populatorFileMultiProp.getNumberOfProperties() > 0 ) { - defaultPopulator.load( defaultPopFile ); - lastLoadException = defaultPopulator.getLastLoadException(); - popFileMap.put( defaultPopulator.getUniqueId(), defaultPopFile ); + String defaultPopFile = populatorFileMultiProp.getPropertyString( defaultPopClassNumber ); + + if( defaultPopFile != null ) + { + defaultPopulator.load( defaultPopFile ); + lastLoadException = defaultPopulator.getLastLoadException(); + popFileMap.put( defaultPopulator.getUniqueId(), defaultPopFile ); + } + else + { + tracer.println( "Created default populator with name = " + defaultPopName + + " OK but populator file is null" ); + defaultPopulator.load(); + lastLoadException = defaultPopulator.getLastLoadException(); + } } else { - tracer.println( "Created default populator with name = " + defaultPopName + - " OK but populator file is null" ); defaultPopulator.load(); lastLoadException = defaultPopulator.getLastLoadException(); } - } - else - { - defaultPopulator.load(); - lastLoadException = defaultPopulator.getLastLoadException(); - } - if( defaultPopulator != null ) setDefaultPopulator( defaultPopulator ); + } else tracer.println( "Did not add default populator by : " + "<" + defaultPopName + ", " + defaultPopClass + ">" ); while( popClasses.hasNext() ) { - String popName = (String)popClasses.next(); + String popName = popClasses.next(); String popClass = populatorClassMultiProp.getPropertyString( popName ); int popClassNumber = populatorClassMultiProp.propertyNumber( popName ); @@ -314,7 +310,7 @@ public void load() { if( populatorFileMultiProp != null && populatorFileMultiProp.getNumberOfProperties() > 0 ) { - String popFile = (String)populatorFileMultiProp.getPropertyString( popClassNumber ); + String popFile = populatorFileMultiProp.getPropertyString( popClassNumber ); populator.load( popFile ); popFileMap.put( populator.getUniqueId(), popFile ); } @@ -349,21 +345,24 @@ public void load() * number of entries in populator. Could do better by keeping more data structure * but not worth it if n and m are small (as expected in typical system) */ - public Enumeration getEntries() + @SuppressWarnings("rawtypes") + public Enumeration getEntries() { - Vector entryVector = new Vector(); - Iterator populators = getPopulators(); + List entryList = new ArrayList<>(); + @SuppressWarnings("unchecked") + Iterator populators = getPopulators(); while( populators.hasNext() ) { - JposRegPopulator pop = (JposRegPopulator)populators.next(); + JposRegPopulator pop = populators.next(); - Enumeration entries = pop.getEntries(); + @SuppressWarnings("unchecked") + Enumeration entries = pop.getEntries(); while( entries.hasMoreElements() ) - entryVector.add( entries.nextElement() ); + entryList.add( entries.nextElement() ); } - return entryVector.elements(); + return Collections.enumeration(entryList); } /** @@ -411,6 +410,7 @@ public Enumeration getEntries() public void remove( JposRegPopulator populator ) { popMap.remove( populator.getUniqueId() ); } /** @return an iterator over all populators in this composite */ + @SuppressWarnings("rawtypes") public Iterator getPopulators() { return popMap.values().iterator(); } /** @@ -419,7 +419,7 @@ public Enumeration getEntries() * @see jpos.config.JposRegPopulator#getUniqueId() */ public JposRegPopulator getPopulator( String uniqueId ) - { return (JposRegPopulator)popMap.get( uniqueId ); } + { return popMap.get( uniqueId ); } /** @return the number of populator in this composite */ public int size() { return popMap.size(); } @@ -428,8 +428,8 @@ public JposRegPopulator getPopulator( String uniqueId ) // Instance variables // - private HashMap popMap = new HashMap(); - private HashMap popFileMap = new HashMap(); + private HashMap popMap = new HashMap<>(); + private HashMap popFileMap = new HashMap<>(); private JposRegPopulator defaultPop = null; private Exception lastLoadException = null; diff --git a/src/main/java/jpos/config/JposConfigException.java b/src/main/java/jpos/config/JposConfigException.java index 684b9fd..d4ab2fe 100644 --- a/src/main/java/jpos/config/JposConfigException.java +++ b/src/main/java/jpos/config/JposConfigException.java @@ -28,6 +28,8 @@ */ public class JposConfigException extends JposException { + private static final long serialVersionUID = 6080819630774250157L; + //------------------------------------------------------------------------- // Ctor(s) // diff --git a/src/main/java/jpos/config/JposEntry.java b/src/main/java/jpos/config/JposEntry.java index 79959d9..e546f66 100644 --- a/src/main/java/jpos/config/JposEntry.java +++ b/src/main/java/jpos/config/JposEntry.java @@ -32,6 +32,7 @@ * @since 0.1 (Philly 99 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ +@SuppressWarnings("rawtypes") public interface JposEntry extends Serializable, Comparable { /** @@ -90,6 +91,7 @@ public interface JposEntry extends Serializable, Comparable * NOTE: any property with the same name gets overlaid * @param propName the name of this property (should be unique per property) * @param propValue the properties value Object + * @return the property value object as passed as 2nd argument * @since 0.1 (Philly 99 meeting) * @throws java.lang.IllegalArgumentException if the propName or propValue is null */ @@ -98,11 +100,17 @@ public interface JposEntry extends Serializable, Comparable /** * Looks for a property with name specified and removes it * @param propName the name String of the property to remove + * @return the property object that has been removed * @since 0.1 (Philly 99 meeting) */ public Object removeProperty( String propName ); /** + * A {@link JposEntry} specific equal method.
+ * This considered a design flow today as {@link Object} defines an {@link Object#equals(Object)} which never should be + * redefined. However, this interface is from the early days when Java was only 4 years old and that was unknown. + * For backward compatibility reasons ths will remain. + * @param otherEntry the other {@link JposEntry} object the comparison has to be done against * @return true if the two JposEntries have the same properties * @since 0.1 (Philly 99 meeting) */ diff --git a/src/main/java/jpos/config/JposEntryConst.java b/src/main/java/jpos/config/JposEntryConst.java index 0c8b0b6..5a29f84 100644 --- a/src/main/java/jpos/config/JposEntryConst.java +++ b/src/main/java/jpos/config/JposEntryConst.java @@ -136,10 +136,10 @@ public interface JposEntryConst extends RS232Const, Serializable // /** The default JposEntry property type */ - public static final Class DEFAULT_PROP_TYPE = String.class; + public static final Class DEFAULT_PROP_TYPE = String.class; /** Array of all the property types allowed for a JposEntry property */ - public static final Class[] PROP_TYPES = + public static final Class[] PROP_TYPES = { String.class, Boolean.class, diff --git a/src/main/java/jpos/config/JposEntryRegistry.java b/src/main/java/jpos/config/JposEntryRegistry.java index 46da0ab..ae4f4ef 100644 --- a/src/main/java/jpos/config/JposEntryRegistry.java +++ b/src/main/java/jpos/config/JposEntryRegistry.java @@ -42,7 +42,8 @@ public interface JposEntryRegistry * @return an enumeration of JposEntry objects * @since 0.1 (Philly 99 meeting) */ - public Enumeration getEntries(); + @SuppressWarnings("rawtypes") + public Enumeration getEntries(); /** * @return the JposEntry for the logicalName specified @@ -115,6 +116,7 @@ public interface JposEntryRegistry * Tell the JposEntryRegistry to save the current entries to the file * specified. Depending on the current JposEntryPopulator the file might * be an XML or serialized or other file. + * @param file the {@link File} object the registry entries has to to be persisted to * @since 2.1.0 * @throws java.lang.Exception if any error occurs while saving or if the * current populator does not support saving in a file diff --git a/src/main/java/jpos/config/JposEntryRegistryEvent.java b/src/main/java/jpos/config/JposEntryRegistryEvent.java index bff2cc0..eb86838 100644 --- a/src/main/java/jpos/config/JposEntryRegistryEvent.java +++ b/src/main/java/jpos/config/JposEntryRegistryEvent.java @@ -27,7 +27,9 @@ */ public class JposEntryRegistryEvent extends EventObject { - /** + private static final long serialVersionUID = 8674029321997818382L; + + /** * Creates a new event * @param source the source of this event * @param entry the JposEntry associated with the event diff --git a/src/main/java/jpos/config/JposRegPopulator.java b/src/main/java/jpos/config/JposRegPopulator.java index 9041cf8..4e4fe41 100644 --- a/src/main/java/jpos/config/JposRegPopulator.java +++ b/src/main/java/jpos/config/JposRegPopulator.java @@ -52,7 +52,7 @@ public interface JposRegPopulator * @since 1.2 (NY 2K meeting) * @throws java.lang.Exception if any error occurs while saving */ - public void save( Enumeration entries ) throws Exception; + public void save( @SuppressWarnings("rawtypes") Enumeration entries ) throws Exception; /** * Tell the populator to save the current entries in the file specified @@ -61,7 +61,7 @@ public interface JposRegPopulator * @since 1.3 (SF 2K meeting) * @throws java.lang.Exception if any error occurs while saving */ - public void save( Enumeration entries, String fileName ) throws Exception; + public void save( @SuppressWarnings("rawtypes") Enumeration entries, String fileName ) throws Exception; /** * Tell the populator to load the entries @@ -93,7 +93,8 @@ public interface JposRegPopulator * @return an Enumeration of JposEntry objects * @since 1.2 (NY 2K meeting) */ - public Enumeration getEntries(); + @SuppressWarnings("rawtypes") + public Enumeration getEntries(); /** * @return true if this populator is a composite populator or false diff --git a/src/main/java/jpos/config/Version.java b/src/main/java/jpos/config/Version.java index ef3055f..4de5c95 100644 --- a/src/main/java/jpos/config/Version.java +++ b/src/main/java/jpos/config/Version.java @@ -25,7 +25,7 @@ * @since 1.2 (NY 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public final class Version extends Object +public final class Version { /** * Main entry point for the Version application diff --git a/src/main/java/jpos/config/simple/AbstractRegPopulator.java b/src/main/java/jpos/config/simple/AbstractRegPopulator.java index 5cd3188..4d35e34 100644 --- a/src/main/java/jpos/config/simple/AbstractRegPopulator.java +++ b/src/main/java/jpos/config/simple/AbstractRegPopulator.java @@ -26,6 +26,7 @@ import jpos.config.*; import jpos.loader.JposServiceLoader; import jpos.util.JposProperties; +import jpos.util.JposPropertiesConst; import jpos.util.tracing.Tracer; import jpos.util.tracing.TracerFactory; @@ -36,8 +37,7 @@ * @since 1.2 (NY 2K 99 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public abstract class AbstractRegPopulator extends Object - implements JposRegPopulator +public abstract class AbstractRegPopulator implements JposRegPopulator { //------------------------------------------------------------------------- // Ctor(s) @@ -51,55 +51,6 @@ public abstract class AbstractRegPopulator extends Object */ public AbstractRegPopulator( String id ) { setUniqueId( id ); } - //------------------------------------------------------------------------- - // Public abstract methods - // - - /** - * Tell the populator to save the current entries - * @param entries an enumeration of JposEntry objects - * @since 1.2 (NY 2K meeting) - * @throws java.lang.Exception if any error occurs while saving - */ - public abstract void save( Enumeration entries ) throws Exception; - - /** - * Tell the populator to save the current entries in the file specified - * @param entries an enumeration of JposEntry objects - * @param fileName the file name to save entries - * @since 1.3 (SF 2K meeting) - * @throws java.lang.Exception if any error occurs while saving - */ - public abstract void save( Enumeration entries, String fileName ) - throws Exception; - - /** - * Tell the populator to load the entries - * @since 1.2 (NY 2K meeting) - */ - public abstract void load(); - - - /** - * Loads the entries specified in the fileName - * @param fileName the entries file name - * @since 1.3 (SF 2K meeting) - */ - public abstract void load( String fileName ); - - /** - * @return the URL pointing to the entries file loaded or saved - * @since 1.2 (NY 2K meeting) - */ - public abstract URL getEntriesURL(); - - /** - * @return the name of this populator. This should be a short descriptive - * name - * @since 1.3 (Washington DC 2001 meeting) - */ - public abstract String getName(); - //------------------------------------------------------------------------- // Public methods // @@ -116,15 +67,16 @@ public String getUniqueId() * @return an Enumeration of JposEntry objects * @since 1.2 (NY 2K meeting) */ - public Enumeration getEntries() + @SuppressWarnings("rawtypes") + public Enumeration getEntries() { - Vector vector = new Vector(); - Enumeration entries = jposEntries.elements(); + List entryList = new ArrayList<>(); + Enumeration entries = jposEntries.elements(); while( entries.hasMoreElements() ) - vector.addElement( entries.nextElement() ); + entryList.add( entries.nextElement() ); - return vector.elements(); + return Collections.enumeration(entryList); } /** @@ -194,10 +146,33 @@ protected URL createURLFromFile( ZipFile zipFile ) } /** - * @return the jposEntries Hashtable to allow access to subclasses + * The {@link Hashtable} of {@link JposEntry}s of this registry populator instance.
+ * @return the jposEntries Hashtable to allow access for subclasses * @since 1.2 (NY 2K meeting) + * @deprecated use {@link #addJposEntry(String, JposEntry)} for adding and {@link #clearAllJposEntries()} for clearing, + * both are type safe + */ + @Deprecated + @SuppressWarnings("rawtypes") + protected Hashtable getJposEntries() { return jposEntries; } + + /** + * Adds a {@link JposEntry} to the internal data structure of {@link JposEntry}s represented by + * this registry populator instance. + * @param logicalName the logical name the {@link JposEntry} is associated with + * @param entry the {@link JposEntry} object to be added + * @since 4.0 */ - protected Hashtable getJposEntries() { return jposEntries; } + protected void addJposEntry(String logicalName, JposEntry entry) { + this.jposEntries.put(logicalName, entry); + } + + /** + * Clears all {@link JposEntry}s in the internal data structure of this registry populator instance. + */ + protected void clearAllJposEntries() { + this.jposEntries.clear(); + } /** * @return true if a populator file (or URL) is defined @@ -209,14 +184,11 @@ protected boolean isPopulatorFileDefined() JposProperties jposProperties = JposServiceLoader.getManager().getProperties(); - if( jposProperties. - isPropertyDefined( JposProperties.JPOS_POPULATOR_FILE_PROP_NAME ) ) - defined = true; - else - if( jposProperties. - isPropertyDefined( JposProperties. - JPOS_POPULATOR_FILE_URL_PROP_NAME ) ) - defined = true; + if( jposProperties.isPropertyDefined( JposPropertiesConst.JPOS_POPULATOR_FILE_PROP_NAME ) || + jposProperties.isPropertyDefined( JposPropertiesConst.JPOS_POPULATOR_FILE_URL_PROP_NAME ) ) + { + defined = true; + } return defined; } @@ -230,15 +202,16 @@ protected boolean isPopulatorFileDefined() */ protected InputStream getPopulatorFileIS() throws Exception { + InputStream populatorIS; + JposProperties jposProperties = JposServiceLoader. getManager().getProperties(); - if( jposProperties.isPropertyDefined( JposProperties. - JPOS_POPULATOR_FILE_PROP_NAME ) ) + if( jposProperties.isPropertyDefined( JposPropertiesConst.JPOS_POPULATOR_FILE_PROP_NAME ) ) { - populatorFileName = jposProperties. - getPropertyString( JposProperties. - JPOS_POPULATOR_FILE_PROP_NAME ); + populatorFileName = + jposProperties.getPropertyString( + JposPropertiesConst.JPOS_POPULATOR_FILE_PROP_NAME ); tracer.println( "getPopulatorFileIS(): populatorFileName=" + populatorFileName ); @@ -246,13 +219,11 @@ protected InputStream getPopulatorFileIS() throws Exception populatorIS = new FileInputStream( populatorFileName ); } else - if( jposProperties. - isPropertyDefined( JposProperties. - JPOS_POPULATOR_FILE_URL_PROP_NAME ) ) + if( jposProperties.isPropertyDefined( JposPropertiesConst.JPOS_POPULATOR_FILE_URL_PROP_NAME ) ) { - populatorFileURL = jposProperties. - getPropertyString( JposProperties. - JPOS_POPULATOR_FILE_URL_PROP_NAME ); + populatorFileURL = + jposProperties.getPropertyString( + JposPropertiesConst.JPOS_POPULATOR_FILE_URL_PROP_NAME ); URL url = new URL( populatorFileURL ); @@ -283,26 +254,26 @@ protected InputStream getPopulatorFileIS() throws Exception */ protected OutputStream getPopulatorFileOS() throws Exception { + OutputStream populatorOS; + JposProperties jposProperties = JposServiceLoader. getManager().getProperties(); if( jposProperties. - isPropertyDefined( JposProperties.JPOS_POPULATOR_FILE_PROP_NAME ) ) + isPropertyDefined( JposPropertiesConst.JPOS_POPULATOR_FILE_PROP_NAME ) ) { - populatorFileName = jposProperties. - getPropertyString( JposProperties. - JPOS_POPULATOR_FILE_PROP_NAME ); + populatorFileName = + jposProperties.getPropertyString( + JposPropertiesConst.JPOS_POPULATOR_FILE_PROP_NAME ); populatorOS = new FileOutputStream( populatorFileName ); } else - if( jposProperties. - isPropertyDefined( JposProperties. - JPOS_POPULATOR_FILE_URL_PROP_NAME ) ) + if( jposProperties.isPropertyDefined( JposPropertiesConst.JPOS_POPULATOR_FILE_URL_PROP_NAME ) ) { - populatorFileURL = jposProperties. - getPropertyString( JposProperties. - JPOS_POPULATOR_FILE_URL_PROP_NAME ); + populatorFileURL = + jposProperties.getPropertyString( + JposPropertiesConst.JPOS_POPULATOR_FILE_URL_PROP_NAME ); URL url = new URL( populatorFileURL ); @@ -344,11 +315,11 @@ protected OutputStream getPopulatorFileOS() throws Exception /** * Finds the first file matching the fileName in the CLASSPATH - * directory or each - * JAR or Zip file in the CLASSPATH - * NOTE:Decorated the FileInputStream with a BufferedInputStream to - * improve load time... + * directory or each JAR or Zip file in the CLASSPATH. + * * @param fileName the fileName to find + * @return an {@link InputStream} object opened for the given file name.
+ * This object must be closed by the caller! * @since 2.0 (Long Beach 2001) */ protected InputStream findFileInClasspath( String fileName ) @@ -376,7 +347,7 @@ protected InputStream findFileInClasspath( String fileName ) String path = ""; - Vector jarZipFilesVector = new Vector(); + List jarZipFilesVector = new ArrayList<>(); for( StringTokenizer st = new StringTokenizer( classpath, pathSeparator, false ); @@ -389,7 +360,7 @@ protected InputStream findFileInClasspath( String fileName ) if( ( path.length() > 4 ) && ( path.endsWith( ".zip" ) || path.endsWith( ".jar" ) ) ) - jarZipFilesVector.addElement( path ); + jarZipFilesVector.add( path ); else { String absoluteFileName = path + @@ -412,28 +383,42 @@ protected InputStream findFileInClasspath( String fileName ) } /** - * Finds the occurrence of the fileName in the JAR or Zip files + * Finds the occurrence of the fileName in the JAR or Zip files. * @param fileName the file to find * @param jarZipFilesVector a vector of JAR/Zip file names + * @return an {@link InputStream} object opened for the given file name.
+ * This object must be closed by the caller! * @since 2.0 (Long Beach 2001) + * @deprecated use {@link #findFileInJarZipFiles(String, List)} instead */ - protected InputStream findFileInJarZipFiles( String fileName, - Vector jarZipFilesVector ) + @Deprecated + protected InputStream findFileInJarZipFiles( String fileName, Vector jarZipFilesVector ) { + return findFileInJarZipFiles(fileName, jarZipFilesVector); + } + + /** + * Finds the occurrence of the fileName in the JAR or Zip files. + * @param fileName the file to find + * @param jarZipFilesList a list of JAR/Zip file names + * @return an {@link InputStream} object opened for the given file name.
+ * This object must be closed by the caller! + * @since 4.0 (Long Beach 2001) + */ + private InputStream findFileInJarZipFiles( String fileName, List jarZipFilesList ) { InputStream is = null; - for( int i = 0; i < jarZipFilesVector.size(); ++i ) + for( int i = 0; i < jarZipFilesList.size(); ++i ) { - String jarZipFileName = (String)jarZipFilesVector.elementAt( i ); + String jarZipFileName = jarZipFilesList.get( i ); - try + try (ZipFile zipFile = new ZipFile( jarZipFileName )) { - ZipFile zipFile = new ZipFile( jarZipFileName ); - Enumeration zipEntries = zipFile.entries(); + Enumeration zipEntries = zipFile.entries(); while( zipEntries.hasMoreElements() ) { - ZipEntry zipEntry = (ZipEntry)zipEntries.nextElement(); + ZipEntry zipEntry = zipEntries.nextElement(); String entryName = zipEntry.getName(); if( entryName.endsWith( fileName ) ) @@ -460,10 +445,7 @@ protected InputStream findFileInJarZipFiles( String fileName, // Instance variables // - private Hashtable jposEntries = new Hashtable(); - - private InputStream populatorIS = null; - private OutputStream populatorOS = null; + private final Hashtable jposEntries = new Hashtable<>(); private String populatorFileName = ""; private String populatorFileURL = ""; diff --git a/src/main/java/jpos/config/simple/SimpleEntry.java b/src/main/java/jpos/config/simple/SimpleEntry.java index 9f5b746..1c0f933 100644 --- a/src/main/java/jpos/config/simple/SimpleEntry.java +++ b/src/main/java/jpos/config/simple/SimpleEntry.java @@ -32,7 +32,7 @@ * @since 0.1 (Philly 99 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class SimpleEntry implements JposEntry, Serializable, Comparable +public class SimpleEntry implements JposEntry, Serializable { //-------------------------------------------------------------------------- // Ctor(s) @@ -85,7 +85,8 @@ public SimpleEntry( String logicalName, JposRegPopulator populator ) * @return an enumerator for the properties names * @since 0.1 (Philly 99 meeting) */ - public Enumeration getPropertyNames() { return properties.keys(); } + @SuppressWarnings("rawtypes") + public Enumeration getPropertyNames() { return properties.keys(); } /** * @return true if there is a property by the name specified @@ -114,7 +115,7 @@ public SimpleEntry( String logicalName, JposRegPopulator populator ) * @param propName the property's name String * @since 2.0.0 */ - public Class getPropertyType( String propName ) { return getPropertyValue( propName ).getClass(); } + public Class getPropertyType( String propName ) { return getPropertyValue( propName ).getClass(); } /** * Modifies the property value of the property passed @@ -129,7 +130,7 @@ public Object modifyPropertyValue( String propName, Object propValue ) throws Il checkNull( propName ); checkNull( propValue ); - if( hasPropertyWithName( propName ) == false ) + if( !hasPropertyWithName( propName ) ) return null; Object oldValue = removeProperty( propName ); @@ -174,11 +175,12 @@ public boolean equals( JposEntry otherEntry ) if( getPropertyCount() != otherEntry.getPropertyCount() ) return false; - Enumeration otherPropNames = otherEntry.getPropertyNames(); + @SuppressWarnings("unchecked") + Enumeration otherPropNames = otherEntry.getPropertyNames(); while( otherPropNames.hasMoreElements() ) { - String name = (String)otherPropNames.nextElement(); + String name = otherPropNames.nextElement(); Object value = otherEntry.getPropertyValue( name ); if( !hasPropertyWithName( name ) ) return false; @@ -197,11 +199,12 @@ public JposEntry copy() { JposEntry entryCopy = new SimpleEntry(); - Enumeration entryNames = getPropertyNames(); + @SuppressWarnings("unchecked") + Enumeration entryNames = getPropertyNames(); while( entryNames.hasMoreElements() ) { - String propName = (String)entryNames.nextElement(); + String propName = entryNames.nextElement(); entryCopy.addProperty( propName,getPropertyValue( propName ) ); } @@ -242,15 +245,17 @@ public JposEntry.Prop getProp( String propName ) * @return an Iterator over the properties in this JposEntry as JposEntry.Prop objects * @since 1.3 (Washington DC 2001) */ + @SuppressWarnings("rawtypes") public Iterator getProps() { - List list = new ArrayList(); + List list = new ArrayList<>(); - Enumeration names = getPropertyNames(); + @SuppressWarnings("unchecked") + Enumeration names = getPropertyNames(); while( names.hasMoreElements() ) { - String name = (String)names.nextElement(); + String name = names.nextElement(); list.add( new Prop( name, getPropertyValue( name ) ) ); } @@ -289,7 +294,7 @@ public void modify( JposEntry.Prop prop ) throws IllegalArgumentException { checkNull( prop ); - if( hasPropertyWithName( prop.getName() ) == false ) return; + if( !hasPropertyWithName( prop.getName() ) ) return; modifyPropertyValue( prop.getName(), prop.getValue() ); } @@ -312,12 +317,12 @@ public void modify( JposEntry.Prop prop ) throws IllegalArgumentException * @see jpos.config.JposEntryConst#PROP_TYPES * @since 2.0.0 */ - public JposEntry.Prop createProp( String propName, Object propValue, Class propType ) throws JposConfigException + public JposEntry.Prop createProp( String propName, Object propValue, @SuppressWarnings("rawtypes") Class propType ) throws JposConfigException { if( propName == null || propValue == null || propType == null ) throw new JposConfigException( "Cannot create JposEntry.Prop with null argument" ); - if( JposEntryUtility.validatePropValue( propValue, propType ) == false ) + if( !JposEntryUtility.validatePropValue( propValue, propType ) ) throw new JposConfigException( "Cannot create JposEntry.Prop with invalid value or type" ); return new Prop( propName, propValue ); @@ -331,14 +336,15 @@ public JposEntry.Prop createProp( String propName, Object propValue, Class propT * @return true if the two JposEntries have the same properties * @since 1.3 (SF 2K meeting) */ + @Override public boolean equals( Object object ) { if( object instanceof JposEntry ) - return equals( (JposEntry)object ); + return this.equals( (JposEntry)object ); return false; } - + /** * @return 0 if two entries are the same -1 if this is less or 1 of more than other * the comparison for > and < uses the logicalName of the entry to decide @@ -346,7 +352,7 @@ public boolean equals( Object object ) */ public int compareTo( Object other ) { - if( other == null || ( (other instanceof JposEntry ) == false ) ) + if( !(other instanceof JposEntry ) ) throw new RuntimeException( "Cannot compare: " + other + " with JposEntry: " + this ); JposEntry otherEntry = (JposEntry)other; @@ -362,7 +368,7 @@ public int compareTo( Object other ) */ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append( "\n" ); sb.append( "\t\n" ); @@ -372,10 +378,10 @@ public String toString() sb.append( "\n" ); - Enumeration otherPropNames = JposEntryUtility.getNonRequiredPropNames( this ); + Enumeration otherPropNames = JposEntryUtility.getNonRequiredPropNames( this ); while( otherPropNames.hasMoreElements() ) { - String name = (String)otherPropNames.nextElement(); + String name = otherPropNames.nextElement(); String value = getPropertyValue( name ).toString(); String typeClassName = JposEntryUtility.shortClassName( getPropertyValue( name ).getClass() ); @@ -418,7 +424,7 @@ protected static void checkNull( Object object ) throws IllegalArgumentException // Instance variables // - private Hashtable properties = new Hashtable(); + private Hashtable properties = new Hashtable<>(); private transient JposRegPopulator regPopulator = null; //------------------------------------------------------------------------- @@ -430,7 +436,7 @@ protected static void checkNull( Object object ) throws IllegalArgumentException * @author E. Michael Maximilien * @since 1.3 (Washington DC 2001) */ - public static class Prop implements JposEntry.Prop, Comparable + public static class Prop implements JposEntry.Prop { //--------------------------------------------------------------------- // Ctor(s) @@ -471,7 +477,7 @@ public Prop( String name, Object value ) throws IllegalArgumentException * primitive types e.g. Integer, Byte, Boolean, ... * @return the type of this property as a java.lang.Class object */ - public Class getType() { return typeClass; } + public Class getType() { return typeClass; } /** * Sets the name of this property @@ -496,7 +502,7 @@ public void setValue( Object objValue ) throws IllegalArgumentException { checkNull( objValue ); - if( JposEntryUtility.validatePropValue( objValue, objValue.getClass() ) == false ) + if( !JposEntryUtility.validatePropValue( objValue, objValue.getClass() ) ) throw new IllegalArgumentException( "Cannot set property named = " + getName() + " with value = " + objValue + " invalid value or type" ); @@ -509,7 +515,7 @@ public void setValue( Object objValue ) throws IllegalArgumentException * object passed * @param type the Class object */ - public boolean isOfType( Class type ) + public boolean isOfType( @SuppressWarnings("rawtypes") Class type ) { if( type == null || typeClass == null ) return false; @@ -573,7 +579,7 @@ public int compareTo( Object other ) * @throws java.lang.IllegalArgumentException if the object value type does not * match the Class type */ - private void setValue( Object object, Class type ) throws IllegalArgumentException + private void setValue( Object object, @SuppressWarnings("rawtypes") Class type ) throws IllegalArgumentException { checkNull( object ); checkNull( type ); @@ -591,7 +597,7 @@ private void setValue( Object object, Class type ) throws IllegalArgumentExcepti private String name = ""; private Object value = null; - private Class typeClass = null; + private Class typeClass = null; } //-------------------------------------------------------------------------- diff --git a/src/main/java/jpos/config/simple/SimpleEntryRegistry.java b/src/main/java/jpos/config/simple/SimpleEntryRegistry.java index ccca0f3..5031245 100644 --- a/src/main/java/jpos/config/simple/SimpleEntryRegistry.java +++ b/src/main/java/jpos/config/simple/SimpleEntryRegistry.java @@ -33,15 +33,8 @@ * @since 0.1 (Philly 99 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class SimpleEntryRegistry extends Object implements JposEntryRegistry +public class SimpleEntryRegistry implements JposEntryRegistry { - /** - * Default ctor - * @deprecated no longer used, see the 1 argument ctor - * @since 0.1 (Philly 99 meeting) - */ - public SimpleEntryRegistry() {} - /** * One-argument constructor * @param populator the JposRegPopulator used by the registry @@ -66,15 +59,16 @@ public boolean hasJposEntry( String logicalName ) * @return an enumeration of JposEntry objects * @since 0.1 (Philly 99 meeting) */ - public Enumeration getEntries() + @SuppressWarnings("rawtypes") + public Enumeration getEntries() { - Vector vector = new Vector(); - Enumeration entries = jposEntries.elements(); + List entryList = new ArrayList<>(); + Enumeration entries = jposEntries.elements(); while( entries.hasMoreElements() ) - vector.addElement( entries.nextElement() ); + entryList.add( entries.nextElement() ); - return vector.elements(); + return Collections.enumeration(entryList); } /** @@ -83,7 +77,7 @@ public Enumeration getEntries() * @since 0.1 (Philly 99 meeting) */ public JposEntry getJposEntry( String logicalName ) - { return (JposEntry)jposEntries.get( logicalName ); } + { return jposEntries.get( logicalName ); } /** * Modify the JposEntry with logicalName with the new entry indicated @@ -135,17 +129,16 @@ public void addJposEntry( JposEntry entry ) */ public void removeJposEntry( JposEntry entry ) { - Enumeration entries = jposEntries.elements(); + Enumeration entries = jposEntries.elements(); while( entries.hasMoreElements() ) { - JposEntry jposEntry = (JposEntry)entries.nextElement(); + JposEntry jposEntry = entries.nextElement(); - if( jposEntry.hasPropertyWithName( JposEntry. - LOGICAL_NAME_PROP_NAME ) ) + if( jposEntry.hasPropertyWithName( JposEntry.LOGICAL_NAME_PROP_NAME ) ) { - JposEntry removedEntry = (JposEntry)jposEntries. - remove( entry.getPropertyValue( JposEntry. + JposEntry removedEntry = + jposEntries.remove( entry.getPropertyValue( JposEntry. LOGICAL_NAME_PROP_NAME ) ); tracer.println( "Removed entry.logicalName = " + @@ -169,14 +162,13 @@ public void removeJposEntry( JposEntry entry ) */ public void removeJposEntry( String logicalName ) { - JposEntry entry = (JposEntry)jposEntries.get( logicalName ); + JposEntry entry = jposEntries.get( logicalName ); if( entry != null ) { jposEntries.remove( logicalName ); - fireJposEntryRegistryEventRemoved( new JposEntryRegistryEvent( - this, entry ) ); + fireJposEntryRegistryEventRemoved( new JposEntryRegistryEvent( this, entry ) ); } } @@ -233,18 +225,16 @@ public void load() jposEntries.clear(); getRegPopulator().load(); - Enumeration entries = getRegPopulator().getEntries(); + @SuppressWarnings("unchecked") + Enumeration entries = getRegPopulator().getEntries(); while( entries.hasMoreElements() ) { try { - JposEntry jposEntry = (JposEntry)entries.nextElement(); + JposEntry jposEntry = entries.nextElement(); - jposEntries.put( jposEntry. - getPropertyValue( JposEntry. - LOGICAL_NAME_PROP_NAME ), - jposEntry ); + jposEntries.put( jposEntry.getLogicalName(), jposEntry ); } catch( Exception e ) { tracer.print( e ); } } @@ -274,7 +264,7 @@ public void load() */ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append( "\n" ); @@ -285,13 +275,14 @@ public String toString() sb.append( "\n" ); - Enumeration entries = getEntries(); + @SuppressWarnings("unchecked") + Enumeration entries = getEntries(); int count = 0; while( entries.hasMoreElements() ) { sb.append( "" ); count++; } @@ -318,12 +309,13 @@ protected void fireJposEntryRegistryEventAdded( JposEntryRegistryEvent e ) "e.getJposEntry().logicalName = " + e.getJposEntry().getLogicalName() ); - Vector listenersClone = (Vector)listeners.clone(); + @SuppressWarnings("unchecked") + Vector listenersClone = (Vector) listeners.clone(); synchronized( listenersClone ) { for( int i = 0; i < listenersClone.size(); ++i ) - ((JposEntryRegistryListener)listenersClone.elementAt( i ) ). + (listenersClone.elementAt( i ) ). jposEntryAdded( e ); } } @@ -339,13 +331,13 @@ protected void fireJposEntryRegistryEventRemoved( JposEntryRegistryEvent e ) "e.getJposEntry().logicalName = " + e.getJposEntry().getLogicalName() ); - Vector listenersClone = (Vector)listeners.clone(); + @SuppressWarnings("unchecked") + Vector listenersClone = (Vector) listeners.clone(); synchronized( listenersClone ) { for( int i = 0; i < listenersClone.size(); ++i ) - ((JposEntryRegistryListener)listenersClone.elementAt( i ) ). - jposEntryRemoved( e ); + (listenersClone.elementAt( i ) ).jposEntryRemoved( e ); } } @@ -360,13 +352,13 @@ protected void fireJposEntryRegistryEventModified( JposEntryRegistryEvent e ) "e.getJposEntry().logicalName = " + e.getJposEntry().getLogicalName() ); - Vector listenersClone = (Vector)listeners.clone(); + @SuppressWarnings("unchecked") + Vector listenersClone = (Vector) listeners.clone(); synchronized( listenersClone ) { for( int i = 0; i < listenersClone.size(); ++i ) - ((JposEntryRegistryListener)listenersClone.elementAt( i ) ). - jposEntryModified( e ); + (listenersClone.elementAt( i ) ).jposEntryModified( e ); } } @@ -374,8 +366,8 @@ protected void fireJposEntryRegistryEventModified( JposEntryRegistryEvent e ) // Instance variables // - public Hashtable jposEntries = new Hashtable(); - private Vector listeners = new Vector(); + private final Hashtable jposEntries = new Hashtable<>(); + private final Vector listeners = new Vector<>(); private JposRegPopulator regPopulator = null; private boolean loaded = false; diff --git a/src/main/java/jpos/config/simple/SimpleRegPopulator.java b/src/main/java/jpos/config/simple/SimpleRegPopulator.java index fab4925..093ad11 100644 --- a/src/main/java/jpos/config/simple/SimpleRegPopulator.java +++ b/src/main/java/jpos/config/simple/SimpleRegPopulator.java @@ -26,7 +26,6 @@ import jpos.util.tracing.Tracer; import jpos.util.tracing.TracerFactory; import jpos.config.*; -import jpos.config.simple.AbstractRegPopulator; /** * Simple implementation of the JposRegPopulator loading and saving from a @@ -78,7 +77,8 @@ public String getClassName() * @since 1.2 (NY 2K meeting) * @throws java.lang.Exception if any error occurs while saving */ - public void save( Enumeration entries ) throws Exception + @SuppressWarnings("unchecked") + public void save( @SuppressWarnings("rawtypes") Enumeration entries ) throws Exception { saveJposEntries( entries ); } @@ -90,14 +90,14 @@ public void save( Enumeration entries ) throws Exception * @since 1.3 (SF 2K meeting) * @throws java.lang.Exception if any error occurs while saving */ - public void save( Enumeration entries, String fileName ) throws Exception + public void save( @SuppressWarnings("rawtypes") Enumeration entries, String fileName ) throws Exception { File file = new File( fileName ); - FileOutputStream fos = new FileOutputStream( file ); - - saveJposEntries( entries, fos ); - - fos.close(); + try (FileOutputStream fos = new FileOutputStream( file )) { + @SuppressWarnings("unchecked") + Enumeration typeSafeEntries = entries; + saveJposEntries( typeSafeEntries, fos ); + } } /** @@ -106,19 +106,18 @@ public void save( Enumeration entries, String fileName ) throws Exception */ public void load() { - getJposEntries().clear(); - Enumeration entries = readJposEntries(); + clearAllJposEntries(); + Enumeration entries = readJposEntries(); while( entries.hasMoreElements() ) { try { - JposEntry entry = (JposEntry)entries.nextElement(); - String logicalName = logicalName = (String)entry. - getPropertyValue( JposEntry.LOGICAL_NAME_PROP_NAME ); + JposEntry entry = entries.nextElement(); + String logicalName = entry.getLogicalName(); if( logicalName != null ) - getJposEntries().put( logicalName, entry ); + addJposEntry( logicalName, entry ); lastLoadException = null; } @@ -138,19 +137,19 @@ public void load() */ public void load( String fileName ) { - try + try (FileInputStream fis = new FileInputStream( fileName )) { - getJposEntries().clear(); - Enumeration entries = readJposEntries( new FileInputStream( fileName ) ); + clearAllJposEntries(); + Enumeration entries = readJposEntries( fis ); while( entries.hasMoreElements() ) { - JposEntry entry = (JposEntry)entries.nextElement(); + JposEntry entry = entries.nextElement(); String logicalName = (String)entry. getPropertyValue( JposEntry.LOGICAL_NAME_PROP_NAME ); if( logicalName != null ) - getJposEntries().put( logicalName, entry ); + addJposEntry(logicalName, entry); } lastLoadException = null; @@ -201,66 +200,62 @@ public URL getEntriesURL() * @since 1.2 (NY 2K meeting) * @throws java.lang.Exception if any problems occurs while saving */ - protected void saveSerInZipFile( Enumeration entries ) throws Exception + protected void saveSerInZipFile( Enumeration entries ) throws Exception { - ZipOutputStream zos = new ZipOutputStream( new - FileOutputStream( zipSerFile.getName() + ".temp.jar" ) ); - - Enumeration zipEntries = zipSerFile.entries(); - - while( zipEntries.hasMoreElements() ) + try( ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( zipSerFile.getName() + ".temp.jar" ) ) ) { - ZipEntry zipEntry = (ZipEntry)zipEntries.nextElement(); - - zos.putNextEntry( zipEntry ); - - if( zipEntry.getName() != serFileName ) - { - InputStream is = zipSerFile.getInputStream( zipEntry ); - - while( is.available() > 0 ) - { - byte[] byteArray = new byte[ is.available() ]; - - is.read( byteArray ); - - zos.write( byteArray ); - } - - zos.closeEntry(); - } - else - { - ObjectOutputStream oos = new ObjectOutputStream( new - FileOutputStream( TEMP_SER_FILE_NAME ) ); - - while( entries.hasMoreElements() ) - { - JposEntry entry = (JposEntry)entries.nextElement(); - - oos.writeObject( entry ); - } - - oos.flush(); - oos.close(); - - FileInputStream fis = new FileInputStream( TEMP_SER_FILE_NAME ); - - while( fis.available() > 0 ) - { - byte[] byteArray = new byte[ fis.available() ]; - - fis.read( byteArray ); - - zos.write( byteArray ); - } - - zos.closeEntry(); - } + Enumeration zipEntries = zipSerFile.entries(); + + while( zipEntries.hasMoreElements() ) + { + ZipEntry zipEntry = zipEntries.nextElement(); + + zos.putNextEntry( zipEntry ); + + if( zipEntry.getName() != serFileName ) + { + InputStream is = zipSerFile.getInputStream( zipEntry ); + + while( is.available() > 0 ) + { + byte[] byteArray = new byte[ is.available() ]; + + is.read( byteArray ); + + zos.write( byteArray ); + } + + zos.closeEntry(); + } + else + { + try( ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( TEMP_SER_FILE_NAME ) ) ) + { + while( entries.hasMoreElements() ) + { + JposEntry entry = entries.nextElement(); + + oos.writeObject( entry ); + } + } + + + try( FileInputStream fis = new FileInputStream( TEMP_SER_FILE_NAME ) ) + { + while( fis.available() > 0 ) + { + byte[] byteArray = new byte[ fis.available() ]; + + fis.read( byteArray ); + + zos.write( byteArray ); + } + + zos.closeEntry(); + } + } + } } - - zos.flush(); - zos.close(); } /** @@ -269,9 +264,11 @@ protected void saveSerInZipFile( Enumeration entries ) throws Exception * @since 1.2 (NY 2K meeting) * @throws java.lang.Exception if any problems occurs while saving */ - protected void saveSerFile( Enumeration entries ) throws Exception + protected void saveSerFile( Enumeration entries ) throws Exception { - saveJposEntries( entries, new FileOutputStream( serFileName ) ); + try (OutputStream os = new FileOutputStream( serFileName )) { + saveJposEntries( entries, os); + } } /** @@ -281,19 +278,17 @@ protected void saveSerFile( Enumeration entries ) throws Exception * @since 1.2 (NY 2K meeting) * @throws java.lang.Exception if any error occurs while saving */ - protected void saveJposEntries( Enumeration entries, OutputStream os ) + protected void saveJposEntries( Enumeration entries, OutputStream os ) throws Exception { - ObjectOutputStream oos = new ObjectOutputStream( os ); - - while( entries.hasMoreElements() ) - { - JposEntry entry = (JposEntry)entries.nextElement(); - - oos.writeObject( entry ); + try (ObjectOutputStream oos = new ObjectOutputStream( os )) { + while( entries.hasMoreElements() ) + { + JposEntry entry = entries.nextElement(); + + oos.writeObject( entry ); + } } - - oos.close(); } /** @@ -304,7 +299,7 @@ protected void saveJposEntries( Enumeration entries, OutputStream os ) */ protected ObjectInputStream findSerOIS() { - Vector classpathJarFiles = new Vector(); + List classpathJarFiles = new ArrayList<>(); //Try to find the serialized file in the directory of each path in CLASSPATH //As a side effect put each JAR/Zip file in the vector @@ -320,12 +315,12 @@ protected ObjectInputStream findSerOIS() /** * Finds the first serialized JposEntry file in directory of each classpath - * NOTE:Decorated the FileInputStream with a BufferedInputStream to - * improve load time... - * @param jarZipFilesVector a vector of JAR/Zip file names + * @param jarZipFilePaths a list of JAR/Zip file names + * @return an {@link ObjectInputStream} object opened for the given ZIP files paths.
+ * This object must be closed by the caller! * @since 1.2 (NY 2K meeting) */ - protected ObjectInputStream findSerOISInClasspath( Vector jarZipFilesVector ) + protected ObjectInputStream findSerOISInClasspath( List jarZipFilePaths ) { ObjectInputStream ois = null; @@ -345,7 +340,7 @@ protected ObjectInputStream findSerOISInClasspath( Vector jarZipFilesVector ) if( path.equals("") ) continue; if( path.length() > 4 && ( path.endsWith( ".zip" ) || path.endsWith( ".jar" ) ) ) - jarZipFilesVector.addElement( path ); + jarZipFilePaths.add( path ); else { absoluteFileName = path + fileSeparator + serFileName; @@ -365,25 +360,25 @@ protected ObjectInputStream findSerOISInClasspath( Vector jarZipFilesVector ) /** * Finds the first serialized JposEntry file in the JAR files - * @param jarFilesVector a vector of JAR/Zip file names + * @param jarFilePaths a vector of JAR/Zip file names + * @return an {@link ObjectInputStream} object opened for the given JAR files paths.
+ * This object must be closed by the caller! * @since 1.2 (NY 2K meeting) */ - protected ObjectInputStream findSerOISInJar( Vector jarFilesVector ) + protected ObjectInputStream findSerOISInJar( List jarFilePaths ) { ObjectInputStream ois = null; - for( int i = 0; i < jarFilesVector.size(); ++i ) + for(String jarFileName : jarFilePaths) { - String jarFileName = (String)jarFilesVector.elementAt( i ); - try + try (ZipFile zipFile = new ZipFile( jarFileName )) { - ZipFile zipFile = new ZipFile( jarFileName ); - Enumeration zipEntries = zipFile.entries(); + Enumeration zipEntries = zipFile.entries(); while( zipEntries.hasMoreElements() ) { - ZipEntry zipEntry = (ZipEntry)zipEntries.nextElement(); + ZipEntry zipEntry = zipEntries.nextElement(); String entryName = zipEntry.getName(); if( entryName.endsWith( serFileName ) ) { @@ -408,9 +403,9 @@ protected ObjectInputStream findSerOISInJar( Vector jarFilesVector ) * @param is the InputStream from which to read the serialized entries from * @since 1.2 (NY 2K meeting) */ - protected Enumeration readJposEntries( InputStream is ) + protected Enumeration readJposEntries( InputStream is ) { - Vector entries = new Vector(); + List entries = new ArrayList<>(); try { @@ -429,33 +424,51 @@ protected Enumeration readJposEntries( InputStream is ) serFileName ); else while( true ) - entries.addElement( in.readObject() ); + entries.add( (JposEntry) in.readObject() ); serFileName = absoluteFileName; } - catch( EOFException eofe ) {} + catch( EOFException eofe ) { + tracer.println( "ERROR while reading serialized JposEntry file: " + + serFileName + " Exception.message=" + + eofe.getMessage() ); + + } catch( Exception e ) - { tracer.println( "ERROR while reading serialized JposEntry file: " + + { + tracer.println( "ERROR while reading serialized JposEntry file: " + serFileName + " Exception.message=" + - e.getMessage() ); } - - return entries.elements(); + e.getMessage() ); + } + return Collections.enumeration(entries); } /** * @return an Enumeration of JposEntry objects * @since 1.2 (NY 2K meeting) */ - protected Enumeration readJposEntries() + protected Enumeration readJposEntries() { - Enumeration entries = null; - - if( isPopulatorFileDefined() ) - try { entries = readJposEntries( getPopulatorFileIS() ); } - catch( Exception e ) - { entries = ( new Vector() ).elements(); } - else - entries = readJposEntries( findSerOIS() ); + Enumeration entries = null; + + if( isPopulatorFileDefined() ) { + try (InputStream inputStream = getPopulatorFileIS()) { + entries = readJposEntries( inputStream ); + } + catch( Exception e ) + { + entries = Collections.enumeration(new ArrayList<>()); + } + } + else { + try (InputStream inputStream = findSerOIS()) { + entries = readJposEntries( findSerOIS() ); + } + catch( Exception e ) + { + entries = Collections.enumeration(new ArrayList<>()); + } + } return entries; } @@ -466,10 +479,12 @@ protected Enumeration readJposEntries() * @since 1.2 (NY 2K meeting) * @throws java.lang.Exception if any error occurs while saving */ - protected void saveJposEntries( Enumeration entries ) throws Exception + protected void saveJposEntries( Enumeration entries ) throws Exception { if( isPopulatorFileDefined() ) - saveJposEntries( entries, getPopulatorFileOS() ); + try (OutputStream outputStream = getPopulatorFileOS()) { + saveJposEntries( entries, outputStream ); + } else { if( serInZipFile ) @@ -491,7 +506,7 @@ protected void saveJposEntries( Enumeration entries ) throws Exception private String absoluteFileName = ""; private String serFileName = DEFAULT_JPOS_SER_FILE_NAME; - private Tracer tracer = TracerFactory.getInstance(). + private final Tracer tracer = TracerFactory.getInstance(). createTracer( "SimpleRegPopulator" ); //-------------------------------------------------------------------------- diff --git a/src/main/java/jpos/config/simple/xml/AbstractXercesRegPopulator.java b/src/main/java/jpos/config/simple/xml/AbstractXercesRegPopulator.java deleted file mode 100644 index d77542d..0000000 --- a/src/main/java/jpos/config/simple/xml/AbstractXercesRegPopulator.java +++ /dev/null @@ -1,545 +0,0 @@ -package jpos.config.simple.xml; - -/////////////////////////////////////////////////////////////////////////////// -// -// This software is provided "AS IS". The JavaPOS working group (including -// each of the Corporate members, contributors and individuals) MAKES NO -// REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, -// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED -// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NON-INFRINGEMENT. The JavaPOS working group shall not be liable for -// any damages suffered as a result of using, modifying or distributing this -// software or its derivatives. Permission to use, copy, modify, and distribute -// the software and its documentation for any purpose is hereby granted. -// -// The JavaPOS Config/Loader (aka JCL) is now under the CPL license, which -// is an OSS Apache-like license. The complete license is located at: -// http://www.ibm.com/developerworks/library/os-cpl.html -// -/////////////////////////////////////////////////////////////////////////////// - -import java.io.*; -import java.util.*; -import java.net.URL; -import java.text.DateFormat; - -import java.io.PrintWriter; - -import org.apache.xerces.dom.DOMImplementationImpl; -import org.apache.xerces.parsers.DOMParser; -import org.apache.xml.serialize.OutputFormat; -import org.apache.xml.serialize.XMLSerializer; - -import org.w3c.dom.*; - -import jpos.config.*; -import jpos.config.simple.*; -import jpos.util.*; -import jpos.util.tracing.Tracer; -import jpos.util.tracing.TracerFactory; - -/** - * This class is an abstract super class for all Xerces based parser/reg - * populator with functionality to serialize an enumeration of JposEntry - * objects into XML - *

- * NOTE: this class must define a public no-argument ctor so that it may be - * created via reflection when its defined in the jpos.properties as the - * jpos.config.regPopulatorClass - *

- * @see jpos.util.JposProperties#JPOS_REG_POPULATOR_CLASS_PROP_NAME - * @since 2.1.0 - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ -public abstract class AbstractXercesRegPopulator - extends AbstractRegPopulator - implements XmlRegPopulator -{ - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** - * 1-arg constructor that takes the unique ID - * @param s the unique ID string - * @since 1.3 (Washington DC 2001) - */ - public AbstractXercesRegPopulator( String s ) { super( s ); } - - //------------------------------------------------------------------------- - // Public methods - // - - /** - * Tell the populator to save the current entries - * @param entries an enumeration of JposEntry objects - * @since 1.2 (NY 2K meeting) - * @throws java.lang.Exception if any error occurs while saving - */ - public void save( Enumeration entries ) throws Exception - { - if( isPopulatorFileDefined() ) - convertJposEntriesToXml( entries, getPopulatorFileOS() ); - else - convertJposEntriesToXml( entries, - new FileOutputStream( getDefaultXmlFileName() ) ); - } - - /** - * Tell the populator to save the current entries in the file specified - * @param entries an enumeration of JposEntry objects - * @param xmlFileName the XML file name to save entries - * @since 1.3 (SF 2K meeting) - * @throws java.lang.Exception if any error occurs while saving - */ - public void save( Enumeration entries, String xmlFileName ) - throws Exception - { - File xmlFile = new File( xmlFileName ); - FileOutputStream fos = new FileOutputStream( xmlFile ); - - convertJposEntriesToXml( entries, fos ); - - fos.close(); - } - - /** - * @return the URL pointing to the entries file loaded or saved - * @since 1.2 (NY 2K meeting) - */ - public URL getEntriesURL() - { - URL url = null; - - if( getPopulatorFileURL() != null && - !getPopulatorFileURL().equals( "" ) ) - try - { url = new URL( getPopulatorFileURL() ); } - catch( Exception e ) - { - tracer.println( "getEntriesURL: Exception.message=" + - e.getMessage() ); - } - else - url = createURLFromFile( new File( getPopulatorFileName() ) ); - - // - tracer.println( "getPopulatorFileURL()=" + getPopulatorFileURL() ); - tracer.println( "getPopulatorFileName()=" + getPopulatorFileName() ); - // - - return url; - } - - //-------------------------------------------------------------------------- - // Protected methods - // - - /** @return the Tracer object */ - protected Tracer getTracer() { return tracer; } - - /** - * @return the default XML file name that this populator will save - * entries to - */ - protected String getDefaultXmlFileName() { return xmlFileName; } - - /** - * Converts an Enumeration of JposEntry objects to XML - * @param entries an Enumeration of JposEntry objects - * @param os the OutputStream to stream the entries to - * @exception java.lang.Exception if something goes wrong serializing - * @since 1.2 (NY 2K meeting) - */ - protected void convertJposEntriesToXml( Enumeration entries, - OutputStream os ) - throws Exception - { - Document document = getParser().getDocument(); - serializeDocument( document, entries, os ); - } - - /** - * @return the DOM parser object - * @since 1.2 (NY 2K meeting) - */ - protected DOMParser getParser() { return domParser; } - - /** - * Serializes the JposEntry objects to an XML document and save to OutputStream - * @param document the XML document object - * @param entries an Enumeration of JposEntry objects - * @param os the OuputStream object - * @exception java.lang.Exception anything goes wrong while saving - * @since 1.2 (NY 2K meeting) - */ - protected void serializeDocument( Document document, - Enumeration entries, - OutputStream os ) throws Exception - { - Document newDoc = createEmptyDocument(); - - insertJposEntriesInDoc( newDoc, entries ); - - insertDateSavedComment( newDoc ); - - OutputFormat outFormat = new OutputFormat( "xml", "UTF-8", true ); - - outFormat.setStandalone( false ); - outFormat.setIndenting( true ); - outFormat.setIndent( 4 ); - outFormat.setPreserveSpace( true ); - outFormat.setLineWidth( 0 ); - - insertDTDInfo( newDoc, outFormat ); - - PrintWriter outWriter = null; - try - { - outWriter = new PrintWriter - (new BufferedWriter(new OutputStreamWriter(os, "UTF-8"))); - } - catch( UnsupportedEncodingException ex ) - { - tracer.println( "Error making PrintWriter: " + - "UnsupportedEncodingException.message = " + - ex.getMessage() ); - } - - if( outWriter != null ) - { - XMLSerializer xmlSerializer = new XMLSerializer( outWriter, outFormat ); - xmlSerializer.serialize( newDoc ); - } - } - - /** - * @return a String with the document type definition value. For DTD this - * would be the DTD relative path/file and for schemas the XSD - * relative path/file - * @since 2.1.0 - */ - protected String getDoctypeValue() { return "jpos/res/jcl.dtd"; } - - /** - * Inset DTD information in the XML Document object - * @param doc the XML Document object - * @param outFormat the OuputFormat object - * @exception java.lang.Exception in case something goes wrong - * @since 1.2 (NY 2K meeting) - */ - protected void insertDTDInfo( Document doc, OutputFormat outFormat ) throws Exception - { - String publicId = OutputFormat.whichDoctypePublic( doc ); - String systemId = OutputFormat.whichDoctypeSystem( doc ); - - outFormat.setDoctype( "JposEntries", getDoctypeValue() ); - } - - /** - * @return an empty XML Document object - * @since 1.2 (NY 2K meeting) - */ - protected Document createEmptyDocument() - { - DOMImplementationImpl domImpImpl = (DOMImplementationImpl) - DOMImplementationImpl.getDOMImplementation(); - DocumentType docType = domImpImpl. - createDocumentType( "JposEntries", - "-//JavaPOS//DTD//EN", - getDoctypeValue() ); - - Document doc = domImpImpl.createDocument( null, "JposEntries", docType ); - - return doc; - } - - /** - * Inserts date and info saved in the XML Document object - * @param document the XML Document object - * @exception java.lang.Exception in case something goes wrong - * @since 1.2 (NY 2K meeting) - */ - protected void insertDateSavedComment( Document document ) - throws Exception - { - String dateString = DateFormat.getInstance(). - format( new Date( System.currentTimeMillis() ) ); - - String commentString = - "Saved by JavaPOS jpos.config/loader (JCL) version " + - Version.getVersionString() + " on " + dateString; - - Comment comment = document.createComment( commentString ); - - document.getDocumentElement(). - insertBefore( comment, document.getDocumentElement().getFirstChild() ); - - document.getDocumentElement(). - insertBefore( document.createTextNode( "\n" ), comment ); - - document.getDocumentElement(). - appendChild( document.createTextNode( "\n" ) ); - } - - /** - * Appends the <creation> element to the document - * @param doc the XML Document object - * @param jposEntryElement the <JposEntryElement> XML Element object - * @param jposEntry the JposEntry object - * @since 1.2 (NY 2K meeting) - */ - protected void appendCreationElement( Document doc, - Element jposEntryElement, - JposEntry jposEntry ) - { - jposEntryElement.appendChild( doc.createTextNode( " " + " " ) ); - - Element creationElement = doc.createElement( "creation" ); - - Attr factoryClassAttr = doc.createAttribute( "factoryClass" ); - Attr serviceClassAttr = doc.createAttribute( "serviceClass" ); - - factoryClassAttr.setValue( (String)jposEntry. - getPropertyValue( "serviceInstanceFactoryClass" ) ); - - serviceClassAttr.setValue( (String)jposEntry. - getPropertyValue( "serviceClass" ) ); - - creationElement.setAttributeNode( factoryClassAttr ); - creationElement.setAttributeNode( serviceClassAttr ); - - jposEntryElement.appendChild( creationElement ); - jposEntryElement.appendChild( doc.createTextNode( "\n" ) ); - } - - /** - * Appends the <vendor> element to the document - * @param doc the XML Document object - * @param jposEntryElement the <JposEntryElement> XML Element object - * @param jposEntry the JposEntry object - * @since 1.2 (NY 2K meeting) - */ - protected void appendVendorElement( Document doc, Element jposEntryElement, - JposEntry jposEntry ) - { - jposEntryElement.appendChild( doc.createTextNode( " " + " " ) ); - - Element vendorElement = doc.createElement( "vendor" ); - - Attr nameAttr = doc.createAttribute( "name" ); - Attr urlAttr = doc.createAttribute( "url" ); - - nameAttr.setValue( (String)jposEntry.getPropertyValue( "vendorName" ) ); - urlAttr.setValue( (String)jposEntry.getPropertyValue( "vendorURL" ) ); - - vendorElement.setAttributeNode( nameAttr ); - vendorElement.setAttributeNode( urlAttr ); - - jposEntryElement.appendChild( vendorElement ); - jposEntryElement.appendChild( doc.createTextNode( "\n" ) ); - } - - /** - * Appends the <jpos> element to the document - * @param doc the XML Document object - * @param jposEntryElement the <JposEntryElement> XML Element object - * @param jposEntry the JposEntry object - * @since 1.2 (NY 2K meeting) - */ - protected void appendJposElement( Document doc, Element jposEntryElement, - JposEntry jposEntry ) - { - jposEntryElement.appendChild( doc.createTextNode( " " + " " ) ); - - Element jposElement = doc.createElement( "jpos" ); - - Attr versionAttr = doc.createAttribute( "version" ); - Attr categoryAttr = doc.createAttribute( "category" ); - - versionAttr.setValue( (String)jposEntry. - getPropertyValue( "jposVersion" ) ); - - categoryAttr.setValue( (String)jposEntry. - getPropertyValue( "deviceCategory" ) ); - - jposElement.setAttributeNode( versionAttr ); - jposElement.setAttributeNode( categoryAttr ); - - jposEntryElement.appendChild( jposElement ); - jposEntryElement.appendChild( doc.createTextNode( "\n" ) ); - } - - /** - * Appends the <product> element to the document - * @param doc the XML Document object - * @param jposEntryElement the <JposEntryElement> XML Element object - * @param jposEntry the JposEntry object - * @since 1.2 (NY 2K meeting) - */ - protected void appendProductElement( Document doc, - Element jposEntryElement, - JposEntry jposEntry ) - { - jposEntryElement.appendChild( doc.createTextNode( " " + " " ) ); - - Element productElement = doc.createElement( "product" ); - - Attr nameAttr = doc.createAttribute( "name" ); - Attr descriptionAttr = doc.createAttribute( "description" ); - Attr urlAttr = doc.createAttribute( "url" ); - - nameAttr.setValue( (String)jposEntry.getPropertyValue( "productName" ) ); - - descriptionAttr. - setValue( (String)jposEntry.getPropertyValue( "productDescription" ) ); - - urlAttr.setValue( (String)jposEntry.getPropertyValue( "productURL" ) ); - - productElement.setAttributeNode( nameAttr ); - productElement.setAttributeNode( descriptionAttr ); - productElement.setAttributeNode( urlAttr ); - - jposEntryElement.appendChild( productElement ); - jposEntryElement.appendChild( doc.createTextNode( "\n" ) ); - } - - /** - * Appends the <prop> element to the document - * @param doc the XML Document object - * @param jposEntryElement the <JposEntryElement> XML Element object - * @param propName the property name - * @param propValue the property value - * @since 1.2 (NY 2K meeting) - */ - protected void appendPropElement( Document doc, Element jposEntryElement, - String propName, Object propValue ) - { - jposEntryElement.appendChild( doc.createTextNode( " " + " " ) ); - - Element propElement = doc.createElement( "prop" ); - - Attr nameAttr = doc.createAttribute( "name" ); - Attr valueAttr = doc.createAttribute( "value" ); - Attr typeAttr = doc.createAttribute( "type" ); - - nameAttr.setValue( propName ); - valueAttr.setValue( propValue.toString() ); - typeAttr.setValue( JposEntryUtility. - shortClassName( propValue.getClass() ) ); - - propElement.setAttributeNode( nameAttr ); - propElement.setAttributeNode( valueAttr ); - propElement.setAttributeNode( typeAttr ); - - jposEntryElement.appendChild( propElement ); - jposEntryElement.appendChild( doc.createTextNode( "\n" ) ); - } - - /** - * Appends non-required properties name and value - * @param doc the XML Document object - * @param jposEntryElement the <JposEntryElement> XML Element object - * @param jposEntry the JposEntry object - * @since 1.2 (NY 2K meeting) - */ - protected void appendPropElements( Document doc, Element jposEntryElement, - JposEntry jposEntry ) - { - jposEntryElement.appendChild( doc. - createTextNode( "\n" + " " + " " ) ); - - String comment = "Other non JavaPOS required property" + - " (mostly vendor properties and bus specific " + - "properties i.e. RS232 )"; - - jposEntryElement.appendChild( doc.createComment( comment ) ); - - jposEntryElement.appendChild( doc.createTextNode( "\n" ) ); - - Enumeration props = jposEntry.getPropertyNames(); - - while( props.hasMoreElements() ) - { - String propName = (String)props.nextElement(); - - if( !JposEntryUtility.isRequiredPropName( propName ) ) - appendPropElement( doc, jposEntryElement, propName, - jposEntry.getPropertyValue( propName ) ); - } - } - - /** - * Insert the <JposEntryElement> in the XML document object - * @param doc the XML Document object - * @param jposEntryElement the <JposEntryElement> XML Element object - * @param jposEntry the JposEntry object - * @since 1.2 (NY 2K meeting) - */ - protected void insertJposEntryInDoc( Document doc, Element jposEntryElement, - JposEntry jposEntry ) - { - appendCreationElement( doc, jposEntryElement, jposEntry ); - appendVendorElement( doc, jposEntryElement, jposEntry ); - appendJposElement( doc, jposEntryElement, jposEntry ); - appendProductElement( doc, jposEntryElement, jposEntry ); - appendPropElements( doc, jposEntryElement, jposEntry ); - - doc.getDocumentElement(). - appendChild( doc.createTextNode( "\n" + " " ) ); - doc.getDocumentElement(). - appendChild( jposEntryElement ); - doc.getDocumentElement(). - appendChild( doc.createTextNode( "\n" + " " ) ); - } - - /** - * Insert an Enumeration of <JposEntryElement> objects in the XML document - * @param doc the XML Document object - * @param entries an Enumeration of JposEntry objects - * @since 1.2 (NY 2K meeting) - */ - protected void insertJposEntriesInDoc( Document doc, Enumeration entries ) - { - while( entries.hasMoreElements() ) - { - JposEntry jposEntry = (JposEntry)entries.nextElement(); - - if( JposEntryUtility.isValidJposEntry( jposEntry ) ) - { - doc.getDocumentElement(). - appendChild( doc.createTextNode( "\n" + " " ) ); - - Element jposEntryElement = doc.createElement( "JposEntry" ); - - Attr logicalNameAttr = doc.createAttribute( "logicalName" ); - logicalNameAttr.setValue( (String)jposEntry. - getPropertyValue( "logicalName" ) ); - - jposEntryElement.setAttributeNode( logicalNameAttr ); - - jposEntryElement.appendChild( doc.createTextNode( "\n" ) ); - - insertJposEntryInDoc( doc, jposEntryElement, jposEntry ); - } - } - } - - //-------------------------------------------------------------------------- - // Instance variables - // - - protected String xmlFileName = DEFAULT_XML_FILE_NAME; - - protected DOMParser domParser = new DOMParser(); - - private Tracer tracer = TracerFactory.getInstance(). - createTracer( "AbstractXercesRegPopulator" ); - - //-------------------------------------------------------------------------- - // Public constants - // - - public static final String DTD_FILE_PATH = "jpos" + File.separator + "res"; - public static final String DTD_FILE_NAME = DTD_FILE_PATH + File.separator + "jcl.dtd"; -} \ No newline at end of file diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java new file mode 100644 index 0000000..4b61fac --- /dev/null +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -0,0 +1,490 @@ +package jpos.config.simple.xml; + +/////////////////////////////////////////////////////////////////////////////// +// +// This software is provided "AS IS". The JavaPOS working group (including +// each of the Corporate members, contributors and individuals) MAKES NO +// REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, +// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED +// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NON-INFRINGEMENT. The JavaPOS working group shall not be liable for +// any damages suffered as a result of using, modifying or distributing this +// software or its derivatives. Permission to use, copy, modify, and distribute +// the software and its documentation for any purpose is hereby granted. +// +// The JavaPOS Config/Loader (aka JCL) is now under the CPL license, which +// is an OSS Apache-like license. The complete license is located at: +// http://www.ibm.com/developerworks/library/os-cpl.html +// +/////////////////////////////////////////////////////////////////////////////// + +import jpos.config.JposEntry; +import jpos.config.JposEntry.Prop; +import jpos.config.simple.AbstractRegPopulator; +import jpos.config.simple.SimpleEntry; +import jpos.loader.Version; +import jpos.util.JposEntryUtility; +import jpos.util.tracing.Tracer; +import jpos.util.tracing.TracerFactory; +import org.w3c.dom.*; +import org.xml.sax.*; +import org.xml.sax.ext.DefaultHandler2; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import java.io.*; +import java.net.URL; +import java.text.DateFormat; +import java.util.*; +import java.util.stream.Collectors; + +/** + * Simple implementation of the JposRegPopulator that loads and saves + * the entries in XML using the Javax API. + *
+ * NOTE: this class must define a public no-argument constructor so that it may be + * created via reflection when it is defined in the jpos.properties file by + * the jpos.config.regPopulatorClass property. + * + * @since 4.0 + * @author M. Conrad (martin.conrad@freenet.de) + */ +public class JavaxRegPopulator + extends AbstractRegPopulator + implements XmlRegPopulator +{ + /** + * Default ctor + */ + public JavaxRegPopulator() { + super(JavaxRegPopulator.class.getName()); + } + + /** + * 1-arg constructor that takes the unique ID + * + * @param s the unique ID string + * @since 1.3 + */ + public JavaxRegPopulator(String s) { + super(s); + } + + //------------------------------------------------------------------------- + // Public methods + // + + @Override + public String getClassName() { + return JavaxRegPopulator.class.getName(); + } + + @Override + public URL getEntriesURL() { + URL url = null; + + if (getPopulatorFileURL() != null && !getPopulatorFileURL().equals("")) { + try { + url = new URL(getPopulatorFileURL()); + } catch (Exception e) { + tracer.println("getEntriesURL: Exception.message=" + e.getMessage()); + } + } + else + url = createURLFromFile(new File(getPopulatorFileName())); + + tracer.println("getPopulatorFileURL()=" + getPopulatorFileURL()); + tracer.println("getPopulatorFileName()=" + getPopulatorFileName()); + + return url; + } + + @SuppressWarnings("unchecked") + @Override + public void save(@SuppressWarnings("rawtypes") Enumeration entries) throws Exception + { + if (isPopulatorFileDefined()) + save(entries, getPopulatorFileOS()); + else { + try (FileOutputStream os = new FileOutputStream(DEFAULT_XML_FILE_NAME)) { + save(entries, os); + } + } + } + + @SuppressWarnings("unchecked") + @Override + public void save(@SuppressWarnings("rawtypes") Enumeration entries, String fileName) throws Exception + { + tracer.println("saving JavaPOS configuration to file " + new File(fileName).getAbsolutePath()); + try (FileOutputStream os = new FileOutputStream(fileName)) { + save(entries, os); + } + } + + @Override + public void load() { + try (InputStream is = isPopulatorFileDefined() ? new FileInputStream(DEFAULT_XML_FILE_NAME) : getPopulatorFileIS()) { + load(is); + } catch (Exception e) { + tracer.println("Error while loading populator file Exception.message: " + e.getMessage()); + lastLoadException = e; + } + } + + @Override + public void load(String fileName) { + tracer.println("loading JavaPOS configuration from file " + new File(fileName).getAbsolutePath()); + try (InputStream is = new File(fileName).exists() ? new FileInputStream(fileName) : findFileInClasspath(fileName)) { + load(is); + } catch (Exception e) { + tracer.println("Error while loading populator file Exception.message: " + e.getMessage()); + lastLoadException = e; + } + } + + @Override + public String getName() { + return JAVAX_REG_POPULATOR_NAME_STRING; + } + + //-------------------------------------------------------------------------- + // Private methods + // + + private void save(Enumeration entries, OutputStream outputStream) + throws ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException { + Document document = createEmptyDocument(); + + insertJposEntriesInDoc(entries, document); + insertDateSavedComment(document); + + TransformerFactory factory = TransformerFactory.newInstance(); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, XML_RESTRICTED_ACCESS_ATTRIBUTE); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, XML_RESTRICTED_ACCESS_ATTRIBUTE); + + DOMSource source = new DOMSource(document); + Transformer transformer = factory.newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, document.getDoctype().getPublicId()); + transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, document.getDoctype().getSystemId()); + transformer.transform(source, new StreamResult(outputStream)); + } + + private Document createEmptyDocument() + throws ParserConfigurationException { + DOMImplementation domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); + DocumentType docType = domImpl.createDocumentType(XML_TAG_JPOSENTRIES, DTD_DOC_TYPE_VALUE, DTD_FILE_NAME); + + return domImpl.createDocument(null, XML_TAG_JPOSENTRIES, docType); + } + + + private void insertJposEntriesInDoc(Enumeration entries, Document doc) { + while (entries.hasMoreElements()) { + JposEntry jposEntry = entries.nextElement(); + + if (JposEntryUtility.isValidJposEntry(jposEntry)) { + Element jposEntryElement = doc.createElement(XML_TAG_JPOSENTRY); + + jposEntryElement.setAttribute("logicalName", jposEntry.getLogicalName()); + insertJposPropertiesInElement(doc, jposEntry, jposEntryElement); + doc.getDocumentElement().appendChild(jposEntryElement); + } + } + } + + /** + * Inserts {@value XmlRegPopulator#XML_TAG_JPOSENTRY} node content for a given {@link JposEntry} object into + * the given XML {@link Document}. For better readability, first the XML elements for + * {@value XmlRegPopulator#XML_TAG_CREATION}, {@value XmlRegPopulator#XML_TAG_JPOS}, + * {@value XmlRegPopulator#XML_TAG_PRODUCT}, and {@value XmlRegPopulator#XML_TAG_VENDOR} + * will be created, followed by (optional) {@value XmlRegPopulator#XML_TAG_PROP} elements in alphabetic order.
+ * Keep in mind that the {@link JposEntry.Prop}s represent either an XML attribute of XML element of the above listed + * tag names or an XML element with tag name {@value XmlRegPopulator#XML_TAG_PROP}. + * + * @param doc The XML document to be modified. + * @param jposEntry The JposEntry object to be added. + * @param jposEntryElement The XML element the JposEntry tag shall be added to. This should be the JposEntries + * tag of the document. + */ + private void insertJposPropertiesInElement(Document doc, JposEntry jposEntry, Element jposEntryElement) { + Element creation = doc.createElement(XML_TAG_CREATION); + Element jpos = doc.createElement(XML_TAG_JPOS); + Element product = doc.createElement(XML_TAG_PRODUCT); + Element vendor = doc.createElement(XML_TAG_VENDOR); + + for (Element tag : new Element[]{creation, vendor, jpos, product}) + jposEntryElement.appendChild(tag); + + List sortedProps = getSortedProperties(jposEntry); + + List propsAsAttributes = sortedProps.stream() + .filter(prop -> isAttribute(prop)).collect(Collectors.toList()); + List properties = sortedProps.stream() + .filter(prop -> !isAttribute(prop)).collect(Collectors.toList()); + + for (Prop prop : propsAsAttributes) { + addPropAsAttribute(prop, creation, jpos, product, vendor); + } + + for (Prop prop : properties) { + addPropElement( doc, jposEntryElement, prop ); + } + } + + private static List getSortedProperties(JposEntry jposEntry) { + @SuppressWarnings("unchecked") + Iterator props = jposEntry.getProps(); + List sortedProps = new ArrayList<>(); + props.forEachRemaining(sortedProps::add); + Collections.sort(sortedProps, + (JposEntry.Prop p1, JposEntry.Prop p2) -> p1.getName().compareToIgnoreCase(p2.getName())); + return sortedProps; + } + + private static final Set PROPS_AS_ATTRIBUTES = new HashSet<>(Arrays.asList( + JposEntry.SERVICE_CLASS_PROP_NAME, + JposEntry.SI_FACTORY_CLASS_PROP_NAME, + JposEntry.JPOS_VERSION_PROP_NAME, + JposEntry.DEVICE_CATEGORY_PROP_NAME, + JposEntry.VENDOR_NAME_PROP_NAME, + JposEntry.VENDOR_URL_PROP_NAME, + JposEntry.PRODUCT_NAME_PROP_NAME, + JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, + JposEntry.PRODUCT_URL_PROP_NAME + )); + + private static boolean isAttribute(Prop prop) { + return PROPS_AS_ATTRIBUTES.contains(prop.getName()); + } + + private void addPropAsAttribute(Prop prop, Element creation, Element jpos, Element product, Element vendor) { + String attrName = prop.getName(); + String attrValue = prop.getValueAsString(); + switch (attrName) { + case JposEntry.SERVICE_CLASS_PROP_NAME: + creation.setAttribute(XML_ATTR_SERVICECLASS, attrValue); + break; + case JposEntry.SI_FACTORY_CLASS_PROP_NAME: + creation.setAttribute(XML_ATTR_FACTORYCLASS, attrValue); + break; + case JposEntry.JPOS_VERSION_PROP_NAME: + jpos.setAttribute(XML_ATTR_VERSION, attrValue); + break; + case JposEntry.DEVICE_CATEGORY_PROP_NAME: + jpos.setAttribute(XML_ATTR_CATEGORY, attrValue); + break; + case JposEntry.VENDOR_NAME_PROP_NAME: + vendor.setAttribute(XML_ATTR_NAME, attrValue); + break; + case JposEntry.VENDOR_URL_PROP_NAME: + vendor.setAttribute(XML_ATTR_URL, attrValue); + break; + case JposEntry.PRODUCT_NAME_PROP_NAME: + product.setAttribute(XML_ATTR_NAME, attrValue); + break; + case JposEntry.PRODUCT_DESCRIPTION_PROP_NAME: + product.setAttribute(XML_ATTR_DESCRIPTION, attrValue); + break; + case JposEntry.PRODUCT_URL_PROP_NAME: + product.setAttribute(XML_ATTR_URL, attrValue); + break; + default: + tracer.print("WARN: unexpected XML attribute (will be skipped): " + attrName); + break; + } + + } + + private static void addPropElement(Document doc, Element jposEntryElement, JposEntry.Prop prop) { + Element propElement = doc.createElement(XML_TAG_PROP); + jposEntryElement.appendChild(propElement); + propElement.setAttribute(XML_ATTR_NAME, prop.getName()); + propElement.setAttribute(XML_ATTR_VALUE, prop.getValueAsString()); + if (!(prop.getValue() instanceof String)) + propElement.setAttribute(XML_ATTR_TYPE, prop.getValue().getClass().getSimpleName()); + } + + private static void insertDateSavedComment(Document document) { + String dateString = DateFormat.getInstance().format(new Date(System.currentTimeMillis())); + String commentString = "Saved by javapos-config-loader (JCL) version " + Version.getVersionString() + + " on " + dateString; + Comment comment = document.createComment(commentString); + document.getDocumentElement().insertBefore(comment, document.getDocumentElement().getFirstChild()); + } + + private void load(InputStream inputStream) { + SAXParserFactory parserFactory = SAXParserFactory.newInstance(); + JavaxSaxHandler saxHandler = new JavaxSaxHandler(); + try(InputStream is = findFileInClasspath(XSD_FILE_NAME)) { + StreamSource ss = new StreamSource(is == null ? new FileInputStream(XSD_FILE_NAME) : is); + Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(ss); + parserFactory.setSchema(schema); + tracer.println("XML file will be XSD schema validated against " + XSD_FILE_NAME); + } catch (Exception e) { + parserFactory.setValidating(true); + } + parserFactory.setNamespaceAware(true); + jposEntryList.clear(); + clearAllJposEntries(); + try { + SAXParser parser = parserFactory.newSAXParser(); + parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, XML_RESTRICTED_ACCESS_ATTRIBUTE); + parser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, XML_RESTRICTED_ACCESS_ATTRIBUTE); + parser.parse(inputStream, saxHandler); + } catch (SAXException e) { + tracer.println("SAX Parser error, msg=" + e.getMessage()); + lastLoadException = e; + } catch (IOException e) { + tracer.println("XML file access error, msg=" + e.getMessage()); + lastLoadException = e; + } catch (ParserConfigurationException e) { + tracer.println("SAX Parser configuration error, msg=" + e.getMessage()); + lastLoadException = e; + } + for (JposEntry jposEntry : jposEntryList) { + addJposEntry(jposEntry.getLogicalName(), jposEntry); + } + } + + //-------------------------------------------------------------------------- + // Private inner classes + // + + private class JavaxSaxHandler + extends DefaultHandler2 { + private JposEntry currentEntry = null; + private SAXException theException = null; + + //---------------------------------------------------------------- + // ContentHandler + + @Override + public void startElement(String uri, String lname, String qname, Attributes attrs) { + if (theException != null) { + tracer.println(": Parse error: " + theException.getMessage()); + lastLoadException = theException; + theException = null; + return; + } + if (qname.equals(XML_TAG_JPOSENTRIES)) + jposEntryList.clear(); + else if (qname.equals(XML_TAG_JPOSENTRY)) + currentEntry = new SimpleEntry(attrs.getValue("logicalName"), JavaxRegPopulator.this); + else if (currentEntry != null) { + if (qname.equals(XML_TAG_CREATION)) { + currentEntry.addProperty(JposEntry.SI_FACTORY_CLASS_PROP_NAME, attrs.getValue(XML_ATTR_FACTORYCLASS)); + currentEntry.addProperty(JposEntry.SERVICE_CLASS_PROP_NAME, attrs.getValue(XML_ATTR_SERVICECLASS)); + } else if (qname.equals(XML_TAG_VENDOR)) { + currentEntry.addProperty(JposEntry.VENDOR_NAME_PROP_NAME, attrs.getValue(XML_ATTR_NAME)); + addOptionalProperty(JposEntry.VENDOR_URL_PROP_NAME, attrs.getValue(XML_ATTR_URL)); + } else if (qname.equals(XML_TAG_JPOS)) { + currentEntry.addProperty(JposEntry.JPOS_VERSION_PROP_NAME, attrs.getValue(XML_ATTR_VERSION)); + currentEntry.addProperty(JposEntry.DEVICE_CATEGORY_PROP_NAME, attrs.getValue(XML_ATTR_CATEGORY)); + } else if (qname.equals(XML_TAG_PRODUCT)) { + currentEntry.addProperty(JposEntry.PRODUCT_NAME_PROP_NAME, attrs.getValue(XML_ATTR_NAME)); + currentEntry.addProperty(JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, attrs.getValue(XML_ATTR_DESCRIPTION)); + addOptionalProperty(JposEntry.PRODUCT_URL_PROP_NAME, attrs.getValue(XML_ATTR_URL)); + } else if (qname.equals(XML_TAG_PROP)) { + addPropElement(attrs); + } + } + } + + private void addPropElement(Attributes attrs) { + String typeName = attrs.getValue(XML_ATTR_TYPE); + try { + Class type = typeName == null ? String.class : Class.forName("java.lang." + typeName); + Object obj = JposEntryUtility.parsePropValue(attrs.getValue(XML_ATTR_VALUE), type); + currentEntry.addProperty(attrs.getValue(XML_ATTR_NAME), obj); + } catch (Exception e) { + currentEntry = null; + String msg = "Invalid prop: name=" + attrs.getValue(XML_ATTR_NAME) + + ":value=" + attrs.getValue(XML_ATTR_VALUE); + tracer.println(": " + msg); + lastLoadException = new SAXException(msg, e); + } + } + + private void addOptionalProperty(String name, String value) { + currentEntry.addProperty(name, value == null ? "" : value); + } + + @Override + public void endElement(String uri, String lname, String qname) { + if (theException != null) { + tracer.println("Parsing XML element " + qname + " failed with: " + theException.getMessage()); + theException = null; + } + if (qname.equals(XML_TAG_JPOSENTRY)) { + if (currentEntry != null) + jposEntryList.add(currentEntry); + currentEntry = null; + theException = null; + } + } + + //---------------------------------------------------------------- + // ErrorHandler + + @Override + public void error(SAXParseException e) { + currentEntry = null; + theException = e; + } + + @Override + public void fatalError(SAXParseException e) { + currentEntry = null; + theException = e; + } + + //---------------------------------------------------------------- + // EntityResolver + + @Override + public InputSource resolveEntity(String name, String publicId, String uri, String systemId) { + + if (publicId.equals(DTD_DOC_TYPE_VALUE)) { + tracer.println("XML file will be DTD validated against public Id '" + publicId + "' and system Id " + systemId); + + InputStream is = getClass().getResourceAsStream(DTD_FILE_NAME); + + if (is == null) + is = findFileInClasspath(DTD_FILE_NAME); + + if (is != null) + return new InputSource(new InputStreamReader(is)); + } + + return null; + } + } + + //-------------------------------------------------------------------------- + // Instance variables + // + + private List jposEntryList = new ArrayList<>(); + + private Tracer tracer = + TracerFactory.getInstance().createTracer(this.getClass().getSimpleName()); + + //-------------------------------------------------------------------------- + // Constants + // + + private static final String JAVAX_REG_POPULATOR_NAME_STRING = "JAVAX XML Entries Populator"; + private static final String XML_RESTRICTED_ACCESS_ATTRIBUTE = "file,jar:file"; +} diff --git a/src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java b/src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java index 6032fc9..35cc05f 100644 --- a/src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java @@ -72,7 +72,7 @@ public SimpleXmlRegPopulator() * @since 1.2 (NY 2K meeting) * @throws java.lang.Exception if any error occurs while saving */ - public void save( Enumeration entries ) throws Exception + public void save( @SuppressWarnings("rawtypes") Enumeration entries ) throws Exception { regPopulator.save( entries ); } /** @@ -82,7 +82,7 @@ public void save( Enumeration entries ) throws Exception * @since 1.3 (SF 2K meeting) * @throws java.lang.Exception if any error occurs while saving */ - public void save( Enumeration entries, String fileName ) throws Exception + public void save( @SuppressWarnings("rawtypes") Enumeration entries, String fileName ) throws Exception { regPopulator.save( entries, fileName ); } /** @@ -105,7 +105,8 @@ public void save( Enumeration entries, String fileName ) throws Exception * to the regPopulator instance * @since 1.2 (NY 2K meeting) */ - public Enumeration getEntries() { return regPopulator.getEntries(); } + @Override + public Enumeration getEntries() { return regPopulator.getEntries(); } /** * @return the URL pointing to the entries file loaded or saved @@ -129,5 +130,5 @@ public void save( Enumeration entries, String fileName ) throws Exception // Instance variables // - private XmlRegPopulator regPopulator = new XercesRegPopulator(); + private XmlRegPopulator regPopulator = new JavaxRegPopulator(); } diff --git a/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java b/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java deleted file mode 100644 index c73244e..0000000 --- a/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java +++ /dev/null @@ -1,527 +0,0 @@ -package jpos.config.simple.xml; - -/////////////////////////////////////////////////////////////////////////////// -// -// This software is provided "AS IS". The JavaPOS working group (including -// each of the Corporate members, contributors and individuals) MAKES NO -// REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, -// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED -// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NON-INFRINGEMENT. The JavaPOS working group shall not be liable for -// any damages suffered as a result of using, modifying or distributing this -// software or its derivatives. Permission to use, copy, modify, and distribute -// the software and its documentation for any purpose is hereby granted. -// -// The JavaPOS Config/Loader (aka JCL) is now under the CPL license, which -// is an OSS Apache-like license. The complete license is located at: -// http://www.ibm.com/developerworks/library/os-cpl.html -// -/////////////////////////////////////////////////////////////////////////////// - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.io.Reader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.EntityResolver; -import org.xml.sax.ErrorHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.DefaultHandler; - -import org.apache.xerces.jaxp.SAXParserFactoryImpl; - -import jpos.config.JposEntry; -import jpos.config.JposConfigException; -import jpos.config.simple.SimpleEntry; -import jpos.util.JposEntryUtility; -import jpos.util.tracing.Tracer; -import jpos.util.tracing.TracerFactory; - -/** - * This class implements a SAX parser for the JCL XML DB supporting both: - *
    - *
  1. DTD definition: jpos/res/jcl.dtd
  2. - *
  3. XML Schemas: jpos/res/jcl.xsd
  4. - *
- *

- * NOTE: this class must define a public no-argument ctor so that it may be - * created via reflection when its defined in the jpos.properties as the - * jpos.config.regPopulatorClass - *

- * @see jpos.util.JposProperties#JPOS_REG_POPULATOR_CLASS_PROP_NAME - * @since 2.1.0 - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ -public class Xerces2RegPopulator extends AbstractXercesRegPopulator -{ - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** - * Default ctor - * @since 1.2 (NY 2K meeting) - */ - public Xerces2RegPopulator() - { super( XercesRegPopulator.class.getName() ); } - - /** - * 1-arg constructor that takes the unique ID - * @param s the unique ID string - * @since 2.1.0 - */ - public Xerces2RegPopulator( String s ) { super( s ); } - - //------------------------------------------------------------------------- - // Public methods - // - - /** - * @return the fully qualified class name implementing the - * JposRegPopulator interface - * @since 2.1.0 - */ - public String getClassName() - { return Xerces2RegPopulator.class.getName(); } - - /** - * Tell the populator to load the entries - * @since 2.1.0 - */ - public void load() - { - try - { - //NEED TO REPLACE WITH A METHOD THAT FIGURES OUT FILE NAME - //needed to set file name - InputStream is = getPopulatorFileIS(); - // - - load( getPopulatorFileName() ); - // - } - catch( Exception e ) - { - tracer.println( "Error while loading populator file Exception.message: " + - e.getMessage() ); - lastLoadException = e; - } - } - - /** - * Loads the entries specified in the xmlFileName - * @param xmlFileName the XML file name - * @since 2.1.0 - */ - public void load( String xmlFileName ) - { - Reader reader = null; - - try - { - reader = new FileReader( new File( xmlFileName ) ); - InputSource inputSource = new InputSource( reader ); - - XMLReader xmlReader = getSAXParser().getXMLReader(); - - initXMLReader( xmlReader ); - - xmlReader.setErrorHandler( errorHandler ); - xmlReader.setContentHandler( contentHandler ); - xmlReader.setEntityResolver( entityResolver ); - - jposEntryList.clear(); - lastLoadException = null; - - xmlReader.parse( inputSource ); - - Iterator entries = jposEntryList.iterator(); - while( entries.hasNext() ) - { - JposEntry jposEntry = (JposEntry)entries.next(); - getJposEntries().put( jposEntry.getLogicalName(), jposEntry ); - } - } - catch( FileNotFoundException fne ) - { - tracer.println( "Could not find file: " + xmlFileName ); - lastLoadException = fne; - } - catch( ParserConfigurationException pce ) - { - tracer.println( "Could not create and configure SAX parser/factory" - + pce.getMessage() ); - lastLoadException = pce; - } - catch( IOException ioe ) - { - tracer.println( "Error while parsing XML file:IOException.msg=" + - ioe.getMessage() ); - lastLoadException = ioe; - } - catch( SAXException se ) - { - tracer.println( "Error creating or using the SAXParser:" + - "SAXException.message=" + se.getMessage() ); - lastLoadException = se; - } - finally - { - try{ if( reader != null ) reader.close(); } - catch( IOException ioe ) - { - tracer.println( "load( " + xmlFileName + ") IOException.msg=" + - ioe.getMessage() ); - } - } - } - - /** - * @return the name of this populator. - * This should be a short descriptive name - * @since 1.3 (Washington DC 2001 meeting) - */ - public String getName() { return XERCES2_REG_POPULATOR_NAME_STRING; } - - //-------------------------------------------------------------------------- - // Protected methods - // - - /** - * @return a SAXParser object creating and initializing SAXParserFactory - * and necessary objects if they are not yet created - * @since 2.1.0 - * @throws javax.xml.parsers.ParserConfigurationException if the parser - * factory not be properly configured - * @throws org.xml.sax.SAXException if the SAXParser could not be created - */ - protected SAXParser getSAXParser() throws ParserConfigurationException, - SAXException - { - if( saxParser == null ) - { - SAXParserFactory factory = new SAXParserFactoryImpl(); - saxParser = factory.newSAXParser(); - } - - return saxParser; - } - - /** - * Initializes XMLReader instance - * @param xmlReader the XMLReader instance - * @throws org.xml.sax.SAXException - */ - protected void initXMLReader( XMLReader xmlReader ) throws SAXException - { - xmlReader.setFeature( "http://xml.org/sax/features/namespaces", true ); - xmlReader.setFeature( "http://xml.org/sax/features/validation", true ); - } - - //-------------------------------------------------------------------------- - // Instance variables - // - - private XMLReader xmlReader = null; - private SAXParser saxParser = null; - - private ErrorHandler errorHandler = this.new JposErrorHandler(); - private ContentHandler contentHandler = this.new JposContentHandler(); - private EntityResolver entityResolver = this.new JposEntityResolver(); - - private List jposEntryList = new LinkedList(); - - private Tracer tracer = TracerFactory.getInstance(). - createTracer( "Xerces2RegPopulator", true ); - - //-------------------------------------------------------------------------- - // Public constants - // - - public static final String XERCES2_REG_POPULATOR_NAME_STRING = - "JCL XML Entries Populator 2"; - - //-------------------------------------------------------------------------- - // Inner classes - // - - /** - * SAX XML Handler interface---essentially implements the XML parser/driver - * @author E. Michael Maximilien - */ - protected class JposContentHandler extends DefaultHandler - implements ContentHandler - { - //---------------------------------------------------------------------- - // Public methods - // - - public void startDocument() throws SAXException - { - tracer.println( "" ); - } - - public void endDocument() throws SAXException - { - tracer.println( "" ); - } - - public void startElement( String namespaceUri, String localName, - String qName, Attributes attributes ) - throws SAXException - { - tracer.println( "" ); - - if( qName.equals( "JposEntries" ) ) - { - jposEntryList.clear(); - currentEntry = null; - } - else - if( qName.equals( "JposEntry" ) ) - currentEntry = createEntry( attributes ); - else - if( qName.equals( "creation" ) ) - addCreationProp( currentEntry, attributes ); - else - if( qName.equals( "vendor" ) ) - addVendorProp( currentEntry, attributes ); - else - if( qName.equals( "jpos" ) ) - addJposProp( currentEntry, attributes ); - else - if( qName.equals( "product" ) ) - addProductProp( currentEntry, attributes ); - else - if( qName.equals( "prop" ) ) - addProp( currentEntry, attributes ); - else - { - tracer.println( "Invalid qName=" + qName ); - throw new SAXException( "Invalid qName=" + qName ); - } - - } - - public void endElement( String namespaceUri, String localName, - String qName ) throws SAXException - { - tracer.println( "" ); - - if( qName.equals( "JposEntry" ) ) - jposEntryList.add( currentEntry ); - } - - //---------------------------------------------------------------------- - // Protected methods - // - - protected JposEntry createEntry( Attributes attributes ) - throws SAXException - { - String logicalName = attributes.getValue( "logicalName" ); - - return new SimpleEntry( logicalName, Xerces2RegPopulator.this ); - } - - protected void addCreationProp( JposEntry entry, - Attributes attributes ) - throws SAXException - { - String factoryClass = attributes.getValue( "factoryClass" ); - String serviceClass = attributes.getValue( "serviceClass" ); - - //TODO: Check values - - currentEntry.addProperty( JposEntry.SI_FACTORY_CLASS_PROP_NAME, - factoryClass ); - currentEntry.addProperty( JposEntry.SERVICE_CLASS_PROP_NAME, - serviceClass ); - } - - protected void addVendorProp( JposEntry entry, - Attributes attributes ) - throws SAXException - { - String factoryClass = attributes.getValue( "name" ); - String serviceClass = attributes.getValue( "url" ); - - //TODO: Check values - - currentEntry.addProperty( JposEntry.VENDOR_NAME_PROP_NAME, - factoryClass ); - - currentEntry.addProperty( JposEntry.VENDOR_URL_PROP_NAME, - serviceClass ); - } - - protected void addJposProp( JposEntry entry, - Attributes attributes ) - throws SAXException - { - String category = attributes.getValue( "category" ); - String version = attributes.getValue( "version" ); - - //TODO: Check values - - currentEntry.addProperty( JposEntry.DEVICE_CATEGORY_PROP_NAME, - category ); - - currentEntry.addProperty( JposEntry.JPOS_VERSION_PROP_NAME, - version ); - } - - protected void addProductProp( JposEntry entry, - Attributes attributes ) - throws SAXException - { - String description = attributes.getValue( "description" ); - String name = attributes.getValue( "name" ); - String url = attributes.getValue( "url" ); - - //TODO: Check values - - currentEntry.addProperty( JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, - description ); - - currentEntry.addProperty( JposEntry.PRODUCT_NAME_PROP_NAME, - name ); - - currentEntry.addProperty( JposEntry.PRODUCT_URL_PROP_NAME, - url ); - } - - protected void addProp( JposEntry entry, - Attributes attributes ) - throws SAXException - { - String name = attributes.getValue( "name" ); - String valueString = attributes.getValue( "value" ); - String typeString = attributes.getValue( "type" ); - - if( typeString == null || typeString.equals( "" ) ) - typeString = "String"; - - try - { - - Class typeClass = JposEntryUtility. - propTypeFromString( attributes.getValue( "type" ) ); - - Object value = JposEntryUtility.parsePropValue( valueString, typeClass ); - - //TODO: Check values - - JposEntry.Prop prop = currentEntry. - createProp( name, value, typeClass ); - currentEntry.add( prop ); - } - catch( JposConfigException jce ) - { - String msg = "Invalid prop: name=" + name + ":value=" + - valueString; - tracer.println( msg ); - throw new SAXException( msg ); - } - } - - //---------------------------------------------------------------------- - // Instance variables - // - - private JposEntry currentEntry = null; - } - - /** - * JposEntityResolver to resolve XML schema - * @author E. Michael Maximilien - * @version 2.1.0 - */ - public class JposEntityResolver implements EntityResolver - { - /** - * @return the XML schema as an InputSource if it is found in a JAR - * file in the CLASSPATH otherwise - * return null - */ - public InputSource resolveEntity( String publicId, String systemId ) - { - tracer.println( "JposEntityResolver:resolveEntity:publicId=" + - publicId ); - - tracer.println( "JposEntityResolver:resolveEntity:systemId=" + - systemId ); - - if( publicId.equals( getDoctypeValue() ) ) - { - InputStream is = - getClass().getResourceAsStream( getDoctypeValue() ); - - if( is != null ) - return new InputSource( new InputStreamReader( is ) ); - } - - return null; - } - } - - /** - * SAX XML Handler interface - * @author E. Michael Maximilien - */ - protected class JposErrorHandler extends Object implements ErrorHandler - { - //---------------------------------------------------------------------- - // Private/protected methods - // - - private String createMessage( String header, SAXParseException spe ) - { - return header + "parsing XML file:SAXParseException.message = " + - spe.getMessage(); - } - - //---------------------------------------------------------------------- - // Public methods - // - - public void error( SAXParseException spe ) throws SAXException - { - String message = createMessage( "JposErrorHandler:Error:", spe ); - tracer.print( message ); - - throw new SAXException( message ); - } - - public void fatalError( SAXParseException spe ) throws SAXException - { - String message = createMessage( "JposErrorHandler:FatalError:", spe ); - tracer.print( message ); - - throw new SAXException( message ); - } - - public void warning( SAXParseException spe ) throws SAXException - { - String message = createMessage( "JposErrorHandler:Warning:", spe ); - tracer.print( message ); - - throw new SAXException( message ); - } - } -} \ No newline at end of file diff --git a/src/main/java/jpos/config/simple/xml/XercesRegPopulator.java b/src/main/java/jpos/config/simple/xml/XercesRegPopulator.java deleted file mode 100644 index b3d6d89..0000000 --- a/src/main/java/jpos/config/simple/xml/XercesRegPopulator.java +++ /dev/null @@ -1,464 +0,0 @@ -package jpos.config.simple.xml; - -/////////////////////////////////////////////////////////////////////////////// -// -// This software is provided "AS IS". The JavaPOS working group (including -// each of the Corporate members, contributors and individuals) MAKES NO -// REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, -// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED -// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NON-INFRINGEMENT. The JavaPOS working group shall not be liable for -// any damages suffered as a result of using, modifying or distributing this -// software or its derivatives. Permission to use, copy, modify, and distribute -// the software and its documentation for any purpose is hereby granted. -// -// The JavaPOS Config/Loader (aka JCL) is now under the CPL license, which -// is an OSS Apache-like license. The complete license is located at: -// http://www.ibm.com/developerworks/library/os-cpl.html -// -/////////////////////////////////////////////////////////////////////////////// - -import java.io.*; -import java.util.*; - -import java.io.FileInputStream; - -import org.w3c.dom.*; - -import org.xml.sax.InputSource; - -import jpos.config.*; -import jpos.config.simple.*; -import jpos.util.*; -import jpos.util.tracing.Tracer; -import jpos.util.tracing.TracerFactory; - -/** - * Simple implementation of the JposRegPopulator that loads and saves - * the entries in XML using the "jpos/res/jcl.dtd" DTD and the XML4J - * (Xerces) API - * NOTE: this class must define a public no-argument ctor so that it may be - * created via reflection when its defined in the jpos.properties as - * the jpos.config.regPopulatorClass - * @see jpos.util.JposProperties#JPOS_REG_POPULATOR_CLASS_PROP_NAME - * @since 1.2 (NY 2K meeting) - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ -public class XercesRegPopulator extends AbstractXercesRegPopulator -{ - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** - * Default ctor - * @since 1.2 (NY 2K meeting) - */ - public XercesRegPopulator() - { super( XercesRegPopulator.class.getName() ); } - - /** - * 1-arg constructor that takes the unique ID - * @param s the unique ID string - * @since 1.3 (Washington DC 2001) - */ - public XercesRegPopulator( String s ) { super( s ); } - - //------------------------------------------------------------------------- - // Public methods - // - - /** - * @return the fully qualified class name implementing the - * JposRegPopulator interface - * @since 1.3 (Washington DC 2001 meeting) - */ - public String getClassName() - { return XercesRegPopulator.class.getName(); } - - /** - * Tell the populator to load the entries - * @since 1.2 (NY 2K meeting) - */ - public void load() - { - tracer.println( "load(): isPopulatorFileDefined=" + - isPopulatorFileDefined() ); - - if( isPopulatorFileDefined() == false ) - { - getJposEntries().clear(); - xmlFileName = DEFAULT_XML_FILE_NAME; - load( xmlFileName ); - - return; - } - - try - { - getJposEntries().clear(); - - domParser.setEntityResolver(new XercesRegPopulator.JPOSDTDEntityResolver()); - - domParser.parse( new InputSource( getPopulatorFileIS() ) ); - - Document document = domParser.getDocument(); - - Enumeration entries = extractJposEntries( document ); - - while( entries.hasMoreElements() ) - { - JposEntry jposEntry = (JposEntry)entries.nextElement(); - - if( jposEntry.hasPropertyWithName( JposEntry. - LOGICAL_NAME_PROP_NAME ) ) - getJposEntries().put( jposEntry.getLogicalName(), - jposEntry ); - } - - lastLoadException = null; - } - catch( Exception e ) - { - lastLoadException = e; - tracer.println( "Error loading XML file. Exception.msg = " + - e.getMessage() ); - } - finally - { } - } - - /** - * Loads the entries specified in the xmlFileName - * @param xmlFileName the XML file name - * @since 1.3 (SF 2K meeting) - */ - public void load( String xmlFileName ) - { - tracer.println( "load: xmlFileName=" + xmlFileName ); - - InputStream is = null; - File xmlFile = new File( xmlFileName ); - - try - { - if( xmlFile.exists() ) - is = new FileInputStream( xmlFile ); - else - is = findFileInClasspath( xmlFileName ); - - if (is == null) - { - is = getClass().getClassLoader().getResourceAsStream(xmlFileName); - } - - if( is == null ) - { - getJposEntries().clear(); - - tracer.println( "Could not find file: " + xmlFileName + - " in path or CLASSPATH" ); - - lastLoadException = new FileNotFoundException( xmlFileName ); - - return; - } - - lastLoadException = null; - } - catch( Exception e ) - { - lastLoadException = e; - tracer.println( "Error loading XML file. Exception.message = " + - e.getMessage() ); - } - - try - { - getJposEntries().clear(); - - domParser.setEntityResolver(new XercesRegPopulator.JPOSDTDEntityResolver()); - - domParser.parse( new InputSource( is ) ); - - Document document = domParser.getDocument(); - - Enumeration entries = extractJposEntries( document ); - - while( entries.hasMoreElements() ) - { - JposEntry jposEntry = (JposEntry)entries.nextElement(); - - if( jposEntry.hasPropertyWithName( JposEntry. - LOGICAL_NAME_PROP_NAME ) ) - getJposEntries().put( jposEntry.getLogicalName(), - jposEntry ); - } - - lastLoadException = null; - - } - catch( Exception e ) - { - lastLoadException = e; - tracer.println( "Error loading XML file. Exception.message = " + - e.getMessage() ); - } - finally - { } - } - - /** - * @return the name of this populator. This should be a short descriptive name - * @since 1.3 (Washington DC 2001 meeting) - */ - public String getName() { return XERCES_REG_POPULATOR_NAME_STRING; } - - //-------------------------------------------------------------------------- - // Protected methods - // - - /** - * @return an enumeration of JposEntry objects read from the XML document object - * @param document the XML document object - * @since 1.2 (NY 2K meeting) - */ - protected Enumeration extractJposEntries( Document document ) - { - Vector entries = new Vector(); - - NodeList nodeList = document.getElementsByTagName( "JposEntry" ); - - String currentEntryLogicalName = ""; - - try - { - for( int i = 0; i < nodeList.getLength(); ++i ) - { - Node node = nodeList.item( i ); - - if( node.getNodeType() != Node.ELEMENT_NODE ) - continue; - - JposEntry jposEntry = new SimpleEntry(); - - Element jposEntryElement = (Element)node; - - currentEntryLogicalName = jposEntryElement. - getAttribute( "logicalName" ); - jposEntry.addProperty( "logicalName", currentEntryLogicalName ); - - NodeList childList = nodeList.item( i ).getChildNodes(); - - for( int j = 0; j < childList.getLength(); ++j ) - { - Node child = childList.item( j ); - - if( child.getNodeType() != Node.ELEMENT_NODE ) - continue; - - Element element = (Element)child; - - String elementName = element.getNodeName(); - - if( elementName.equals( "creation" ) ) - extractCreationAttr( jposEntry, element ); - else - if( elementName.equals( "vendor" ) ) - extractVendorAttr( jposEntry, element ); - else - if( elementName.equals( "jpos" ) ) - extractJposAttr( jposEntry, element ); - else - if( elementName.equals( "product" ) ) - extractProductAttr( jposEntry, element ); - else - extractPropAttr( jposEntry, element ); - } - - if( JposEntryUtility.isValidJposEntry( jposEntry ) ) - entries.addElement( jposEntry ); - else - { - String msg = "JposEntry with logicalName: " + - currentEntryLogicalName + - " is not valid (missing required properties)"; - throw new JposConfigException( msg ); - } - } - } - catch( JposConfigException jce ) - { - tracer.println( "Skipping invalid entry with logicalName: " + - currentEntryLogicalName ); - tracer.println( "--->JposConfigException.message = " + - jce.getMessage() ); - - tracer.print( jce ); - - if( jce.getOrigException() != null ) - tracer.print( jce.getOrigException() ); - } - - return entries.elements(); - } - - /** - * Get the <creation> element attributes and adds corresponding - * properties to JposEntry - * @param jposEntry the entry to add properties to - * @param element the <creation> XML element - * @since 1.2 (NY 2K meeting) - */ - protected void extractCreationAttr( JposEntry jposEntry, Element element ) - { - jposEntry.addProperty( "serviceInstanceFactoryClass", - element.getAttribute( "factoryClass" ) ); - - jposEntry.addProperty( "serviceClass", - element.getAttribute( "serviceClass" ) ); - } - - /** - * Get the <vendor> element attributes and adds corresponding - * properties to JposEntry - * @param jposEntry the entry to add properties to - * @param element the <vendor> XML element - * @since 1.2 (NY 2K meeting) - */ - protected void extractVendorAttr( JposEntry jposEntry, Element element ) - { - jposEntry.addProperty( "vendorName", element.getAttribute( "name" ) ); - jposEntry.addProperty( "vendorURL", element.getAttribute( "url" ) ); - } - - /** - * Get the <jpos> element attributes and adds corresponding properties - * to JposEntry - * @param jposEntry the entry to add properties to - * @param element the <jpos> XML element - * @since 1.2 (NY 2K meeting) - */ - protected void extractJposAttr( JposEntry jposEntry, Element element ) - { - jposEntry.addProperty( "jposVersion", - element.getAttribute( "version" ) ); - - jposEntry.addProperty( "deviceCategory", - element.getAttribute( "category" ) ); - } - - /** - * Get the <product> element attributes and adds corresponding - * properties to JposEntry - * @param jposEntry the entry to add properties to - * @param element the <product> XML element - * @since 1.2 (NY 2K meeting) - */ - protected void extractProductAttr( JposEntry jposEntry, Element element ) - { - jposEntry.addProperty( "productName", element.getAttribute( "name" ) ); - - jposEntry.addProperty( "productDescription", - element.getAttribute( "description" ) ); - - jposEntry.addProperty( "productURL", element.getAttribute( "url" ) ); - } - - /** - * Get the <prop> element attributes and adds corresponding properties - * to JposEntry - * @param jposEntry the entry to add properties to - * @param element the <prop> XML element - * @since 1.2 (NY 2K meeting) - * @throws jpos.config.JposConfigException if the property value does - * not match the type or is not a valid value (like for instance - * an invalid number) - */ - protected void extractPropAttr( JposEntry jposEntry, Element element ) - throws JposConfigException - { - String propName = element.getAttribute( "name" ); - String propValueString = element.getAttribute( "value" ); - String propTypeString = element.getAttribute( "type" ); - - if( propTypeString.equals( "" ) ) propTypeString = "String"; - - Object propValue = null; - Class propType = null; - - try - { - propType = Class.forName( ( propTypeString. - startsWith( "java.lang." ) ? - propTypeString : - "java.lang." + propTypeString ) ); - } - catch( ClassNotFoundException cnfe ) - { - throw new JposConfigException( "Invalid property type: " + - propTypeString + - " for property named: " + - propName , cnfe ); - } - - if( JposEntryUtility.isValidPropType( propType ) == false ) - throw new JposConfigException( "Invalid property type: " + - propTypeString + - " for property named: " + - propName ); - - propValue = JposEntryUtility. - parsePropValue( propValueString, propType ); - - if( JposEntryUtility.validatePropValue( propValue, propType ) == false ) - throw new JposConfigException( "Invalid property type: " + - propTypeString + - " for property named: " + - propName ); - - jposEntry.add( jposEntry.createProp( propName, propValue, propType ) ); - } - - /** - * JposDTDEntityResolver to resolve DTD - */ - public class JPOSDTDEntityResolver implements org.xml.sax.EntityResolver - { - /** - * @return the DTD as an InputSource if it is found in a JAR - * file in the CLASSPATH otherwise return null - */ - public org.xml.sax.InputSource resolveEntity(String publicId, String systemId) throws org.xml.sax.SAXException, java.io.IOException - { - if (publicId.equals("-//JavaPOS//DTD//EN")) - { - InputStream is = getClass().getResourceAsStream( DTD_JAR_FILE_NAME ); - - if (is != null) - { - return (new org.xml.sax.InputSource(new InputStreamReader(is))); - } - } - - return null; - } - - } - - //-------------------------------------------------------------------------- - // Instance variables - // - - private Tracer tracer = TracerFactory.getInstance(). - createTracer( "XercesRegPopulator" ); - - //-------------------------------------------------------------------------- - // Public constants - // - - public static final String DTD_JAR_FILE_NAME = "/jpos/res/jcl.dtd"; - - public static final String XERCES_REG_POPULATOR_NAME_STRING = - "JCL XML Entries Populator"; -} \ No newline at end of file diff --git a/src/main/java/jpos/config/simple/xml/XmlRegPopulator.java b/src/main/java/jpos/config/simple/xml/XmlRegPopulator.java index f2e859a..4c0afb0 100644 --- a/src/main/java/jpos/config/simple/xml/XmlRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/XmlRegPopulator.java @@ -37,4 +37,123 @@ public interface XmlRegPopulator extends JposRegPopulator * @since 1.2 (NY 2K meeting) */ public static final String DEFAULT_XML_FILE_NAME = "jpos.xml"; + + /** + * Default path for dtd and / or xsd file + * @since 4.0 + */ + public static final String DTD_FILE_PATH = "jpos/res"; + + /** + * Default name for dtd file + * @since 4.0 + */ + public static final String DTD_FILE_NAME = DTD_FILE_PATH + "/jcl.dtd"; + + /** + * Default name for xsd file + * @since 4.0 + */ + public static final String XSD_FILE_NAME = DTD_FILE_PATH + "/jcl.xsd"; + + /** + * Default DTD document type value + */ + public static final String DTD_DOC_TYPE_VALUE = "-//JavaPOS//DTD//EN"; + + /** + * Define for tag name JposEntries + * @since 4.0 + */ + public static final String XML_TAG_JPOSENTRIES = "JposEntries"; + + /** + * Define for tag name JposEntry + * @since 4.0 + */ + public static final String XML_TAG_JPOSENTRY = "JposEntry"; + + /** + * Define for tag name creation + * @since 4.0 + */ + public static final String XML_TAG_CREATION = "creation"; + + /** + * Define for tag name vendor + * @since 4.0 + */ + public static final String XML_TAG_VENDOR = "vendor"; + + /** + * Define for tag name jpos + * @since 4.0 + */ + public static final String XML_TAG_JPOS = "jpos"; + + /** + * Define for tag name product + * @since 4.0 + */ + public static final String XML_TAG_PRODUCT = "product"; + + /** + * Define for tag name prop + * @since 4.0 + */ + public static final String XML_TAG_PROP = "prop"; + + /** + * Define for attribute name serviceClass + * @since 4.0 + */ + public static final String XML_ATTR_SERVICECLASS = "serviceClass"; + + /** + * Define for attribute name factoryClass + * @since 4.0 + */ + public static final String XML_ATTR_FACTORYCLASS= "factoryClass"; + + /** + * Define for attribute name name + * @since 4.0 + */ + public static final String XML_ATTR_NAME = "name"; + + /** + * Define for attribute name url + * @since 4.0 + */ + public static final String XML_ATTR_URL = "url"; + + /** + * Define for attribute name name + * @since 4.0 + */ + public static final String XML_ATTR_VERSION = "version"; + + /** + * Define for attribute name category + * @since 4.0 + */ + public static final String XML_ATTR_CATEGORY = "category"; + + /** + * Define for attribute name description + * @since 4.0 + */ + public static final String XML_ATTR_DESCRIPTION = "description"; + + /** + * Define for attribute name value + * @since 4.0 + */ + public static final String XML_ATTR_VALUE = "value"; + + /** + * Define for attribute name type + * @since 4.0 + */ + public static final String XML_ATTR_TYPE = "type"; } \ No newline at end of file diff --git a/src/main/java/jpos/loader/JposLoaderException.java b/src/main/java/jpos/loader/JposLoaderException.java index 0723c6a..8c0f83e 100644 --- a/src/main/java/jpos/loader/JposLoaderException.java +++ b/src/main/java/jpos/loader/JposLoaderException.java @@ -28,10 +28,12 @@ */ public class JposLoaderException extends JposException { + private static final long serialVersionUID = -2552211746747253072L; + //------------------------------------------------------------------------- // Ctor(s) // - + /** * Creates a JposLoaderException with the description passed * @param description the description String diff --git a/src/main/java/jpos/loader/JposServiceLoader.java b/src/main/java/jpos/loader/JposServiceLoader.java index fd2dbf3..e7576f3 100644 --- a/src/main/java/jpos/loader/JposServiceLoader.java +++ b/src/main/java/jpos/loader/JposServiceLoader.java @@ -36,7 +36,7 @@ * @since 0.1 (Philly 99 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public final class JposServiceLoader extends Object +public final class JposServiceLoader { //-------------------------------------------------------------------------- // Class variables @@ -69,23 +69,21 @@ public final class JposServiceLoader extends Object boolean customManagerDefined = false; String customManagerClassName = ""; - if( jposProperties.isPropertyDefined( JposProperties. - JPOS_SERVICE_MANAGER_CLASS_PROP_NAME ) ) + if( jposProperties.isPropertyDefined( + JposPropertiesConst.JPOS_SERVICE_MANAGER_CLASS_PROP_NAME ) ) { customManagerDefined = true; customManagerClassName = jposProperties. - getPropertyString( JposProperties. - JPOS_SERVICE_MANAGER_CLASS_PROP_NAME ); + getPropertyString( JposPropertiesConst.JPOS_SERVICE_MANAGER_CLASS_PROP_NAME ); } else - if( jposProperties.isPropertyDefined( JposProperties. - JPOS_SERVICE_MANAGER_CLASS_PROP_NAME2 ) ) + if( jposProperties.isPropertyDefined( + JposPropertiesConst.JPOS_SERVICE_MANAGER_CLASS_PROP_NAME2 ) ) { customManagerDefined = true; customManagerClassName = jposProperties. - getPropertyString( JposProperties. - JPOS_SERVICE_MANAGER_CLASS_PROP_NAME2 ); + getPropertyString( JposPropertiesConst.JPOS_SERVICE_MANAGER_CLASS_PROP_NAME2 ); } if( customManagerDefined ) @@ -95,12 +93,12 @@ public final class JposServiceLoader extends Object try { - Class managerClass = Class.forName( customManagerClassName ); + Class managerClass = Class.forName( customManagerClassName ); - Class arg1Class = Class.forName( "jpos.util.JposProperties" ); - Class[] argsClass = { arg1Class }; + Class arg1Class = Class.forName( "jpos.util.JposProperties" ); + Class[] argsClass = { arg1Class }; - Constructor oneArgCtor = managerClass. + Constructor oneArgCtor = managerClass. getConstructor( argsClass ); Object[] args = { jposProperties }; @@ -141,7 +139,7 @@ public static JposServiceConnection findService( String logicalName ) if( manager == null ) { String msg = "Did not find a valid " + - JposProperties.JPOS_SERVICE_MANAGER_CLASS_PROP_NAME + + JposPropertiesConst.JPOS_SERVICE_MANAGER_CLASS_PROP_NAME + " to create"; tracer.println( msg ); diff --git a/src/main/java/jpos/loader/JposServiceManager.java b/src/main/java/jpos/loader/JposServiceManager.java index a422cbb..22a24ca 100644 --- a/src/main/java/jpos/loader/JposServiceManager.java +++ b/src/main/java/jpos/loader/JposServiceManager.java @@ -41,6 +41,7 @@ public interface JposServiceManager * @return a JposServiceConnection for the service with the logical name specified * This should use the populator to see if there exist any entry with the logical * name provided, get the entry and create the JposServiceConnection + * @param logicalName the logical name the connection shall be created for * @since 0.1 (Philly 99 meeting) * @throws jpos.JposException if an error occurs while creating this connection */ diff --git a/src/main/java/jpos/loader/Version.java b/src/main/java/jpos/loader/Version.java index 5f514d6..435f905 100644 --- a/src/main/java/jpos/loader/Version.java +++ b/src/main/java/jpos/loader/Version.java @@ -29,7 +29,7 @@ * @since 1.2 (NY 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public final class Version extends Object +public final class Version { /** * Main entry point for the Version application diff --git a/src/main/java/jpos/loader/simple/SimpleServiceConnection.java b/src/main/java/jpos/loader/simple/SimpleServiceConnection.java index 6ccf479..9aa96c0 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceConnection.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceConnection.java @@ -33,8 +33,7 @@ * @since 0.1 (Philly 99 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class SimpleServiceConnection extends Object - implements JposServiceConnection +public class SimpleServiceConnection implements JposServiceConnection { /** * Creates a new SimpleServiceConnection by passing the logicalName, @@ -76,26 +75,26 @@ public void connect() throws JposException { try { - Class[] parameterTypes = new Class[ 2 ]; + Class[] parameterTypes = new Class[ 2 ]; parameterTypes[ 0 ] = Class.forName( "java.lang.String" ); parameterTypes[ 1 ] = Class.forName( "jpos.config.JposEntry"); JposServiceInstanceFactory siFactory = null; if( siFactoryTable.containsKey( siFactoryClassName ) ) - siFactory = (JposServiceInstanceFactory)siFactoryTable. + siFactory = siFactoryTable. get( siFactoryClassName ); else { - Class instanceClass = Class.forName( siFactoryClassName ); - Constructor defaultCtor = instanceClass. + Class instanceClass = Class.forName( siFactoryClassName ); + Constructor defaultCtor = instanceClass. getConstructor( new Class[ 0 ] ); siFactory = (JposServiceInstanceFactory)defaultCtor. newInstance( new Object[ 0 ] ); siFactoryTable.put( siFactoryClassName, siFactory ); } - service = (JposServiceInstance)siFactory. + service = siFactory. createInstance( logicalName, entry ); } catch( Exception e ) @@ -137,7 +136,7 @@ public void disconnect() throws JposException // Class variables // - private static Hashtable siFactoryTable = new Hashtable(); + private static Hashtable siFactoryTable = new Hashtable<>(); //-------------------------------------------------------------------------- // Instance variables diff --git a/src/main/java/jpos/loader/simple/SimpleServiceManager.java b/src/main/java/jpos/loader/simple/SimpleServiceManager.java index 0186e22..7e22e78 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceManager.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceManager.java @@ -34,26 +34,12 @@ * @since 0.1 (Philly 99 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class SimpleServiceManager extends Object -implements JposServiceManager +public class SimpleServiceManager implements JposServiceManager { //-------------------------------------------------------------------------- // Ctor(s) // - /** - * Default ctor - * NOTE: necessary because it will be used by jpos.config.JposServiceLoader - * to create the simple factory - * @deprecated replaced by 1-argument ctor - * @since 0.1 (Philly 99 meeting) - */ - public SimpleServiceManager() - { - getProperties().loadJposProperties(); - init(); - } - /** * One argument ctor * @param properties the JposProperties for this manager @@ -96,16 +82,16 @@ private void initRegPopulator() { JposProperties properties = getProperties(); - if( properties.isPropertyDefined( JposProperties. - JPOS_REG_POPULATOR_CLASS_PROP_NAME ) ) + if( properties.isPropertyDefined( + JposPropertiesConst.JPOS_REG_POPULATOR_CLASS_PROP_NAME ) ) { - String regPopulatorClassName = properties. - getPropertyString( JposProperties. - JPOS_REG_POPULATOR_CLASS_PROP_NAME ); + String regPopulatorClassName = + properties.getPropertyString( + JposPropertiesConst.JPOS_REG_POPULATOR_CLASS_PROP_NAME ); try { - Class regPopulatorClass = Class.forName( regPopulatorClassName ); + Class regPopulatorClass = Class.forName( regPopulatorClassName ); regPopulator = (JposRegPopulator)regPopulatorClass.newInstance(); } @@ -117,9 +103,8 @@ private void initRegPopulator() regPopulator = new SimpleRegPopulator(); } } - else - if( properties.hasMultiProperty( JposPropertiesConst.JPOS_CONFIG_POPULATOR_CLASS_MULTIPROP_NAME ) ) - regPopulator = new DefaultCompositeRegPopulator(); + else if( properties.hasMultiProperty( JposPropertiesConst.JPOS_CONFIG_POPULATOR_CLASS_MULTIPROP_NAME ) ) + regPopulator = new DefaultCompositeRegPopulator(); else regPopulator = new SimpleRegPopulator(); } @@ -131,7 +116,7 @@ private void initRegPopulator() private ProfileFactory getProfileFactory() { if( profileFactory == null ) - profileFactory = new XercesProfileFactory(); + profileFactory = new DefaultProfileFactory(); return profileFactory; } @@ -160,10 +145,10 @@ private ProfileFactory getProfileFactory() public JposRegPopulator getRegPopulator() { return regPopulator; } /** - * @return a ServiceConnection used to connect to the service + * @return a {@link JposServiceConnection} object used to connect to the service * @param logicalName the logical name of the service to find * @since 0.1 (Philly 99 meeting) - * @throws JposException + * @throws JposException in case the connection could not established for any reason */ public JposServiceConnection createConnection( String logicalName ) throws JposException @@ -172,7 +157,7 @@ public JposServiceConnection createConnection( String logicalName ) try { - JposEntry jposEntry = (JposEntry)entryRegistry. + JposEntry jposEntry = entryRegistry. getJposEntry( logicalName ); if( jposEntry == null ) diff --git a/src/main/java/jpos/profile/AbstractPropType.java b/src/main/java/jpos/profile/AbstractPropType.java index 3921f84..6cc7d05 100644 --- a/src/main/java/jpos/profile/AbstractPropType.java +++ b/src/main/java/jpos/profile/AbstractPropType.java @@ -25,37 +25,9 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public abstract class AbstractPropType extends Object - implements PropType, Serializable +public abstract class AbstractPropType implements PropType, Serializable { - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor */ - AbstractPropType() {} - - //------------------------------------------------------------------------- - // Public abstract methods - // - - /** @return a String representation of this PropType */ - public abstract String toString(); - - /** @return a Java class that defines this type */ - public abstract Class getJavaTypeClass(); - - /** - * @return true if the object passed is of this PropType - * @param obj the Java Object - */ - public abstract boolean isValidValue( Object obj ); - - /** - * @return true if the PropValue passed is of this PropType - * @param obj the PropValue - */ - public abstract boolean isValidValue( PropValue obj ); + private static final long serialVersionUID = 3913079070484345271L; //------------------------------------------------------------------------- // Public methods diff --git a/src/main/java/jpos/profile/BooleanPropType.java b/src/main/java/jpos/profile/BooleanPropType.java index 1b5de79..fb6e74d 100644 --- a/src/main/java/jpos/profile/BooleanPropType.java +++ b/src/main/java/jpos/profile/BooleanPropType.java @@ -31,8 +31,9 @@ public class BooleanPropType extends AbstractPropType implements PropType, Seria // Ctor(s) // - /** Default ctor */ - BooleanPropType() {} + private static final long serialVersionUID = -5426902119474210358L; + + private BooleanPropType() {} //--------------------------------------------------------------------- // Class methods @@ -41,9 +42,6 @@ public class BooleanPropType extends AbstractPropType implements PropType, Seria /** @return the unique instance of this class (create if necessary) */ public static PropType getInstance() { - if( instance == null ) - instance = new BooleanPropType(); - return instance; } @@ -55,7 +53,7 @@ public static PropType getInstance() public String toString() { return "BooleanPropType"; } /** @return a Java class that defines this type */ - public Class getJavaTypeClass() { return ( new Boolean( false ) ).getClass(); } + public Class getJavaTypeClass() { return Boolean.class; } /** * @return true if the object passed is of this PropType @@ -79,5 +77,5 @@ public static PropType getInstance() // Class instance // - private static PropType instance = null; + private static final PropType instance = new BooleanPropType(); } diff --git a/src/main/java/jpos/profile/CharacterPropType.java b/src/main/java/jpos/profile/CharacterPropType.java index 775e53b..acec74c 100644 --- a/src/main/java/jpos/profile/CharacterPropType.java +++ b/src/main/java/jpos/profile/CharacterPropType.java @@ -31,8 +31,9 @@ public class CharacterPropType extends AbstractPropType implements PropType, Ser // Ctor(s) // - /** Default ctor */ - CharacterPropType() {} + private static final long serialVersionUID = -8746645376918501427L; + + private CharacterPropType() {} //--------------------------------------------------------------------- // Class methods @@ -41,9 +42,6 @@ public class CharacterPropType extends AbstractPropType implements PropType, Ser /** @return the unique instance of this class (create if necessary) */ public static PropType getInstance() { - if( instance == null ) - instance = new CharacterPropType(); - return instance; } @@ -55,7 +53,7 @@ public static PropType getInstance() public String toString() { return "CharacterPropType"; } /** @return a Java class that defines this type */ - public Class getJavaTypeClass() { return ( new Character( 'c' ) ).getClass(); } + public Class getJavaTypeClass() { return Character.class; } /** * @return true if the object passed is of this PropType @@ -79,5 +77,5 @@ public static PropType getInstance() // Class instance // - private static PropType instance = null; + private static final PropType instance = new CharacterPropType(); } diff --git a/src/main/java/jpos/profile/DefaultDevCatInfo.java b/src/main/java/jpos/profile/DefaultDevCatInfo.java index 49a7767..4bfbab0 100644 --- a/src/main/java/jpos/profile/DefaultDevCatInfo.java +++ b/src/main/java/jpos/profile/DefaultDevCatInfo.java @@ -23,7 +23,7 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -class DefaultDevCatInfo extends Object implements DevCatInfo +class DefaultDevCatInfo implements DevCatInfo { //------------------------------------------------------------------------- // Ctor(s) @@ -62,7 +62,7 @@ class DefaultDevCatInfo extends Object implements DevCatInfo /** @return the String representation of this DevCat */ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append( "\n" ); sb.append( " \n" ); diff --git a/src/main/java/jpos/profile/XercesProfileFactory.java b/src/main/java/jpos/profile/DefaultProfileFactory.java similarity index 87% rename from src/main/java/jpos/profile/XercesProfileFactory.java rename to src/main/java/jpos/profile/DefaultProfileFactory.java index 45103b4..fc71820 100644 --- a/src/main/java/jpos/profile/XercesProfileFactory.java +++ b/src/main/java/jpos/profile/DefaultProfileFactory.java @@ -26,7 +26,6 @@ import javax.xml.parsers.*; import org.w3c.dom.*; -import org.apache.xerces.parsers.DOMParser; import org.xml.sax.*; import jpos.util.XmlHelper; @@ -34,20 +33,12 @@ import jpos.util.tracing.TracerFactory; /** - * Default implementation of the ProfileFactory interface uses the Apache Xerces + * Default implementation of the ProfileFactory interface using an * XML parser to create profiles from the XML file passed - * @since 1.3 (SF 2K meeting) - * @author E. Michael Maximilien (maxim@us.ibm.com) + * @since 4.0 */ -public class XercesProfileFactory extends Object implements ProfileFactory +public class DefaultProfileFactory implements ProfileFactory { - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor */ - public XercesProfileFactory() {} - //------------------------------------------------------------------------- // Private methods // @@ -118,8 +109,8 @@ Document parse( String xmlFileName ) throws ProfileException Document document = docBuilder.parse( new File( xmlFileName ) ); - if( errorHandler.getErrorList().size() > 0 || - errorHandler.getFatalErrorList().size() > 0 ) + if( !errorHandler.getErrorList().isEmpty() || + !errorHandler.getFatalErrorList().isEmpty() ) { String msg = "Error while parsing XML file, set properties"+ " jpos.tracing = ON in jpos.properties" + @@ -169,9 +160,7 @@ Document parseSchema( String xmlFileName ) throws ProfileException DefaultErrorHandler errorHandler = this.new DefaultErrorHandler(); docBuilder.setErrorHandler( errorHandler ); - Document document = docBuilder.parse( new File( xmlFileName ) ); - - return document; + return docBuilder.parse( new File( xmlFileName ) ); } catch( IOException ioe ) { @@ -225,12 +214,8 @@ public Profile createProfile( String xmlProfileFileName ) throws ProfileExceptio // Instance variables // - private Profile profile = null; - - private DOMParser domParser = new DOMParser(); - private DefaultErrorHandler errorHandler = this.new DefaultErrorHandler(); private Tracer tracer = TracerFactory.getInstance(). - createTracer( "XercesProfileFactory" ); + createTracer( this.getClass().getSimpleName() ); //------------------------------------------------------------------------- // Inner classes @@ -241,17 +226,17 @@ public Profile createProfile( String xmlProfileFileName ) throws ProfileExceptio * @since 1.3 (Washington DC 2001 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ - class DefaultErrorHandler extends Object implements org.xml.sax.ErrorHandler + class DefaultErrorHandler implements org.xml.sax.ErrorHandler { //--------------------------------------------------------------------- // Package methods // - List getErrorList() { return errorList; } + List getErrorList() { return errorList; } - List getWarningList() { return warningList; } + List getWarningList() { return warningList; } - List getFatalErrorList() { return fatalErrorList; } + List getFatalErrorList() { return fatalErrorList; } //--------------------------------------------------------------------- // Public methods @@ -279,9 +264,9 @@ public void fatalError( SAXParseException e ) throws SAXException // Private variables // - private List warningList = new ArrayList(); - private List errorList = new ArrayList(); - private List fatalErrorList = new ArrayList(); + private final List warningList = new ArrayList<>(); + private final List errorList = new ArrayList<>(); + private final List fatalErrorList = new ArrayList<>(); } //------------------------------------------------------------------------- diff --git a/src/main/java/jpos/profile/DefaultProfileRegistry.java b/src/main/java/jpos/profile/DefaultProfileRegistry.java index e74a67a..d3ba1f7 100644 --- a/src/main/java/jpos/profile/DefaultProfileRegistry.java +++ b/src/main/java/jpos/profile/DefaultProfileRegistry.java @@ -25,15 +25,8 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class DefaultProfileRegistry extends Object implements ProfileRegistry +public class DefaultProfileRegistry implements ProfileRegistry { - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor*/ - public DefaultProfileRegistry() {} - //------------------------------------------------------------------------- // Public methods // @@ -57,13 +50,13 @@ public DefaultProfileRegistry() {} public boolean hasProfile( Profile profile ) { return table.containsValue( profile ); } /** @return an enumeration of Profile objects */ - public Enumeration getProfiles() { return table.elements(); } + public Enumeration getProfiles() { return table.elements(); } /** * @return the Profile for the profileName specified * @param profileName the unique name of this profile */ - public Profile getProfile( String profileName ) { return (Profile)table.get( profileName ); } + public Profile getProfile( String profileName ) { return table.get( profileName ); } /** * Add an Profile for the service with logical name specified @@ -94,5 +87,5 @@ public DefaultProfileRegistry() {} // Instance varaibles // - private Hashtable table = new Hashtable(); + private final Hashtable table = new Hashtable<>(); } diff --git a/src/main/java/jpos/profile/DefaultPropInfo.java b/src/main/java/jpos/profile/DefaultPropInfo.java index 74ce4a3..444f084 100644 --- a/src/main/java/jpos/profile/DefaultPropInfo.java +++ b/src/main/java/jpos/profile/DefaultPropInfo.java @@ -25,12 +25,10 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -class DefaultPropInfo extends Object implements PropInfo, Serializable +class DefaultPropInfo implements PropInfo, Serializable { - //------------------------------------------------------------------------- - // Ctor(s) - // - + private static final long serialVersionUID = -4186241572660113196L; + /** * Creates a PropInfo with name and profile passed * @param name the PropInfo name diff --git a/src/main/java/jpos/profile/DefaultPropInfoList.java b/src/main/java/jpos/profile/DefaultPropInfoList.java index b39ecde..6343fcd 100644 --- a/src/main/java/jpos/profile/DefaultPropInfoList.java +++ b/src/main/java/jpos/profile/DefaultPropInfoList.java @@ -25,15 +25,8 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -class DefaultPropInfoList extends Object implements PropInfoList +class DefaultPropInfoList implements PropInfoList { - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor */ - public DefaultPropInfoList() {} - //------------------------------------------------------------------------- // Public methods // @@ -77,7 +70,7 @@ public DefaultPropInfoList() {} * @author E. Michael Maximilien (maxim@us.ibm.com) * @since 1.3 (SF 2K meeting) */ - class DefaultIterator extends Object implements PropInfoList.Iterator + class DefaultIterator implements PropInfoList.Iterator { //--------------------------------------------------------------------- // Ctor(s) @@ -91,7 +84,7 @@ class DefaultIterator extends Object implements PropInfoList.Iterator // /** @return the next PropInfo in the iterator */ - public PropInfo next() { return (PropInfo)iterator.next(); } + public PropInfo next() { return iterator.next(); } /** @return true if there is a next PropInfo in the iterator */ public boolean hasNext() { return iterator.hasNext(); } @@ -100,12 +93,12 @@ class DefaultIterator extends Object implements PropInfoList.Iterator // Instance variables // - private java.util.Iterator iterator = null; + private final java.util.Iterator iterator; } //------------------------------------------------------------------------- // Instance variables // - private List list = new ArrayList(); + private final List list = new ArrayList<>(); } diff --git a/src/main/java/jpos/profile/DefaultPropValue.java b/src/main/java/jpos/profile/DefaultPropValue.java index 9e26c39..964fc72 100644 --- a/src/main/java/jpos/profile/DefaultPropValue.java +++ b/src/main/java/jpos/profile/DefaultPropValue.java @@ -19,17 +19,16 @@ /////////////////////////////////////////////////////////////////////////////// import java.io.Serializable; +import java.util.Objects; /** * Class implementing the PropValue interface * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -class DefaultPropValue extends Object implements PropValue, Serializable +class DefaultPropValue implements PropValue, Serializable { - //------------------------------------------------------------------------- - // Ctor(s) - // + private static final long serialVersionUID = 8895132456252900824L; /** * 2-arg ctor taking Object value @@ -55,19 +54,23 @@ class DefaultPropValue extends Object implements PropValue, Serializable /** @return the PropType for this PropValue */ public PropType getType() { return type; } - /** - * @return true if this and the other value are equal - * @param other the other PropValue - */ - public boolean equals( Object propValue ) - { - if( propValue == null ) return false; - - if( !( propValue instanceof PropValue ) ) return false; - - return value.equals( ( (PropValue)propValue ).getValue() ); + @Override + public int hashCode() { + return Objects.hash(type, value); } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DefaultPropValue other = (DefaultPropValue) obj; + return Objects.equals(type, other.type) && Objects.equals(value, other.value); + } + //------------------------------------------------------------------------- // Instance variables // diff --git a/src/main/java/jpos/profile/DefaultPropValueList.java b/src/main/java/jpos/profile/DefaultPropValueList.java index 448b0e2..f2a509d 100644 --- a/src/main/java/jpos/profile/DefaultPropValueList.java +++ b/src/main/java/jpos/profile/DefaultPropValueList.java @@ -25,15 +25,8 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -class DefaultPropValueList extends Object implements PropValueList +class DefaultPropValueList implements PropValueList { - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor */ - DefaultPropValueList() {} - //------------------------------------------------------------------------- // Public methods // @@ -77,7 +70,7 @@ class DefaultPropValueList extends Object implements PropValueList * @author E. Michael Maximilien (maxim@us.ibm.com) * @since 1.3 (SF 2K meeting) */ - class DefaultIterator extends Object implements PropValueList.Iterator + class DefaultIterator implements PropValueList.Iterator { //--------------------------------------------------------------------- // Ctor(s) @@ -91,7 +84,7 @@ class DefaultIterator extends Object implements PropValueList.Iterator // /** @return the next PropValue in the iterator */ - public PropValue next() { return (PropValue)iterator.next(); } + public PropValue next() { return iterator.next(); } /** @return true if there is a next PropValue in the iterator */ public boolean hasNext() { return iterator.hasNext(); } @@ -100,12 +93,12 @@ class DefaultIterator extends Object implements PropValueList.Iterator // Instance variables // - private java.util.Iterator iterator = null; + private final java.util.Iterator iterator; } //------------------------------------------------------------------------- // Instance variables // - private List list = new ArrayList(); + private List list = new ArrayList<>(); } diff --git a/src/main/java/jpos/profile/FloatPropType.java b/src/main/java/jpos/profile/FloatPropType.java index eb1e5db..bae99de 100644 --- a/src/main/java/jpos/profile/FloatPropType.java +++ b/src/main/java/jpos/profile/FloatPropType.java @@ -27,12 +27,9 @@ */ public class FloatPropType extends AbstractPropType implements PropType, Serializable { - //------------------------------------------------------------------------- - // Ctor(s) - // + private static final long serialVersionUID = 5727449475781327225L; - /** Default ctor */ - FloatPropType() {} + private FloatPropType() {} //--------------------------------------------------------------------- // Class methods @@ -41,9 +38,6 @@ public class FloatPropType extends AbstractPropType implements PropType, Seriali /** @return the unique instance of this class (create if necessary) */ public static PropType getInstance() { - if( instance == null ) - instance = new FloatPropType(); - return instance; } @@ -52,10 +46,11 @@ public static PropType getInstance() // /** @return a String representation of this PropType */ + @Override public String toString() { return "FloatPropType"; } /** @return a Java class that defines this type */ - public Class getJavaTypeClass() { return ( new Float( 1.0 ) ).getClass(); } + public Class getJavaTypeClass() { return Float.class; } /** * @return true if the object passed is of this PropType @@ -79,5 +74,5 @@ public static PropType getInstance() // Class instance // - private static PropType instance = null; + private static final PropType instance = new FloatPropType(); } diff --git a/src/main/java/jpos/profile/IntegerPropType.java b/src/main/java/jpos/profile/IntegerPropType.java index 831c921..02746e4 100644 --- a/src/main/java/jpos/profile/IntegerPropType.java +++ b/src/main/java/jpos/profile/IntegerPropType.java @@ -27,12 +27,9 @@ */ public class IntegerPropType extends AbstractPropType implements PropType, Serializable { - //------------------------------------------------------------------------- - // Ctor(s) - // + private static final long serialVersionUID = 5055260665874384882L; - /** Default ctor */ - IntegerPropType() {} + private IntegerPropType() {} //--------------------------------------------------------------------- // Class methods @@ -41,9 +38,6 @@ public class IntegerPropType extends AbstractPropType implements PropType, Seria /** @return the unique instance of this class (create if necessary) */ public static PropType getInstance() { - if( instance == null ) - instance = new IntegerPropType(); - return instance; } @@ -55,7 +49,7 @@ public static PropType getInstance() public String toString() { return "IntegerPropType"; } /** @return a Java class that defines this type */ - public Class getJavaTypeClass() { return ( new Integer( 0 ) ).getClass(); } + public Class getJavaTypeClass() { return Integer.class; } /** * @return true if the object passed is of this PropType @@ -79,5 +73,5 @@ public static PropType getInstance() // Class instance // - private static PropType instance = null; + private static final PropType instance = new IntegerPropType(); } diff --git a/src/main/java/jpos/profile/JposDevCats.java b/src/main/java/jpos/profile/JposDevCats.java index 97abe65..40fbe82 100644 --- a/src/main/java/jpos/profile/JposDevCats.java +++ b/src/main/java/jpos/profile/JposDevCats.java @@ -25,13 +25,13 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class JposDevCats extends Object +public class JposDevCats { //------------------------------------------------------------------------- // Private class constants // - private static final Hashtable DEVCAT_TABLE = new Hashtable(); + private static final HashMap DEVCAT_TABLE = new HashMap<>(); //------------------------------------------------------------------------- // Class constants @@ -232,7 +232,7 @@ public class JposDevCats extends Object public static DevCat getDevCatForName( String devCatName ) { if( DEVCAT_TABLE.containsKey( devCatName ) ) - return (DevCat)DEVCAT_TABLE.get( devCatName ); + return DEVCAT_TABLE.get( devCatName ); return UNKNOWN_DEVCAT; } @@ -246,8 +246,7 @@ public static DevCat getDevCatForName( String devCatName ) * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ - public static abstract class AbstractDevCat extends Object - implements DevCat + public abstract static class AbstractDevCat implements DevCat { //--------------------------------------------------------------------- // Public overriden methods @@ -261,6 +260,7 @@ public static abstract class AbstractDevCat extends Object * category * @param obj the other object to compare to */ + @Override public boolean equals( Object obj ) { if( obj == null ) return false; @@ -269,6 +269,12 @@ public boolean equals( Object obj ) return toString().equals( obj.toString() ); } + + @Override + public int hashCode() { + return toString().hashCode(); + } + } /** diff --git a/src/main/java/jpos/profile/ProfileException.java b/src/main/java/jpos/profile/ProfileException.java index 6c5f6b4..c46db4f 100644 --- a/src/main/java/jpos/profile/ProfileException.java +++ b/src/main/java/jpos/profile/ProfileException.java @@ -25,22 +25,29 @@ */ public class ProfileException extends Exception { + private static final long serialVersionUID = -5646225627523470777L; + //------------------------------------------------------------------------- // Ctor(s) // - + /** * 1-arg ctor * @param msg the exception's message */ - public ProfileException( String msg ) { super( msg ); } + public ProfileException( String msg ) { + this( msg, null ); + } /** * 2 args ctor * @param msg the exception's message * @param e the original exception causing this one */ - public ProfileException( String msg, Exception e ) { this( msg ); origException = e; } + public ProfileException( String msg, Exception e ) { + super( msg ); + origException = e; + } //------------------------------------------------------------------------- // Public methods @@ -53,5 +60,5 @@ public class ProfileException extends Exception // Private instance variables // - private Exception origException = null; + private final Exception origException; } diff --git a/src/main/java/jpos/profile/ProfileRegistry.java b/src/main/java/jpos/profile/ProfileRegistry.java index d580a08..1d4953d 100644 --- a/src/main/java/jpos/profile/ProfileRegistry.java +++ b/src/main/java/jpos/profile/ProfileRegistry.java @@ -46,7 +46,8 @@ public interface ProfileRegistry public boolean hasProfile( Profile profile ); /** @return an enumeration of Profile objects */ - public Enumeration getProfiles(); + @SuppressWarnings("rawtypes") + public Enumeration getProfiles(); /** * @return the Profile for the profileName specified diff --git a/src/main/java/jpos/profile/PropType.java b/src/main/java/jpos/profile/PropType.java index 0f9af75..49857a6 100644 --- a/src/main/java/jpos/profile/PropType.java +++ b/src/main/java/jpos/profile/PropType.java @@ -32,7 +32,7 @@ public interface PropType public String getDescription(); /** @return a Java class that defines this type */ - public Class getJavaTypeClass(); + public Class getJavaTypeClass(); /** * @return true if the object passed is of this PropType diff --git a/src/main/java/jpos/profile/StringPropType.java b/src/main/java/jpos/profile/StringPropType.java index 4b073ea..3b0d881 100644 --- a/src/main/java/jpos/profile/StringPropType.java +++ b/src/main/java/jpos/profile/StringPropType.java @@ -27,12 +27,9 @@ */ public class StringPropType extends AbstractPropType implements PropType, Serializable { - //------------------------------------------------------------------------- - // Ctor(s) - // + private static final long serialVersionUID = -8148982860254316606L; - /** Default ctor */ - StringPropType() {} + private StringPropType() {} //--------------------------------------------------------------------- // Class methods @@ -41,9 +38,6 @@ public class StringPropType extends AbstractPropType implements PropType, Serial /** @return the unique instance of this class (create if necessary) */ public static PropType getInstance() { - if( instance == null ) - instance = new StringPropType(); - return instance; } @@ -55,7 +49,7 @@ public static PropType getInstance() public String toString() { return "StringPropType"; } /** @return a Java class that defines this type */ - public Class getJavaTypeClass() { return "".getClass(); } + public Class getJavaTypeClass() { return String.class; } /** * @return true if the object passed is of this PropType @@ -79,5 +73,5 @@ public static PropType getInstance() // Class instance // - private static PropType instance = null; + private static final PropType instance = new StringPropType(); } diff --git a/src/main/java/jpos/util/Comparable.java b/src/main/java/jpos/util/Comparable.java index a95126c..ee7c10f 100644 --- a/src/main/java/jpos/util/Comparable.java +++ b/src/main/java/jpos/util/Comparable.java @@ -28,12 +28,14 @@ public interface Comparable /** * Compares this and other arguments for order * @param other object to compare to + * @return same as defined by {@link Comparable#compareTo(Object)} */ public int compareTo( Object other ); /** * Indicates this object is "equal to" the other * @param other object to compare to + * @return same as defined by {@link Object#equals(Object)} */ public boolean equals( Object other ); } diff --git a/src/main/java/jpos/util/DefaultComparableElement.java b/src/main/java/jpos/util/DefaultComparableElement.java index 9ef896e..0fd2b97 100644 --- a/src/main/java/jpos/util/DefaultComparableElement.java +++ b/src/main/java/jpos/util/DefaultComparableElement.java @@ -24,7 +24,7 @@ * @author E. Michael Maximilien (maxim@us.ibm.com) * @version 1.2.0 (JDK 1.1.x) */ -public class DefaultComparableElement extends Object implements Comparable +public class DefaultComparableElement implements Comparable { //------------------------------------------------------------------------- // Ctor diff --git a/src/main/java/jpos/util/DefaultProperties.java b/src/main/java/jpos/util/DefaultProperties.java index 9a223cb..1fc3f05 100644 --- a/src/main/java/jpos/util/DefaultProperties.java +++ b/src/main/java/jpos/util/DefaultProperties.java @@ -30,7 +30,7 @@ * @since 1.2 (NY 2K 99 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class DefaultProperties extends Object implements JposProperties +public class DefaultProperties implements JposProperties { //------------------------------------------------------------------------- // Public methods @@ -88,7 +88,8 @@ public boolean isPropertyDefined( String propName ) if( jposProperties != null ) { - Enumeration keys = jposProperties.keys(); + @SuppressWarnings("rawtypes") + Enumeration keys = jposProperties.keys(); while( keys.hasMoreElements() ) { @@ -106,7 +107,8 @@ public boolean isPropertyDefined( String propName ) * @return an enumeration of properties names defined * @since 1.2 (NY 2K meeting) */ - public Enumeration getPropertyNames() { return getJposProperties().keys(); } + @SuppressWarnings("rawtypes") + public Enumeration getPropertyNames() { return getJposProperties().keys(); } /** * @return the MultiProperty by the name passed. MultiProperty are properties @@ -139,10 +141,10 @@ public boolean hasMultiProperty( String multiPropName ) * @param propName the property name for which the values will be parsed from * @since 2.1.0 */ - public List getStringListProperty( String propName ) + public List getStringListProperty( String propName ) { String propValue = getPropertyString( propName ); - List list = new ArrayList(); + List list = new ArrayList<>(); if( propValue == null ) return list; @@ -164,29 +166,23 @@ public List getStringListProperty( String propName ) */ public String toString() { - StringBuffer sb = new StringBuffer(); - sb.append( "\n" ); - - Enumeration propNames = getPropertyNames(); + @SuppressWarnings("unchecked") + Enumeration propNames = getPropertyNames(); - List list = new ArrayList(); + List list = new ArrayList<>(); while( propNames.hasMoreElements() ) - list.add( propNames.nextElement() ); + list.add( propNames.nextElement() ); Collections.sort( list ); - Iterator iterator = list.iterator(); - - while( iterator.hasNext() ) - { - String propName = (String)iterator.next(); + StringBuilder sb = new StringBuilder(); + sb.append( "\n" ); + for (String propName : list) { String propValue = (String)getPropertyString( propName ); - sb.append( "\n" ); - } - + } sb.append( "\n" ); return sb.toString(); @@ -200,6 +196,7 @@ public String toString() * @return a Comparator object to compare 2 JposProperties.Prop objects * @since 1.3 (Washington DC 2001 meeting) */ + @SuppressWarnings("rawtypes") public static Comparator propComparator() { if( propComparator == null ) @@ -225,13 +222,10 @@ public int compare( Object o1, Object o2 ) * @return an Iterator of valid property names * @since 1.3 (Washington DC 2001) */ - public static Iterator getDefinedPropertyNames() + public static Iterator getDefinedPropertyNames() { - List list = new ArrayList(); - - for( int i = 0; i < PROP_NAME_ARRAY.length; ++i ) - list.add( PROP_NAME_ARRAY[ i ] ); - + List list = Arrays.asList(Arrays.copyOf(PROP_NAME_ARRAY, PROP_NAME_ARRAY.length)); + return list.iterator(); } @@ -245,6 +239,7 @@ public static Iterator getDefinedPropertyNames() */ protected void createMultiProperties() { + @SuppressWarnings("rawtypes") Enumeration propNames = jposProperties.keys(); while( propNames.hasMoreElements() ) @@ -313,14 +308,15 @@ protected String getMultiPropBaseName( String propName ) throws IllegalArgumentE * @see jpos.util.JposProperties.Prop * @since 1.3 (Washington DC 2001) */ - public Iterator getProps() + public Iterator getProps() { - List list = new ArrayList(); + List list = new ArrayList<>(); - Enumeration names = getPropertyNames(); + @SuppressWarnings("unchecked") + Enumeration names = getPropertyNames(); while( names.hasMoreElements() ) { - String name = (String)names.nextElement(); + String name = names.nextElement(); String value = getPropertyString( name ); JposProperties.Prop prop = new DefaultProperties.Prop( name, value ); @@ -415,7 +411,7 @@ Properties findProperties( String propFileName ) // private Properties jposProperties = null; - private HashMap multiPropMap = new HashMap(); + private final HashMap multiPropMap = new HashMap<>(); private String loadedPropFileName = ""; @@ -426,6 +422,7 @@ Properties findProperties( String propFileName ) // Class variables // + @SuppressWarnings("rawtypes") private static Comparator propComparator = null; //------------------------------------------------------------------------- @@ -439,7 +436,7 @@ Properties findProperties( String propFileName ) * @author E. Michael Maximilien (maxim@us.ibm.com) * @since 1.3 (Washington DC 2001 meeting) */ - class MultiProp extends Object implements JposProperties.MultiProperty + class MultiProp implements JposProperties.MultiProperty { //--------------------------------------------------------------------- // Ctor(s) @@ -465,12 +462,12 @@ class MultiProp extends Object implements JposProperties.MultiProperty public String getBasePropertyName() { return basePropName; } /** @return an iterator of the property names for this multi-property */ - public Iterator getPropertyNames() { return propMap.keySet().iterator(); } + public Iterator getPropertyNames() { return propMap.keySet().iterator(); } /** @return an iterator of the property names alphabetically sorted for this multi-property */ - public Iterator getSortedPropertyNames() + public Iterator getSortedPropertyNames() { - List namesList = new ArrayList( propMap.keySet() ); + List namesList = new ArrayList<>( propMap.keySet() ); Collections.sort( namesList ); @@ -478,14 +475,14 @@ public Iterator getSortedPropertyNames() } /** @return an iterator of the property values for this multi-property */ - public Iterator getPropertyValues() { return propMap.values().iterator(); } + public Iterator getPropertyValues() { return propMap.values().iterator(); } /** * @return the value for this property from the full property name * @param propName the full property name <name>.x */ public String getPropertyString( String propName ) - { return (String)propMap.get( propName ); } + { return propMap.get( propName ); } /** * @return the value for this property from the full property name @@ -558,14 +555,14 @@ void add( String propName, String propValue ) throws IllegalArgumentException * @return the propValue of the property removed or null if not found * @param propName the property name */ - String remove( String propName ) { return (String)propMap.remove( propName ); } + String remove( String propName ) { return propMap.remove( propName ); } //--------------------------------------------------------------------- // Instance variables // private String basePropName = ""; - private HashMap propMap = new HashMap(); + private final HashMap propMap = new HashMap<>(); } /** @@ -573,7 +570,7 @@ void add( String propName, String propValue ) throws IllegalArgumentException * @author E. Michael Maximilien (maxim@us.ibm.com) * @since 1.3 (Washington DC 2001) */ - public static class Prop extends Object implements JposProperties.Prop + public static class Prop implements JposProperties.Prop { //--------------------------------------------------------------------- // Ctor diff --git a/src/main/java/jpos/util/FileUtil.java b/src/main/java/jpos/util/FileUtil.java index 130ed32..b2e0a25 100644 --- a/src/main/java/jpos/util/FileUtil.java +++ b/src/main/java/jpos/util/FileUtil.java @@ -25,7 +25,7 @@ import java.io.InputStream; import java.util.List; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.Enumeration; import java.util.StringTokenizer; @@ -41,7 +41,7 @@ * @version 0.0.1 * @since 2.1.0 */ -public class FileUtil extends Object +public class FileUtil { //------------------------------------------------------------------------- // Ctor(s) @@ -54,11 +54,11 @@ protected FileUtil() {} // Private/protected static methods // - protected synchronized static List getCpDirList() + protected static synchronized List getCpDirList() { String classpath = System.getProperty( "java.class.path" ); - List cpDirList = new LinkedList(); + List cpDirList = new ArrayList<>(); StringTokenizer st = new StringTokenizer( classpath, File.pathSeparator ); while( st.hasMoreTokens() ) @@ -77,11 +77,11 @@ protected synchronized static List getCpDirList() return cpDirList; } - protected synchronized static List getJarList() + protected static synchronized List getJarList() { String classpath = System.getProperty( "java.class.path" ); - List cpJarFilesList = new LinkedList(); + List cpJarFilesList = new ArrayList<>(); StringTokenizer st = new StringTokenizer( classpath, File.pathSeparator ); while( st.hasMoreTokens() ) @@ -96,17 +96,17 @@ protected synchronized static List getJarList() return cpJarFilesList; } - protected synchronized static JarEntry getJarEntry( JarFile jarFile, String fileName ) + protected static synchronized JarEntry getJarEntry( JarFile jarFile, String fileName ) { tracer.println( "" ); if( jarFile == null ) return null; - Enumeration entries = jarFile.entries(); + Enumeration entries = jarFile.entries(); while( entries.hasMoreElements() ) { - JarEntry jarEntry = (JarEntry)entries.nextElement(); + JarEntry jarEntry = entries.nextElement(); if( jarEntry.getName().equals( fileName ) ) { @@ -127,7 +127,7 @@ protected synchronized static JarEntry getJarEntry( JarFile jarFile, String file * @param fileName the relative fileName to search for * directories specified by CLASSPATH */ - protected synchronized static JarFile lookForFileInJars( String fileName ) + protected static synchronized JarFile lookForFileInJars( String fileName ) { try { @@ -137,12 +137,11 @@ protected synchronized static JarFile lookForFileInJars( String fileName ) tracer.println( "classpath="+classpath ); - List cpJarFilesList = getJarList(); - List cpDirList = getCpDirList(); + List cpJarFilesList = getJarList(); for( int i = 0; i < cpJarFilesList.size(); ++i ) { - String jarFileName = (String)cpJarFilesList.get( i ); + String jarFileName = cpJarFilesList.get( i ); tracer.println( "jarFileName=" + jarFileName ); @@ -207,13 +206,12 @@ public synchronized static File findFile( String fileName, File file = new File( fileName ); if( file.exists() ) return file; - List cpJarFilesList = getJarList(); - List cpDirList = getCpDirList(); + List cpDirList = getCpDirList(); if( searchInClassPath ) for( int i = 0; i < cpDirList.size(); ++i ) { - String path = (String)cpDirList.get( i ); + String path = cpDirList.get( i ); File file2 = new File( path + File.separator + fileName ); if( file2.exists() ) return file2; @@ -235,10 +233,9 @@ public synchronized static File findFile( String fileName, * @throws java.io.FileNotFoundException if the file could not be found * @throws java.io.IOException if an error occurred while loading file */ - public synchronized static InputStream - loadFile( String fileName, boolean searchInClassPath, - boolean searchInJarFile ) - throws FileNotFoundException, IOException + public static synchronized InputStream + loadFile( String fileName, boolean searchInClassPath, boolean searchInJarFile ) + throws IOException { tracer.println( " getStandardPropNames() { return STANDARD_PROP_NAMES_LIST.iterator(); } /** * @return a enumeration of all non-required property names from the jposEntry * @param jposEntry the JposEntry */ - public static Enumeration getNonRequiredPropNames( JposEntry jposEntry ) + public static Enumeration getNonRequiredPropNames( JposEntry jposEntry ) { - Vector vector = new Vector(); + List list = new ArrayList<>(); - Enumeration names = jposEntry.getPropertyNames(); + @SuppressWarnings("unchecked") + Enumeration names = jposEntry.getPropertyNames(); while( names.hasMoreElements() ) { - String name = (String)names.nextElement(); + String name = names.nextElement(); - if( isRequiredPropName( name ) == false ) - vector.add( name ); + if( !isRequiredPropName( name ) ) + list.add( name ); } - return vector.elements(); + return Collections.enumeration(list); } /** @@ -152,41 +154,41 @@ public static Enumeration getNonRequiredPropNames( JposEntry jposEntry ) * from the JposEntry object passed * @param jposEntry the entry to validate */ - public static Enumeration getMissingRequiredPropNames( JposEntry jposEntry ) + public static Enumeration getMissingRequiredPropNames( JposEntry jposEntry ) { - Vector vector = new Vector(); + List list = new ArrayList<>(); - if( !jposEntry.hasPropertyWithName( JposEntry.LOGICAL_NAME_PROP_NAME ) ) - vector.add( JposEntry.LOGICAL_NAME_PROP_NAME ); + if( !jposEntry.hasPropertyWithName( JposEntry.LOGICAL_NAME_PROP_NAME ) ) + list.add( JposEntry.LOGICAL_NAME_PROP_NAME ); if( !jposEntry.hasPropertyWithName( JposEntry.SI_FACTORY_CLASS_PROP_NAME ) ) - vector.add( JposEntry.SI_FACTORY_CLASS_PROP_NAME ); + list.add( JposEntry.SI_FACTORY_CLASS_PROP_NAME ); if( !jposEntry.hasPropertyWithName( JposEntry.SERVICE_CLASS_PROP_NAME ) ) - vector.add( JposEntry.SERVICE_CLASS_PROP_NAME ); + list.add( JposEntry.SERVICE_CLASS_PROP_NAME ); if( !jposEntry.hasPropertyWithName( JposEntry.DEVICE_CATEGORY_PROP_NAME ) ) - vector.add( JposEntry.DEVICE_CATEGORY_PROP_NAME ); + list.add( JposEntry.DEVICE_CATEGORY_PROP_NAME ); if( !jposEntry.hasPropertyWithName( JposEntry.JPOS_VERSION_PROP_NAME ) ) - vector.add( JposEntry.JPOS_VERSION_PROP_NAME ); + list.add( JposEntry.JPOS_VERSION_PROP_NAME ); if( !jposEntry.hasPropertyWithName( JposEntry.VENDOR_NAME_PROP_NAME ) ) - vector.add( JposEntry.VENDOR_NAME_PROP_NAME ); + list.add( JposEntry.VENDOR_NAME_PROP_NAME ); if( !jposEntry.hasPropertyWithName( JposEntry.VENDOR_URL_PROP_NAME ) ) - vector.add( JposEntry.VENDOR_URL_PROP_NAME ); + list.add( JposEntry.VENDOR_URL_PROP_NAME ); if( !jposEntry.hasPropertyWithName( JposEntry.PRODUCT_NAME_PROP_NAME ) ) - vector.add( JposEntry.PRODUCT_NAME_PROP_NAME ); + list.add( JposEntry.PRODUCT_NAME_PROP_NAME ); if( !jposEntry.hasPropertyWithName( JposEntry.PRODUCT_URL_PROP_NAME ) ) - vector.add( JposEntry.PRODUCT_URL_PROP_NAME ); + list.add( JposEntry.PRODUCT_URL_PROP_NAME ); if( !jposEntry.hasPropertyWithName( JposEntry.PRODUCT_DESCRIPTION_PROP_NAME ) ) - vector.add( JposEntry.PRODUCT_DESCRIPTION_PROP_NAME ); + list.add( JposEntry.PRODUCT_DESCRIPTION_PROP_NAME ); - return vector.elements(); + return Collections.enumeration(list); } /** @@ -194,52 +196,53 @@ public static Enumeration getMissingRequiredPropNames( JposEntry jposEntry ) * from the JposEntry object passed * @param jposEntry the entry to validate */ - public static Enumeration getMissingRS232PropNames( JposEntry jposEntry ) + public static Enumeration getMissingRS232PropNames( JposEntry jposEntry ) { - Vector vector = new Vector(); + List list = new ArrayList<>(); - if( !jposEntry.hasPropertyWithName( JposEntryConst.RS232_PORT_NAME_PROP_NAME ) ) - vector.add( JposEntryConst.RS232_PORT_NAME_PROP_NAME ); + if( !jposEntry.hasPropertyWithName( RS232Const.RS232_PORT_NAME_PROP_NAME ) ) + list.add( RS232Const.RS232_PORT_NAME_PROP_NAME ); - if( !jposEntry.hasPropertyWithName( JposEntryConst.RS232_BAUD_RATE_PROP_NAME ) ) - vector.add( JposEntryConst.RS232_BAUD_RATE_PROP_NAME ); + if( !jposEntry.hasPropertyWithName( RS232Const.RS232_BAUD_RATE_PROP_NAME ) ) + list.add( RS232Const.RS232_BAUD_RATE_PROP_NAME ); - if( !jposEntry.hasPropertyWithName( JposEntryConst.RS232_DATA_BITS_PROP_NAME ) ) - vector.add( JposEntryConst.RS232_DATA_BITS_PROP_NAME ); + if( !jposEntry.hasPropertyWithName( RS232Const.RS232_DATA_BITS_PROP_NAME ) ) + list.add( RS232Const.RS232_DATA_BITS_PROP_NAME ); - if( !jposEntry.hasPropertyWithName( JposEntryConst.RS232_PARITY_PROP_NAME ) ) - vector.add( JposEntryConst.RS232_PARITY_PROP_NAME ); + if( !jposEntry.hasPropertyWithName( RS232Const.RS232_PARITY_PROP_NAME ) ) + list.add( RS232Const.RS232_PARITY_PROP_NAME ); - if( !jposEntry.hasPropertyWithName( JposEntryConst.RS232_STOP_BITS_PROP_NAME ) ) - vector.add( JposEntryConst.RS232_STOP_BITS_PROP_NAME ); + if( !jposEntry.hasPropertyWithName( RS232Const.RS232_STOP_BITS_PROP_NAME ) ) + list.add( RS232Const.RS232_STOP_BITS_PROP_NAME ); - if( !jposEntry.hasPropertyWithName( JposEntryConst.RS232_FLOW_CONTROL_PROP_NAME ) ) - vector.add( JposEntryConst.RS232_FLOW_CONTROL_PROP_NAME ); + if( !jposEntry.hasPropertyWithName( RS232Const.RS232_FLOW_CONTROL_PROP_NAME ) ) + list.add( RS232Const.RS232_FLOW_CONTROL_PROP_NAME ); - return vector.elements(); + return Collections.enumeration(list); } /** * @return an Enumeration of all non-standard properties, that is vendor properties * @param jposEntry the JposEntry to find the vendor property names from */ - public static Enumeration getVendorPropNames( JposEntry jposEntry ) + public static Enumeration getVendorPropNames( JposEntry jposEntry ) { - Vector vector = new Vector(); + List list = new ArrayList<>(); - Enumeration propNames = jposEntry.getPropertyNames(); + @SuppressWarnings("unchecked") + Enumeration propNames = jposEntry.getPropertyNames(); while( propNames.hasMoreElements() ) - vector.add( propNames.nextElement() ); + list.add( propNames.nextElement() ); - Iterator standardPropNames = getStandardPropNames(); + Iterator standardPropNames = getStandardPropNames(); while( standardPropNames.hasNext() ) { - String name = (String)standardPropNames.next(); - vector.remove( name ); + String name = standardPropNames.next(); + list.remove( name ); } - return vector.elements(); + return Collections.enumeration(list); } /** @@ -248,13 +251,13 @@ public static Enumeration getVendorPropNames( JposEntry jposEntry ) */ public static void addMissingRequiredProps( JposEntry jposEntry ) { - Enumeration missingPropNames = getMissingRequiredPropNames( jposEntry ); + Enumeration missingPropNames = getMissingRequiredPropNames( jposEntry ); JposEntry prototype = getEntryPrototype(); while( missingPropNames.hasMoreElements() ) { - String propName = (String)missingPropNames.nextElement(); + String propName = missingPropNames.nextElement(); jposEntry.addProperty( propName, prototype.getPropertyValue( propName ) ); } } @@ -290,7 +293,7 @@ public static JposEntry getEntryPrototype() * @param classObject the Class object * @since 2.0.0 */ - public static String shortClassName( Class classObject ) + public static String shortClassName( @SuppressWarnings("rawtypes") Class classObject ) { return classObject.toString().substring( classObject.toString().lastIndexOf( "." ) + 1 ); } /** @@ -300,7 +303,7 @@ public static String shortClassName( Class classObject ) * @see jpos.config.JposEntryConst#PROP_TYPES * @since 2.0.0 */ - public static boolean validatePropValue( Object propValue, Class propType ) + public static boolean validatePropValue( Object propValue, @SuppressWarnings("rawtypes") Class propType ) { if( propValue == null || propType == null ) return false; @@ -310,12 +313,14 @@ public static boolean validatePropValue( Object propValue, Class propType ) } /** + * Predicate for checking whether the given Java type is a valid JavaPOS property type. + * @param propType a {@link Class} object as type * @return true if the propType object passed is a valid JposEntry property type * that is one of the JposEntryConst.PROP_TYPES * @see jpos.config.JposEntryConst#PROP_TYPES * @since 2.0.0 */ - public static boolean isValidPropType( Class propType ) + public static boolean isValidPropType( @SuppressWarnings("rawtypes") Class propType ) { if( propType == null ) return false; @@ -332,9 +337,9 @@ public static boolean isValidPropType( Class propType ) * @exception jpos.config.JposConfigException if this property type is not * valid */ - public static Object getDefaultValueForType( Class propType ) throws JposConfigException + public static Object getDefaultValueForType( @SuppressWarnings("rawtypes") Class propType ) throws JposConfigException { - if( isValidPropType( propType ) == false ) + if( !isValidPropType( propType ) ) throw new JposConfigException( "Invalid property type: " + propType ); Object propValue = ""; @@ -345,28 +350,28 @@ public static Object getDefaultValueForType( Class propType ) throws JposConfigE propValue = ""; else if( propType.equals( Boolean.class ) ) - propValue = new Boolean( false ); + propValue = Boolean.valueOf(false); else if( propType.equals( Character.class ) ) - propValue = new Character( 'a' ); + propValue = Character.valueOf('a'); else if( propType.equals( Double.class ) ) - propValue = new Double( 0 ); + propValue = Double.valueOf(0); else if( propType.equals( Float.class ) ) - propValue = new Float( 0 ); + propValue = Float.valueOf(0); else if( propType.equals( Byte.class ) ) - propValue = new Byte( (byte)0 ); + propValue = Byte.valueOf((byte) 0); else if( propType.equals( Integer.class ) ) - propValue = new Integer( 0 ); + propValue = Integer.valueOf(0) ; else if( propType.equals( Long.class ) ) - propValue = new Long( 0 ); + propValue = Long.valueOf(0) ; else if( propType.equals( Short.class ) ) - propValue = new Short( (short)0 ); + propValue = Short.valueOf((short) 0); } catch( Exception e ) { throw new JposConfigException( "Invalid property type" ); } @@ -384,9 +389,9 @@ public static Object getDefaultValueForType( Class propType ) throws JposConfigE * from the arguments passed * @since 2.0.0 */ - public static Object parsePropValue( String stringValue, Class propType ) throws JposConfigException + public static Object parsePropValue( String stringValue, @SuppressWarnings("rawtypes") Class propType ) throws JposConfigException { - if( isValidPropType( propType ) == false ) + if( !isValidPropType( propType ) ) throw new JposConfigException( "Invalid property type: " + propType ); Object propValue = ""; @@ -400,7 +405,7 @@ public static Object parsePropValue( String stringValue, Class propType ) throws propValue = Boolean.valueOf( stringValue ); else if( propType.equals( Character.class ) ) - propValue = new Character( stringValue.charAt( 0 ) ); + propValue = Character.valueOf( stringValue.charAt( 0 ) ); else if( propType.equals( Double.class ) ) propValue = Double.valueOf( stringValue ); @@ -433,7 +438,7 @@ public static Object parsePropValue( String stringValue, Class propType ) throws * @param typeString the type string name * @throws jpos.config.JposConfigException if the typeString is not a valid property type string */ - public static Class propTypeFromString( String typeString ) throws JposConfigException + public static Class propTypeFromString( String typeString ) throws JposConfigException { if( typeString == null ) throw new JposConfigException( "typeString cannot be null" ); @@ -441,9 +446,9 @@ public static Class propTypeFromString( String typeString ) throws JposConfigExc try { - Class typeClass = Class.forName( className ); + Class typeClass = Class.forName( className ); - if( isValidPropType( typeClass ) == false ) + if( !isValidPropType( typeClass ) ) throw new JposConfigException( "Invalid property type: " + typeString ); return typeClass; @@ -461,7 +466,7 @@ public static Class propTypeFromString( String typeString ) throws JposConfigExc // Class constants // - private static final List STANDARD_PROP_NAMES_LIST = new ArrayList(); + private static final List STANDARD_PROP_NAMES_LIST = new ArrayList<>(); //------------------------------------------------------------------------- // Static initializer @@ -475,7 +480,7 @@ public static Class propTypeFromString( String typeString ) throws JposConfigExc STANDARD_PROP_NAMES_LIST.add( JposEntryConst.DEVICE_BUS_PROP_NAME ); - for( int i = 0; i < JposEntryConst.RS232_PROPS.length; ++i ) - STANDARD_PROP_NAMES_LIST.add( JposEntryConst.RS232_PROPS[ i ] ); + for( int i = 0; i < RS232Const.RS232_PROPS.length; ++i ) + STANDARD_PROP_NAMES_LIST.add( RS232Const.RS232_PROPS[ i ] ); } } diff --git a/src/main/java/jpos/util/JposProperties.java b/src/main/java/jpos/util/JposProperties.java index 7a608d4..92ada81 100644 --- a/src/main/java/jpos/util/JposProperties.java +++ b/src/main/java/jpos/util/JposProperties.java @@ -65,7 +65,8 @@ public interface JposProperties extends JposPropertiesConst * @return an enumeration of properties names defined * @since 1.2 (NY 2K meeting) */ - public Enumeration getPropertyNames(); + @SuppressWarnings("rawtypes") + public Enumeration getPropertyNames(); /** * @return a List of property value parsed from the property value found @@ -73,6 +74,7 @@ public interface JposProperties extends JposPropertiesConst * @param propName the property name for which the values will be parsed from * @since 2.1.0 */ + @SuppressWarnings("rawtypes") public List getStringListProperty( String propName ); /** @@ -97,6 +99,7 @@ public interface JposProperties extends JposPropertiesConst * @see jpos.util.JposProperties.Prop * @since 1.3 (Washington DC 2001) */ + @SuppressWarnings("rawtypes") public Iterator getProps(); /** @@ -131,12 +134,15 @@ public interface MultiProperty public String getBasePropertyName(); /** @return an iterator of the property names for this multi-property */ + @SuppressWarnings("rawtypes") public Iterator getPropertyNames(); /** @return an iterator of the property names alphabetically sorted for this multi-property */ + @SuppressWarnings("rawtypes") public Iterator getSortedPropertyNames(); /** @return an iterator of the property values for this multi-property */ + @SuppressWarnings("rawtypes") public Iterator getPropertyValues(); /** diff --git a/src/main/java/jpos/util/JposPropertiesViewer.java b/src/main/java/jpos/util/JposPropertiesViewer.java index 54cb238..dcb6210 100644 --- a/src/main/java/jpos/util/JposPropertiesViewer.java +++ b/src/main/java/jpos/util/JposPropertiesViewer.java @@ -26,7 +26,7 @@ * @since 1.2 (NY 2K 99 meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class JposPropertiesViewer extends Object +public class JposPropertiesViewer { //------------------------------------------------------------------------- // Public class methods diff --git a/src/main/java/jpos/util/PopupHelper.java b/src/main/java/jpos/util/PopupHelper.java index 9a97572..943ae9a 100644 --- a/src/main/java/jpos/util/PopupHelper.java +++ b/src/main/java/jpos/util/PopupHelper.java @@ -32,7 +32,7 @@ */ public class PopupHelper { - static Dictionary dictionary = new Hashtable(); + static Dictionary dictionary = new Hashtable<>(); static MouseListener popupMouseListener = new java.awt.event.MouseAdapter() { @@ -44,7 +44,7 @@ void tryPopup( MouseEvent evt ) { Component mouseEvtComp = evt.getComponent(); - Object o[] = (Object [])dictionary.get( mouseEvtComp ); + Object[] o = dictionary.get( mouseEvtComp ); if ( o == null ) return; @@ -73,6 +73,8 @@ void tryPopup( MouseEvent evt ) { /** * registers a component to listen to mouse events for the popupMenu, does not use * the PopupListener to do extra setting up for the menu + * @param mouseEvtComp a {@link JComponent} object the event listener is registered at + * @param popupMenu a {@link JPopupMenu} object the event listener is registered for * @since 1.3 (SF 2K meeting) */ public static void setPopup( JComponent mouseEvtComp, JPopupMenu popupMenu ) { setPopup( mouseEvtComp, popupMenu, null ); } @@ -80,11 +82,14 @@ void tryPopup( MouseEvent evt ) { /** * registers a component to listen to mouse events for the popupMenu, uses * the PopupListener to do extra setting up for the menu when mouse is clicked + * @param mouseEvtComp a {@link JComponent} object the event listener is registered at + * @param popupMenu a {@link JPopupMenu} object the event listener is registered for + * @param popupListener the {@link PopupListener} object to be registered * @since 1.3 (SF 2K meeting) */ public static void setPopup( JComponent mouseEvtComp, JPopupMenu popupMenu, PopupListener popupListener ) { - Object o[] = { popupMenu, popupListener }; + Object[] o = { popupMenu, popupListener }; dictionary.put( mouseEvtComp, o ); diff --git a/src/main/java/jpos/util/Sorter.java b/src/main/java/jpos/util/Sorter.java index 3f4948e..5bd9d64 100644 --- a/src/main/java/jpos/util/Sorter.java +++ b/src/main/java/jpos/util/Sorter.java @@ -25,7 +25,7 @@ * @author E. Michael Maximilien (maxim@us.ibm.com) * @version 1.1.x (JDK 1.1.x) */ -public final class Sorter extends Object +public final class Sorter { //------------------------------------------------------------------------- // Ctor @@ -43,10 +43,11 @@ private Sorter() {} * NOTE:implements a simple one pass algorithm in O(n) time * @param comparables what to compare with */ - public static Comparable min( Vector comparables ) + public static Comparable min( @SuppressWarnings("rawtypes") Vector comparables ) { Comparable smallest = null; - Enumeration elements = comparables.elements(); + @SuppressWarnings("rawtypes") + Enumeration elements = comparables.elements(); while( elements.hasMoreElements() ) { @@ -69,10 +70,11 @@ public static Comparable min( Vector comparables ) * NOTE:implements a simple one pass algorithm in O(n) time * @param comparables what to compare with */ - public static Comparable max( Vector comparables ) + public static Comparable max( @SuppressWarnings("rawtypes") Vector comparables ) { Comparable greatest = null; - Enumeration elements = comparables.elements(); + @SuppressWarnings("rawtypes") + Enumeration elements = comparables.elements(); while( elements.hasMoreElements() ) { @@ -96,7 +98,8 @@ public static Comparable max( Vector comparables ) * Sorts n element in place in O(n^2) worst-case time * @param comparables a Vector with the initial Comparable objects */ - public static Vector insertionSort( Vector comparables ) + @SuppressWarnings("unchecked") + public static Vector insertionSort( @SuppressWarnings("rawtypes") Vector comparables ) { Comparable min = min( comparables ); @@ -123,7 +126,7 @@ public static Vector insertionSort( Vector comparables ) * Sorts n element in O(nlgn) worst-case time * @param comparables a Vector with the initial Comparable objects */ - public static Vector mergeSort( Vector comparables ) + public static Vector mergeSort( @SuppressWarnings("rawtypes") Vector comparables ) { // throw new RuntimeException( "Not yet implemented!" ); diff --git a/src/main/java/jpos/util/Tracer.java b/src/main/java/jpos/util/Tracer.java deleted file mode 100644 index f01c792..0000000 --- a/src/main/java/jpos/util/Tracer.java +++ /dev/null @@ -1,187 +0,0 @@ -package jpos.util; - -/////////////////////////////////////////////////////////////////////////////// -// -// This software is provided "AS IS". The JavaPOS working group (including -// each of the Corporate members, contributors and individuals) MAKES NO -// REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, -// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED -// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NON-INFRINGEMENT. The JavaPOS working group shall not be liable for -// any damages suffered as a result of using, modifying or distributing this -// software or its derivatives. Permission to use, copy, modify, and distribute -// the software and its documentation for any purpose is hereby granted. -// -// The JavaPOS Config/Loader (aka JCL) is now under the CPL license, which -// is an OSS Apache-like license. The complete license is located at: -// http://www.ibm.com/developerworks/library/os-cpl.html -// -/////////////////////////////////////////////////////////////////////////////// - -/** - * Tracing class to help in the debugging of the JCL and JavaPOS controls: - * This class is a Singleton (see GoF Design Pattern book) - * This class is superceded by the classes in the jpos.util.tracing package - * access the sole instance by doing: Tracer.getInstance() call - * @see jpos.util.tracing.Tracer - * @see jpos.util.tracing.TracerFactory - * @deprecated see the classes in the jpos.util.tracing package - * @since 1.2 (NY 2K meeting) - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ -public class Tracer extends Object -{ - //-------------------------------------------------------------------------- - // Ctor - // - - /** - * Make ctor private to avoid construction (this is a Singleton class) - * @since 1.2 (NY 2K meeting) - */ - private Tracer() {} - - //-------------------------------------------------------------------------- - // Public class methods - // - - /** - * @return the sole instance of this class (creating it if necessary) - * @since 1.2 (NY 2K meeting) - */ - public static Tracer getInstance() - { - if( instance == null ) - { - instance = new Tracer(); - - instance.init(); - } - - return instance; - } - - //-------------------------------------------------------------------------- - // Public methods - // - - /** - * Prints a string appended with a new line to the tracer output - * @param s the String to print - */ - public void println( String s ) { getTracerOutput().println( s ); } - - /** - * Prints a string appended without a new line to the tracer output - * @param s the String to print - */ - public void print( String s ) { getTracerOutput().print( s ); } - - /** - * Sets this tracer ON or OFF - * @param b the boolean parameter - * @since 1.2 (NY 2K meeting) - */ - public void setOn( boolean b ) { tracerOn = b; } - - /** - * @return true if the tracer is ON (i.e. enabled) - * @since 1.2 (NY 2K meeting) - */ - public boolean isOn() { return tracerOn; } - - //-------------------------------------------------------------------------- - // Private methods - // - - /** - * Intialize the current JCL instance using the DefaultProperties class - * @since 1.2 (NY 2K meeting) - */ - private void init() - { - JposProperties props = new DefaultProperties(); - props.loadJposProperties(); - - if( !props.isPropertyDefined( JposProperties.JPOS_TRACING_PROP_NAME ) ) - setOn( false ); - else - { - String tracingPropValue = - props.getPropertyString( JposProperties. - JPOS_TRACING_PROP_NAME ); - - if( tracingPropValue. - equalsIgnoreCase( JposProperties. - JPOS_TRACING_ON_PROP_VALUE ) || - tracingPropValue. - equalsIgnoreCase( JposProperties. - JPOS_TRACING_TRUE_PROP_VALUE ) ) - setOn( true ); - } - } - - /** - * @return the tracerOutput object for the Tracer - * @since 1.2 (NY 2K meeting) - */ - private TracerOutput getTracerOutput() - { - return ( isOn() ? onTracerOutput : offTracerOutput ); - } - - //-------------------------------------------------------------------------- - // Private instance variables - // - - private boolean tracerOn = false; - - private TracerOutput onTracerOutput = new DefaultTracerOutput(); - private TracerOutput offTracerOutput = - new TracerOutput() - { - public void println( String s ) {} - public void print( String s ) {} - }; - - //-------------------------------------------------------------------------- - // Private class variables - // - - private static Tracer instance = null; - - //-------------------------------------------------------------------------- - // Private static inner classes - // - - /** - * Inner class for a default TracerOutput. Just prints out info to - * System.err - * @since 1.2 (NY 2K meeting) - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ - static class DefaultTracerOutput extends Object implements TracerOutput - { - /** - * Default ctor - * @since 1.2 (NY 2K meeting) - */ - public DefaultTracerOutput() {} - - //---------------------------------------------------------------------- - // Public methods - // - - /** - * Prints a string appended with a new line to the tracer output - * @param s the String to print - */ - public void println( String s ) { System.err.println( s ); } - - /** - * Prints a string appended without a new line to the tracer output - * @param s the String to print - */ - public void print( String s ) { System.err.print( s ); } - } -} \ No newline at end of file diff --git a/src/main/java/jpos/util/XmlHelper.java b/src/main/java/jpos/util/XmlHelper.java index 2571099..350fec3 100644 --- a/src/main/java/jpos/util/XmlHelper.java +++ b/src/main/java/jpos/util/XmlHelper.java @@ -19,6 +19,7 @@ /////////////////////////////////////////////////////////////////////////////// import java.io.*; +import java.nio.file.Files; import java.util.*; import jpos.util.tracing.Tracer; @@ -29,7 +30,7 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class XmlHelper extends Object +public class XmlHelper { //------------------------------------------------------------------------- // Public methods @@ -76,7 +77,7 @@ public void checkAndCreateTempDtd() File dtdFile = new File( dtdJarFullFileName ); if( dtdFile.exists() ) - return; + return; if( !dtdPath.exists() ) { @@ -88,12 +89,12 @@ public void checkAndCreateTempDtd() dtdFilePath ); } - is = ClassLoader.getSystemClassLoader().getResourceAsStream( dtdJarFullFileName ); + is = getClass().getClassLoader().getResourceAsStream( dtdJarFullFileName ); tracer.println( "Got DTD InputStream from current ClassLoader" ); if( is != null ) - readAndCreateTempDtdFile( is ); + readAndCreateTempDtdFile( is ); } catch( IOException ioe ) { @@ -120,7 +121,7 @@ public void removeTempDtd() if( createdTempDTD ) { File dtdFile = new File( dtdJarFullFileName ); - dtdFile.delete(); + Files.delete(dtdFile.toPath()); if( createdTempDir ) removeDirs( dtdFilePath ); @@ -147,50 +148,39 @@ private void readAndCreateTempDtdFile( InputStream is ) throws IOException { File dtdFile = new File( dtdJarFullFileName ); - FileOutputStream fos = new FileOutputStream( dtdFile ); - OutputStreamWriter osw = new OutputStreamWriter( fos ); - - StringBuffer sb = new StringBuffer(); - - while( is.available() > 0 ) - { - byte[] buffer = new byte[ is.available() ]; - - is.read( buffer ); - - sb.append( new String( buffer ) ); - } - - osw.write( sb.toString().trim() ); - - createdTempDTD = true; - - try - { - if( osw != null ) osw.close(); - if( fos != null ) fos.close(); - } - catch( IOException ioe ) - { - tracer.println( "Error while closing streams: IOExeption.msg=" + - ioe.getMessage() ); + try ( OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream( dtdFile ) )) { + + StringBuilder sb = new StringBuilder(); + + while( is.available() > 0 ) + { + byte[] buffer = new byte[ is.available() ]; + + is.read( buffer ); + + sb.append( new String( buffer ) ); + } + + osw.write( sb.toString().trim() ); + + createdTempDTD = true; + + tracer.println( "Read and created temp " + dtdFilePath + dtdFileName ); } - - tracer.println( "Read and created temp " + dtdFilePath + dtdFileName ); } /** * @return a Vector of the different directories from a directory string * @param originalDirName the full directory name */ - private Vector getSubdirNames( String originalDirName ) + private List getSubdirNames( String originalDirName ) { String dirName = originalDirName. replace( "\\".charAt( 0 ), "/".charAt( 0 ) ); if( !dirName.endsWith( "/" ) ) dirName = dirName + "/"; - Vector dirs = new Vector(); + List dirs = new ArrayList<>(); String s = dirName; while( s.indexOf( "/" ) != -1 ) @@ -213,25 +203,24 @@ private Vector getSubdirNames( String originalDirName ) */ void removeDirs( String dirName ) throws IOException { - Vector subdirNames = getSubdirNames( dirName ); - - while( subdirNames.size() > 0 ) + List subdirNames = getSubdirNames( dirName ); + + while( !subdirNames.isEmpty() ) { - Vector v = (Vector)subdirNames.clone(); - String subdirName = ""; - for( int i = 0; i < subdirNames.size(); ++i ) - subdirName += (String)subdirNames.elementAt( i ) + - File.separator; + StringBuilder filePath = new StringBuilder(); + for (String subDirName : subdirNames) { + filePath.append(subDirName).append(File.separator); + } - File subdirFile = new File( subdirName ); + File subdirFile = new File( filePath.toString() ); if( subdirFile.list() != null && subdirFile.list().length == 0 ) - subdirFile.delete(); + Files.delete(subdirFile.toPath()); - if( subdirNames.size() > 0 ) - subdirNames.removeElementAt( subdirNames.size() - 1 ); + if( !subdirNames.isEmpty() ) + subdirNames.remove( subdirNames.size() - 1 ); } } diff --git a/src/main/java/jpos/util/tracing/Tracer.java b/src/main/java/jpos/util/tracing/Tracer.java index e2c16e4..2f8b890 100644 --- a/src/main/java/jpos/util/tracing/Tracer.java +++ b/src/main/java/jpos/util/tracing/Tracer.java @@ -41,7 +41,7 @@ * @author E. Michael Maximilien * @since 2.1.0 */ -public class Tracer extends Object +public class Tracer { //------------------------------------------------------------------------- // Ctor @@ -229,7 +229,7 @@ public void flush() {} * Inner class for a default TracerOutput. Just prints out info to System.err * @author E. Michael Maximilien */ - class DefaultTracerOutput extends Object implements TracerOutput + class DefaultTracerOutput implements TracerOutput { //--------------------------------------------------------------------- // Ctor(s) diff --git a/src/main/java/jpos/util/tracing/TracerFactory.java b/src/main/java/jpos/util/tracing/TracerFactory.java index 3d88a7f..bbf829a 100644 --- a/src/main/java/jpos/util/tracing/TracerFactory.java +++ b/src/main/java/jpos/util/tracing/TracerFactory.java @@ -42,7 +42,7 @@ * @author E. Michael Maximilien * @since 2.1.0 */ -public class TracerFactory extends Object +public class TracerFactory { //------------------------------------------------------------------------- // Ctor @@ -74,12 +74,14 @@ public static TracerFactory getInstance() /** * Sets the File that all subsequently created Tracer objects will print to * if these Tracer objects are turned on. Successfully calling this method - * overrides the current setting for TracerOutput to files + * overrides the current setting for TracerOutput to files. + * Note: This is not implemented yet! + * @param file the {@link File} the output should be written to * @throws java.io.IOException if a PrintStream could not be created from File */ public void setOutputFile( File file ) throws IOException { - // + // not implemented yet } /** @@ -107,16 +109,17 @@ public Tracer createGlobalTracer( boolean b ) public Tracer createTracer( String name ) { if( tracerMap.containsKey( name ) ) - return (Tracer)tracerMap.get( name ); + return tracerMap.get( name ); Tracer tracer = new Tracer( name ); if( namedTracerState.containsKey( name ) ) - tracer.setOn( ( (Boolean)namedTracerState.get( name ) ).booleanValue() ); + tracer.setOn( ( namedTracerState.get( name ) ).booleanValue() ); else tracer.setOn( false ); - if( turnOnAllNamedTracers ) tracer.setOn( true ); + if( turnOnAllNamedTracers ) + tracer.setOn( true ); tracerMap.put( name, tracer ); @@ -162,13 +165,14 @@ protected void finalize() */ private boolean isPropertyTrue( String propValue ) { - if( propValue == null ) return false; + if( propValue == null ) + return false; - if( propValue. - equalsIgnoreCase( JposProperties.TRACING_ON_PROP_VALUE ) || - propValue. - equalsIgnoreCase( JposProperties.TRACING_TRUE_PROP_VALUE ) ) + if( propValue.equalsIgnoreCase( JposProperties.TRACING_ON_PROP_VALUE ) || + propValue.equalsIgnoreCase( JposProperties.TRACING_TRUE_PROP_VALUE ) ) + { return true; + } return false; } @@ -182,7 +186,6 @@ private void init() initGlobalTracer( props ); initTurnedOnTracers( props ); initNamedTracers( props ); - initTracerOutput(); } /** @@ -223,14 +226,13 @@ private void initTurnedOnTracers( JposProperties props ) else if( props.isPropertyDefined( TURN_ON_NAMED_TRACERS_PROP_NAME ) ) { - List turnOnNamedTracersList = + @SuppressWarnings("unchecked") + List turnOnNamedTracersList = props.getStringListProperty( TURN_ON_NAMED_TRACERS_PROP_NAME ); - for( int i = 0; i < turnOnNamedTracersList.size(); ++i ) - { - String tracerName = turnOnNamedTracersList.get( i ).toString(); - namedTracerState.put( tracerName, Boolean.TRUE ); - } + for (String tracerName : turnOnNamedTracersList) { + namedTracerState.put( tracerName.trim(), Boolean.TRUE ); + } } } @@ -240,22 +242,20 @@ private void initTurnedOnTracers( JposProperties props ) */ private void initNamedTracers( JposProperties props ) { - Enumeration propNames = props.getPropertyNames(); + @SuppressWarnings("unchecked") + Enumeration propNames = props.getPropertyNames(); while( propNames.hasMoreElements() ) { - String propName = (String)propNames.nextElement(); + String propName = propNames.nextElement(); if( propName.startsWith( TRACER_PROP_NAME ) ) { - String name = propName. - substring( ( TRACER_PROP_NAME + "." ).length(), - propName.length() ); + String name = propName.substring( ( TRACER_PROP_NAME + "." ).length(), propName.length() ); if( props.isPropertyDefined( propName ) ) { - String propValue = (String)props. - getPropertyString( propName ); + String propValue = props.getPropertyString( propName ); if( propValue.equalsIgnoreCase( JposProperties. TRACING_ON_PROP_VALUE ) || @@ -269,22 +269,12 @@ private void initNamedTracers( JposProperties props ) } } - /** - * Initializes whether TracerOutput will be to a file or System.err and - * if to a file its location and name. By default TracerOutput is to - * System.err for all tracers that are turned on - */ - private void initTracerOutput() - { - // - } - //------------------------------------------------------------------------- // Private instance variables // - private HashMap tracerMap = new HashMap(); - private HashMap namedTracerState = new HashMap(); + private final HashMap tracerMap = new HashMap<>(); + private final HashMap namedTracerState = new HashMap<>(); private Tracer globalTracer = Tracer.getInstance(); diff --git a/src/main/java/jpos/util/tracing/Tracing.java b/src/main/java/jpos/util/tracing/Tracing.java index 96587b0..6e32f25 100644 --- a/src/main/java/jpos/util/tracing/Tracing.java +++ b/src/main/java/jpos/util/tracing/Tracing.java @@ -23,10 +23,9 @@ * This class uses the Tracer class for all its static methods implementations * its a convinient class to avoid having to cache the Tracer object or having * to do Tracer.getInstance() everytime you need to access the Tracer. - * @see jpos.util.Tracer#getInstance * @author E. Michael Maximilien */ -public class Tracing extends Object +public class Tracing { //------------------------------------------------------------------------- // Ctor diff --git a/src/test/java/jpos/JposTestCase.java b/src/test/java/jpos/JposTestCase.java index 59f59a3..3e163f9 100644 --- a/src/test/java/jpos/JposTestCase.java +++ b/src/test/java/jpos/JposTestCase.java @@ -313,7 +313,7 @@ public void sleep( long time ) "data" + File.separator ; public static final boolean CONSOLE_OUTPUT_ENABLED = false; - public static final String JPOS_UTIL_TRACING_VALUE = "OFF"; + public static final String JPOS_UTIL_TRACING_VALUE = "ON"; /** * This method is needed as on different OS the temp directory path may come without diff --git a/src/test/java/jpos/config/CompositeRegPopulatorTestCase.java b/src/test/java/jpos/config/CompositeRegPopulatorTestCase.java index ee3e949..c7194f0 100644 --- a/src/test/java/jpos/config/CompositeRegPopulatorTestCase.java +++ b/src/test/java/jpos/config/CompositeRegPopulatorTestCase.java @@ -125,7 +125,7 @@ private void createEntriesFiles() throws Exception private void createCompositePropFile() throws IOException { Properties jclProps = new Properties(); - jclProps.put( "jpos.util.tracing", JPOS_UTIL_TRACING_VALUE ); + jclProps.put( "jpos.util.tracing.TurnOnAllNamedTracers", JPOS_UTIL_TRACING_VALUE ); jclProps.put( "jpos.config.populator.class.0", "jpos.config.simple.SimpleRegPopulator" ); jclProps.put( "jpos.config.populator.class.1", "jpos.config.simple.xml.SimpleXmlRegPopulator" ); jclProps.put( "jpos.config.populator.class.2", "jpos.config.simple.SimpleRegPopulator" ); diff --git a/src/test/java/jpos/config/simple/SimpleRegPopulatorTestCase.java b/src/test/java/jpos/config/simple/SimpleRegPopulatorTestCase.java index b0bcf42..c545bbb 100644 --- a/src/test/java/jpos/config/simple/SimpleRegPopulatorTestCase.java +++ b/src/test/java/jpos/config/simple/SimpleRegPopulatorTestCase.java @@ -60,7 +60,7 @@ protected void setUp() throws IOException deleteFileIfExists( JCL_CFG_FILE_NAME ); createParentDirectoryForFileName(JCL_CFG_FILE_NAME); - ( new File( JCL_CFG_FILE_NAME ) ).createNewFile(); + assertTrue("file could not be created: " + JCL_CFG_FILE_NAME, ( new File( JCL_CFG_FILE_NAME ) ).createNewFile()); assertTrue( "Could not create the empty file: " + JCL_CFG_FILE_NAME, ( new File( JCL_CFG_FILE_NAME ) ).exists() ); @@ -116,7 +116,7 @@ public void testRegPopulatorGetClassName() public void testRegPopulatorSave1() throws Exception { Properties jclProps = new Properties(); - jclProps.put( "jpos.util.tracing", JPOS_UTIL_TRACING_VALUE ); + jclProps.put( "jpos.util.tracing.TurnOnAllNamedTracers", JPOS_UTIL_TRACING_VALUE ); jclProps.put( "jpos.config.regPopulatorClass", "jpos.config.simple.SimpleRegPopulator" ); jclProps.put( "jpos.config.populatorFile", JCL_CFG_FILE_NAME ); createPropFile( jclProps ); @@ -166,7 +166,7 @@ public void testRegPopulatorSave2() throws Exception public void testRegPopulatorLoad1() throws Exception { Properties jclProps = new Properties(); - jclProps.put( "jpos.util.tracing", JPOS_UTIL_TRACING_VALUE ); + jclProps.put( "jpos.util.tracing.TurnOnAllNamedTracers", JPOS_UTIL_TRACING_VALUE ); jclProps.put( "jpos.config.regPopulatorClass", "jpos.config.simple.SimpleRegPopulator" ); jclProps.put( "jpos.config.populatorFile", JCL_CFG_FILE_NAME ); createPropFile( jclProps ); diff --git a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java new file mode 100644 index 0000000..da7b259 --- /dev/null +++ b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java @@ -0,0 +1,305 @@ +package jpos.config.simple.xml; + +/////////////////////////////////////////////////////////////////////////////// +// +// This software is provided "AS IS". The JavaPOS working group (including +// each of the Corporate members, contributors and individuals) MAKES NO +// REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, +// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED +// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NON-INFRINGEMENT. The JavaPOS working group shall not be liable for +// any damages suffered as a result of using, modifying or distributing this +// software or its derivatives. Permission to use, copy, modify, and distribute +// the software and its documentation for any purpose is hereby granted. +// +/////////////////////////////////////////////////////////////////////////////// + +import java.util.*; +import java.io.*; + +import jpos.config.*; +import jpos.config.simple.*; +import jpos.test.*; + +/** + * A JUnit TestCase for the Loading/saving XML entries + * @author E. Michael Maximilien (maxim@us.ibm.com) + */ +public class JavaxRegPopulatorTestCase extends AbstractRegPopulatorTestCase +{ + //------------------------------------------------------------------------- + // Ctor(s) + // + + public JavaxRegPopulatorTestCase( String name ) { super( name ); } + + //------------------------------------------------------------------------- + // Protected overridden methods + // + + protected void setUp() + { + javaxRegPopulator = new JavaxRegPopulator(); + + try + { + File file = new File( JCL_JUNIT_XML_FILE_NAME ); + file.delete(); + } + catch( SecurityException se ) + { + println( "Could not delete XML test file: " + JCL_JUNIT_XML_FILE_NAME ); + println( " Exception message = " + se.getMessage() ); + } + + createDirectory(TEST_DATA_PATH); + addToClasspath( TEST_DATA_PATH ); + } + + protected void tearDown() + { + javaxRegPopulator = null; + } + + //------------------------------------------------------------------------- + // Private methods + // + + private JposEntry createJposEntry( String logicalName, String factoryClass, + String serviceClass, String vendorName, + String vendorURL, String deviceCategory, + String jposVersion, String productName, + String productDescription, String productURL ) + { + JposEntry jposEntry = new SimpleEntry(); + + jposEntry.addProperty( JposEntry.LOGICAL_NAME_PROP_NAME, logicalName ); + jposEntry.addProperty( JposEntry.SI_FACTORY_CLASS_PROP_NAME, factoryClass ); + jposEntry.addProperty( JposEntry.SERVICE_CLASS_PROP_NAME, serviceClass ); + jposEntry.addProperty( JposEntry.VENDOR_NAME_PROP_NAME, vendorName ); + jposEntry.addProperty( JposEntry.VENDOR_URL_PROP_NAME, vendorURL ); + jposEntry.addProperty( JposEntry.DEVICE_CATEGORY_PROP_NAME, deviceCategory ); + jposEntry.addProperty( JposEntry.JPOS_VERSION_PROP_NAME, jposVersion ); + jposEntry.addProperty( JposEntry.PRODUCT_NAME_PROP_NAME, productName ); + jposEntry.addProperty( JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, productDescription ); + jposEntry.addProperty( JposEntry.PRODUCT_URL_PROP_NAME, productURL ); + + return jposEntry; + } + + //------------------------------------------------------------------------- + // Public test methods + // + + /** + * Test the loading/saving of XML entries using the XercesRegPopulator + */ + public void testJavaxPopulatorReloadToTheSameInstance() + { + //Save and load an empty set of registry entries + List v1 = new ArrayList<>(); + + try + { + javaxRegPopulator.save( Collections.enumeration(v1), JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + + assertNull(javaxRegPopulator.getLastLoadException()); + + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); + + assertTrue( "Expected an empty set of entries...", JUnitUtility.isIdentical( entries, Collections.enumeration(v1) ) ); + assertTrue( "Expected an empty set of entries...", JUnitUtility.isEquals( entries, Collections.enumeration(v1) ) ); + + //Add some entries and save and load + JposEntry entry1 = createJposEntry( "entry1", "com.xyz.jpos.XyzJposServiceInstanceFactory", + "com.xyz.jpos.LineDisplayService", "Xyz, Corp.", + "http://www.javapos.com", "LineDisplay", "1.4a", + "Virtual LineDisplay JavaPOS Service", + "Example virtual LineDisplay JavaPOS Service from virtual Xyz Corporation", + "http://www.javapos.com" ); + + JposEntry entry2 = createJposEntry( "entry2", "com.xyz.jpos.XyzJposServiceInstanceFactory", + "com.xyz.jpos.LineDisplayService", "Xyz, Corp.", + "http://www.javapos.com", "LineDisplay", "1.4a", + "Virtual LineDisplay JavaPOS Service", + "Example virtual LineDisplay JavaPOS Service from virtual Xyz Corporation", + "http://www.javapos.com" ); + + v1.clear(); + v1.add( entry1 ); + v1.add( entry2 ); + + javaxRegPopulator.save( Collections.enumeration(v1), JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + + assertNull(javaxRegPopulator.getLastLoadException()); + + @SuppressWarnings("unchecked") + Enumeration entries2 = javaxRegPopulator.getEntries(); + + assertTrue( "Expected 2 entries...", JUnitUtility.isEquals( entries2, Collections.enumeration(v1) ) ); + + //Remove entries save and load reg + v1.remove( entry1 ); + + javaxRegPopulator.save( Collections.enumeration(v1), JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + + assertNull(javaxRegPopulator.getLastLoadException()); + + @SuppressWarnings("unchecked") + Enumeration entries3 = javaxRegPopulator.getEntries(); + + assertTrue( "Expected 1 entries...", JUnitUtility.isEquals( entries3, Collections.enumeration(v1) ) ); + } + catch( Exception e ) + { + assertTrue( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage(), true ); + } + } + + /** + * Test the loading/saving of XML entries using the XercesRegPopulator + */ + public void testJavaxPopulator() + { + List v1 = new ArrayList<>(); + + for( int i = 0; i < 100; i++ ) + { + + JposEntry entry = createJposEntry( "entry" + i, "com.xyz.jpos.XyzJposServiceInstanceFactory", + "com.xyz.jpos.LineDisplayService", "Xyz, Corp.", + "http://www.javapos.com", "LineDisplay", "1.4a", + "Virtual LineDisplay JavaPOS Service", + "Example virtual LineDisplay JavaPOS Service from virtual Xyz Corporation", + "http://www.javapos.com" ); + v1.add( entry ); + } + + try + { + javaxRegPopulator.save( Collections.enumeration(v1), JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + + assertNull(javaxRegPopulator.getLastLoadException()); + + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); + + assertTrue( "Expected 100 entries...", JUnitUtility.isEquals( entries, Collections.enumeration(v1) ) ); + } + catch( Exception e ) + { + fail( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage() ); + } + } + + public void testGetName() + { + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + + assertTrue( javaxRegPopulator.getName().startsWith("JAVAX XML Entries Populator") ); + } + + public void testLoadDefect6562() + { + try + { + assertTrue( "Expected file: " + DEFECT_6562_XML_FILE + " to exist", + ( new File( DEFECT_6562_XML_FILE ) ).exists() ); + + javaxRegPopulator.load( DEFECT_6562_XML_FILE ); + + assertNull(javaxRegPopulator.getLastLoadException()); + + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); + + JposEntry defect6562Entry = (JposEntry)entries.nextElement(); + + assertNotNull( "defect6562Entry == null", defect6562Entry ); + assertEquals( "defect6562Entry.logicalName != defect6562", + "defect6562", defect6562Entry.getLogicalName() ); + + } + catch( Exception e ) { + fail( "Unexpected exception.message = " + e.getMessage() ); + } + } + + public void testLoadwithPropType() + { + try + { + assertTrue( "Expected file: " + JCL_JUNIT_TEST_PROP_TYPE_XML_FILE + " to exist", + ( new File( JCL_JUNIT_TEST_PROP_TYPE_XML_FILE ) ).exists() ); + + javaxRegPopulator.load( JCL_JUNIT_TEST_PROP_TYPE_XML_FILE ); + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); + + JposEntry testPropTypeEntry = (JposEntry)entries.nextElement(); + + assertNotNull( "testPropTypeEntry == null", testPropTypeEntry ); + assertEquals( "testPropTypeEntry.logicalName != testPropType", + "testPropType", testPropTypeEntry.getLogicalName() ); + + assertEquals( "testPropTypeEntry.getProp( \"stringProp\" ).getType() != String.class", + String.class, testPropTypeEntry.getProp( "stringProp" ).getType() ); + + assertEquals( "testPropTypeEntry.getProp( \"booleanProp\" ).getType() != Boolean.class", + Boolean.class, testPropTypeEntry.getProp( "booleanProp" ).getType() ); + + assertEquals( "testPropTypeEntry.getProp( \"byteProp\" ).getType() != Byte.class", + Byte.class, testPropTypeEntry.getProp( "byteProp" ).getType() ); + + assertEquals( "testPropTypeEntry.getProp( \"characterProp\" ).getType() != Character.class", + Character.class, testPropTypeEntry.getProp( "characterProp" ).getType() ); + + assertEquals( "testPropTypeEntry.getProp( \"doubleProp\" ).getType() != Double.class", + Double.class, testPropTypeEntry.getProp( "doubleProp" ).getType() ); + + assertEquals( "testPropTypeEntry.getProp( \"floatProp\" ).getType() != Float.class", + Float.class, testPropTypeEntry.getProp( "floatProp" ).getType() ); + + assertEquals( "testPropTypeEntry.getProp( \"integerProp\" ).getType() != Integer.class", + Integer.class, testPropTypeEntry.getProp( "integerProp" ).getType() ); + + assertEquals( "testPropTypeEntry.getProp( \"longProp\" ).getType() != Long.class", + Long.class, testPropTypeEntry.getProp( "longProp" ).getType() ); + + assertEquals( "testPropTypeEntry.getProp( \"shortProp\" ).getType() != Short.class", + Short.class, testPropTypeEntry.getProp( "shortProp" ).getType() ); + } + catch( Exception e ) { fail( "Unexpected exception.message = " + e.getMessage() ); } + } + + public void testSaveWithPropType() + { + emptyTest(); + } + + public void testGetLastLoadException() + { + emptyTest(); + } + + //------------------------------------------------------------------------- + // Instance variables + // + + private JavaxRegPopulator javaxRegPopulator = null; + + //------------------------------------------------------------------------- + // Instance variables + // + + public static final String JUNIT_CORP_STRING = "JUnit Corp."; + + public static final String JCL_JUNIT_XML_FILE_NAME = loadResourceAsTemporaryFile("jcl-junit.xml"); + public static final String DEFECT_6562_XML_FILE = loadResourceAsTemporaryFile("defect6562.xml"); + public static final String JCL_JUNIT_TEST_PROP_TYPE_XML_FILE = loadResourceAsTemporaryFile("jcl_junit_test_prop_type.xml"); +} \ No newline at end of file diff --git a/src/test/java/jpos/config/simple/xml/Xerces2RegPopulatorTestCase.java b/src/test/java/jpos/config/simple/xml/Xerces2RegPopulatorTestCase.java deleted file mode 100644 index 2b4f6a3..0000000 --- a/src/test/java/jpos/config/simple/xml/Xerces2RegPopulatorTestCase.java +++ /dev/null @@ -1,183 +0,0 @@ -package jpos.config.simple.xml; - -/////////////////////////////////////////////////////////////////////////////// -// -// This software is provided "AS IS". The JavaPOS working group (including -// each of the Corporate members, contributors and individuals) MAKES NO -// REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, -// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED -// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NON-INFRINGEMENT. The JavaPOS working group shall not be liable for -// any damages suffered as a result of using, modifying or distributing this -// software or its derivatives. Permission to use, copy, modify, and distribute -// the software and its documentation for any purpose is hereby granted. -// -/////////////////////////////////////////////////////////////////////////////// - -import java.util.*; - -import jpos.config.*; -import jpos.config.simple.*; -import jpos.util.tracing.Tracer; -import jpos.util.tracing.TracerFactory; - - -/** - * A JUnit TestCase for the Loading/saving XML entries testing the - * Xerces2RegPopulator class - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ -public class Xerces2RegPopulatorTestCase extends AbstractRegPopulatorTestCase -{ - //------------------------------------------------------------------------- - // Ctor(s) - // - - public Xerces2RegPopulatorTestCase( String name ) { super( name ); } - - //------------------------------------------------------------------------- - // Protected overridden methods - // - - protected void setUp() - { - pop = new Xerces2RegPopulator(); - createEntry0(); - - createDirectory(TEST_DATA_PATH); - addToClasspath( TEST_DATA_PATH ); - } - - protected void tearDown() - { - pop = null; - entry0 = null; - } - - //------------------------------------------------------------------------- - // Private methods - // - - private JposEntry createEntry0() - { - entry0 = - createJposEntry( "entry0", - "com.xyz.jpos.XyzJposServiceInstanceFactory", - "com.xyz.jpos.LineDisplayService", - "Xyz, Corp.", "http://www.javapos.com", - "LineDisplay", "1.5", - "Example virtual LineDisplay JavaPOS Service from " + - "virtual Xyz Corporation", - "Virtual LineDisplay JavaPOS Service", - "http://www.javapos.com" ); - - entry0.addProperty( "deviceBus", "Unknown" ); - entry0.addProperty( "vendor.prop.name2", "vendor.prop.value2" ); - entry0.addProperty( "vendor.prop.name1", "vendor.prop.value1" ); - entry0.addProperty( "vendor.prop.name0", "vendor.prop.value0" ); - - return entry0; - } - - private JposEntry - createJposEntry( String logicalName, String factoryClass, - String serviceClass, String vendorName, - String vendorURL, String deviceCategory, - String jposVersion, String productName, - String productDescription, String productURL ) - { - JposEntry jposEntry = new SimpleEntry(); - - jposEntry.addProperty( JposEntry.LOGICAL_NAME_PROP_NAME, logicalName ); - jposEntry.addProperty( JposEntry.SI_FACTORY_CLASS_PROP_NAME, - factoryClass ); - jposEntry.addProperty( JposEntry.SERVICE_CLASS_PROP_NAME, serviceClass ); - jposEntry.addProperty( JposEntry.VENDOR_NAME_PROP_NAME, vendorName ); - jposEntry.addProperty( JposEntry.VENDOR_URL_PROP_NAME, vendorURL ); - jposEntry.addProperty( JposEntry.DEVICE_CATEGORY_PROP_NAME, - deviceCategory ); - jposEntry.addProperty( JposEntry.JPOS_VERSION_PROP_NAME, jposVersion ); - jposEntry.addProperty( JposEntry.PRODUCT_NAME_PROP_NAME, productName ); - jposEntry.addProperty( JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, - productDescription ); - jposEntry.addProperty( JposEntry.PRODUCT_URL_PROP_NAME, productURL ); - - return jposEntry; - } - - private Enumeration searchEntriesForVendorName( Enumeration entries, - String vendorName ) - { - Vector v = new Vector(); - - while( entries.hasMoreElements() ) - { - JposEntry jposEntry = (JposEntry)entries.nextElement(); - - if( jposEntry.hasPropertyWithName( JposEntry.VENDOR_NAME_PROP_NAME ) ) - if( jposEntry.getPropertyValue( JposEntry.VENDOR_NAME_PROP_NAME ). - toString().equals( "JUnit Corp." ) ) - v.addElement( jposEntry ); - } - - return v.elements(); - } - - //------------------------------------------------------------------------- - // Public testXyz() methods - // - - public void testGetClassName() - { - assertTrue( "Incorrect class name returned", - pop.getClassName(). - equals( Xerces2RegPopulator.class.getName() ) ); - } - - public void testGetName() - { - assertTrue( "Incorrect class name returned", - pop.getName(). - equals( Xerces2RegPopulator. - XERCES2_REG_POPULATOR_NAME_STRING ) ); - - } - - public void testLoad1() - { - pop.load( JCL_JUNIT_XML_FILE_NAME ); - } - - public void testLoad2() - { - // - } - - public void testSave1() - { - // - } - - public void testSave2() - { - // - } - - //------------------------------------------------------------------------- - // Instance variables - // - - private JposEntry entry0 = null; - - private Xerces2RegPopulator pop = null; - - private Tracer tracer = TracerFactory.getInstance(). - createTracer( "Xerces2RegPopulatorTestCase", - true ); - - //------------------------------------------------------------------------- - // Class constants - // - - public static final String JCL_JUNIT_XML_FILE_NAME = loadResourceAsTemporaryFile("jcl-junit-schema.xml"); -} \ No newline at end of file diff --git a/src/test/java/jpos/config/simple/xml/XercesRegPopulatorTestCase.java b/src/test/java/jpos/config/simple/xml/XercesRegPopulatorTestCase.java deleted file mode 100644 index 28b0f73..0000000 --- a/src/test/java/jpos/config/simple/xml/XercesRegPopulatorTestCase.java +++ /dev/null @@ -1,315 +0,0 @@ -package jpos.config.simple.xml; - -/////////////////////////////////////////////////////////////////////////////// -// -// This software is provided "AS IS". The JavaPOS working group (including -// each of the Corporate members, contributors and individuals) MAKES NO -// REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, -// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED -// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NON-INFRINGEMENT. The JavaPOS working group shall not be liable for -// any damages suffered as a result of using, modifying or distributing this -// software or its derivatives. Permission to use, copy, modify, and distribute -// the software and its documentation for any purpose is hereby granted. -// -/////////////////////////////////////////////////////////////////////////////// - -import java.util.*; -import java.io.*; - -import jpos.config.*; -import jpos.config.simple.*; -import jpos.test.*; - -/** - * A JUnit TestCase for the Loading/saving XML entries - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ -public class XercesRegPopulatorTestCase extends AbstractRegPopulatorTestCase -{ - //------------------------------------------------------------------------- - // Ctor(s) - // - - public XercesRegPopulatorTestCase( String name ) { super( name ); } - - //------------------------------------------------------------------------- - // Protected overridden methods - // - - protected void setUp() - { - xercesRegPopulator = new XercesRegPopulator(); - - try - { - File file = new File( JCL_JUNIT_XML_FILE_NAME ); - file.delete(); - } - catch( SecurityException se ) - { - println( "Could not delete XML test file: " + JCL_JUNIT_XML_FILE_NAME ); - println( " Exception message = " + se.getMessage() ); - } - - createDirectory(TEST_DATA_PATH); - addToClasspath( TEST_DATA_PATH ); - } - - protected void tearDown() - { - xercesRegPopulator = null; - } - - //------------------------------------------------------------------------- - // Private methods - // - - private JposEntry createJposEntry( String logicalName, String factoryClass, - String serviceClass, String vendorName, - String vendorURL, String deviceCategory, - String jposVersion, String productName, - String productDescription, String productURL ) - { - JposEntry jposEntry = new SimpleEntry(); - - jposEntry.addProperty( JposEntry.LOGICAL_NAME_PROP_NAME, logicalName ); - jposEntry.addProperty( JposEntry.SI_FACTORY_CLASS_PROP_NAME, factoryClass ); - jposEntry.addProperty( JposEntry.SERVICE_CLASS_PROP_NAME, serviceClass ); - jposEntry.addProperty( JposEntry.VENDOR_NAME_PROP_NAME, vendorName ); - jposEntry.addProperty( JposEntry.VENDOR_URL_PROP_NAME, vendorURL ); - jposEntry.addProperty( JposEntry.DEVICE_CATEGORY_PROP_NAME, deviceCategory ); - jposEntry.addProperty( JposEntry.JPOS_VERSION_PROP_NAME, jposVersion ); - jposEntry.addProperty( JposEntry.PRODUCT_NAME_PROP_NAME, productName ); - jposEntry.addProperty( JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, productDescription ); - jposEntry.addProperty( JposEntry.PRODUCT_URL_PROP_NAME, productURL ); - - return jposEntry; - } - - private Enumeration searchEntriesForVendorName( Enumeration entries, String vendorName ) - { - Vector v = new Vector(); - - while( entries.hasMoreElements() ) - { - JposEntry jposEntry = (JposEntry)entries.nextElement(); - - if( jposEntry.hasPropertyWithName( JposEntry.VENDOR_NAME_PROP_NAME ) ) - if( jposEntry.getPropertyValue( JposEntry.VENDOR_NAME_PROP_NAME ). - toString().equals( JUNIT_CORP_STRING ) ) - v.addElement( jposEntry ); - } - - return v.elements(); - } - - //------------------------------------------------------------------------- - // Public testXyz() methods - // - - /** - * Test the loading/saving of XML entries using the XercesRegPopulator - */ - public void testXercesPopulator1() - { - //Save and load an empty set of registry entries - Vector v1 = new Vector(); - - try - { - xercesRegPopulator.save( v1.elements(), JCL_JUNIT_XML_FILE_NAME ); - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - - Enumeration entries = xercesRegPopulator.getEntries(); - - assertTrue( "Expected an empty set of entries...", JUnitUtility.isIdentical( entries, v1.elements() ) ); - assertTrue( "Expected an empty set of entries...", JUnitUtility.isEquals( entries, v1.elements() ) ); - } - catch( Exception e ) - { fail( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage() ); } - - //Add some entries and save and load - JposEntry entry1 = createJposEntry( "entry1", "com.xyz.jpos.XyzJposServiceInstanceFactory", - "com.xyz.jpos.LineDisplayService", "Xyz, Corp.", - "http://www.javapos.com", "LineDisplay", "1.4a", - "Virtual LineDisplay JavaPOS Service", - "Example virtual LineDisplay JavaPOS Service from virtual Xyz Corporation", - "http://www.javapos.com" ); - - JposEntry entry2 = createJposEntry( "entry2", "com.xyz.jpos.XyzJposServiceInstanceFactory", - "com.xyz.jpos.LineDisplayService", "Xyz, Corp.", - "http://www.javapos.com", "LineDisplay", "1.4a", - "Virtual LineDisplay JavaPOS Service", - "Example virtual LineDisplay JavaPOS Service from virtual Xyz Corporation", - "http://www.javapos.com" ); - - try - { - v1.clear(); - v1.addElement( entry1 ); - v1.addElement( entry2 ); - - xercesRegPopulator.save( v1.elements(), JCL_JUNIT_XML_FILE_NAME ); - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - - Enumeration entries = xercesRegPopulator.getEntries(); - - assertTrue( "Expected 2 entries...", JUnitUtility.isEquals( entries, v1.elements() ) ); - } - catch( Exception e ) - { assertTrue( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage(), true ); } - - //Remove entries save and load reg - v1.remove( entry1 ); - - - try - { - xercesRegPopulator.save( v1.elements(), JCL_JUNIT_XML_FILE_NAME ); - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - - Enumeration entries = xercesRegPopulator.getEntries(); - - assertTrue( "Expected 1 entries...", JUnitUtility.isEquals( entries, v1.elements() ) ); - } - catch( Exception e ) - { assertTrue( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage(), true ); } - } - - /** - * Test the loading/saving of XML entries using the XercesRegPopulator - */ - public void testXercesPopulator2() - { - Vector v1 = new Vector(); - - for( int i = 0; i < 100; i++ ) - { - - JposEntry entry = createJposEntry( "entry" + i, "com.xyz.jpos.XyzJposServiceInstanceFactory", - "com.xyz.jpos.LineDisplayService", "Xyz, Corp.", - "http://www.javapos.com", "LineDisplay", "1.4a", - "Virtual LineDisplay JavaPOS Service", - "Example virtual LineDisplay JavaPOS Service from virtual Xyz Corporation", - "http://www.javapos.com" ); - v1.addElement( entry ); - } - - try - { - xercesRegPopulator.save( v1.elements(), JCL_JUNIT_XML_FILE_NAME ); - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - - Enumeration entries = xercesRegPopulator.getEntries(); - - assertTrue( "Expected 100 entries...", JUnitUtility.isEquals( entries, v1.elements() ) ); - } - catch( Exception e ) - { fail( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage() ); } - } - - public void testGetName() - { - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - - assertEquals( XercesRegPopulator.XERCES_REG_POPULATOR_NAME_STRING, - xercesRegPopulator.getName() ); - } - - public void testLoad1() - { - try - { - assertTrue( "Expected file: " + DEFECT_6562_XML_FILE + " to exist", - ( new File( DEFECT_6562_XML_FILE ) ).exists() ); - - xercesRegPopulator.load( DEFECT_6562_XML_FILE ); - Enumeration entries = xercesRegPopulator.getEntries(); - - JposEntry defect6562Entry = (JposEntry)entries.nextElement(); - - assertTrue( "defect6562Entry == null", defect6562Entry != null ); - assertTrue( "defect6562Entry.logicalName != defect6562", - defect6562Entry.getLogicalName().equals( "defect6562" ) ); - - } - catch( Exception e ) { fail( "Unexpected exception.message = " + e.getMessage() ); } - } - - public void testLoadwithPropType() - { - try - { - assertTrue( "Expected file: " + JCL_JUNIT_TEST_PROP_TYPE_XML_FILE + " to exist", - ( new File( JCL_JUNIT_TEST_PROP_TYPE_XML_FILE ) ).exists() ); - - xercesRegPopulator.load( JCL_JUNIT_TEST_PROP_TYPE_XML_FILE ); - Enumeration entries = xercesRegPopulator.getEntries(); - - JposEntry testPropTypeEntry = (JposEntry)entries.nextElement(); - - // - //System.out.println( testPropTypeEntry ); - // - - assertTrue( "testPropTypeEntry == null", testPropTypeEntry != null ); - assertTrue( "testPropTypeEntry.logicalName != testPropType", - testPropTypeEntry.getLogicalName().equals( "testPropType" ) ); - - assertTrue( "testPropTypeEntry.getProp( \"stringProp\" ).getType() != String.class", - testPropTypeEntry.getProp( "stringProp" ).getType().equals( String.class ) ); - - assertTrue( "testPropTypeEntry.getProp( \"booleanProp\" ).getType() != Boolean.class", - testPropTypeEntry.getProp( "booleanProp" ).getType().equals( Boolean.class ) ); - - assertTrue( "testPropTypeEntry.getProp( \"byteProp\" ).getType() != Byte.class", - testPropTypeEntry.getProp( "byteProp" ).getType().equals( Byte.class ) ); - - assertTrue( "testPropTypeEntry.getProp( \"characterProp\" ).getType() != Character.class", - testPropTypeEntry.getProp( "characterProp" ).getType().equals( Character.class ) ); - - assertTrue( "testPropTypeEntry.getProp( \"doubleProp\" ).getType() != Double.class", - testPropTypeEntry.getProp( "doubleProp" ).getType().equals( Double.class ) ); - - assertTrue( "testPropTypeEntry.getProp( \"floatProp\" ).getType() != Float.class", - testPropTypeEntry.getProp( "floatProp" ).getType().equals( Float.class ) ); - - assertTrue( "testPropTypeEntry.getProp( \"integerProp\" ).getType() != Integer.class", - testPropTypeEntry.getProp( "integerProp" ).getType().equals( Integer.class ) ); - - assertTrue( "testPropTypeEntry.getProp( \"longProp\" ).getType() != Long.class", - testPropTypeEntry.getProp( "longProp" ).getType().equals( Long.class ) ); - - assertTrue( "testPropTypeEntry.getProp( \"shortProp\" ).getType() != Short.class", - testPropTypeEntry.getProp( "shortProp" ).getType().equals( Short.class ) ); - } - catch( Exception e ) { fail( "Unexpected exception.message = " + e.getMessage() ); } - } - - public void testSaveWithPropType() - { - emptyTest(); - } - - public void testGetLastLoadException() - { - emptyTest(); - } - - //------------------------------------------------------------------------- - // Instance variables - // - - private XercesRegPopulator xercesRegPopulator = null; - - //------------------------------------------------------------------------- - // Instance variables - // - - public static final String JUNIT_CORP_STRING = "JUnit Corp."; - - public static final String JCL_JUNIT_XML_FILE_NAME = loadResourceAsTemporaryFile("jcl-junit.xml"); - public static final String DEFECT_6562_XML_FILE = loadResourceAsTemporaryFile("defect6562.xml"); - public static final String JCL_JUNIT_TEST_PROP_TYPE_XML_FILE = loadResourceAsTemporaryFile("jcl_junit_test_prop_type.xml"); -} \ No newline at end of file diff --git a/src/test/java/jpos/loader/AbstractTestCase.java b/src/test/java/jpos/loader/AbstractTestCase.java index bf624c1..9bd4cb4 100644 --- a/src/test/java/jpos/loader/AbstractTestCase.java +++ b/src/test/java/jpos/loader/AbstractTestCase.java @@ -127,7 +127,7 @@ private void createPropertiesMap( Properties prop ) private void createSimplePropFile() throws Exception { Properties jclProps = new Properties(); - jclProps.put( "jpos.util.tracing", JPOS_UTIL_TRACING_VALUE ); + jclProps.put( "jpos.util.tracing.TurnOnAllNamedTracers", JPOS_UTIL_TRACING_VALUE ); jclProps.put( "jpos.loader.serviceManagerClass", MANAGER_CLASS ); jclProps.put( "jpos.config.regPopulatorClass", POPULATOR_CLASS ); jclProps.put( "jpos.config.populatorFile", POPULATOR_FILE ); @@ -152,7 +152,7 @@ private void deleteSimplePropFile() throws IOException private void createMultiPropFile() throws Exception { Properties jclProps = new Properties(); - jclProps.put( "jpos.util.tracing", JPOS_UTIL_TRACING_VALUE ); + jclProps.put( "jpos.util.tracing.TurnOnAllNamedTracers", JPOS_UTIL_TRACING_VALUE ); jclProps.put( "jpos.loader.serviceManagerClass", MANAGER_CLASS ); int startIndex = 0; diff --git a/src/test/java/jpos/profile/XercesProfileFactoryTestCase.java b/src/test/java/jpos/profile/DefaultProfileFactoryTestCase.java similarity index 81% rename from src/test/java/jpos/profile/XercesProfileFactoryTestCase.java rename to src/test/java/jpos/profile/DefaultProfileFactoryTestCase.java index bc70065..743b1d9 100644 --- a/src/test/java/jpos/profile/XercesProfileFactoryTestCase.java +++ b/src/test/java/jpos/profile/DefaultProfileFactoryTestCase.java @@ -25,13 +25,13 @@ * @author E. Michael Maximilien (maxim@us.ibm.com) * @since 1.3 (SF 2K meeting) */ -public class XercesProfileFactoryTestCase extends JposTestCase +public class DefaultProfileFactoryTestCase extends JposTestCase { //------------------------------------------------------------------------- // Ctor(s) // - public XercesProfileFactoryTestCase( String name ) { super( name ); } + public DefaultProfileFactoryTestCase( String name ) { super( name ); } //------------------------------------------------------------------------- // Protected overridden methods @@ -39,14 +39,14 @@ public class XercesProfileFactoryTestCase extends JposTestCase protected void setUp() { - xProfFactory = new XercesProfileFactory(); + profFactory = new DefaultProfileFactory(); profileFileName = PROFILE_FILE_NAME; schemaProfileFileName = SCHEMA_PROFILE_FILE_NAME; } protected void tearDown() { - xProfFactory = null; + profFactory = null; profileFileName = ""; schemaProfileFileName = ""; } @@ -76,7 +76,7 @@ public void _testParseSchema() throws ProfileException, IOException assertTrue( "JCL JUnit Schema Profile file named = " + schemaProfileFileName + ", does not exist", profileFile.exists() ); - Document document = xProfFactory.parseSchema( schemaProfileFileName ); + Document document = profFactory.parseSchema( schemaProfileFileName ); assertTrue( "XercesProfileFactory.parseSchema returned a null object", document != null ); @@ -89,7 +89,7 @@ public void _testCreateProfile() throws ProfileException, IOException assertTrue( "JCL JUnit Profile file named = " + profileFileName + ", does not exist", profileFile.exists() ); - Profile profile = xProfFactory.createProfile( profileFileName ); + Profile profile = profFactory.createProfile( profileFileName ); assertTrue( "XercesProfileFactory.createProfile returned a null object", profile != null ); @@ -109,34 +109,11 @@ public void _testCreateProfile() throws ProfileException, IOException // Private methods // - private File copyFile( File file, File newDir ) throws IOException - { - BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) ); - - String newFileName = newDir.getAbsolutePath() + File.separator + file.getName(); - - BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( newFileName ) ); - - while( bis.available() > 0 ) - { - int byteArraySize = bis.available(); - byte[] buffer = new byte[ byteArraySize ]; - - bis.read( buffer ); - bos.write( buffer , 0, byteArraySize ); - } - - bis.close(); - bos.close(); - - return new File( newFileName ); - } - //------------------------------------------------------------------------- // Instance variables // - private XercesProfileFactory xProfFactory = null; + private DefaultProfileFactory profFactory = null; private String profileFileName = ""; private String schemaProfileFileName = ""; diff --git a/src/test/java/jpos/util/JposPropertiesTestCase.java b/src/test/java/jpos/util/JposPropertiesTestCase.java index 4c50e6a..d732256 100644 --- a/src/test/java/jpos/util/JposPropertiesTestCase.java +++ b/src/test/java/jpos/util/JposPropertiesTestCase.java @@ -111,8 +111,8 @@ public void testGetPropertyString() properties.getPropertyString( "jpos.loader.serviceManagerClass" ) != null ); - assertTrue( "All JposProperties should contain property \"jpos.util.tracing\"", - properties.getPropertyString( "jpos.util.tracing" ) != null ); + assertTrue( "All JposProperties should contain property \"jpos.util.tracing.TurnOnAllNamedTracers\"", + properties.getPropertyString( "jpos.util.tracing.TurnOnAllNamedTracers" ) != null ); assertTrue( "JposProperties should NOT contain property \"__this.is.a.fake.property_name__\"", properties.getPropertyString( "__this.is.a.fake.property_name__" ) == null ); @@ -126,8 +126,8 @@ public void testIsPropertyDefined() properties.isPropertyDefined( "jpos.loader.serviceManagerClass" ) == true ); - assertTrue( "All JposProperties should contain property \"jpos.util.tracing\"", - properties.isPropertyDefined( "jpos.util.tracing" ) == true ); + assertTrue( "All JposProperties should contain property \"jpos.util.tracing.TurnOnAllNamedTracers\"", + properties.isPropertyDefined( "jpos.util.tracing.TurnOnAllNamedTracers" ) == true ); assertTrue( "JposProperties should NOT contain property \"__this.is.a.fake.property_name__\"", properties.isPropertyDefined( "__this.is.a.fake.property_name__" ) == false ); diff --git a/src/test/resources/jpos/res/jpos_junit.properties b/src/test/resources/jpos/res/jpos_junit.properties index f46e012..739e3ce 100644 --- a/src/test/resources/jpos/res/jpos_junit.properties +++ b/src/test/resources/jpos/res/jpos_junit.properties @@ -3,7 +3,7 @@ jpos.loader.serviceManagerClass=jpos.loader.simple.SimpleServiceManager jpos.config.regPopulatorClass=jpos.config.simple.xml.SimpleXmlRegPopulator -jpos.util.tracing=OFF +jpos.util.tracing.TurnOnAllNamedTracers=ON # Use this property to for the JCL to load a specific file (cfg or XML) #jpos.config.populatorFile=jpos1.cfg