From 3fc6ff6432a8ac65412c83146cb82d2fd86305e6 Mon Sep 17 00:00:00 2001 From: mjpcger <37701263+mjpcger@users.noreply.github.com> Date: Mon, 18 Dec 2023 20:54:52 +0100 Subject: [PATCH 01/48] Assignments of typed values via deprecated constructors replaced by type-casted values. --- src/main/java/jpos/util/JposEntryUtility.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/jpos/util/JposEntryUtility.java b/src/main/java/jpos/util/JposEntryUtility.java index 54abb8a..65ae984 100644 --- a/src/main/java/jpos/util/JposEntryUtility.java +++ b/src/main/java/jpos/util/JposEntryUtility.java @@ -345,28 +345,28 @@ public static Object getDefaultValueForType( Class propType ) throws JposConfigE propValue = ""; else if( propType.equals( Boolean.class ) ) - propValue = new Boolean( false ); + propValue = false; else if( propType.equals( Character.class ) ) - propValue = new Character( 'a' ); + propValue = 'a'; else if( propType.equals( Double.class ) ) - propValue = new Double( 0 ); + propValue = (double) 0; else if( propType.equals( Float.class ) ) - propValue = new Float( 0 ); + propValue = (float) 0; else if( propType.equals( Byte.class ) ) - propValue = new Byte( (byte)0 ); + propValue = (byte) 0; else if( propType.equals( Integer.class ) ) - propValue = new Integer( 0 ); + propValue = (int) 0 ; else if( propType.equals( Long.class ) ) - propValue = new Long( 0 ); + propValue = (long) 0 ; else if( propType.equals( Short.class ) ) - propValue = new Short( (short)0 ); + propValue = (short) 0 ; } catch( Exception e ) { throw new JposConfigException( "Invalid property type" ); } @@ -400,7 +400,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 = (char) stringValue.charAt( 0 ); else if( propType.equals( Double.class ) ) propValue = Double.valueOf( stringValue ); From f596e3d58a770649be8639f646b380187436da2b Mon Sep 17 00:00:00 2001 From: mjpcger <37701263+mjpcger@users.noreply.github.com> Date: Fri, 22 Dec 2023 01:13:02 +0100 Subject: [PATCH 02/48] Avoid IllegalArgumentExceptions if an optional URL related XML attribute is missing. Xerces2RegPopularor did not work correctly whenever one of the optional attributes are missing. Using an empty string in that case. --- .../java/jpos/config/simple/xml/Xerces2RegPopulator.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java b/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java index c73244e..41bf6fd 100644 --- a/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java @@ -367,7 +367,7 @@ protected void addVendorProp( JposEntry entry, factoryClass ); currentEntry.addProperty( JposEntry.VENDOR_URL_PROP_NAME, - serviceClass ); + serviceClass == null ? "" : serviceClass ); } protected void addJposProp( JposEntry entry, @@ -403,7 +403,7 @@ protected void addProductProp( JposEntry entry, name ); currentEntry.addProperty( JposEntry.PRODUCT_URL_PROP_NAME, - url ); + url == null ? "" : url); } protected void addProp( JposEntry entry, @@ -421,7 +421,7 @@ protected void addProp( JposEntry entry, { Class typeClass = JposEntryUtility. - propTypeFromString( attributes.getValue( "type" ) ); + propTypeFromString( typeString ); Object value = JposEntryUtility.parsePropValue( valueString, typeClass ); From 24284a315a48f6d935321d150e627129ea9c1a2e Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 27 May 2024 18:04:31 +0200 Subject: [PATCH 03/48] Using scalar types' .valueOf method instead of auto-boxing for better readability. --- src/main/java/jpos/util/JposEntryUtility.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/jpos/util/JposEntryUtility.java b/src/main/java/jpos/util/JposEntryUtility.java index 65ae984..a5fbd52 100644 --- a/src/main/java/jpos/util/JposEntryUtility.java +++ b/src/main/java/jpos/util/JposEntryUtility.java @@ -345,28 +345,28 @@ public static Object getDefaultValueForType( Class propType ) throws JposConfigE propValue = ""; else if( propType.equals( Boolean.class ) ) - propValue = false; + propValue = Boolean.valueOf(false); else if( propType.equals( Character.class ) ) - propValue = 'a'; + propValue = Character.valueOf('a'); else if( propType.equals( Double.class ) ) - propValue = (double) 0; + propValue = Double.valueOf(0); else if( propType.equals( Float.class ) ) - propValue = (float) 0; + propValue = Float.valueOf(0); else if( propType.equals( Byte.class ) ) - propValue = (byte) 0; + propValue = Byte.valueOf((byte) 0); else if( propType.equals( Integer.class ) ) - propValue = (int) 0 ; + propValue = Integer.valueOf(0) ; else if( propType.equals( Long.class ) ) - propValue = (long) 0 ; + propValue = Long.valueOf(0) ; else if( propType.equals( Short.class ) ) - propValue = (short) 0 ; + propValue = Short.valueOf((short) 0); } catch( Exception e ) { throw new JposConfigException( "Invalid property type" ); } @@ -400,7 +400,7 @@ public static Object parsePropValue( String stringValue, Class propType ) throws propValue = Boolean.valueOf( stringValue ); else if( propType.equals( Character.class ) ) - propValue = (char) stringValue.charAt( 0 ); + propValue = Character.valueOf( stringValue.charAt( 0 ) ); else if( propType.equals( Double.class ) ) propValue = Double.valueOf( stringValue ); From 9736fde945aea84145f654115862839123142e0d Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 27 May 2024 21:43:11 +0200 Subject: [PATCH 04/48] Code clean-up: Get rid of raw types and Vector usage at JposEntryUtility where possible as we are going to change it anyway. However, avoided changes at public method signature to ensure backward compatibility. --- src/main/java/jpos/util/JposEntryUtility.java | 115 +++++++++--------- 1 file changed, 58 insertions(+), 57 deletions(-) diff --git a/src/main/java/jpos/util/JposEntryUtility.java b/src/main/java/jpos/util/JposEntryUtility.java index a5fbd52..a1b463d 100644 --- a/src/main/java/jpos/util/JposEntryUtility.java +++ b/src/main/java/jpos/util/JposEntryUtility.java @@ -22,6 +22,7 @@ import jpos.config.JposEntry; import jpos.config.JposEntryConst; +import jpos.config.RS232Const; import jpos.config.JposConfigException; import jpos.config.simple.SimpleEntry; @@ -98,12 +99,12 @@ public static boolean isRS232PropName( String name ) { boolean valid = false; - if( name.equals( JposEntryConst.RS232_PORT_NAME_PROP_NAME ) || - name.equals( JposEntryConst.RS232_BAUD_RATE_PROP_NAME ) || - name.equals( JposEntryConst.RS232_DATA_BITS_PROP_NAME ) || - name.equals( JposEntryConst.RS232_PARITY_PROP_NAME ) || - name.equals( JposEntryConst.RS232_STOP_BITS_PROP_NAME ) || - name.equals( JposEntryConst.RS232_FLOW_CONTROL_PROP_NAME ) ) + if( name.equals( RS232Const.RS232_PORT_NAME_PROP_NAME ) || + name.equals( RS232Const.RS232_BAUD_RATE_PROP_NAME ) || + name.equals( RS232Const.RS232_DATA_BITS_PROP_NAME ) || + name.equals( RS232Const.RS232_PARITY_PROP_NAME ) || + name.equals( RS232Const.RS232_STOP_BITS_PROP_NAME ) || + name.equals( RS232Const.RS232_FLOW_CONTROL_PROP_NAME ) ) valid = true; return valid; @@ -115,12 +116,12 @@ public static boolean isRS232PropName( String name ) */ public static void removeAllRS232Props( JposEntry jposEntry ) { - jposEntry.removeProperty( JposEntryConst.RS232_PORT_NAME_PROP_NAME ); - jposEntry.removeProperty( JposEntryConst.RS232_BAUD_RATE_PROP_NAME ); - jposEntry.removeProperty( JposEntryConst.RS232_DATA_BITS_PROP_NAME ); - jposEntry.removeProperty( JposEntryConst.RS232_PARITY_PROP_NAME ); - jposEntry.removeProperty( JposEntryConst.RS232_STOP_BITS_PROP_NAME ); - jposEntry.removeProperty( JposEntryConst.RS232_FLOW_CONTROL_PROP_NAME ); + jposEntry.removeProperty( RS232Const.RS232_PORT_NAME_PROP_NAME ); + jposEntry.removeProperty( RS232Const.RS232_BAUD_RATE_PROP_NAME ); + jposEntry.removeProperty( RS232Const.RS232_DATA_BITS_PROP_NAME ); + jposEntry.removeProperty( RS232Const.RS232_PARITY_PROP_NAME ); + jposEntry.removeProperty( RS232Const.RS232_STOP_BITS_PROP_NAME ); + jposEntry.removeProperty( RS232Const.RS232_FLOW_CONTROL_PROP_NAME ); } @@ -133,18 +134,18 @@ public static void removeAllRS232Props( JposEntry jposEntry ) */ public static Enumeration getNonRequiredPropNames( JposEntry jposEntry ) { - Vector vector = new Vector(); + List list = new ArrayList<>(); - Enumeration names = jposEntry.getPropertyNames(); + Enumeration names = jposEntry.getPropertyNames(); while( names.hasMoreElements() ) { - String name = (String)names.nextElement(); + String name = names.nextElement(); if( isRequiredPropName( name ) == false ) - vector.add( name ); + list.add( name ); } - return vector.elements(); + return Collections.enumeration(list); } /** @@ -154,39 +155,39 @@ public static Enumeration getNonRequiredPropNames( 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); } /** @@ -196,27 +197,27 @@ public static Enumeration getMissingRequiredPropNames( 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); } /** @@ -225,21 +226,21 @@ public static Enumeration getMissingRS232PropNames( JposEntry jposEntry ) */ public static Enumeration getVendorPropNames( JposEntry jposEntry ) { - Vector vector = new Vector(); + List list = new ArrayList<>(); - Enumeration propNames = jposEntry.getPropertyNames(); + 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 +249,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 ) ); } } @@ -441,7 +442,7 @@ public static Class propTypeFromString( String typeString ) throws JposConfigExc try { - Class typeClass = Class.forName( className ); + Class typeClass = Class.forName( className ); if( isValidPropType( typeClass ) == false ) throw new JposConfigException( "Invalid property type: " + typeString ); @@ -461,7 +462,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 +476,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 ] ); } } From 1592d1559a97a2920454a79fb4b99158e2c06937 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 27 May 2024 21:55:07 +0200 Subject: [PATCH 05/48] Code clean-up: simplified negated boolean expressions --- src/main/java/jpos/util/JposEntryUtility.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/jpos/util/JposEntryUtility.java b/src/main/java/jpos/util/JposEntryUtility.java index a1b463d..22f56e0 100644 --- a/src/main/java/jpos/util/JposEntryUtility.java +++ b/src/main/java/jpos/util/JposEntryUtility.java @@ -141,7 +141,7 @@ public static Enumeration getNonRequiredPropNames( JposEntry jposEntry ) { String name = names.nextElement(); - if( isRequiredPropName( name ) == false ) + if( !isRequiredPropName( name ) ) list.add( name ); } @@ -335,7 +335,7 @@ public static boolean isValidPropType( Class propType ) */ public static Object getDefaultValueForType( Class propType ) throws JposConfigException { - if( isValidPropType( propType ) == false ) + if( !isValidPropType( propType ) ) throw new JposConfigException( "Invalid property type: " + propType ); Object propValue = ""; @@ -387,7 +387,7 @@ public static Object getDefaultValueForType( Class propType ) throws JposConfigE */ public static Object parsePropValue( String stringValue, Class propType ) throws JposConfigException { - if( isValidPropType( propType ) == false ) + if( !isValidPropType( propType ) ) throw new JposConfigException( "Invalid property type: " + propType ); Object propValue = ""; @@ -444,7 +444,7 @@ public static Class propTypeFromString( String typeString ) throws JposConfigExc { Class typeClass = Class.forName( className ); - if( isValidPropType( typeClass ) == false ) + if( !isValidPropType( typeClass ) ) throw new JposConfigException( "Invalid property type: " + typeString ); return typeClass; From 8eee5d54920e92b1aa5b56e31dc05bc7f2bf6f5c Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Thu, 27 Jun 2024 15:57:39 +0200 Subject: [PATCH 06/48] Avoiding file resources remaining unclosed for the SimpleRegPopulator by implementing all resources access using the try-with-resources code schema where possible and meaningful. --- .../config/simple/SimpleRegPopulator.java | 172 +++++++++--------- .../simple/SimpleRegPopulatorTestCase.java | 2 +- 2 files changed, 91 insertions(+), 83 deletions(-) diff --git a/src/main/java/jpos/config/simple/SimpleRegPopulator.java b/src/main/java/jpos/config/simple/SimpleRegPopulator.java index fab4925..175a1a1 100644 --- a/src/main/java/jpos/config/simple/SimpleRegPopulator.java +++ b/src/main/java/jpos/config/simple/SimpleRegPopulator.java @@ -93,11 +93,9 @@ public void save( Enumeration entries ) throws Exception public void save( 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 )) { + saveJposEntries( entries, fos ); + } } /** @@ -138,10 +136,10 @@ public void load() */ public void load( String fileName ) { - try + try (FileInputStream fis = new FileInputStream( fileName )) { getJposEntries().clear(); - Enumeration entries = readJposEntries( new FileInputStream( fileName ) ); + Enumeration entries = readJposEntries( fis ); while( entries.hasMoreElements() ) { @@ -203,64 +201,60 @@ public URL getEntriesURL() */ 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 = (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 = (JposEntry)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(); } /** @@ -271,7 +265,9 @@ protected void saveSerInZipFile( 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); + } } /** @@ -284,16 +280,14 @@ protected void saveSerFile( Enumeration entries ) throws Exception 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 = (JposEntry)entries.nextElement(); + + oos.writeObject( entry ); + } } - - oos.close(); } /** @@ -433,12 +427,18 @@ protected Enumeration readJposEntries( InputStream is ) 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() ); } - + e.getMessage() ); + } return entries.elements(); } @@ -451,11 +451,17 @@ protected Enumeration readJposEntries() Enumeration entries = null; if( isPopulatorFileDefined() ) - try { entries = readJposEntries( getPopulatorFileIS() ); } - catch( Exception e ) - { entries = ( new Vector() ).elements(); } + try (InputStream inputStream = getPopulatorFileIS()) { + entries = readJposEntries( inputStream ); + } + catch( Exception e ) + { entries = ( new Vector() ).elements(); } else - entries = readJposEntries( findSerOIS() ); + try (InputStream inputStream = findSerOIS()) { + entries = readJposEntries( findSerOIS() ); + } + catch( Exception e ) + { entries = ( new Vector() ).elements(); } return entries; } @@ -469,7 +475,9 @@ protected Enumeration readJposEntries() protected void saveJposEntries( Enumeration entries ) throws Exception { if( isPopulatorFileDefined() ) - saveJposEntries( entries, getPopulatorFileOS() ); + try (OutputStream outputStream = getPopulatorFileOS()) { + saveJposEntries( entries, outputStream ); + } else { if( serInZipFile ) diff --git a/src/test/java/jpos/config/simple/SimpleRegPopulatorTestCase.java b/src/test/java/jpos/config/simple/SimpleRegPopulatorTestCase.java index b0bcf42..4347c04 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() ); From 468949ded2e33b4d16c8c23fa43ab99a7212fdb5 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Thu, 27 Jun 2024 15:58:31 +0200 Subject: [PATCH 07/48] Fixing a NPE in case multi-prop definition is missing in jpos.properties if implicitly requested by usage of the DefaultCompositeRegPopulator. Instead throwing a runtime exception . Restricted the check from null pointer check to empty list check, because in case of an empty list the subsequent code may cause the NPE seen in the tests after having applied the try-with-resources code schema strictly. --- src/main/java/jpos/config/DefaultCompositeRegPopulator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java index 70026a6..3baa49e 100644 --- a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java +++ b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java @@ -259,7 +259,7 @@ public void load() JposProperties.MultiProperty populatorFileMultiProp = jposProperties.getMultiProperty( JposProperties.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" ); From eb728a18c8578657afc0279840fae2ee5aab0448 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Thu, 27 Jun 2024 19:22:09 +0200 Subject: [PATCH 08/48] Code cleanup: Got rid of compile warning, introduce generics where possible. If not possible SuppressWarnig annotation had been added, e.g. on public APIs. Furthermore: - clean up not done for Xerces code as it will be removed - low hanging fruits taken, e.g. using StringBuilder instead of StringBuffer - improved initialization of singletons in jpos.profile - further code improvements to get less code smell (no complains for this old code...) Tests passed locally. --- .../jpos/config/CompositeRegPopulator.java | 1 + .../config/DefaultCompositeRegPopulator.java | 75 ++++++++------- .../java/jpos/config/JposConfigException.java | 5 + src/main/java/jpos/config/JposEntry.java | 1 + src/main/java/jpos/config/JposEntryConst.java | 4 +- .../java/jpos/config/JposEntryRegistry.java | 3 +- .../jpos/config/JposEntryRegistryEvent.java | 5 + .../java/jpos/config/JposRegPopulator.java | 7 +- .../config/simple/AbstractRegPopulator.java | 79 +++------------- .../java/jpos/config/simple/SimpleEntry.java | 56 +++++++----- .../config/simple/SimpleEntryRegistry.java | 65 +++++++------ .../config/simple/SimpleRegPopulator.java | 91 ++++++++++--------- .../simple/xml/SimpleXmlRegPopulator.java | 7 +- .../java/jpos/loader/JposLoaderException.java | 5 + .../java/jpos/loader/JposServiceLoader.java | 8 +- .../simple/SimpleServiceConnection.java | 10 +- .../loader/simple/SimpleServiceManager.java | 2 +- .../java/jpos/profile/AbstractPropType.java | 29 +----- .../java/jpos/profile/BooleanPropType.java | 12 +-- .../java/jpos/profile/CharacterPropType.java | 12 +-- .../jpos/profile/DefaultDevCatInfoList.java | 13 +-- .../java/jpos/profile/DefaultProfile.java | 6 +- .../jpos/profile/DefaultProfileRegistry.java | 13 +-- .../java/jpos/profile/DefaultPropInfo.java | 6 +- .../jpos/profile/DefaultPropInfoList.java | 13 +-- .../java/jpos/profile/DefaultPropValue.java | 31 ++++--- .../jpos/profile/DefaultPropValueList.java | 13 +-- src/main/java/jpos/profile/FloatPropType.java | 15 +-- .../java/jpos/profile/IntegerPropType.java | 14 +-- src/main/java/jpos/profile/JposDevCats.java | 13 ++- .../java/jpos/profile/ProfileException.java | 16 +++- .../java/jpos/profile/ProfileRegistry.java | 2 +- src/main/java/jpos/profile/PropType.java | 2 +- .../java/jpos/profile/StringPropType.java | 14 +-- .../jpos/profile/XercesProfileFactory.java | 32 ++----- .../java/jpos/util/DefaultProperties.java | 69 +++++++------- src/main/java/jpos/util/FileUtil.java | 37 ++++---- src/main/java/jpos/util/JposEntryUtility.java | 24 ++--- src/main/java/jpos/util/JposProperties.java | 8 +- src/main/java/jpos/util/PopupHelper.java | 6 +- src/main/java/jpos/util/Sorter.java | 15 +-- src/main/java/jpos/util/XmlHelper.java | 28 +++--- .../java/jpos/util/tracing/TracerFactory.java | 38 +++----- 43 files changed, 401 insertions(+), 504 deletions(-) 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 3baa49e..c041905 100644 --- a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java +++ b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java @@ -73,19 +73,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 +164,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 +200,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 +230,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,10 +248,10 @@ 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 == null || populatorClassMultiProp.getNumberOfProperties() == 0 ) { @@ -265,9 +259,10 @@ public void load() throw new RuntimeException( "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 ); @@ -275,7 +270,7 @@ public void load() if( populatorFileMultiProp != null && populatorFileMultiProp.getNumberOfProperties() > 0 ) { - String defaultPopFile = (String)populatorFileMultiProp.getPropertyString( defaultPopClassNumber ); + String defaultPopFile = populatorFileMultiProp.getPropertyString( defaultPopClassNumber ); if( defaultPopFile != null ) { @@ -304,7 +299,7 @@ public void load() "<" + defaultPopName + ", " + defaultPopClass + ">" ); while( popClasses.hasNext() ) { - String popName = (String)popClasses.next(); + String popName = popClasses.next(); String popClass = populatorClassMultiProp.getPropertyString( popName ); int popClassNumber = populatorClassMultiProp.propertyNumber( popName ); @@ -349,21 +344,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 +409,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(); } /** @@ -428,8 +427,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..c6bfe8e 100644 --- a/src/main/java/jpos/config/JposConfigException.java +++ b/src/main/java/jpos/config/JposConfigException.java @@ -28,6 +28,11 @@ */ 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..c516699 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 { /** 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..c8ff7fe 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 diff --git a/src/main/java/jpos/config/JposEntryRegistryEvent.java b/src/main/java/jpos/config/JposEntryRegistryEvent.java index bff2cc0..dd09500 100644 --- a/src/main/java/jpos/config/JposEntryRegistryEvent.java +++ b/src/main/java/jpos/config/JposEntryRegistryEvent.java @@ -28,6 +28,11 @@ 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/simple/AbstractRegPopulator.java b/src/main/java/jpos/config/simple/AbstractRegPopulator.java index 5cd3188..910e8f7 100644 --- a/src/main/java/jpos/config/simple/AbstractRegPopulator.java +++ b/src/main/java/jpos/config/simple/AbstractRegPopulator.java @@ -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); } /** @@ -197,7 +149,7 @@ protected URL createURLFromFile( ZipFile zipFile ) * @return the jposEntries Hashtable to allow access to subclasses * @since 1.2 (NY 2K meeting) */ - protected Hashtable getJposEntries() { return jposEntries; } + protected Hashtable getJposEntries() { return jposEntries; } /** * @return true if a populator file (or URL) is defined @@ -376,7 +328,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 +341,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 + @@ -418,22 +370,21 @@ protected InputStream findFileInClasspath( String fileName ) * @since 2.0 (Long Beach 2001) */ protected InputStream findFileInJarZipFiles( String fileName, - Vector jarZipFilesVector ) + List jarZipFilesVector ) { InputStream is = null; for( int i = 0; i < jarZipFilesVector.size(); ++i ) { - String jarZipFileName = (String)jarZipFilesVector.elementAt( i ); + String jarZipFileName = jarZipFilesVector.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,7 +411,7 @@ protected InputStream findFileInJarZipFiles( String fileName, // Instance variables // - private Hashtable jposEntries = new Hashtable(); + private final Hashtable jposEntries = new Hashtable<>(); private InputStream populatorIS = null; private OutputStream populatorOS = null; diff --git a/src/main/java/jpos/config/simple/SimpleEntry.java b/src/main/java/jpos/config/simple/SimpleEntry.java index 9f5b746..bbdb6ab 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 @@ -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 @@ -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..04df58b 100644 --- a/src/main/java/jpos/config/simple/SimpleEntryRegistry.java +++ b/src/main/java/jpos/config/simple/SimpleEntryRegistry.java @@ -66,15 +66,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); } /** @@ -135,17 +136,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 +169,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 +232,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 +271,7 @@ public void load() */ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append( "\n" ); @@ -285,13 +282,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 +316,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 +338,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 +359,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 +373,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 175a1a1..56bbf26 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,11 +90,13 @@ 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 ); try (FileOutputStream fos = new FileOutputStream( file )) { - saveJposEntries( entries, fos ); + @SuppressWarnings("unchecked") + Enumeration typeSafeEntries = entries; + saveJposEntries( typeSafeEntries, fos ); } } @@ -105,15 +107,14 @@ public void save( Enumeration entries, String fileName ) throws Exception public void load() { getJposEntries().clear(); - Enumeration entries = readJposEntries(); + 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 ); @@ -139,7 +140,7 @@ public void load( String fileName ) try (FileInputStream fis = new FileInputStream( fileName )) { getJposEntries().clear(); - Enumeration entries = readJposEntries( fis ); + Enumeration entries = readJposEntries( fis ); while( entries.hasMoreElements() ) { @@ -199,15 +200,15 @@ 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 { try( ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( zipSerFile.getName() + ".temp.jar" ) ) ) { - Enumeration zipEntries = zipSerFile.entries(); + Enumeration zipEntries = zipSerFile.entries(); while( zipEntries.hasMoreElements() ) { - ZipEntry zipEntry = (ZipEntry)zipEntries.nextElement(); + ZipEntry zipEntry = zipEntries.nextElement(); zos.putNextEntry( zipEntry ); @@ -263,7 +264,7 @@ 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 { try (OutputStream os = new FileOutputStream( serFileName )) { saveJposEntries( entries, os); @@ -277,7 +278,7 @@ 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 { try (ObjectOutputStream oos = new ObjectOutputStream( os )) { @@ -298,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 @@ -316,10 +317,10 @@ 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 vector of JAR/Zip file names * @since 1.2 (NY 2K meeting) */ - protected ObjectInputStream findSerOISInClasspath( Vector jarZipFilesVector ) + protected ObjectInputStream findSerOISInClasspath( List jarZipFilePaths ) { ObjectInputStream ois = null; @@ -339,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; @@ -359,25 +360,23 @@ 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 * @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 ) ) { @@ -402,9 +401,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 { @@ -423,7 +422,7 @@ protected Enumeration readJposEntries( InputStream is ) serFileName ); else while( true ) - entries.addElement( in.readObject() ); + entries.add( (JposEntry) in.readObject() ); serFileName = absoluteFileName; } @@ -439,29 +438,35 @@ protected Enumeration readJposEntries( InputStream is ) serFileName + " Exception.message=" + e.getMessage() ); } - return entries.elements(); + 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; + Enumeration entries = null; - if( isPopulatorFileDefined() ) - try (InputStream inputStream = getPopulatorFileIS()) { - entries = readJposEntries( inputStream ); - } - catch( Exception e ) - { entries = ( new Vector() ).elements(); } - else + 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 = ( new Vector() ).elements(); } + catch( Exception e ) + { + entries = Collections.enumeration(new ArrayList<>()); + } + } return entries; } @@ -472,7 +477,7 @@ 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() ) try (OutputStream outputStream = getPopulatorFileOS()) { @@ -499,7 +504,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/SimpleXmlRegPopulator.java b/src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java index 6032fc9..321e97a 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 diff --git a/src/main/java/jpos/loader/JposLoaderException.java b/src/main/java/jpos/loader/JposLoaderException.java index 0723c6a..ab2e567 100644 --- a/src/main/java/jpos/loader/JposLoaderException.java +++ b/src/main/java/jpos/loader/JposLoaderException.java @@ -32,6 +32,11 @@ public class JposLoaderException extends JposException // Ctor(s) // + /** + * + */ + private static final long serialVersionUID = -2552211746747253072L; + /** * 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..31fef32 100644 --- a/src/main/java/jpos/loader/JposServiceLoader.java +++ b/src/main/java/jpos/loader/JposServiceLoader.java @@ -95,12 +95,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 }; diff --git a/src/main/java/jpos/loader/simple/SimpleServiceConnection.java b/src/main/java/jpos/loader/simple/SimpleServiceConnection.java index 6ccf479..e3adce4 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceConnection.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceConnection.java @@ -76,7 +76,7 @@ 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"); @@ -87,15 +87,15 @@ public void connect() throws JposException 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 +137,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..e78962e 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceManager.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceManager.java @@ -105,7 +105,7 @@ private void initRegPopulator() try { - Class regPopulatorClass = Class.forName( regPopulatorClassName ); + Class regPopulatorClass = Class.forName( regPopulatorClassName ); regPopulator = (JposRegPopulator)regPopulatorClass.newInstance(); } diff --git a/src/main/java/jpos/profile/AbstractPropType.java b/src/main/java/jpos/profile/AbstractPropType.java index 3921f84..5b0eaf6 100644 --- a/src/main/java/jpos/profile/AbstractPropType.java +++ b/src/main/java/jpos/profile/AbstractPropType.java @@ -28,34 +28,7 @@ public abstract class AbstractPropType extends Object 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/DefaultDevCatInfoList.java b/src/main/java/jpos/profile/DefaultDevCatInfoList.java index 3f5a468..f9681ae 100644 --- a/src/main/java/jpos/profile/DefaultDevCatInfoList.java +++ b/src/main/java/jpos/profile/DefaultDevCatInfoList.java @@ -27,13 +27,6 @@ */ class DefaultDevCatInfoList extends Object implements DevCatInfoList { - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor */ - public DefaultDevCatInfoList() {} - //------------------------------------------------------------------------- // Public methods // @@ -91,7 +84,7 @@ class DefaultIterator extends Object implements DevCatInfoList.Iterator // /** @return the next DevCatInfo in the iterator */ - public DevCatInfo next() { return (DevCatInfo)iterator.next(); } + public DevCatInfo next() { return iterator.next(); } /** @return true if there is a next DevCatInfo in the iterator */ public boolean hasNext() { return iterator.hasNext(); } @@ -100,12 +93,12 @@ class DefaultIterator extends Object implements DevCatInfoList.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/DefaultProfile.java b/src/main/java/jpos/profile/DefaultProfile.java index cc9814e..d1f6b90 100644 --- a/src/main/java/jpos/profile/DefaultProfile.java +++ b/src/main/java/jpos/profile/DefaultProfile.java @@ -29,9 +29,7 @@ */ class DefaultProfile extends Object implements Profile, Serializable { - //------------------------------------------------------------------------- - // Ctor(s) - // + private static final long serialVersionUID = 1478221492781830174L; /** * Creates a profile with name passed @@ -68,7 +66,7 @@ class DefaultProfile extends Object implements Profile, Serializable /** @return a String representation of this Profile */ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append( "\n" ); diff --git a/src/main/java/jpos/profile/DefaultProfileRegistry.java b/src/main/java/jpos/profile/DefaultProfileRegistry.java index e74a67a..f90fbc1 100644 --- a/src/main/java/jpos/profile/DefaultProfileRegistry.java +++ b/src/main/java/jpos/profile/DefaultProfileRegistry.java @@ -27,13 +27,6 @@ */ public class DefaultProfileRegistry extends Object 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..9a4aaf0 100644 --- a/src/main/java/jpos/profile/DefaultPropInfo.java +++ b/src/main/java/jpos/profile/DefaultPropInfo.java @@ -27,10 +27,8 @@ */ class DefaultPropInfo extends Object 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..74bfbd9 100644 --- a/src/main/java/jpos/profile/DefaultPropInfoList.java +++ b/src/main/java/jpos/profile/DefaultPropInfoList.java @@ -27,13 +27,6 @@ */ class DefaultPropInfoList extends Object implements PropInfoList { - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor */ - public DefaultPropInfoList() {} - //------------------------------------------------------------------------- // Public methods // @@ -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..78655dd 100644 --- a/src/main/java/jpos/profile/DefaultPropValue.java +++ b/src/main/java/jpos/profile/DefaultPropValue.java @@ -19,6 +19,7 @@ /////////////////////////////////////////////////////////////////////////////// import java.io.Serializable; +import java.util.Objects; /** * Class implementing the PropValue interface @@ -27,9 +28,7 @@ */ class DefaultPropValue extends Object 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..264e7ee 100644 --- a/src/main/java/jpos/profile/DefaultPropValueList.java +++ b/src/main/java/jpos/profile/DefaultPropValueList.java @@ -27,13 +27,6 @@ */ class DefaultPropValueList extends Object implements PropValueList { - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor */ - DefaultPropValueList() {} - //------------------------------------------------------------------------- // Public methods // @@ -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..11a0601 100644 --- a/src/main/java/jpos/profile/JposDevCats.java +++ b/src/main/java/jpos/profile/JposDevCats.java @@ -31,7 +31,7 @@ public class JposDevCats extends Object // 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,7 +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 + public abstract static class AbstractDevCat extends Object implements DevCat { //--------------------------------------------------------------------- @@ -261,6 +261,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 +270,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..f275c05 100644 --- a/src/main/java/jpos/profile/ProfileException.java +++ b/src/main/java/jpos/profile/ProfileException.java @@ -29,18 +29,28 @@ public class ProfileException extends Exception // Ctor(s) // + /** + * + */ + private static final long serialVersionUID = -5646225627523470777L; + /** * 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 +63,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..4cdb8d7 100644 --- a/src/main/java/jpos/profile/ProfileRegistry.java +++ b/src/main/java/jpos/profile/ProfileRegistry.java @@ -46,7 +46,7 @@ public interface ProfileRegistry public boolean hasProfile( Profile profile ); /** @return an enumeration of Profile objects */ - public Enumeration getProfiles(); + 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/profile/XercesProfileFactory.java b/src/main/java/jpos/profile/XercesProfileFactory.java index 45103b4..07db80d 100644 --- a/src/main/java/jpos/profile/XercesProfileFactory.java +++ b/src/main/java/jpos/profile/XercesProfileFactory.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; @@ -41,13 +40,6 @@ */ public class XercesProfileFactory extends Object implements ProfileFactory { - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor */ - public XercesProfileFactory() {} - //------------------------------------------------------------------------- // Private methods // @@ -118,8 +110,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 +161,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,10 +215,6 @@ 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" ); @@ -247,11 +233,11 @@ class DefaultErrorHandler extends Object 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 +265,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/util/DefaultProperties.java b/src/main/java/jpos/util/DefaultProperties.java index 9a223cb..e8d67c0 100644 --- a/src/main/java/jpos/util/DefaultProperties.java +++ b/src/main/java/jpos/util/DefaultProperties.java @@ -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; //------------------------------------------------------------------------- @@ -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<>(); } /** diff --git a/src/main/java/jpos/util/FileUtil.java b/src/main/java/jpos/util/FileUtil.java index 130ed32..2cc2786 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; @@ -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 ) { List list = new ArrayList<>(); + @SuppressWarnings("unchecked") Enumeration names = jposEntry.getPropertyNames(); while( names.hasMoreElements() ) { @@ -153,7 +154,7 @@ 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 ) { List list = new ArrayList<>(); @@ -195,7 +196,7 @@ 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 ) { List list = new ArrayList<>(); @@ -224,10 +225,11 @@ public static Enumeration getMissingRS232PropNames( JposEntry jposEntry ) * @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 ) { List list = new ArrayList<>(); + @SuppressWarnings("unchecked") Enumeration propNames = jposEntry.getPropertyNames(); while( propNames.hasMoreElements() ) list.add( propNames.nextElement() ); @@ -291,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 ); } /** @@ -301,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; @@ -316,7 +318,7 @@ public static boolean validatePropValue( Object propValue, Class propType ) * @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; @@ -333,7 +335,7 @@ 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 ) ) throw new JposConfigException( "Invalid property type: " + propType ); @@ -385,7 +387,7 @@ 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 ) ) throw new JposConfigException( "Invalid property type: " + propType ); @@ -434,7 +436,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" ); 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/PopupHelper.java b/src/main/java/jpos/util/PopupHelper.java index 9a97572..94b9fb9 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; @@ -84,7 +84,7 @@ void tryPopup( MouseEvent evt ) { */ 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..90572db 100644 --- a/src/main/java/jpos/util/Sorter.java +++ b/src/main/java/jpos/util/Sorter.java @@ -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/XmlHelper.java b/src/main/java/jpos/util/XmlHelper.java index 2571099..1dbe0b8 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; @@ -183,14 +184,14 @@ private void readAndCreateTempDtdFile( InputStream is ) throws IOException * @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 +214,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/TracerFactory.java b/src/main/java/jpos/util/tracing/TracerFactory.java index 3d88a7f..b082379 100644 --- a/src/main/java/jpos/util/tracing/TracerFactory.java +++ b/src/main/java/jpos/util/tracing/TracerFactory.java @@ -182,7 +182,6 @@ private void init() initGlobalTracer( props ); initTurnedOnTracers( props ); initNamedTracers( props ); - initTracerOutput(); } /** @@ -223,14 +222,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, Boolean.TRUE ); + } } } @@ -240,22 +238,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 +265,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(); From 68c6eae72207835178ce9a7cdf2bdfc645e1b784 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 28 Jun 2024 14:22:25 +0200 Subject: [PATCH 09/48] Code cleanup: get rid of a dead code warning by restructuring the control flow. --- .../config/DefaultCompositeRegPopulator.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java index c041905..e329c5b 100644 --- a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java +++ b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java @@ -268,32 +268,34 @@ public void load() JposRegPopulator defaultPopulator = createPopulator( defaultPopName, defaultPopClass ); - if( populatorFileMultiProp != null && populatorFileMultiProp.getNumberOfProperties() > 0 ) - { - String defaultPopFile = 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 + ">" ); From 32f536168f3261b60f451dce47b865f51a3462cf Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 28 Jun 2024 14:48:16 +0200 Subject: [PATCH 10/48] Code cleanup: get rid of some code smell like - bad constant referencing - StringBuffer usage - unnecessary casting --- .../config/DefaultCompositeRegPopulator.java | 4 +- .../config/simple/AbstractRegPopulator.java | 58 +++++++++---------- .../java/jpos/config/simple/SimpleEntry.java | 4 +- .../config/simple/SimpleEntryRegistry.java | 2 +- .../config/simple/SimpleRegPopulator.java | 6 +- .../java/jpos/loader/JposServiceLoader.java | 16 +++-- .../simple/SimpleServiceConnection.java | 2 +- .../loader/simple/SimpleServiceManager.java | 17 +++--- .../java/jpos/profile/DefaultDevCatInfo.java | 2 +- .../java/jpos/profile/ProfileRegistry.java | 3 +- 10 files changed, 53 insertions(+), 61 deletions(-) diff --git a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java index e329c5b..d76244f 100644 --- a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java +++ b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java @@ -311,7 +311,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 ); } @@ -420,7 +420,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(); } diff --git a/src/main/java/jpos/config/simple/AbstractRegPopulator.java b/src/main/java/jpos/config/simple/AbstractRegPopulator.java index 910e8f7..131dc41 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; @@ -161,14 +162,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; } @@ -182,15 +180,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 ); @@ -198,13 +197,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 ); @@ -235,26 +232,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 ); @@ -413,9 +410,6 @@ protected InputStream findFileInJarZipFiles( String fileName, private final Hashtable jposEntries = new Hashtable<>(); - private InputStream populatorIS = null; - private OutputStream populatorOS = null; - 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 bbdb6ab..1c0f933 100644 --- a/src/main/java/jpos/config/simple/SimpleEntry.java +++ b/src/main/java/jpos/config/simple/SimpleEntry.java @@ -130,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 ); @@ -352,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; diff --git a/src/main/java/jpos/config/simple/SimpleEntryRegistry.java b/src/main/java/jpos/config/simple/SimpleEntryRegistry.java index 04df58b..2739195 100644 --- a/src/main/java/jpos/config/simple/SimpleEntryRegistry.java +++ b/src/main/java/jpos/config/simple/SimpleEntryRegistry.java @@ -84,7 +84,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 diff --git a/src/main/java/jpos/config/simple/SimpleRegPopulator.java b/src/main/java/jpos/config/simple/SimpleRegPopulator.java index 56bbf26..37d6af9 100644 --- a/src/main/java/jpos/config/simple/SimpleRegPopulator.java +++ b/src/main/java/jpos/config/simple/SimpleRegPopulator.java @@ -144,7 +144,7 @@ public void load( String fileName ) while( entries.hasMoreElements() ) { - JposEntry entry = (JposEntry)entries.nextElement(); + JposEntry entry = entries.nextElement(); String logicalName = (String)entry. getPropertyValue( JposEntry.LOGICAL_NAME_PROP_NAME ); @@ -233,7 +233,7 @@ protected void saveSerInZipFile( Enumeration entries ) throws Excepti { while( entries.hasMoreElements() ) { - JposEntry entry = (JposEntry)entries.nextElement(); + JposEntry entry = entries.nextElement(); oos.writeObject( entry ); } @@ -284,7 +284,7 @@ protected void saveJposEntries( Enumeration entries, OutputStream os try (ObjectOutputStream oos = new ObjectOutputStream( os )) { while( entries.hasMoreElements() ) { - JposEntry entry = (JposEntry)entries.nextElement(); + JposEntry entry = entries.nextElement(); oos.writeObject( entry ); } diff --git a/src/main/java/jpos/loader/JposServiceLoader.java b/src/main/java/jpos/loader/JposServiceLoader.java index 31fef32..7c66264 100644 --- a/src/main/java/jpos/loader/JposServiceLoader.java +++ b/src/main/java/jpos/loader/JposServiceLoader.java @@ -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 ) @@ -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/simple/SimpleServiceConnection.java b/src/main/java/jpos/loader/simple/SimpleServiceConnection.java index e3adce4..437eace 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceConnection.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceConnection.java @@ -83,7 +83,7 @@ public void connect() throws JposException JposServiceInstanceFactory siFactory = null; if( siFactoryTable.containsKey( siFactoryClassName ) ) - siFactory = (JposServiceInstanceFactory)siFactoryTable. + siFactory = siFactoryTable. get( siFactoryClassName ); else { diff --git a/src/main/java/jpos/loader/simple/SimpleServiceManager.java b/src/main/java/jpos/loader/simple/SimpleServiceManager.java index e78962e..98caf58 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceManager.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceManager.java @@ -96,12 +96,12 @@ 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 { @@ -117,9 +117,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(); } @@ -172,7 +171,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/DefaultDevCatInfo.java b/src/main/java/jpos/profile/DefaultDevCatInfo.java index 49a7767..71c365c 100644 --- a/src/main/java/jpos/profile/DefaultDevCatInfo.java +++ b/src/main/java/jpos/profile/DefaultDevCatInfo.java @@ -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( " Date: Fri, 28 Jun 2024 14:59:12 +0200 Subject: [PATCH 11/48] Removed deprecated constructors and deprecated Tracer class This is a backward incompatible change! But OK for the new major number 4.0. All deprecated since long time (1999) - jpos.config.simple.SimpleEntryRegistry parameterless constructor - jpos.loader.simple.SimpleServiceManager parameterless constructor - jpos.util.Tracer internal class --- .../config/simple/SimpleEntryRegistry.java | 7 - .../loader/simple/SimpleServiceManager.java | 13 -- src/main/java/jpos/util/Tracer.java | 187 ------------------ 3 files changed, 207 deletions(-) delete mode 100644 src/main/java/jpos/util/Tracer.java diff --git a/src/main/java/jpos/config/simple/SimpleEntryRegistry.java b/src/main/java/jpos/config/simple/SimpleEntryRegistry.java index 2739195..63a538b 100644 --- a/src/main/java/jpos/config/simple/SimpleEntryRegistry.java +++ b/src/main/java/jpos/config/simple/SimpleEntryRegistry.java @@ -35,13 +35,6 @@ */ public class SimpleEntryRegistry extends Object 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 diff --git a/src/main/java/jpos/loader/simple/SimpleServiceManager.java b/src/main/java/jpos/loader/simple/SimpleServiceManager.java index 98caf58..17122dc 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceManager.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceManager.java @@ -41,19 +41,6 @@ public class SimpleServiceManager extends Object // 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 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 From abb4e30a97b43fb9cb1c723545639c7778fb159f Mon Sep 17 00:00:00 2001 From: mjpcger <37701263+mjpcger@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:05:09 +0100 Subject: [PATCH 12/48] Initial Javax based XML populator implementation to replace Xerces The new class JavaxRegPopulator extends the config loader with an XML populator that works with Javax instead of Xerces and thus enables JavaPOS to be used without the external modules of the Xalan / Xerces project. --- .../config/simple/xml/JavaxRegPopulator.java | 513 ++++++++++++++++++ 1 file changed, 513 insertions(+) create mode 100644 src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java 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..8d0e85b --- /dev/null +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -0,0 +1,513 @@ +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.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.helpers.DefaultHandler; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.*; +import java.net.URL; +import java.text.DateFormat; +import java.util.*; + +/** + * Simple implementation of the JposRegPopulator that loads and saves + * the entries in XML using 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 as + * the jpos.config.regPopulatorClass + * @see jpos.util.JposProperties#JPOS_REG_POPULATOR_CLASS_PROP_NAME + * @since 1.16 + * @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 (Washington DC 2001) + */ + 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; + } + + + @Override + public void save( Enumeration entries ) + throws Exception + { + if( isPopulatorFileDefined() ) + save(entries, getPopulatorFileOS()); + else + try( FileOutputStream os = new FileOutputStream( "jpos.xml" ) ) + { save( entries, os ); } + catch (Exception e) + { throw e; } + } + + @Override + public void save( Enumeration entries, String fileName ) + throws Exception + { + try( FileOutputStream os = new FileOutputStream( fileName ) ) + { save(entries, os); } + catch ( Exception e ) + { throw e; } + } + + @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 ) + { + 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; } + + //-------------------------------------------------------------------------- + // 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; } + + private void save( Enumeration entries, OutputStream outputStream ) + throws Exception + { + Document document = CreateEmptyDocument(); + + insertJposEntriesInDoc( entries, document ); + insertDateSavedComment( document ); + + DOMSource source = new DOMSource( document ); + Transformer transformer = TransformerFactory.newInstance().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( "JposEntries", DTD_DOC_TYPE_VALUE, DTD_FILE_NAME ); + + return domImpl.createDocument(null, "JposEntries", docType); + } + + + private void insertJposEntriesInDoc( Enumeration entries, Document doc ) + { + while( entries.hasMoreElements() ) + { + JposEntry jposEntry = (JposEntry) entries.nextElement(); + + if( JposEntryUtility.isValidJposEntry( jposEntry ) ) + { + Element jposEntryElement = doc.createElement( "JposEntry" ); + + jposEntryElement.setAttribute( "logicalName", jposEntry.getLogicalName() ); + insertJposPropertiesInElement( doc, jposEntry, jposEntryElement ); + doc.getDocumentElement().appendChild( jposEntryElement ); + } + } + } + + private void insertJposPropertiesInElement( Document doc, JposEntry jposEntry, Element jposEntryElement ) + { + Element creation = doc.createElement( "creation" ); + Element jpos = doc.createElement( "jpos" ); + Element product = doc.createElement( "product" ); + Element vendor = doc.createElement( "vendor" ); + + for( Element tag : new Element[] { creation, vendor, jpos, product } ) + jposEntryElement.appendChild(tag); + + List sortedProps = getSortedList( jposEntry.getProps() ); + for( JposEntry.Prop prop : sortedProps ) + { + if( notAttribute( prop, creation, JposEntry.SERVICE_CLASS_PROP_NAME, "serviceClass" ) && + notAttribute( prop, creation, JposEntry.SI_FACTORY_CLASS_PROP_NAME, "factoryClass" ) && + notAttribute( prop, vendor, JposEntry.VENDOR_NAME_PROP_NAME, "name" ) && + notAttribute( prop, vendor, JposEntry.VENDOR_URL_PROP_NAME, "url" ) && + notAttribute( prop, jpos, JposEntry.JPOS_VERSION_PROP_NAME, "version" ) && + notAttribute( prop, jpos, JposEntry.DEVICE_CATEGORY_PROP_NAME, "category" ) && + notAttribute( prop, product, JposEntry.PRODUCT_NAME_PROP_NAME, "name" ) && + notAttribute( prop, product, JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, "description" ) && + notAttribute( prop, product, JposEntry.PRODUCT_URL_PROP_NAME, "url" ) + ) + addPropElement( doc, jposEntryElement, prop ); + } + } + + private List getSortedList( Iterator props ) + { + ArrayList sortedProps = new ArrayList<>(); + while( props.hasNext() ) + { + JposEntry.Prop entry = (JposEntry.Prop) props.next(); + int lowBound = 0; + int highBound = sortedProps.size() - 1; + + while( lowBound <= highBound ) + { + int middle = (lowBound + highBound) / 2; + int compareresult = entry.getName().compareToIgnoreCase( sortedProps.get( middle ).getName() ); + if( compareresult == 0 ) + lowBound = highBound = middle; + else if ( compareresult > 0 ) + lowBound = middle + 1; + else + highBound = middle - 1; + } + sortedProps.add( lowBound, entry ); + } + return sortedProps; + } + + private boolean notAttribute( JposEntry.Prop prop, Element elem, String propName, String attrName ) + { + if( prop.getName().equals( propName ) ) + { + String value = prop.getValueAsString(); + /* Change /* to //* to generate implicit attribute values only if value is not empty. + if( value.length() > 0 || + ( !JposEntry.PRODUCT_URL_PROP_NAME.equals(propName) && + !JposEntry.VENDOR_URL_PROP_NAME.equals(propName) + ) + ) + //*/ + elem.setAttribute( attrName, prop.getValueAsString() ); + return false; + } + return true; + } + + private static void addPropElement( Document doc, Element jposEntryElement, JposEntry.Prop prop ) + { + Element more = doc.createElement( "prop" ); + jposEntryElement.appendChild( more ); + more.setAttribute( "name", prop.getName() ); + more.setAttribute( "value", prop.getValueAsString() ); + if( !( prop.getValue() instanceof String ) ) + more.setAttribute( "type", prop.getValue().getClass().getSimpleName() ); + } + + private void insertDateSavedComment( Document document ) + { + 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() ); + } + + private void load( InputStream inputStream ) + { + SAXParserFactory parserFactory = SAXParserFactory.newInstance(); + JavaxSaxHandler saxHandler = new JavaxSaxHandler(); + + parserFactory.setNamespaceAware( true ); + parserFactory.setValidating( true ); + jposEntryList.clear(); + try + { + parserFactory.newSAXParser().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; + } + Iterator entries = jposEntryList.iterator(); + while( entries.hasNext() ) + { + JposEntry jposEntry = (JposEntry) entries.next(); + getJposEntries().put( jposEntry.getLogicalName(), jposEntry ); + } + } + + //-------------------------------------------------------------------------- + // Private inner classes + // + + private class JavaxSaxHandler + extends DefaultHandler + { + private JposEntry CurrentEntry = null; + private SAXException TheException = null; + + //---------------------------------------------------------------- + // ContentHandler + + @Override + public void startElement( String uri, String lname, String qname, Attributes attrs ) + { + tracer.print( "StartElement: "+ qname ); + if( TheException != null ) + { + tracer.println( ": Parse error: " + TheException.getMessage() ); + lastLoadException = TheException; + TheException = null; + return; + } + if( qname.equals( "JposEntries" ) ) + jposEntryList.clear(); + else if( qname.equals( "JposEntry" ) ) + CurrentEntry = new SimpleEntry(attrs.getValue("logicalName"), JavaxRegPopulator.this); + else if( CurrentEntry != null ) + { + String temp; + + if( qname.equals( "creation" ) ) + { + CurrentEntry.addProperty( JposEntry.SI_FACTORY_CLASS_PROP_NAME, attrs.getValue( "factoryClass" ) ); + CurrentEntry.addProperty( JposEntry.SERVICE_CLASS_PROP_NAME, attrs.getValue( "serviceClass" ) ); + } + else if( qname.equals( "vendor" ) ) + { + CurrentEntry.addProperty( JposEntry.VENDOR_NAME_PROP_NAME, attrs.getValue( "name" ) ); + addOptionalProperty( JposEntry.VENDOR_URL_PROP_NAME, attrs.getValue( "url" ) ); + } + else if( qname.equals( "jpos" ) ) + { + CurrentEntry.addProperty( JposEntry.JPOS_VERSION_PROP_NAME, attrs.getValue( "version" ) ); + CurrentEntry.addProperty( JposEntry.DEVICE_CATEGORY_PROP_NAME, attrs.getValue( "category" ) ); + } + else if( qname.equals( "product" ) ) + { + CurrentEntry.addProperty( JposEntry.PRODUCT_NAME_PROP_NAME, attrs.getValue( "name" ) ); + CurrentEntry.addProperty( JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, attrs.getValue( "description" ) ); + addOptionalProperty( JposEntry.PRODUCT_URL_PROP_NAME, attrs.getValue( "url" ) ); + } + else if( qname.equals( "prop" ) ) + { + temp = attrs.getValue( "type" ); + try + { + Class type = temp == null ? String.class : Class.forName( "java.lang." + temp ); + Object obj = null; + obj = JposEntryUtility.parsePropValue( attrs.getValue( "value" ), type ); + CurrentEntry.addProperty( attrs.getValue( "name" ), obj ); + } + catch( Exception e ) + { + CurrentEntry = null; + String msg = "Invalid prop: name=" + attrs.getValue( "name" ) + + ":value=" + attrs.getValue( "value" ); + tracer.println( ": " + msg ); + lastLoadException = new SAXException( msg, e ); + }; + } + } + tracer.println( "" ); + } + + 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( "EndElement: " + qname ); + else + { + tracer.println( "EndElement: " + TheException.getMessage() ); + TheException = null; + } + if( qname.equals( "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 publicId, String systemId ) + { + tracer.println( "JposEntityResolver:resolveEntity:publicId=" + + publicId ); + + tracer.println( "JposEntityResolver:resolveEntity:systemId=" + + systemId ); + + if( publicId.equals( DTD_DOC_TYPE_VALUE ) ) + { + 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 + // + + protected String xmlFileName = DEFAULT_XML_FILE_NAME; + + private List jposEntryList = new LinkedList(); + + private Tracer tracer = TracerFactory.getInstance(). + createTracer( this.getClass().getSimpleName() ); + + //-------------------------------------------------------------------------- + // Constants + // + + public static final String DTD_FILE_PATH = "jpos/res"; + public static final String DTD_FILE_NAME = DTD_FILE_PATH + "/jcl.dtd"; + + public static final String DTD_DOC_TYPE_VALUE = "-//JavaPOS//DTD//EN"; + + private static final String JAVAX_REG_POPULATOR_NAME_STRING = "JAVAX XML Entries Populator"; +} From 609ba003fa3805b0a33ae71003f976374ab71a6e Mon Sep 17 00:00:00 2001 From: mjpcger <37701263+mjpcger@users.noreply.github.com> Date: Fri, 15 Mar 2024 02:22:00 +0100 Subject: [PATCH 13/48] Changes made after PR being reviewed according to discussion in https://github.com/JavaPOSWorkingGroup/javapos-config-loader/pull/7. In addition to requested changes, string constant "jpos.xml" has been replaced by constant DEFAULT_XML_FILE_NAME in method save. --- .../config/simple/xml/JavaxRegPopulator.java | 509 ++++++++---------- 1 file changed, 223 insertions(+), 286 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index 8d0e85b..1da2754 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -27,17 +27,19 @@ import jpos.util.tracing.TracerFactory; import org.w3c.dom.*; import org.xml.sax.*; -import org.xml.sax.helpers.DefaultHandler; +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.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; +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; @@ -45,237 +47,197 @@ /** * Simple implementation of the JposRegPopulator that loads and saves - * the entries in XML using Javax api. + * the entries in XML using 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 as * the jpos.config.regPopulatorClass * @see jpos.util.JposProperties#JPOS_REG_POPULATOR_CLASS_PROP_NAME - * @since 1.16 + * @since 4.0 * @author M. Conrad (martin.conrad@freenet.de) */ public class JavaxRegPopulator extends AbstractRegPopulator - implements XmlRegPopulator -{ + implements XmlRegPopulator { /** * Default ctor */ - public JavaxRegPopulator() - { super( JavaxRegPopulator.class.getName() ); } + public JavaxRegPopulator() { + super(JavaxRegPopulator.class.getName()); + } /** * 1-arg constructor that takes the unique ID * * @param s the unique ID string - * @since 1.3 (Washington DC 2001) + * @since 1.3 */ - public JavaxRegPopulator( String s ) - { super(s); } + public JavaxRegPopulator(String s) { + super(s); + } //------------------------------------------------------------------------- // Public methods // @Override - public String getClassName() { return JavaxRegPopulator.class.getName(); } + public String getClassName() { + return JavaxRegPopulator.class.getName(); + } @Override - public URL getEntriesURL() - { + 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() ); + 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() ) ); + url = createURLFromFile(new File(getPopulatorFileName())); - // - tracer.println( "getPopulatorFileURL()=" + getPopulatorFileURL() ); - tracer.println( "getPopulatorFileName()=" + getPopulatorFileName() ); - // + tracer.println("getPopulatorFileURL()=" + getPopulatorFileURL()); + tracer.println("getPopulatorFileName()=" + getPopulatorFileName()); return url; } @Override - public void save( Enumeration entries ) - throws Exception - { - if( isPopulatorFileDefined() ) - save(entries, getPopulatorFileOS()); + public void save(Enumeration entries) + throws Exception { + if (isPopulatorFileDefined()) + save(entries, getPopulatorFileOS()); else - try( FileOutputStream os = new FileOutputStream( "jpos.xml" ) ) - { save( entries, os ); } - catch (Exception e) - { throw e; } + try (FileOutputStream os = new FileOutputStream(DEFAULT_XML_FILE_NAME)) { + save(entries, os); + } } @Override - public void save( Enumeration entries, String fileName ) - throws Exception - { - try( FileOutputStream os = new FileOutputStream( fileName ) ) - { save(entries, os); } - catch ( Exception e ) - { throw e; } + public void save(Enumeration entries, String fileName) + throws Exception { + 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() ); + 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 ) - { - 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() ); + public void load(String fileName) { + 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; } + public String getName() { + return JAVAX_REG_POPULATOR_NAME_STRING; + } //-------------------------------------------------------------------------- - // Protected methods + // Private methods // + private void save(Enumeration entries, OutputStream outputStream) + throws ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException { + Document document = createEmptyDocument(); - /** @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; } - - private void save( Enumeration entries, OutputStream outputStream ) - throws Exception - { - Document document = CreateEmptyDocument(); - - insertJposEntriesInDoc( entries, document ); - insertDateSavedComment( document ); + insertJposEntriesInDoc(entries, document); + insertDateSavedComment(document); - DOMSource source = new DOMSource( document ); - Transformer transformer = TransformerFactory.newInstance().newTransformer(); + DOMSource source = new DOMSource(document); + TransformerFactory factory = TransformerFactory.newInstance(); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "file,jar:file"); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file,jar:file"); + 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 ) ); + 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 - { + private Document createEmptyDocument() + throws ParserConfigurationException { DOMImplementation domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); - DocumentType docType = domImpl.createDocumentType( "JposEntries", DTD_DOC_TYPE_VALUE, DTD_FILE_NAME ); + DocumentType docType = domImpl.createDocumentType("JposEntries", DTD_DOC_TYPE_VALUE, DTD_FILE_NAME); return domImpl.createDocument(null, "JposEntries", docType); } - private void insertJposEntriesInDoc( Enumeration entries, Document doc ) - { - while( entries.hasMoreElements() ) - { - JposEntry jposEntry = (JposEntry) entries.nextElement(); + private void insertJposEntriesInDoc(Enumeration entries, Document doc) { + while (entries.hasMoreElements()) { + JposEntry jposEntry = entries.nextElement(); - if( JposEntryUtility.isValidJposEntry( jposEntry ) ) - { - Element jposEntryElement = doc.createElement( "JposEntry" ); + if (JposEntryUtility.isValidJposEntry(jposEntry)) { + Element jposEntryElement = doc.createElement("JposEntry"); - jposEntryElement.setAttribute( "logicalName", jposEntry.getLogicalName() ); - insertJposPropertiesInElement( doc, jposEntry, jposEntryElement ); - doc.getDocumentElement().appendChild( jposEntryElement ); + jposEntryElement.setAttribute("logicalName", jposEntry.getLogicalName()); + insertJposPropertiesInElement(doc, jposEntry, jposEntryElement); + doc.getDocumentElement().appendChild(jposEntryElement); } } } - private void insertJposPropertiesInElement( Document doc, JposEntry jposEntry, Element jposEntryElement ) - { - Element creation = doc.createElement( "creation" ); - Element jpos = doc.createElement( "jpos" ); - Element product = doc.createElement( "product" ); - Element vendor = doc.createElement( "vendor" ); + private void insertJposPropertiesInElement(Document doc, JposEntry jposEntry, Element jposEntryElement) { + Element creation = doc.createElement("creation"); + Element jpos = doc.createElement("jpos"); + Element product = doc.createElement("product"); + Element vendor = doc.createElement("vendor"); - for( Element tag : new Element[] { creation, vendor, jpos, product } ) + for (Element tag : new Element[]{creation, vendor, jpos, product}) jposEntryElement.appendChild(tag); - List sortedProps = getSortedList( jposEntry.getProps() ); - for( JposEntry.Prop prop : sortedProps ) - { - if( notAttribute( prop, creation, JposEntry.SERVICE_CLASS_PROP_NAME, "serviceClass" ) && - notAttribute( prop, creation, JposEntry.SI_FACTORY_CLASS_PROP_NAME, "factoryClass" ) && - notAttribute( prop, vendor, JposEntry.VENDOR_NAME_PROP_NAME, "name" ) && - notAttribute( prop, vendor, JposEntry.VENDOR_URL_PROP_NAME, "url" ) && - notAttribute( prop, jpos, JposEntry.JPOS_VERSION_PROP_NAME, "version" ) && - notAttribute( prop, jpos, JposEntry.DEVICE_CATEGORY_PROP_NAME, "category" ) && - notAttribute( prop, product, JposEntry.PRODUCT_NAME_PROP_NAME, "name" ) && - notAttribute( prop, product, JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, "description" ) && - notAttribute( prop, product, JposEntry.PRODUCT_URL_PROP_NAME, "url" ) + List sortedProps = getSortedList(jposEntry.getProps()); + for (JposEntry.Prop prop : sortedProps) { + if (notAttribute(prop, creation, JposEntry.SERVICE_CLASS_PROP_NAME, "serviceClass") && + notAttribute(prop, creation, JposEntry.SI_FACTORY_CLASS_PROP_NAME, "factoryClass") && + notAttribute(prop, vendor, JposEntry.VENDOR_NAME_PROP_NAME, "name") && + notAttribute(prop, vendor, JposEntry.VENDOR_URL_PROP_NAME, "url") && + notAttribute(prop, jpos, JposEntry.JPOS_VERSION_PROP_NAME, "version") && + notAttribute(prop, jpos, JposEntry.DEVICE_CATEGORY_PROP_NAME, "category") && + notAttribute(prop, product, JposEntry.PRODUCT_NAME_PROP_NAME, "name") && + notAttribute(prop, product, JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, "description") && + notAttribute(prop, product, JposEntry.PRODUCT_URL_PROP_NAME, "url") ) - addPropElement( doc, jposEntryElement, prop ); + addPropElement(doc, jposEntryElement, prop); } } - private List getSortedList( Iterator props ) - { + private List getSortedList(Iterator props) { ArrayList sortedProps = new ArrayList<>(); - while( props.hasNext() ) - { - JposEntry.Prop entry = (JposEntry.Prop) props.next(); - int lowBound = 0; - int highBound = sortedProps.size() - 1; - - while( lowBound <= highBound ) - { - int middle = (lowBound + highBound) / 2; - int compareresult = entry.getName().compareToIgnoreCase( sortedProps.get( middle ).getName() ); - if( compareresult == 0 ) - lowBound = highBound = middle; - else if ( compareresult > 0 ) - lowBound = middle + 1; - else - highBound = middle - 1; + props.forEachRemaining(sortedProps::add); + Collections.sort(sortedProps, new Comparator() { + @Override + public int compare(JposEntry.Prop o1, JposEntry.Prop o2) { + return o1.getName().compareToIgnoreCase(o2.getName()); } - sortedProps.add( lowBound, entry ); - } + }); return sortedProps; } - private boolean notAttribute( JposEntry.Prop prop, Element elem, String propName, String attrName ) - { - if( prop.getName().equals( propName ) ) - { + private boolean notAttribute(JposEntry.Prop prop, Element elem, String propName, String attrName) { + if (prop.getName().equals(propName)) { String value = prop.getValueAsString(); /* Change /* to //* to generate implicit attribute values only if value is not empty. if( value.length() > 0 || @@ -284,63 +246,61 @@ private boolean notAttribute( JposEntry.Prop prop, Element elem, String propName ) ) //*/ - elem.setAttribute( attrName, prop.getValueAsString() ); + elem.setAttribute(attrName, prop.getValueAsString()); return false; } return true; } - private static void addPropElement( Document doc, Element jposEntryElement, JposEntry.Prop prop ) - { - Element more = doc.createElement( "prop" ); - jposEntryElement.appendChild( more ); - more.setAttribute( "name", prop.getName() ); - more.setAttribute( "value", prop.getValueAsString() ); - if( !( prop.getValue() instanceof String ) ) - more.setAttribute( "type", prop.getValue().getClass().getSimpleName() ); + private static void addPropElement(Document doc, Element jposEntryElement, JposEntry.Prop prop) { + Element propElement = doc.createElement("prop"); + jposEntryElement.appendChild(propElement); + propElement.setAttribute("name", prop.getName()); + propElement.setAttribute("value", prop.getValueAsString()); + if (!(prop.getValue() instanceof String)) + propElement.setAttribute("type", prop.getValue().getClass().getSimpleName()); } - private void insertDateSavedComment( Document document ) - { - String dateString = DateFormat.getInstance().format( new Date( System.currentTimeMillis() ) ); - String commentString = "Saved by JavaPOS jpos.config/loader (JCL) version " + Version.getVersionString() + private 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() ); + Comment comment = document.createComment(commentString); + document.getDocumentElement().insertBefore(comment, document.getDocumentElement().getFirstChild()); } - private void load( InputStream inputStream ) - { + private void load(InputStream inputStream) { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); JavaxSaxHandler saxHandler = new JavaxSaxHandler(); - - parserFactory.setNamespaceAware( true ); - parserFactory.setValidating( true ); - jposEntryList.clear(); - try - { - parserFactory.newSAXParser().parse( inputStream, saxHandler ); + 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); + } catch (Exception e) { + parserFactory.setValidating(true); } - catch (SAXException e) - { - tracer.println( "SAX Parser error, msg=" + e.getMessage() ); + parserFactory.setNamespaceAware(true); + jposEntryList.clear(); + try { + SAXParser parser = parserFactory.newSAXParser(); + parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "file"); + parser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file,jar:file"); + 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() ); + } 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() ); + } catch (ParserConfigurationException e) { + tracer.println("SAX Parser configuration error, msg=" + e.getMessage()); lastLoadException = e; } - Iterator entries = jposEntryList.iterator(); - while( entries.hasNext() ) - { - JposEntry jposEntry = (JposEntry) entries.next(); - getJposEntries().put( jposEntry.getLogicalName(), jposEntry ); + Iterator entries = jposEntryList.iterator(); + while (entries.hasNext()) { + JposEntry jposEntry = entries.next(); + getJposEntries().put(jposEntry.getLogicalName(), jposEntry); } } @@ -349,98 +309,79 @@ private void load( InputStream inputStream ) // private class JavaxSaxHandler - extends DefaultHandler - { - private JposEntry CurrentEntry = null; - private SAXException TheException = null; + extends DefaultHandler2 { + private JposEntry currentEntry = null; + private SAXException theException = null; //---------------------------------------------------------------- // ContentHandler @Override - public void startElement( String uri, String lname, String qname, Attributes attrs ) - { - tracer.print( "StartElement: "+ qname ); - if( TheException != null ) - { - tracer.println( ": Parse error: " + TheException.getMessage() ); - lastLoadException = TheException; - TheException = null; + public void startElement(String uri, String lname, String qname, Attributes attrs) { + tracer.print("StartElement: " + qname); + if (theException != null) { + tracer.println(": Parse error: " + theException.getMessage()); + lastLoadException = theException; + theException = null; return; } - if( qname.equals( "JposEntries" ) ) + if (qname.equals("JposEntries")) jposEntryList.clear(); - else if( qname.equals( "JposEntry" ) ) - CurrentEntry = new SimpleEntry(attrs.getValue("logicalName"), JavaxRegPopulator.this); - else if( CurrentEntry != null ) - { + else if (qname.equals("JposEntry")) + currentEntry = new SimpleEntry(attrs.getValue("logicalName"), JavaxRegPopulator.this); + else if (currentEntry != null) { String temp; - if( qname.equals( "creation" ) ) - { - CurrentEntry.addProperty( JposEntry.SI_FACTORY_CLASS_PROP_NAME, attrs.getValue( "factoryClass" ) ); - CurrentEntry.addProperty( JposEntry.SERVICE_CLASS_PROP_NAME, attrs.getValue( "serviceClass" ) ); - } - else if( qname.equals( "vendor" ) ) - { - CurrentEntry.addProperty( JposEntry.VENDOR_NAME_PROP_NAME, attrs.getValue( "name" ) ); - addOptionalProperty( JposEntry.VENDOR_URL_PROP_NAME, attrs.getValue( "url" ) ); - } - else if( qname.equals( "jpos" ) ) - { - CurrentEntry.addProperty( JposEntry.JPOS_VERSION_PROP_NAME, attrs.getValue( "version" ) ); - CurrentEntry.addProperty( JposEntry.DEVICE_CATEGORY_PROP_NAME, attrs.getValue( "category" ) ); - } - else if( qname.equals( "product" ) ) - { - CurrentEntry.addProperty( JposEntry.PRODUCT_NAME_PROP_NAME, attrs.getValue( "name" ) ); - CurrentEntry.addProperty( JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, attrs.getValue( "description" ) ); - addOptionalProperty( JposEntry.PRODUCT_URL_PROP_NAME, attrs.getValue( "url" ) ); - } - else if( qname.equals( "prop" ) ) - { - temp = attrs.getValue( "type" ); - try - { - Class type = temp == null ? String.class : Class.forName( "java.lang." + temp ); + if (qname.equals("creation")) { + currentEntry.addProperty(JposEntry.SI_FACTORY_CLASS_PROP_NAME, attrs.getValue("factoryClass")); + currentEntry.addProperty(JposEntry.SERVICE_CLASS_PROP_NAME, attrs.getValue("serviceClass")); + } else if (qname.equals("vendor")) { + currentEntry.addProperty(JposEntry.VENDOR_NAME_PROP_NAME, attrs.getValue("name")); + addOptionalProperty(JposEntry.VENDOR_URL_PROP_NAME, attrs.getValue("url")); + } else if (qname.equals("jpos")) { + currentEntry.addProperty(JposEntry.JPOS_VERSION_PROP_NAME, attrs.getValue("version")); + currentEntry.addProperty(JposEntry.DEVICE_CATEGORY_PROP_NAME, attrs.getValue("category")); + } else if (qname.equals("product")) { + currentEntry.addProperty(JposEntry.PRODUCT_NAME_PROP_NAME, attrs.getValue("name")); + currentEntry.addProperty(JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, attrs.getValue("description")); + addOptionalProperty(JposEntry.PRODUCT_URL_PROP_NAME, attrs.getValue("url")); + } else if (qname.equals("prop")) { + temp = attrs.getValue("type"); + try { + Class type = temp == null ? String.class : Class.forName("java.lang." + temp); Object obj = null; - obj = JposEntryUtility.parsePropValue( attrs.getValue( "value" ), type ); - CurrentEntry.addProperty( attrs.getValue( "name" ), obj ); + obj = JposEntryUtility.parsePropValue(attrs.getValue("value"), type); + currentEntry.addProperty(attrs.getValue("name"), obj); + } catch (Exception e) { + currentEntry = null; + String msg = "Invalid prop: name=" + attrs.getValue("name") + + ":value=" + attrs.getValue("value"); + tracer.println(": " + msg); + lastLoadException = new SAXException(msg, e); } - catch( Exception e ) - { - CurrentEntry = null; - String msg = "Invalid prop: name=" + attrs.getValue( "name" ) - + ":value=" + attrs.getValue( "value" ); - tracer.println( ": " + msg ); - lastLoadException = new SAXException( msg, e ); - }; + ; } } - tracer.println( "" ); + tracer.println(""); } - private void addOptionalProperty( String name, String value ) - { - CurrentEntry.addProperty( name, value == null ? "" : value ); + 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( "EndElement: " + qname ); - else - { - tracer.println( "EndElement: " + TheException.getMessage() ); - TheException = null; + public void endElement(String uri, String lname, String qname) { + if (theException == null) + tracer.println("EndElement: " + qname); + else { + tracer.println("EndElement: " + theException.getMessage()); + theException = null; } - if( qname.equals( "JposEntry" ) ) - { - if( CurrentEntry != null ) - jposEntryList.add( CurrentEntry ); - CurrentEntry = null; - TheException = null; + if (qname.equals("JposEntry")) { + if (currentEntry != null) + jposEntryList.add(currentEntry); + currentEntry = null; + theException = null; } } @@ -448,41 +389,37 @@ public void endElement( String uri, String lname, String qname ) // ErrorHandler @Override - public void error( SAXParseException e ) - { - CurrentEntry = null; - TheException = e; + public void error(SAXParseException e) { + currentEntry = null; + theException = e; } @Override - public void fatalError( SAXParseException e ) - { - CurrentEntry = null; - TheException = e; + public void fatalError(SAXParseException e) { + currentEntry = null; + theException = e; } //---------------------------------------------------------------- // EntityResolver @Override - public InputSource resolveEntity(String publicId, String systemId ) - { - tracer.println( "JposEntityResolver:resolveEntity:publicId=" + - publicId ); + public InputSource resolveEntity(String name, String publicId, String uri, String systemId) { + tracer.println("JposEntityResolver:resolveEntity:publicId=" + + publicId); - tracer.println( "JposEntityResolver:resolveEntity:systemId=" + - systemId ); + tracer.println("JposEntityResolver:resolveEntity:systemId=" + + systemId); - if( publicId.equals( DTD_DOC_TYPE_VALUE ) ) - { + if (publicId.equals(DTD_DOC_TYPE_VALUE)) { InputStream is = - getClass().getResourceAsStream( DTD_FILE_NAME ); + getClass().getResourceAsStream(DTD_FILE_NAME); - if ( is == null ) - is = findFileInClasspath( DTD_FILE_NAME ); + if (is == null) + is = findFileInClasspath(DTD_FILE_NAME); - if( is != null ) - return new InputSource( new InputStreamReader( is ) ); + if (is != null) + return new InputSource(new InputStreamReader(is)); } return null; @@ -493,12 +430,10 @@ public InputSource resolveEntity(String publicId, String systemId ) // Instance variables // - protected String xmlFileName = DEFAULT_XML_FILE_NAME; - - private List jposEntryList = new LinkedList(); + private List jposEntryList = new LinkedList<>(); private Tracer tracer = TracerFactory.getInstance(). - createTracer( this.getClass().getSimpleName() ); + createTracer(this.getClass().getSimpleName()); //-------------------------------------------------------------------------- // Constants @@ -507,6 +442,8 @@ public InputSource resolveEntity(String publicId, String systemId ) public static final String DTD_FILE_PATH = "jpos/res"; public static final String DTD_FILE_NAME = DTD_FILE_PATH + "/jcl.dtd"; + public static final String XSD_FILE_NAME = DTD_FILE_PATH + "/jcl.xsd"; + public static final String DTD_DOC_TYPE_VALUE = "-//JavaPOS//DTD//EN"; private static final String JAVAX_REG_POPULATOR_NAME_STRING = "JAVAX XML Entries Populator"; From f005370bdb4d53a09c63b45d7f52c503a71ff7fb Mon Sep 17 00:00:00 2001 From: mjpcger <37701263+mjpcger@users.noreply.github.com> Date: Fri, 15 Mar 2024 18:51:43 +0100 Subject: [PATCH 14/48] Refactored out string as constants to XmlRegPopulator - Constants for path and names of DTD and XSD file and for the DTD document type value moved to XmlRegPopulator. - Constants for XML tag and attribute values added to XmlRegPopulator. - "Unchecked" warning suppressed for some methods. - Method getSortedList removed. Use Collections.sort directly instead. - Method notAttribute renamed to cannotSetAttributeOfTag for better understanding. - Comments for method cannotSetAttributeOfTag added. - LinkedList replaced by ArrayList where meaningful and vice versa. --- .../config/simple/xml/JavaxRegPopulator.java | 150 ++++++++++-------- .../config/simple/xml/XmlRegPopulator.java | 119 ++++++++++++++ 2 files changed, 200 insertions(+), 69 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index 1da2754..7ccce60 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -105,7 +105,7 @@ public URL getEntriesURL() { return url; } - + @SuppressWarnings("unchecked") @Override public void save(Enumeration entries) throws Exception { @@ -117,6 +117,7 @@ public void save(Enumeration entries) } } + @SuppressWarnings("unchecked") @Override public void save(Enumeration entries, String fileName) throws Exception { @@ -166,7 +167,7 @@ private void save(Enumeration entries, OutputStream outputStream) DOMSource source = new DOMSource(document); TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "file,jar:file"); - factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file,jar:file"); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "file,jar:file"); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); @@ -179,9 +180,9 @@ private void save(Enumeration entries, OutputStream outputStream) private Document createEmptyDocument() throws ParserConfigurationException { DOMImplementation domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); - DocumentType docType = domImpl.createDocumentType("JposEntries", DTD_DOC_TYPE_VALUE, DTD_FILE_NAME); + DocumentType docType = domImpl.createDocumentType(XML_TAG_JPOSENTRIES, DTD_DOC_TYPE_VALUE, DTD_FILE_NAME); - return domImpl.createDocument(null, "JposEntries", docType); + return domImpl.createDocument(null, XML_TAG_JPOSENTRIES, docType); } @@ -190,7 +191,7 @@ private void insertJposEntriesInDoc(Enumeration entries, Document doc JposEntry jposEntry = entries.nextElement(); if (JposEntryUtility.isValidJposEntry(jposEntry)) { - Element jposEntryElement = doc.createElement("JposEntry"); + Element jposEntryElement = doc.createElement(XML_TAG_JPOSENTRY); jposEntryElement.setAttribute("logicalName", jposEntry.getLogicalName()); insertJposPropertiesInElement(doc, jposEntry, jposEntryElement); @@ -199,66 +200,84 @@ private void insertJposEntriesInDoc(Enumeration entries, Document doc } } + /** + * Insert JposEntry tag for a given JposEntry object into an XML document. For better readability, first the + * tags creation, jpos, product and vendor will be created, followed by (optional) prop tags in alphabetic + * order.
+ * Keep in mind that elements of the JposEntry object represent either an attribute of tag creation, jpos, product + * or vendor or a complete prop tag. Method checkAndSetAttributeOfTagIsTrue will be used to add the specific + * attribute into the corresponding tag if the element represents the specified attribute. If + * checkAndSetAttributeOfTagIsTrue fails for all specified attributes, a prop tag will be created and added to + * JposEntry tag. + * @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("creation"); - Element jpos = doc.createElement("jpos"); - Element product = doc.createElement("product"); - Element vendor = doc.createElement("vendor"); + 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 = getSortedList(jposEntry.getProps()); + List sortedProps = new LinkedList<>(); + ((Iterator)jposEntry.getProps()).forEachRemaining(sortedProps::add); + Collections.sort(sortedProps, + (JposEntry.Prop p1, JposEntry.Prop p2) -> p1.getName().compareToIgnoreCase(p2.getName())); + for (JposEntry.Prop prop : sortedProps) { - if (notAttribute(prop, creation, JposEntry.SERVICE_CLASS_PROP_NAME, "serviceClass") && - notAttribute(prop, creation, JposEntry.SI_FACTORY_CLASS_PROP_NAME, "factoryClass") && - notAttribute(prop, vendor, JposEntry.VENDOR_NAME_PROP_NAME, "name") && - notAttribute(prop, vendor, JposEntry.VENDOR_URL_PROP_NAME, "url") && - notAttribute(prop, jpos, JposEntry.JPOS_VERSION_PROP_NAME, "version") && - notAttribute(prop, jpos, JposEntry.DEVICE_CATEGORY_PROP_NAME, "category") && - notAttribute(prop, product, JposEntry.PRODUCT_NAME_PROP_NAME, "name") && - notAttribute(prop, product, JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, "description") && - notAttribute(prop, product, JposEntry.PRODUCT_URL_PROP_NAME, "url") + if (cannotSetAttributeOfTag(prop, creation, JposEntry.SERVICE_CLASS_PROP_NAME, XML_ATTR_SERVICECLASS) && + cannotSetAttributeOfTag(prop, creation, JposEntry.SI_FACTORY_CLASS_PROP_NAME, XML_ATTR_FACTORYCLASS) && + cannotSetAttributeOfTag(prop, vendor, JposEntry.VENDOR_NAME_PROP_NAME, XML_ATTR_NAME) && + cannotSetAttributeOfTag(prop, vendor, JposEntry.VENDOR_URL_PROP_NAME, XML_ATTR_URL) && + cannotSetAttributeOfTag(prop, jpos, JposEntry.JPOS_VERSION_PROP_NAME, XML_ATTR_VERSION) && + cannotSetAttributeOfTag(prop, jpos, JposEntry.DEVICE_CATEGORY_PROP_NAME, XML_ATTR_CATEGORY) && + cannotSetAttributeOfTag(prop, product, JposEntry.PRODUCT_NAME_PROP_NAME, XML_ATTR_NAME) && + cannotSetAttributeOfTag(prop, product, JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, XML_ATTR_DESCRIPTION) && + cannotSetAttributeOfTag(prop, product, JposEntry.PRODUCT_URL_PROP_NAME, XML_ATTR_URL) ) addPropElement(doc, jposEntryElement, prop); } } - private List getSortedList(Iterator props) { - ArrayList sortedProps = new ArrayList<>(); - props.forEachRemaining(sortedProps::add); - Collections.sort(sortedProps, new Comparator() { - @Override - public int compare(JposEntry.Prop o1, JposEntry.Prop o2) { - return o1.getName().compareToIgnoreCase(o2.getName()); - } - }); - return sortedProps; - } - - private boolean notAttribute(JposEntry.Prop prop, Element elem, String propName, String attrName) { + /** + * Checks whether the given JposEntry element represents an attribute of the given tag. If so, the attribute will be + * added to the tag. This method returns true if the element did not represent the specified attribute. + * @param prop Prop element of an JposEntry object. + * @param elem XML tag to be used to store the specified attribute, if prop represents thit attribute. + * @param propName Name of the Prop element. + * @param attrName Attribute name to be used in XML tag. + * @return true if prop does not represent the specified attribute and no attribute has been added to elem. false + * if the specified attribute has been added to elem. + */ + private boolean cannotSetAttributeOfTag(JposEntry.Prop prop, Element elem, String propName, String attrName) { if (prop.getName().equals(propName)) { - String value = prop.getValueAsString(); /* Change /* to //* to generate implicit attribute values only if value is not empty. + String value = prop.getValueAsString(); if( value.length() > 0 || ( !JposEntry.PRODUCT_URL_PROP_NAME.equals(propName) && !JposEntry.VENDOR_URL_PROP_NAME.equals(propName) ) ) - //*/ - elem.setAttribute(attrName, prop.getValueAsString()); + //*/ + { + elem.setAttribute(attrName, prop.getValueAsString()); + } return false; } return true; } private static void addPropElement(Document doc, Element jposEntryElement, JposEntry.Prop prop) { - Element propElement = doc.createElement("prop"); + Element propElement = doc.createElement(XML_TAG_PROP); jposEntryElement.appendChild(propElement); - propElement.setAttribute("name", prop.getName()); - propElement.setAttribute("value", prop.getValueAsString()); + propElement.setAttribute(XML_ATTR_NAME, prop.getName()); + propElement.setAttribute(XML_ATTR_VALUE, prop.getValueAsString()); if (!(prop.getValue() instanceof String)) - propElement.setAttribute("type", prop.getValue().getClass().getSimpleName()); + propElement.setAttribute(XML_ATTR_TYPE, prop.getValue().getClass().getSimpleName()); } private void insertDateSavedComment(Document document) { @@ -325,37 +344,37 @@ public void startElement(String uri, String lname, String qname, Attributes attr theException = null; return; } - if (qname.equals("JposEntries")) + if (qname.equals(XML_TAG_JPOSENTRIES)) jposEntryList.clear(); - else if (qname.equals("JposEntry")) + else if (qname.equals(XML_TAG_JPOSENTRY)) currentEntry = new SimpleEntry(attrs.getValue("logicalName"), JavaxRegPopulator.this); else if (currentEntry != null) { String temp; - if (qname.equals("creation")) { - currentEntry.addProperty(JposEntry.SI_FACTORY_CLASS_PROP_NAME, attrs.getValue("factoryClass")); - currentEntry.addProperty(JposEntry.SERVICE_CLASS_PROP_NAME, attrs.getValue("serviceClass")); - } else if (qname.equals("vendor")) { - currentEntry.addProperty(JposEntry.VENDOR_NAME_PROP_NAME, attrs.getValue("name")); - addOptionalProperty(JposEntry.VENDOR_URL_PROP_NAME, attrs.getValue("url")); - } else if (qname.equals("jpos")) { - currentEntry.addProperty(JposEntry.JPOS_VERSION_PROP_NAME, attrs.getValue("version")); - currentEntry.addProperty(JposEntry.DEVICE_CATEGORY_PROP_NAME, attrs.getValue("category")); - } else if (qname.equals("product")) { - currentEntry.addProperty(JposEntry.PRODUCT_NAME_PROP_NAME, attrs.getValue("name")); - currentEntry.addProperty(JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, attrs.getValue("description")); - addOptionalProperty(JposEntry.PRODUCT_URL_PROP_NAME, attrs.getValue("url")); - } else if (qname.equals("prop")) { - temp = attrs.getValue("type"); + 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)) { + temp = attrs.getValue(XML_ATTR_TYPE); try { Class type = temp == null ? String.class : Class.forName("java.lang." + temp); Object obj = null; - obj = JposEntryUtility.parsePropValue(attrs.getValue("value"), type); - currentEntry.addProperty(attrs.getValue("name"), obj); + 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("name") - + ":value=" + attrs.getValue("value"); + String msg = "Invalid prop: name=" + attrs.getValue(XML_ATTR_NAME) + + ":value=" + attrs.getValue(XML_ATTR_VALUE); tracer.println(": " + msg); lastLoadException = new SAXException(msg, e); } @@ -377,7 +396,7 @@ public void endElement(String uri, String lname, String qname) { tracer.println("EndElement: " + theException.getMessage()); theException = null; } - if (qname.equals("JposEntry")) { + if (qname.equals(XML_TAG_JPOSENTRY)) { if (currentEntry != null) jposEntryList.add(currentEntry); currentEntry = null; @@ -430,7 +449,7 @@ public InputSource resolveEntity(String name, String publicId, String uri, Strin // Instance variables // - private List jposEntryList = new LinkedList<>(); + private List jposEntryList = new ArrayList<>(); private Tracer tracer = TracerFactory.getInstance(). createTracer(this.getClass().getSimpleName()); @@ -439,12 +458,5 @@ public InputSource resolveEntity(String name, String publicId, String uri, Strin // Constants // - public static final String DTD_FILE_PATH = "jpos/res"; - public static final String DTD_FILE_NAME = DTD_FILE_PATH + "/jcl.dtd"; - - public static final String XSD_FILE_NAME = DTD_FILE_PATH + "/jcl.xsd"; - - public static final String DTD_DOC_TYPE_VALUE = "-//JavaPOS//DTD//EN"; - private static final String JAVAX_REG_POPULATOR_NAME_STRING = "JAVAX XML Entries Populator"; } 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 From fd0a2f64a15cc478db7a6c1691cbc66a5ad1e6ad Mon Sep 17 00:00:00 2001 From: mjpcger <37701263+mjpcger@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:10:13 +0100 Subject: [PATCH 15/48] Replaced Xerces populator by new Javax populator. The default SimpleXmlRegPopulator has been changed to use JavaxRegPopulator instead of XercesRegPoputator now. This allows to remove Xerces completely later on. --- src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java b/src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java index 6032fc9..b3cc749 100644 --- a/src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/SimpleXmlRegPopulator.java @@ -129,5 +129,5 @@ public void save( Enumeration entries, String fileName ) throws Exception // Instance variables // - private XmlRegPopulator regPopulator = new XercesRegPopulator(); + private XmlRegPopulator regPopulator = new JavaxRegPopulator(); } From ef2e594a61485af7f271357231dd714c0fbd129b Mon Sep 17 00:00:00 2001 From: mjpcger <37701263+mjpcger@users.noreply.github.com> Date: Fri, 28 Jun 2024 15:55:01 +0200 Subject: [PATCH 16/48] Code cleanup: get rid of some warnings --- .../jpos/config/simple/xml/JavaxRegPopulator.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index 7ccce60..b538381 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -106,8 +106,8 @@ public URL getEntriesURL() { } @SuppressWarnings("unchecked") - @Override - public void save(Enumeration entries) + @Override + public void save(@SuppressWarnings("rawtypes") Enumeration entries) throws Exception { if (isPopulatorFileDefined()) save(entries, getPopulatorFileOS()); @@ -119,7 +119,7 @@ public void save(Enumeration entries) @SuppressWarnings("unchecked") @Override - public void save(Enumeration entries, String fileName) + public void save(@SuppressWarnings("rawtypes") Enumeration entries, String fileName) throws Exception { try (FileOutputStream os = new FileOutputStream(fileName)) { save(entries, os); @@ -224,7 +224,9 @@ private void insertJposPropertiesInElement(Document doc, JposEntry jposEntry, El jposEntryElement.appendChild(tag); List sortedProps = new LinkedList<>(); - ((Iterator)jposEntry.getProps()).forEachRemaining(sortedProps::add); + @SuppressWarnings("unchecked") + Iterator props = jposEntry.getProps(); + props.forEachRemaining(sortedProps::add); Collections.sort(sortedProps, (JposEntry.Prop p1, JposEntry.Prop p2) -> p1.getName().compareToIgnoreCase(p2.getName())); @@ -378,7 +380,6 @@ else if (currentEntry != null) { tracer.println(": " + msg); lastLoadException = new SAXException(msg, e); } - ; } } tracer.println(""); From 881797ba2504cb6f4cb20c48c7383b272cd9ceb5 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 28 Jun 2024 16:20:09 +0200 Subject: [PATCH 17/48] Corrected an initialization error when reloading to the same populator instance a different file. Solved by clearing the internal data structure, which was missing. Detected by adding a copy of XercesRegPopulatorTestCase which instantiates JavaxRegPopulator and fails for test jpos.config.simple.xml.JavaxRegPopulatorTestCase.testXercesPopulator1(). --- .../config/simple/xml/JavaxRegPopulator.java | 1 + .../simple/xml/JavaxRegPopulatorTestCase.java | 314 ++++++++++++++++++ 2 files changed, 315 insertions(+) create mode 100644 src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index b538381..9c50041 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -303,6 +303,7 @@ private void load(InputStream inputStream) { } parserFactory.setNamespaceAware(true); jposEntryList.clear(); + getJposEntries().clear(); try { SAXParser parser = parserFactory.newSAXParser(); parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "file"); 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..57c25b9 --- /dev/null +++ b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java @@ -0,0 +1,314 @@ +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() + { + xercesRegPopulator = 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() + { + 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 ); + + assertTrue( xercesRegPopulator.getName().startsWith("JAVAX XML Entries Populator") ); + } + + 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 JavaxRegPopulator 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 From bcd9ee08a14b4f30a0215e432ef0b948d920df7f Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 28 Jun 2024 16:33:55 +0200 Subject: [PATCH 18/48] Code cleanup for the new JavaxRegPopulator test case. --- .../simple/xml/JavaxRegPopulatorTestCase.java | 145 ++++++++---------- 1 file changed, 65 insertions(+), 80 deletions(-) diff --git a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java index 57c25b9..79be9aa 100644 --- a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java +++ b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java @@ -39,7 +39,7 @@ public class JavaxRegPopulatorTestCase extends AbstractRegPopulatorTestCase protected void setUp() { - xercesRegPopulator = new JavaxRegPopulator(); + javaxRegPopulator = new JavaxRegPopulator(); try { @@ -58,7 +58,7 @@ protected void setUp() protected void tearDown() { - xercesRegPopulator = null; + javaxRegPopulator = null; } //------------------------------------------------------------------------- @@ -87,44 +87,28 @@ private JposEntry createJposEntry( String logicalName, String factoryClass, 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 + // Public test methods // /** * Test the loading/saving of XML entries using the XercesRegPopulator */ - public void testXercesPopulator1() + public void testJavaxPopulatorReloadToTheSameInstance() { //Save and load an empty set of registry entries - Vector v1 = new Vector(); + List v1 = new ArrayList<>(); try { - xercesRegPopulator.save( v1.elements(), JCL_JUNIT_XML_FILE_NAME ); - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.save( Collections.enumeration(v1), JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - Enumeration entries = xercesRegPopulator.getEntries(); + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.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() ) ); + 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) ) ); } catch( Exception e ) { fail( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage() ); } @@ -147,15 +131,16 @@ public void testXercesPopulator1() try { v1.clear(); - v1.addElement( entry1 ); - v1.addElement( entry2 ); + v1.add( entry1 ); + v1.add( entry2 ); - xercesRegPopulator.save( v1.elements(), JCL_JUNIT_XML_FILE_NAME ); - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.save( Collections.enumeration(v1), JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - Enumeration entries = xercesRegPopulator.getEntries(); + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); - assertTrue( "Expected 2 entries...", JUnitUtility.isEquals( entries, v1.elements() ) ); + assertTrue( "Expected 2 entries...", JUnitUtility.isEquals( entries, Collections.enumeration(v1) ) ); } catch( Exception e ) { assertTrue( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage(), true ); } @@ -166,12 +151,13 @@ public void testXercesPopulator1() try { - xercesRegPopulator.save( v1.elements(), JCL_JUNIT_XML_FILE_NAME ); - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.save( Collections.enumeration(v1), JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - Enumeration entries = xercesRegPopulator.getEntries(); + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); - assertTrue( "Expected 1 entries...", JUnitUtility.isEquals( entries, v1.elements() ) ); + assertTrue( "Expected 1 entries...", JUnitUtility.isEquals( entries, Collections.enumeration(v1) ) ); } catch( Exception e ) { assertTrue( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage(), true ); } @@ -180,9 +166,9 @@ public void testXercesPopulator1() /** * Test the loading/saving of XML entries using the XercesRegPopulator */ - public void testXercesPopulator2() + public void testJavaxPopulator() { - Vector v1 = new Vector(); + List v1 = new ArrayList<>(); for( int i = 0; i < 100; i++ ) { @@ -193,17 +179,18 @@ public void testXercesPopulator2() "Virtual LineDisplay JavaPOS Service", "Example virtual LineDisplay JavaPOS Service from virtual Xyz Corporation", "http://www.javapos.com" ); - v1.addElement( entry ); + v1.add( entry ); } try { - xercesRegPopulator.save( v1.elements(), JCL_JUNIT_XML_FILE_NAME ); - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.save( Collections.enumeration(v1), JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - Enumeration entries = xercesRegPopulator.getEntries(); + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); - assertTrue( "Expected 100 entries...", JUnitUtility.isEquals( entries, v1.elements() ) ); + 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() ); } @@ -211,26 +198,27 @@ public void testXercesPopulator2() public void testGetName() { - xercesRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); + javaxRegPopulator.load( JCL_JUNIT_XML_FILE_NAME ); - assertTrue( xercesRegPopulator.getName().startsWith("JAVAX XML Entries Populator") ); + assertTrue( javaxRegPopulator.getName().startsWith("JAVAX XML Entries Populator") ); } - public void testLoad1() + public void testLoadDefect6562() { 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(); + javaxRegPopulator.load( DEFECT_6562_XML_FILE ); + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); JposEntry defect6562Entry = (JposEntry)entries.nextElement(); - assertTrue( "defect6562Entry == null", defect6562Entry != null ); - assertTrue( "defect6562Entry.logicalName != defect6562", - defect6562Entry.getLogicalName().equals( "defect6562" ) ); + assertNotNull( "defect6562Entry == null", defect6562Entry ); + assertEquals( "defect6562Entry.logicalName != defect6562", + "defect6562", defect6562Entry.getLogicalName() ); } catch( Exception e ) { fail( "Unexpected exception.message = " + e.getMessage() ); } @@ -243,45 +231,42 @@ public void testLoadwithPropType() 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(); + javaxRegPopulator.load( JCL_JUNIT_TEST_PROP_TYPE_XML_FILE ); + @SuppressWarnings("unchecked") + Enumeration entries = javaxRegPopulator.getEntries(); JposEntry testPropTypeEntry = (JposEntry)entries.nextElement(); - // - //System.out.println( testPropTypeEntry ); - // - - assertTrue( "testPropTypeEntry == null", testPropTypeEntry != null ); - assertTrue( "testPropTypeEntry.logicalName != testPropType", - testPropTypeEntry.getLogicalName().equals( "testPropType" ) ); + assertNotNull( "testPropTypeEntry == null", testPropTypeEntry ); + assertEquals( "testPropTypeEntry.logicalName != testPropType", + "testPropType", testPropTypeEntry.getLogicalName() ); - assertTrue( "testPropTypeEntry.getProp( \"stringProp\" ).getType() != String.class", - testPropTypeEntry.getProp( "stringProp" ).getType().equals( String.class ) ); + assertEquals( "testPropTypeEntry.getProp( \"stringProp\" ).getType() != String.class", + String.class, testPropTypeEntry.getProp( "stringProp" ).getType() ); - assertTrue( "testPropTypeEntry.getProp( \"booleanProp\" ).getType() != Boolean.class", - testPropTypeEntry.getProp( "booleanProp" ).getType().equals( Boolean.class ) ); + assertEquals( "testPropTypeEntry.getProp( \"booleanProp\" ).getType() != Boolean.class", + Boolean.class, testPropTypeEntry.getProp( "booleanProp" ).getType() ); - assertTrue( "testPropTypeEntry.getProp( \"byteProp\" ).getType() != Byte.class", - testPropTypeEntry.getProp( "byteProp" ).getType().equals( Byte.class ) ); + assertEquals( "testPropTypeEntry.getProp( \"byteProp\" ).getType() != Byte.class", + Byte.class, testPropTypeEntry.getProp( "byteProp" ).getType() ); - assertTrue( "testPropTypeEntry.getProp( \"characterProp\" ).getType() != Character.class", - testPropTypeEntry.getProp( "characterProp" ).getType().equals( Character.class ) ); + assertEquals( "testPropTypeEntry.getProp( \"characterProp\" ).getType() != Character.class", + Character.class, testPropTypeEntry.getProp( "characterProp" ).getType() ); - assertTrue( "testPropTypeEntry.getProp( \"doubleProp\" ).getType() != Double.class", - testPropTypeEntry.getProp( "doubleProp" ).getType().equals( Double.class ) ); + assertEquals( "testPropTypeEntry.getProp( \"doubleProp\" ).getType() != Double.class", + Double.class, testPropTypeEntry.getProp( "doubleProp" ).getType() ); - assertTrue( "testPropTypeEntry.getProp( \"floatProp\" ).getType() != Float.class", - testPropTypeEntry.getProp( "floatProp" ).getType().equals( Float.class ) ); + assertEquals( "testPropTypeEntry.getProp( \"floatProp\" ).getType() != Float.class", + Float.class, testPropTypeEntry.getProp( "floatProp" ).getType() ); - assertTrue( "testPropTypeEntry.getProp( \"integerProp\" ).getType() != Integer.class", - testPropTypeEntry.getProp( "integerProp" ).getType().equals( Integer.class ) ); + assertEquals( "testPropTypeEntry.getProp( \"integerProp\" ).getType() != Integer.class", + Integer.class, testPropTypeEntry.getProp( "integerProp" ).getType() ); - assertTrue( "testPropTypeEntry.getProp( \"longProp\" ).getType() != Long.class", - testPropTypeEntry.getProp( "longProp" ).getType().equals( Long.class ) ); + assertEquals( "testPropTypeEntry.getProp( \"longProp\" ).getType() != Long.class", + Long.class, testPropTypeEntry.getProp( "longProp" ).getType() ); - assertTrue( "testPropTypeEntry.getProp( \"shortProp\" ).getType() != Short.class", - testPropTypeEntry.getProp( "shortProp" ).getType().equals( Short.class ) ); + assertEquals( "testPropTypeEntry.getProp( \"shortProp\" ).getType() != Short.class", + Short.class, testPropTypeEntry.getProp( "shortProp" ).getType() ); } catch( Exception e ) { fail( "Unexpected exception.message = " + e.getMessage() ); } } @@ -300,7 +285,7 @@ public void testGetLastLoadException() // Instance variables // - private JavaxRegPopulator xercesRegPopulator = null; + private JavaxRegPopulator javaxRegPopulator = null; //------------------------------------------------------------------------- // Instance variables From e35f8ad4b8b6f85d26e61224a75a7f9be18d6e64 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 1 Jul 2024 13:53:15 +0200 Subject: [PATCH 19/48] Added assertions checking getLastLoadException is null. Some other source formatting cleanups. --- .../simple/xml/JavaxRegPopulatorTestCase.java | 76 ++++++++++--------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java index 79be9aa..da7b259 100644 --- a/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java +++ b/src/test/java/jpos/config/simple/xml/JavaxRegPopulatorTestCase.java @@ -103,33 +103,30 @@ public void testJavaxPopulatorReloadToTheSameInstance() { 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) ) ); - } - 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 - { + //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 ); @@ -137,30 +134,30 @@ public void testJavaxPopulatorReloadToTheSameInstance() 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(); + Enumeration entries2 = javaxRegPopulator.getEntries(); - assertTrue( "Expected 2 entries...", JUnitUtility.isEquals( entries, Collections.enumeration(v1) ) ); - } - catch( Exception e ) - { assertTrue( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage(), true ); } + assertTrue( "Expected 2 entries...", JUnitUtility.isEquals( entries2, Collections.enumeration(v1) ) ); - //Remove entries save and load reg - v1.remove( entry1 ); + //Remove entries save and load reg + v1.remove( entry1 ); - - 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(); + Enumeration entries3 = javaxRegPopulator.getEntries(); - assertTrue( "Expected 1 entries...", JUnitUtility.isEquals( entries, Collections.enumeration(v1) ) ); + 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 ); } + { + assertTrue( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage(), true ); + } } /** @@ -187,13 +184,17 @@ public void testJavaxPopulator() 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() ); } + { + fail( "Got unexpected Exception from XercesRegPopulator.save method with message = " + e.getMessage() ); + } } public void testGetName() @@ -211,6 +212,9 @@ public void testLoadDefect6562() ( new File( DEFECT_6562_XML_FILE ) ).exists() ); javaxRegPopulator.load( DEFECT_6562_XML_FILE ); + + assertNull(javaxRegPopulator.getLastLoadException()); + @SuppressWarnings("unchecked") Enumeration entries = javaxRegPopulator.getEntries(); @@ -221,7 +225,9 @@ public void testLoadDefect6562() "defect6562", defect6562Entry.getLogicalName() ); } - catch( Exception e ) { fail( "Unexpected exception.message = " + e.getMessage() ); } + catch( Exception e ) { + fail( "Unexpected exception.message = " + e.getMessage() ); + } } public void testLoadwithPropType() From f1e974356182b972342024915c64a212259600de Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 1 Jul 2024 13:54:59 +0200 Subject: [PATCH 20/48] Re-implemented method insertJposPropertiesInElement for a more readable implementation as discussed at https://github.com/JavaPOSWorkingGroup/javapos-config-loader/pull/7#discussion_r1528472649 --- .../config/simple/xml/JavaxRegPopulator.java | 122 +++++++++++------- 1 file changed, 77 insertions(+), 45 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index 9c50041..5f64eeb 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -19,6 +19,7 @@ /////////////////////////////////////////////////////////////////////////////// import jpos.config.JposEntry; +import jpos.config.JposEntry.Prop; import jpos.config.simple.AbstractRegPopulator; import jpos.config.simple.SimpleEntry; import jpos.loader.Version; @@ -44,6 +45,7 @@ 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 @@ -222,56 +224,86 @@ private void insertJposPropertiesInElement(Document doc, JposEntry jposEntry, El 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 ); + } + } - List sortedProps = new LinkedList<>(); - @SuppressWarnings("unchecked") + private 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())); - - for (JposEntry.Prop prop : sortedProps) { - if (cannotSetAttributeOfTag(prop, creation, JposEntry.SERVICE_CLASS_PROP_NAME, XML_ATTR_SERVICECLASS) && - cannotSetAttributeOfTag(prop, creation, JposEntry.SI_FACTORY_CLASS_PROP_NAME, XML_ATTR_FACTORYCLASS) && - cannotSetAttributeOfTag(prop, vendor, JposEntry.VENDOR_NAME_PROP_NAME, XML_ATTR_NAME) && - cannotSetAttributeOfTag(prop, vendor, JposEntry.VENDOR_URL_PROP_NAME, XML_ATTR_URL) && - cannotSetAttributeOfTag(prop, jpos, JposEntry.JPOS_VERSION_PROP_NAME, XML_ATTR_VERSION) && - cannotSetAttributeOfTag(prop, jpos, JposEntry.DEVICE_CATEGORY_PROP_NAME, XML_ATTR_CATEGORY) && - cannotSetAttributeOfTag(prop, product, JposEntry.PRODUCT_NAME_PROP_NAME, XML_ATTR_NAME) && - cannotSetAttributeOfTag(prop, product, JposEntry.PRODUCT_DESCRIPTION_PROP_NAME, XML_ATTR_DESCRIPTION) && - cannotSetAttributeOfTag(prop, product, JposEntry.PRODUCT_URL_PROP_NAME, XML_ATTR_URL) - ) - addPropElement(doc, jposEntryElement, prop); - } - } - - /** - * Checks whether the given JposEntry element represents an attribute of the given tag. If so, the attribute will be - * added to the tag. This method returns true if the element did not represent the specified attribute. - * @param prop Prop element of an JposEntry object. - * @param elem XML tag to be used to store the specified attribute, if prop represents thit attribute. - * @param propName Name of the Prop element. - * @param attrName Attribute name to be used in XML tag. - * @return true if prop does not represent the specified attribute and no attribute has been added to elem. false - * if the specified attribute has been added to elem. - */ - private boolean cannotSetAttributeOfTag(JposEntry.Prop prop, Element elem, String propName, String attrName) { - if (prop.getName().equals(propName)) { - /* Change /* to //* to generate implicit attribute values only if value is not empty. - String value = prop.getValueAsString(); - if( value.length() > 0 || - ( !JposEntry.PRODUCT_URL_PROP_NAME.equals(propName) && - !JposEntry.VENDOR_URL_PROP_NAME.equals(propName) - ) - ) - //*/ - { - elem.setAttribute(attrName, prop.getValueAsString()); - } - return false; - } - return true; - } + 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); From a67824b2a646d53ec7c410cdf077ee279262786c Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 1 Jul 2024 14:52:26 +0200 Subject: [PATCH 21/48] Code cleanup: Get rid of all javadoc warnigs and errors. --- src/main/java/jpos/config/JposEntry.java | 7 +++++++ src/main/java/jpos/config/JposEntryRegistry.java | 1 + .../jpos/config/simple/AbstractRegPopulator.java | 12 +++++++----- .../java/jpos/config/simple/SimpleRegPopulator.java | 8 +++++--- .../jpos/config/simple/xml/Xerces2RegPopulator.java | 2 +- src/main/java/jpos/loader/JposServiceManager.java | 1 + .../jpos/loader/simple/SimpleServiceManager.java | 4 ++-- src/main/java/jpos/util/Comparable.java | 2 ++ src/main/java/jpos/util/JposEntryUtility.java | 2 ++ src/main/java/jpos/util/PopupHelper.java | 5 +++++ src/main/java/jpos/util/tracing/TracerFactory.java | 4 +++- src/main/java/jpos/util/tracing/Tracing.java | 1 - 12 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main/java/jpos/config/JposEntry.java b/src/main/java/jpos/config/JposEntry.java index c516699..e546f66 100644 --- a/src/main/java/jpos/config/JposEntry.java +++ b/src/main/java/jpos/config/JposEntry.java @@ -91,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 */ @@ -99,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/JposEntryRegistry.java b/src/main/java/jpos/config/JposEntryRegistry.java index c8ff7fe..ae4f4ef 100644 --- a/src/main/java/jpos/config/JposEntryRegistry.java +++ b/src/main/java/jpos/config/JposEntryRegistry.java @@ -116,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/simple/AbstractRegPopulator.java b/src/main/java/jpos/config/simple/AbstractRegPopulator.java index 131dc41..10f7768 100644 --- a/src/main/java/jpos/config/simple/AbstractRegPopulator.java +++ b/src/main/java/jpos/config/simple/AbstractRegPopulator.java @@ -293,11 +293,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 ) @@ -363,7 +363,9 @@ protected InputStream findFileInClasspath( String fileName ) /** * 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 + * @param jarZipFilesVector 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 2.0 (Long Beach 2001) */ protected InputStream findFileInJarZipFiles( String fileName, diff --git a/src/main/java/jpos/config/simple/SimpleRegPopulator.java b/src/main/java/jpos/config/simple/SimpleRegPopulator.java index 37d6af9..1217023 100644 --- a/src/main/java/jpos/config/simple/SimpleRegPopulator.java +++ b/src/main/java/jpos/config/simple/SimpleRegPopulator.java @@ -315,9 +315,9 @@ 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 jarZipFilePaths 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( List jarZipFilePaths ) @@ -361,6 +361,8 @@ protected ObjectInputStream findSerOISInClasspath( List jarZipFilePaths /** * Finds the first serialized JposEntry file in the JAR files * @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( List jarFilePaths ) diff --git a/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java b/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java index 41bf6fd..294dbb5 100644 --- a/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java @@ -225,7 +225,7 @@ protected SAXParser getSAXParser() throws ParserConfigurationException, /** * Initializes XMLReader instance * @param xmlReader the XMLReader instance - * @throws org.xml.sax.SAXException + * @throws org.xml.sax.SAXException in case the {@link XMLReader} initialization fails */ protected void initXMLReader( XMLReader xmlReader ) throws SAXException { 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/simple/SimpleServiceManager.java b/src/main/java/jpos/loader/simple/SimpleServiceManager.java index 17122dc..c5bad89 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceManager.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceManager.java @@ -146,10 +146,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 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/JposEntryUtility.java b/src/main/java/jpos/util/JposEntryUtility.java index efbb84c..824e672 100644 --- a/src/main/java/jpos/util/JposEntryUtility.java +++ b/src/main/java/jpos/util/JposEntryUtility.java @@ -313,6 +313,8 @@ public static boolean validatePropValue( Object propValue, @SuppressWarnings("ra } /** + * 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 diff --git a/src/main/java/jpos/util/PopupHelper.java b/src/main/java/jpos/util/PopupHelper.java index 94b9fb9..943ae9a 100644 --- a/src/main/java/jpos/util/PopupHelper.java +++ b/src/main/java/jpos/util/PopupHelper.java @@ -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,6 +82,9 @@ 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 ) diff --git a/src/main/java/jpos/util/tracing/TracerFactory.java b/src/main/java/jpos/util/tracing/TracerFactory.java index b082379..33ef7ca 100644 --- a/src/main/java/jpos/util/tracing/TracerFactory.java +++ b/src/main/java/jpos/util/tracing/TracerFactory.java @@ -74,7 +74,9 @@ 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 diff --git a/src/main/java/jpos/util/tracing/Tracing.java b/src/main/java/jpos/util/tracing/Tracing.java index 96587b0..fb9f694 100644 --- a/src/main/java/jpos/util/tracing/Tracing.java +++ b/src/main/java/jpos/util/tracing/Tracing.java @@ -23,7 +23,6 @@ * 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 From a0518058086ee4b2582a57b4b61ff05724114ca2 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 1 Jul 2024 15:59:08 +0200 Subject: [PATCH 22/48] Code cleanup: Replaced iterator loop by for-each loop. Note, there is still a compiler warning at this changed lines requesting to use generic types. This is already fixed with branch fixes-from-pr7 and will work out when merging both branches together on the main. --- .../java/jpos/config/simple/xml/JavaxRegPopulator.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index 5f64eeb..cc8fcfe 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -351,11 +351,9 @@ private void load(InputStream inputStream) { tracer.println("SAX Parser configuration error, msg=" + e.getMessage()); lastLoadException = e; } - Iterator entries = jposEntryList.iterator(); - while (entries.hasNext()) { - JposEntry jposEntry = entries.next(); - getJposEntries().put(jposEntry.getLogicalName(), jposEntry); - } + for (JposEntry jposEntry : jposEntryList) { + getJposEntries().put(jposEntry.getLogicalName(), jposEntry); + } } //-------------------------------------------------------------------------- From c4a395a44eb66218362f3415c8a00a49fe33f64b Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 1 Jul 2024 16:14:40 +0200 Subject: [PATCH 23/48] Restricted access to DTD files while loading JavaPOS XML configuration. Code cleanup: Defined a constant for that restriction rule attribute. --- .../jpos/config/simple/xml/JavaxRegPopulator.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index cc8fcfe..ffd5b24 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -59,8 +59,11 @@ */ public class JavaxRegPopulator extends AbstractRegPopulator - implements XmlRegPopulator { - /** + implements XmlRegPopulator +{ + private static final String XML_RESTRICTED_ACCESS_ATTRIBUTE = "file,jar:file"; + + /** * Default ctor */ public JavaxRegPopulator() { @@ -168,8 +171,8 @@ private void save(Enumeration entries, OutputStream outputStream) DOMSource source = new DOMSource(document); TransformerFactory factory = TransformerFactory.newInstance(); - factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "file,jar:file"); - factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "file,jar:file"); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, XML_RESTRICTED_ACCESS_ATTRIBUTE); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, XML_RESTRICTED_ACCESS_ATTRIBUTE); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); @@ -338,8 +341,8 @@ private void load(InputStream inputStream) { getJposEntries().clear(); try { SAXParser parser = parserFactory.newSAXParser(); - parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "file"); - parser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file,jar:file"); + 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()); From faedec2d6721a85bbd5c4c609f28ee49a440b5e6 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 1 Jul 2024 16:24:07 +0200 Subject: [PATCH 24/48] Avoiding XSD resource remains unclosed. --- .../java/jpos/config/simple/xml/JavaxRegPopulator.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index ffd5b24..b713042 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -328,11 +328,10 @@ private void insertDateSavedComment(Document document) { private void load(InputStream inputStream) { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); JavaxSaxHandler saxHandler = new JavaxSaxHandler(); - try { - InputStream is = findFileInClasspath(XSD_FILE_NAME); + 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); + Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(ss); + parserFactory.setSchema(schema); } catch (Exception e) { parserFactory.setValidating(true); } From f0087c9e3b76aaf8e1d354bc78e9edc298a7161e Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Mon, 1 Jul 2024 18:38:22 +0200 Subject: [PATCH 25/48] Rolled back 2 generic type improvements to ensure backward compatibility Affected methods are - findFileInJarZipFiles(String fileName, Vector jarZipFilesVector) restored, forwarding to the new method findFileInJarZipFiles( String fileName, List jarZipFilesList ) - getJposEntries() conceptually substituted by methods addJposEntry(String logicalName, JposEntry entry) and clearAllJposEntries() Both old reintroduced methods has been marked as deprecated, however, pointing to the new methods. --- .../config/simple/AbstractRegPopulator.java | 58 +++++++++++++++---- .../config/simple/SimpleRegPopulator.java | 8 +-- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/main/java/jpos/config/simple/AbstractRegPopulator.java b/src/main/java/jpos/config/simple/AbstractRegPopulator.java index 10f7768..60ebd8d 100644 --- a/src/main/java/jpos/config/simple/AbstractRegPopulator.java +++ b/src/main/java/jpos/config/simple/AbstractRegPopulator.java @@ -71,8 +71,8 @@ public String getUniqueId() @SuppressWarnings("rawtypes") public Enumeration getEntries() { - List entryList = new ArrayList<>(); - Enumeration entries = jposEntries.elements(); + List entryList = new ArrayList<>(); + Enumeration entries = jposEntries.elements(); while( entries.hasMoreElements() ) entryList.add( entries.nextElement() ); @@ -147,10 +147,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 */ - protected Hashtable getJposEntries() { return jposEntries; } + @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 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 @@ -361,21 +384,34 @@ 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 list of JAR/Zip file names + * @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 + */ + @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) */ - protected InputStream findFileInJarZipFiles( String fileName, - List jarZipFilesVector ) + 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 = jarZipFilesVector.get( i ); + String jarZipFileName = jarZipFilesList.get( i ); try (ZipFile zipFile = new ZipFile( jarZipFileName )) { @@ -410,7 +446,7 @@ protected InputStream findFileInJarZipFiles( String fileName, // Instance variables // - private final Hashtable jposEntries = new Hashtable<>(); + private final Hashtable jposEntries = new Hashtable<>(); private String populatorFileName = ""; private String populatorFileURL = ""; diff --git a/src/main/java/jpos/config/simple/SimpleRegPopulator.java b/src/main/java/jpos/config/simple/SimpleRegPopulator.java index 1217023..093ad11 100644 --- a/src/main/java/jpos/config/simple/SimpleRegPopulator.java +++ b/src/main/java/jpos/config/simple/SimpleRegPopulator.java @@ -106,7 +106,7 @@ public void save( @SuppressWarnings("rawtypes") Enumeration entries, String file */ public void load() { - getJposEntries().clear(); + clearAllJposEntries(); Enumeration entries = readJposEntries(); while( entries.hasMoreElements() ) @@ -117,7 +117,7 @@ public void load() String logicalName = entry.getLogicalName(); if( logicalName != null ) - getJposEntries().put( logicalName, entry ); + addJposEntry( logicalName, entry ); lastLoadException = null; } @@ -139,7 +139,7 @@ public void load( String fileName ) { try (FileInputStream fis = new FileInputStream( fileName )) { - getJposEntries().clear(); + clearAllJposEntries(); Enumeration entries = readJposEntries( fis ); while( entries.hasMoreElements() ) @@ -149,7 +149,7 @@ public void load( String fileName ) getPropertyValue( JposEntry.LOGICAL_NAME_PROP_NAME ); if( logicalName != null ) - getJposEntries().put( logicalName, entry ); + addJposEntry(logicalName, entry); } lastLoadException = null; From 5b281be28d3e2309bd0fc68d7f2f3536598e4ccf Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Tue, 2 Jul 2024 17:56:05 +0200 Subject: [PATCH 26/48] Code cleanup: some code formatting improvements. No semantic changes at all. --- .../config/simple/xml/JavaxRegPopulator.java | 77 +++++++++---------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index b713042..7eac798 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -49,11 +49,12 @@ /** * Simple implementation of the JposRegPopulator that loads and saves - * the entries in XML using Javax API. + * 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 as - * the jpos.config.regPopulatorClass - * @see jpos.util.JposProperties#JPOS_REG_POPULATOR_CLASS_PROP_NAME + * 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) */ @@ -61,8 +62,6 @@ public class JavaxRegPopulator extends AbstractRegPopulator implements XmlRegPopulator { - private static final String XML_RESTRICTED_ACCESS_ATTRIBUTE = "file,jar:file"; - /** * Default ctor */ @@ -93,14 +92,13 @@ public String getClassName() { 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()); - } + 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())); @@ -112,20 +110,21 @@ public URL getEntriesURL() { @SuppressWarnings("unchecked") @Override - public void save(@SuppressWarnings("rawtypes") Enumeration entries) - throws Exception { + public void save(@SuppressWarnings("rawtypes") Enumeration entries) throws Exception + { if (isPopulatorFileDefined()) save(entries, getPopulatorFileOS()); - else + 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 { + public void save(@SuppressWarnings("rawtypes") Enumeration entries, String fileName) throws Exception + { try (FileOutputStream os = new FileOutputStream(fileName)) { save(entries, os); } @@ -136,8 +135,7 @@ 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()); + tracer.println("Error while loading populator file Exception.message: " + e.getMessage()); lastLoadException = e; } } @@ -147,8 +145,7 @@ public void load(String fileName) { 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()); + tracer.println("Error while loading populator file Exception.message: " + e.getMessage()); lastLoadException = e; } } @@ -169,12 +166,12 @@ private void save(Enumeration entries, OutputStream outputStream) insertJposEntriesInDoc(entries, document); insertDateSavedComment(document); - DOMSource source = new DOMSource(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()); @@ -206,14 +203,14 @@ private void insertJposEntriesInDoc(Enumeration entries, Document doc } /** - * Insert JposEntry tag for a given JposEntry object into an XML document. For better readability, first the - * tags creation, jpos, product and vendor will be created, followed by (optional) prop tags in alphabetic - * order.
- * Keep in mind that elements of the JposEntry object represent either an attribute of tag creation, jpos, product - * or vendor or a complete prop tag. Method checkAndSetAttributeOfTagIsTrue will be used to add the specific - * attribute into the corresponding tag if the element represents the specified attribute. If - * checkAndSetAttributeOfTagIsTrue fails for all specified attributes, a prop tag will be created and added to - * JposEntry tag. + * 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 @@ -244,7 +241,7 @@ private void insertJposPropertiesInElement(Document doc, JposEntry jposEntry, El } } - private List getSortedProperties(JposEntry jposEntry) { + private static List getSortedProperties(JposEntry jposEntry) { @SuppressWarnings("unchecked") Iterator props = jposEntry.getProps(); List sortedProps = new ArrayList<>(); @@ -302,7 +299,7 @@ private void addPropAsAttribute(Prop prop, Element creation, Element jpos, Eleme product.setAttribute(XML_ATTR_URL, attrValue); break; default: - tracer.print("WARN: unexpected XML attribute (will be skipped):" + attrName); + tracer.print("WARN: unexpected XML attribute (will be skipped): " + attrName); break; } @@ -317,7 +314,7 @@ private static void addPropElement(Document doc, Element jposEntryElement, JposE propElement.setAttribute(XML_ATTR_TYPE, prop.getValue().getClass().getSimpleName()); } - private void insertDateSavedComment(Document document) { + 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; @@ -465,8 +462,7 @@ public InputSource resolveEntity(String name, String publicId, String uri, Strin systemId); if (publicId.equals(DTD_DOC_TYPE_VALUE)) { - InputStream is = - getClass().getResourceAsStream(DTD_FILE_NAME); + InputStream is = getClass().getResourceAsStream(DTD_FILE_NAME); if (is == null) is = findFileInClasspath(DTD_FILE_NAME); @@ -485,12 +481,13 @@ public InputSource resolveEntity(String name, String publicId, String uri, Strin private List jposEntryList = new ArrayList<>(); - private Tracer tracer = TracerFactory.getInstance(). - createTracer(this.getClass().getSimpleName()); + 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"; } From 888a23f346785b7fa07698715622ff0f790facea Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Tue, 2 Jul 2024 19:08:22 +0200 Subject: [PATCH 27/48] Get rid of Xerces in the ProfileFactory implementation. DefaultProfileFactory.java is a copy of XercesProfilefactory from the fixes-from-pr7 branch coming with a lot of code clean up. At SimpleServiceManager.java the instantiation of the ProfileFactory has been changed to the new DefaultProfileFactory. --- .../loader/simple/SimpleServiceManager.java | 2 +- .../jpos/profile/DefaultProfileFactory.java | 278 ++++++++++++++++++ 2 files changed, 279 insertions(+), 1 deletion(-) create mode 100644 src/main/java/jpos/profile/DefaultProfileFactory.java diff --git a/src/main/java/jpos/loader/simple/SimpleServiceManager.java b/src/main/java/jpos/loader/simple/SimpleServiceManager.java index 0186e22..c69998d 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceManager.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceManager.java @@ -131,7 +131,7 @@ private void initRegPopulator() private ProfileFactory getProfileFactory() { if( profileFactory == null ) - profileFactory = new XercesProfileFactory(); + profileFactory = new DefaultProfileFactory(); return profileFactory; } diff --git a/src/main/java/jpos/profile/DefaultProfileFactory.java b/src/main/java/jpos/profile/DefaultProfileFactory.java new file mode 100644 index 0000000..fc71820 --- /dev/null +++ b/src/main/java/jpos/profile/DefaultProfileFactory.java @@ -0,0 +1,278 @@ +package jpos.profile; + +/////////////////////////////////////////////////////////////////////////////// +// +// 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.net.MalformedURLException; + +import javax.xml.parsers.*; + +import org.w3c.dom.*; +import org.xml.sax.*; + +import jpos.util.XmlHelper; +import jpos.util.tracing.Tracer; +import jpos.util.tracing.TracerFactory; + +/** + * Default implementation of the ProfileFactory interface using an + * XML parser to create profiles from the XML file passed + * @since 4.0 + */ +public class DefaultProfileFactory implements ProfileFactory +{ + //------------------------------------------------------------------------- + // Private methods + // + + /** + * @return a Profile object created from the Document object + * @param document the XML document object + * @exception jpos.profile.ProfileException if document is not in correct + * format + */ + private Profile extractProfile( Document document ) throws ProfileException + { + Element profileElement = document.getDocumentElement(); + + String name = profileElement.getAttribute( "name" ); + DefaultProfile profile = new DefaultProfile( name ); + + NodeList nodeList = profileElement.getElementsByTagName( "ProfileInfo" ); + + if( nodeList.getLength() != 1 ) + throw new ProfileException( "Profile does not contain 1 ProfileInfo element" ); + + Element profileInfoElement = (Element)nodeList.item( 0 ); + + profile.setVersion( profileInfoElement.getAttribute( "version" ) ); + profile.setVendorName( profileInfoElement.getAttribute( "vendorName" ) ); + + try + { + String vendorUrlString = profileInfoElement.getAttribute( "vendorUrl" ); + profile.setVendorUrl( new URL( vendorUrlString ) ); + } + catch( MalformedURLException e ) + { throw new ProfileException( "ProfileInfo contains an invalid vendorUrl string" ); } + + profile.setDescription( profileInfoElement.getAttribute( "description" ) ); + + return profile; + } + + //------------------------------------------------------------------------- + // Package methods + // + + /** + * Parses the XML file into a valid XML document for the profile DTD + * @param xmlFileName the XML file name + * @exception jpos.profile.ProfileException if the XML file could not be parsed + */ + Document parse( String xmlFileName ) throws ProfileException + { + XmlHelper xmlHelper = new XmlHelper(); + + try + { + xmlHelper.setDtdFileName( PROFILE_DTD_FILE_NAME ); + xmlHelper.setDtdFilePath( PROFILE_DTD_FILE_PATH ); + xmlHelper.checkAndCreateTempDtd(); + + DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + docFactory.setNamespaceAware( true ); + docFactory.setValidating( true ); + + DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); + + DefaultErrorHandler errorHandler = this.new DefaultErrorHandler(); + docBuilder.setErrorHandler( errorHandler ); + + Document document = docBuilder.parse( new File( xmlFileName ) ); + + if( !errorHandler.getErrorList().isEmpty() || + !errorHandler.getFatalErrorList().isEmpty() ) + { + String msg = "Error while parsing XML file, set properties"+ + " jpos.tracing = ON in jpos.properties" + + " file for details"; + throw new ProfileException( msg ); + } + + return document; + } + catch( IOException ioe ) + { + String msg = "Error loading XML profile file"; + tracer.println( msg + ": Exception.message = " + ioe.getMessage() ); + throw new ProfileException( msg, ioe ); + } + catch( SAXException se ) + { + String msg = "Error parsing XML profile file"; + tracer.println( msg + ": Exception.message = " + se.getMessage() ); + throw new ProfileException( msg, se ); + } + catch( ParserConfigurationException pce ) + { + String msg = "Error creating XML parser"; + tracer.println( msg + ": Exception.message = " + pce.getMessage() ); + throw new ProfileException( msg, pce ); + } + finally + { xmlHelper.removeTempDtd(); } + } + + /** + * Parses the XML file into a valid XML document for the profile Schemas + * @param xmlFileName the XML file name + * @exception jpos.profile.ProfileException if the XML file could not be parsed + */ + Document parseSchema( String xmlFileName ) throws ProfileException + { + try + { + DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + docFactory.setNamespaceAware( true ); + docFactory.setValidating( true ); + + DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); + + DefaultErrorHandler errorHandler = this.new DefaultErrorHandler(); + docBuilder.setErrorHandler( errorHandler ); + + return docBuilder.parse( new File( xmlFileName ) ); + } + catch( IOException ioe ) + { + String msg = "Error loading XML profile file"; + tracer.println( msg + ": Excpetion.message = " + ioe.getMessage() ); + throw new ProfileException( msg, ioe ); + } + catch( SAXException se ) + { + String msg = "Error parsing XML profile file"; + tracer.println( msg + ": Exception.message = " + se.getMessage() ); + + throw new ProfileException( msg, se ); + } + catch( ParserConfigurationException pce ) + { + String msg = "Error creating XML parser"; + tracer.println( msg + ": Exception.message = " + pce.getMessage() ); + throw new ProfileException( msg, pce ); + } + } + + /** + * Loads the Profile specified in the xmlFileName as a Profile object + * @param xmlFileName the XML file name + * @exception jpos.profile.ProfileException if the XML file could not be + * parsed and the profile created + */ + Profile load( String xmlFileName ) throws ProfileException + { + Document document = parse( xmlFileName ); + + return extractProfile( document ); + } + + //------------------------------------------------------------------------- + // Public methods + // + + /** + * @return a Profile object created parsing the XML file provided + * @param xmlProfileFileName the XML profile file + * @exception jpos.profile.ProfileException if there is an error loading the profile + */ + public Profile createProfile( String xmlProfileFileName ) throws ProfileException + { + return load( xmlProfileFileName ); + } + + //------------------------------------------------------------------------- + // Instance variables + // + + private Tracer tracer = TracerFactory.getInstance(). + createTracer( this.getClass().getSimpleName() ); + + //------------------------------------------------------------------------- + // Inner classes + // + + /** + * ErrorHandler inner class used to capture errors while parsing XML document + * @since 1.3 (Washington DC 2001 meeting) + * @author E. Michael Maximilien (maxim@us.ibm.com) + */ + class DefaultErrorHandler implements org.xml.sax.ErrorHandler + { + //--------------------------------------------------------------------- + // Package methods + // + + List getErrorList() { return errorList; } + + List getWarningList() { return warningList; } + + List getFatalErrorList() { return fatalErrorList; } + + //--------------------------------------------------------------------- + // Public methods + // + + public void warning( SAXParseException e ) throws SAXException + { + tracer.println( "Line " + e.getLineNumber() + ": WARNING SAXParseException.message = " + e.getMessage() ); + warningList.add( e ); + } + + public void error( SAXParseException e ) throws SAXException + { + tracer.println( "Line " + e.getLineNumber() + ": ERROR SAXParseException.message = " + e.getMessage() ); + errorList.add( e ); + } + + public void fatalError( SAXParseException e ) throws SAXException + { + tracer.println( "Line " + e.getLineNumber() + ": FATALERROR SAXParseException.message = " + e.getMessage() ); + fatalErrorList.add( e ); + } + + //--------------------------------------------------------------------- + // Private variables + // + + private final List warningList = new ArrayList<>(); + private final List errorList = new ArrayList<>(); + private final List fatalErrorList = new ArrayList<>(); + } + + //------------------------------------------------------------------------- + // Class constants + // + + public static final String PROFILE_DTD_FILE_NAME = "jcl_profile.dtd"; + public static final String PROFILE_DTD_FILE_PATH = "jpos" + File.separator + "res" + File.separator; +} From c6b17858bed19e201afc605e703c89767a7ea488 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Tue, 2 Jul 2024 19:18:18 +0200 Subject: [PATCH 28/48] Added test case for DefaultProfileFactory. Follow up to the previous commit. --- .../DefaultProfileFactoryTestCase.java | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/test/java/jpos/profile/DefaultProfileFactoryTestCase.java diff --git a/src/test/java/jpos/profile/DefaultProfileFactoryTestCase.java b/src/test/java/jpos/profile/DefaultProfileFactoryTestCase.java new file mode 100644 index 0000000..743b1d9 --- /dev/null +++ b/src/test/java/jpos/profile/DefaultProfileFactoryTestCase.java @@ -0,0 +1,132 @@ +package jpos.profile; + +/////////////////////////////////////////////////////////////////////////////// +// +// 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.io.*; + +import jpos.*; + +import org.w3c.dom.Document; + +/** + * A JUnit TestCase for the XercesProfileFactory class + * @author E. Michael Maximilien (maxim@us.ibm.com) + * @since 1.3 (SF 2K meeting) + */ +public class DefaultProfileFactoryTestCase extends JposTestCase +{ + //------------------------------------------------------------------------- + // Ctor(s) + // + + public DefaultProfileFactoryTestCase( String name ) { super( name ); } + + //------------------------------------------------------------------------- + // Protected overridden methods + // + + protected void setUp() + { + profFactory = new DefaultProfileFactory(); + profileFileName = PROFILE_FILE_NAME; + schemaProfileFileName = SCHEMA_PROFILE_FILE_NAME; + } + + protected void tearDown() + { + profFactory = null; + profileFileName = ""; + schemaProfileFileName = ""; + } + + //------------------------------------------------------------------------- + // Public testXyz() methods + // + + public void testParse() throws ProfileException, IOException + { + File profileFile = new File( profileFileName ); + assertTrue( "JCL JUnit Profile file named = " + profileFileName + ", does not exist", + profileFile.exists() ); + + /* + Document document = xProfFactory.parse( profileFileName ); + + assertTrue( "XercesProfileFactory.parse returned a null object", + document != null ); + */ + } + + /** Temporary disable test */ + public void _testParseSchema() throws ProfileException, IOException + { + File profileFile = new File( profileFileName ); + assertTrue( "JCL JUnit Schema Profile file named = " + schemaProfileFileName + ", does not exist", + profileFile.exists() ); + + Document document = profFactory.parseSchema( schemaProfileFileName ); + + assertTrue( "XercesProfileFactory.parseSchema returned a null object", + document != null ); + } + + /** Temporary disable test */ + public void _testCreateProfile() throws ProfileException, IOException + { + File profileFile = new File( profileFileName ); + assertTrue( "JCL JUnit Profile file named = " + profileFileName + ", does not exist", + profileFile.exists() ); + + Profile profile = profFactory.createProfile( profileFileName ); + + assertTrue( "XercesProfileFactory.createProfile returned a null object", profile != null ); + + assertTrue( "Profile.name != " + PROFILE_NAME, profile.getName().equals( PROFILE_NAME ) ); + + assertTrue( "Profile.version != " + PROFILE_VERSION, profile.getVersion().equals( PROFILE_VERSION ) ); + assertTrue( "Profile.vendorName != " + PROFILE_VENDOR_NAME, profile.getVendorName().equals( PROFILE_VENDOR_NAME ) ); + assertTrue( "Profile.vendorUrl != " + PROFILE_VENDOR_URL, profile.getVendorUrl().toString().equals( PROFILE_VENDOR_URL ) ); + assertTrue( "Profile.description != " + PROFILE_DESCRIPTION, profile.getDescription().equals( PROFILE_DESCRIPTION ) ); + + // + println( profile ); + // + } + + //------------------------------------------------------------------------- + // Private methods + // + + //------------------------------------------------------------------------- + // Instance variables + // + + private DefaultProfileFactory profFactory = null; + private String profileFileName = ""; + private String schemaProfileFileName = ""; + + //------------------------------------------------------------------------- + // Class constants + // + + private static final String PROFILE_FILE_NAME = loadResourceAsTemporaryFile("jcl_junit_profile.xml"); + private static final String SCHEMA_PROFILE_FILE_NAME = loadResourceAsTemporaryFile("jcl_junit_schema_profile.xml"); + + private static final String PROFILE_NAME = "JCL JUnit Corp. JavaPOS Profile"; + private static final String PROFILE_VERSION = "1.0"; + private static final String PROFILE_VENDOR_NAME = "JCL JUnit, Corp."; + private static final String PROFILE_VENDOR_URL = "http://www.jcl-junit.com"; + private static final String PROFILE_DESCRIPTION = "Simple JCL profile XML file for JUnit testing"; +} From cd1c268e6f7e83eb02205aaf6e0cef14dd79bbf4 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Tue, 2 Jul 2024 19:21:47 +0200 Subject: [PATCH 29/48] Finally, removed Xerces dependencies. --- build.gradle | 1 - .../xml/AbstractXercesRegPopulator.java | 545 ------------------ .../simple/xml/Xerces2RegPopulator.java | 527 ----------------- .../config/simple/xml/XercesRegPopulator.java | 464 --------------- .../jpos/profile/XercesProfileFactory.java | 293 ---------- .../xml/Xerces2RegPopulatorTestCase.java | 183 ------ .../xml/XercesRegPopulatorTestCase.java | 315 ---------- .../profile/XercesProfileFactoryTestCase.java | 155 ----- 8 files changed, 2483 deletions(-) delete mode 100644 src/main/java/jpos/config/simple/xml/AbstractXercesRegPopulator.java delete mode 100644 src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java delete mode 100644 src/main/java/jpos/config/simple/xml/XercesRegPopulator.java delete mode 100644 src/main/java/jpos/profile/XercesProfileFactory.java delete mode 100644 src/test/java/jpos/config/simple/xml/Xerces2RegPopulatorTestCase.java delete mode 100644 src/test/java/jpos/config/simple/xml/XercesRegPopulatorTestCase.java delete mode 100644 src/test/java/jpos/profile/XercesProfileFactoryTestCase.java diff --git a/build.gradle b/build.gradle index cc353bb..ed1ef7c 100644 --- a/build.gradle +++ b/build.gradle @@ -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/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/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/profile/XercesProfileFactory.java b/src/main/java/jpos/profile/XercesProfileFactory.java deleted file mode 100644 index 45103b4..0000000 --- a/src/main/java/jpos/profile/XercesProfileFactory.java +++ /dev/null @@ -1,293 +0,0 @@ -package jpos.profile; - -/////////////////////////////////////////////////////////////////////////////// -// -// 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.net.MalformedURLException; - -import javax.xml.parsers.*; - -import org.w3c.dom.*; -import org.apache.xerces.parsers.DOMParser; -import org.xml.sax.*; - -import jpos.util.XmlHelper; -import jpos.util.tracing.Tracer; -import jpos.util.tracing.TracerFactory; - -/** - * Default implementation of the ProfileFactory interface uses the Apache Xerces - * XML parser to create profiles from the XML file passed - * @since 1.3 (SF 2K meeting) - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ -public class XercesProfileFactory extends Object implements ProfileFactory -{ - //------------------------------------------------------------------------- - // Ctor(s) - // - - /** Default ctor */ - public XercesProfileFactory() {} - - //------------------------------------------------------------------------- - // Private methods - // - - /** - * @return a Profile object created from the Document object - * @param document the XML document object - * @exception jpos.profile.ProfileException if document is not in correct - * format - */ - private Profile extractProfile( Document document ) throws ProfileException - { - Element profileElement = document.getDocumentElement(); - - String name = profileElement.getAttribute( "name" ); - DefaultProfile profile = new DefaultProfile( name ); - - NodeList nodeList = profileElement.getElementsByTagName( "ProfileInfo" ); - - if( nodeList.getLength() != 1 ) - throw new ProfileException( "Profile does not contain 1 ProfileInfo element" ); - - Element profileInfoElement = (Element)nodeList.item( 0 ); - - profile.setVersion( profileInfoElement.getAttribute( "version" ) ); - profile.setVendorName( profileInfoElement.getAttribute( "vendorName" ) ); - - try - { - String vendorUrlString = profileInfoElement.getAttribute( "vendorUrl" ); - profile.setVendorUrl( new URL( vendorUrlString ) ); - } - catch( MalformedURLException e ) - { throw new ProfileException( "ProfileInfo contains an invalid vendorUrl string" ); } - - profile.setDescription( profileInfoElement.getAttribute( "description" ) ); - - return profile; - } - - //------------------------------------------------------------------------- - // Package methods - // - - /** - * Parses the XML file into a valid XML document for the profile DTD - * @param xmlFileName the XML file name - * @exception jpos.profile.ProfileException if the XML file could not be parsed - */ - Document parse( String xmlFileName ) throws ProfileException - { - XmlHelper xmlHelper = new XmlHelper(); - - try - { - xmlHelper.setDtdFileName( PROFILE_DTD_FILE_NAME ); - xmlHelper.setDtdFilePath( PROFILE_DTD_FILE_PATH ); - xmlHelper.checkAndCreateTempDtd(); - - DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - docFactory.setNamespaceAware( true ); - docFactory.setValidating( true ); - - DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - - DefaultErrorHandler errorHandler = this.new DefaultErrorHandler(); - docBuilder.setErrorHandler( errorHandler ); - - Document document = docBuilder.parse( new File( xmlFileName ) ); - - if( errorHandler.getErrorList().size() > 0 || - errorHandler.getFatalErrorList().size() > 0 ) - { - String msg = "Error while parsing XML file, set properties"+ - " jpos.tracing = ON in jpos.properties" + - " file for details"; - throw new ProfileException( msg ); - } - - return document; - } - catch( IOException ioe ) - { - String msg = "Error loading XML profile file"; - tracer.println( msg + ": Exception.message = " + ioe.getMessage() ); - throw new ProfileException( msg, ioe ); - } - catch( SAXException se ) - { - String msg = "Error parsing XML profile file"; - tracer.println( msg + ": Exception.message = " + se.getMessage() ); - throw new ProfileException( msg, se ); - } - catch( ParserConfigurationException pce ) - { - String msg = "Error creating XML parser"; - tracer.println( msg + ": Exception.message = " + pce.getMessage() ); - throw new ProfileException( msg, pce ); - } - finally - { xmlHelper.removeTempDtd(); } - } - - /** - * Parses the XML file into a valid XML document for the profile Schemas - * @param xmlFileName the XML file name - * @exception jpos.profile.ProfileException if the XML file could not be parsed - */ - Document parseSchema( String xmlFileName ) throws ProfileException - { - try - { - DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - docFactory.setNamespaceAware( true ); - docFactory.setValidating( true ); - - DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - - DefaultErrorHandler errorHandler = this.new DefaultErrorHandler(); - docBuilder.setErrorHandler( errorHandler ); - - Document document = docBuilder.parse( new File( xmlFileName ) ); - - return document; - } - catch( IOException ioe ) - { - String msg = "Error loading XML profile file"; - tracer.println( msg + ": Excpetion.message = " + ioe.getMessage() ); - throw new ProfileException( msg, ioe ); - } - catch( SAXException se ) - { - String msg = "Error parsing XML profile file"; - tracer.println( msg + ": Exception.message = " + se.getMessage() ); - - throw new ProfileException( msg, se ); - } - catch( ParserConfigurationException pce ) - { - String msg = "Error creating XML parser"; - tracer.println( msg + ": Exception.message = " + pce.getMessage() ); - throw new ProfileException( msg, pce ); - } - } - - /** - * Loads the Profile specified in the xmlFileName as a Profile object - * @param xmlFileName the XML file name - * @exception jpos.profile.ProfileException if the XML file could not be - * parsed and the profile created - */ - Profile load( String xmlFileName ) throws ProfileException - { - Document document = parse( xmlFileName ); - - return extractProfile( document ); - } - - //------------------------------------------------------------------------- - // Public methods - // - - /** - * @return a Profile object created parsing the XML file provided - * @param xmlProfileFileName the XML profile file - * @exception jpos.profile.ProfileException if there is an error loading the profile - */ - public Profile createProfile( String xmlProfileFileName ) throws ProfileException - { - return load( xmlProfileFileName ); - } - - //------------------------------------------------------------------------- - // Instance variables - // - - private Profile profile = null; - - private DOMParser domParser = new DOMParser(); - private DefaultErrorHandler errorHandler = this.new DefaultErrorHandler(); - private Tracer tracer = TracerFactory.getInstance(). - createTracer( "XercesProfileFactory" ); - - //------------------------------------------------------------------------- - // Inner classes - // - - /** - * ErrorHandler inner class used to capture errors while parsing XML document - * @since 1.3 (Washington DC 2001 meeting) - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ - class DefaultErrorHandler extends Object implements org.xml.sax.ErrorHandler - { - //--------------------------------------------------------------------- - // Package methods - // - - List getErrorList() { return errorList; } - - List getWarningList() { return warningList; } - - List getFatalErrorList() { return fatalErrorList; } - - //--------------------------------------------------------------------- - // Public methods - // - - public void warning( SAXParseException e ) throws SAXException - { - tracer.println( "Line " + e.getLineNumber() + ": WARNING SAXParseException.message = " + e.getMessage() ); - warningList.add( e ); - } - - public void error( SAXParseException e ) throws SAXException - { - tracer.println( "Line " + e.getLineNumber() + ": ERROR SAXParseException.message = " + e.getMessage() ); - errorList.add( e ); - } - - public void fatalError( SAXParseException e ) throws SAXException - { - tracer.println( "Line " + e.getLineNumber() + ": FATALERROR SAXParseException.message = " + e.getMessage() ); - fatalErrorList.add( e ); - } - - //--------------------------------------------------------------------- - // Private variables - // - - private List warningList = new ArrayList(); - private List errorList = new ArrayList(); - private List fatalErrorList = new ArrayList(); - } - - //------------------------------------------------------------------------- - // Class constants - // - - public static final String PROFILE_DTD_FILE_NAME = "jcl_profile.dtd"; - public static final String PROFILE_DTD_FILE_PATH = "jpos" + File.separator + "res" + File.separator; -} 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/profile/XercesProfileFactoryTestCase.java b/src/test/java/jpos/profile/XercesProfileFactoryTestCase.java deleted file mode 100644 index bc70065..0000000 --- a/src/test/java/jpos/profile/XercesProfileFactoryTestCase.java +++ /dev/null @@ -1,155 +0,0 @@ -package jpos.profile; - -/////////////////////////////////////////////////////////////////////////////// -// -// 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.io.*; - -import jpos.*; - -import org.w3c.dom.Document; - -/** - * A JUnit TestCase for the XercesProfileFactory class - * @author E. Michael Maximilien (maxim@us.ibm.com) - * @since 1.3 (SF 2K meeting) - */ -public class XercesProfileFactoryTestCase extends JposTestCase -{ - //------------------------------------------------------------------------- - // Ctor(s) - // - - public XercesProfileFactoryTestCase( String name ) { super( name ); } - - //------------------------------------------------------------------------- - // Protected overridden methods - // - - protected void setUp() - { - xProfFactory = new XercesProfileFactory(); - profileFileName = PROFILE_FILE_NAME; - schemaProfileFileName = SCHEMA_PROFILE_FILE_NAME; - } - - protected void tearDown() - { - xProfFactory = null; - profileFileName = ""; - schemaProfileFileName = ""; - } - - //------------------------------------------------------------------------- - // Public testXyz() methods - // - - public void testParse() throws ProfileException, IOException - { - File profileFile = new File( profileFileName ); - assertTrue( "JCL JUnit Profile file named = " + profileFileName + ", does not exist", - profileFile.exists() ); - - /* - Document document = xProfFactory.parse( profileFileName ); - - assertTrue( "XercesProfileFactory.parse returned a null object", - document != null ); - */ - } - - /** Temporary disable test */ - public void _testParseSchema() throws ProfileException, IOException - { - File profileFile = new File( profileFileName ); - assertTrue( "JCL JUnit Schema Profile file named = " + schemaProfileFileName + ", does not exist", - profileFile.exists() ); - - Document document = xProfFactory.parseSchema( schemaProfileFileName ); - - assertTrue( "XercesProfileFactory.parseSchema returned a null object", - document != null ); - } - - /** Temporary disable test */ - public void _testCreateProfile() throws ProfileException, IOException - { - File profileFile = new File( profileFileName ); - assertTrue( "JCL JUnit Profile file named = " + profileFileName + ", does not exist", - profileFile.exists() ); - - Profile profile = xProfFactory.createProfile( profileFileName ); - - assertTrue( "XercesProfileFactory.createProfile returned a null object", profile != null ); - - assertTrue( "Profile.name != " + PROFILE_NAME, profile.getName().equals( PROFILE_NAME ) ); - - assertTrue( "Profile.version != " + PROFILE_VERSION, profile.getVersion().equals( PROFILE_VERSION ) ); - assertTrue( "Profile.vendorName != " + PROFILE_VENDOR_NAME, profile.getVendorName().equals( PROFILE_VENDOR_NAME ) ); - assertTrue( "Profile.vendorUrl != " + PROFILE_VENDOR_URL, profile.getVendorUrl().toString().equals( PROFILE_VENDOR_URL ) ); - assertTrue( "Profile.description != " + PROFILE_DESCRIPTION, profile.getDescription().equals( PROFILE_DESCRIPTION ) ); - - // - println( profile ); - // - } - - //------------------------------------------------------------------------- - // 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 String profileFileName = ""; - private String schemaProfileFileName = ""; - - //------------------------------------------------------------------------- - // Class constants - // - - private static final String PROFILE_FILE_NAME = loadResourceAsTemporaryFile("jcl_junit_profile.xml"); - private static final String SCHEMA_PROFILE_FILE_NAME = loadResourceAsTemporaryFile("jcl_junit_schema_profile.xml"); - - private static final String PROFILE_NAME = "JCL JUnit Corp. JavaPOS Profile"; - private static final String PROFILE_VERSION = "1.0"; - private static final String PROFILE_VENDOR_NAME = "JCL JUnit, Corp."; - private static final String PROFILE_VENDOR_URL = "http://www.jcl-junit.com"; - private static final String PROFILE_DESCRIPTION = "Simple JCL profile XML file for JUnit testing"; -} From fa68e30fbff90715f0c185fffd43fdab94174ab6 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Wed, 3 Jul 2024 10:38:34 +0200 Subject: [PATCH 30/48] Code cleanup: Removed all "extends Object" declarations --- src/main/java/jpos/config/DefaultCompositeRegPopulator.java | 3 +-- src/main/java/jpos/config/Version.java | 2 +- src/main/java/jpos/config/simple/AbstractRegPopulator.java | 3 +-- src/main/java/jpos/config/simple/SimpleEntryRegistry.java | 2 +- .../java/jpos/config/simple/xml/Xerces2RegPopulator.java | 2 +- src/main/java/jpos/loader/JposServiceLoader.java | 2 +- src/main/java/jpos/loader/Version.java | 2 +- .../java/jpos/loader/simple/SimpleServiceConnection.java | 3 +-- src/main/java/jpos/loader/simple/SimpleServiceManager.java | 3 +-- src/main/java/jpos/profile/AbstractPropType.java | 3 +-- src/main/java/jpos/profile/DefaultDevCatInfo.java | 2 +- src/main/java/jpos/profile/DefaultDevCatInfoList.java | 4 ++-- src/main/java/jpos/profile/DefaultDevCatV.java | 2 +- src/main/java/jpos/profile/DefaultProfile.java | 2 +- src/main/java/jpos/profile/DefaultProfileRegistry.java | 2 +- src/main/java/jpos/profile/DefaultPropInfo.java | 2 +- src/main/java/jpos/profile/DefaultPropInfoList.java | 4 ++-- src/main/java/jpos/profile/DefaultPropValue.java | 2 +- src/main/java/jpos/profile/DefaultPropValueList.java | 4 ++-- src/main/java/jpos/profile/JposDevCats.java | 5 ++--- src/main/java/jpos/profile/XercesProfileFactory.java | 4 ++-- src/main/java/jpos/util/DefaultComparableElement.java | 2 +- src/main/java/jpos/util/DefaultProperties.java | 6 +++--- src/main/java/jpos/util/FileUtil.java | 2 +- src/main/java/jpos/util/JposEntryUtility.java | 2 +- src/main/java/jpos/util/JposPropertiesViewer.java | 2 +- src/main/java/jpos/util/Sorter.java | 2 +- src/main/java/jpos/util/XmlHelper.java | 2 +- src/main/java/jpos/util/tracing/Tracer.java | 4 ++-- src/main/java/jpos/util/tracing/TracerFactory.java | 2 +- src/main/java/jpos/util/tracing/Tracing.java | 2 +- 31 files changed, 39 insertions(+), 45 deletions(-) diff --git a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java index d76244f..b0d3341 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) 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 60ebd8d..4d35e34 100644 --- a/src/main/java/jpos/config/simple/AbstractRegPopulator.java +++ b/src/main/java/jpos/config/simple/AbstractRegPopulator.java @@ -37,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) diff --git a/src/main/java/jpos/config/simple/SimpleEntryRegistry.java b/src/main/java/jpos/config/simple/SimpleEntryRegistry.java index 63a538b..5031245 100644 --- a/src/main/java/jpos/config/simple/SimpleEntryRegistry.java +++ b/src/main/java/jpos/config/simple/SimpleEntryRegistry.java @@ -33,7 +33,7 @@ * @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 { /** * One-argument constructor diff --git a/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java b/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java index 294dbb5..8e3fdff 100644 --- a/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java @@ -484,7 +484,7 @@ public InputSource resolveEntity( String publicId, String systemId ) * SAX XML Handler interface * @author E. Michael Maximilien */ - protected class JposErrorHandler extends Object implements ErrorHandler + protected class JposErrorHandler implements ErrorHandler { //---------------------------------------------------------------------- // Private/protected methods diff --git a/src/main/java/jpos/loader/JposServiceLoader.java b/src/main/java/jpos/loader/JposServiceLoader.java index 7c66264..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 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 437eace..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, diff --git a/src/main/java/jpos/loader/simple/SimpleServiceManager.java b/src/main/java/jpos/loader/simple/SimpleServiceManager.java index c5bad89..74bfde2 100644 --- a/src/main/java/jpos/loader/simple/SimpleServiceManager.java +++ b/src/main/java/jpos/loader/simple/SimpleServiceManager.java @@ -34,8 +34,7 @@ * @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) diff --git a/src/main/java/jpos/profile/AbstractPropType.java b/src/main/java/jpos/profile/AbstractPropType.java index 5b0eaf6..6cc7d05 100644 --- a/src/main/java/jpos/profile/AbstractPropType.java +++ b/src/main/java/jpos/profile/AbstractPropType.java @@ -25,8 +25,7 @@ * @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 { private static final long serialVersionUID = 3913079070484345271L; diff --git a/src/main/java/jpos/profile/DefaultDevCatInfo.java b/src/main/java/jpos/profile/DefaultDevCatInfo.java index 71c365c..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) diff --git a/src/main/java/jpos/profile/DefaultDevCatInfoList.java b/src/main/java/jpos/profile/DefaultDevCatInfoList.java index f9681ae..b678169 100644 --- a/src/main/java/jpos/profile/DefaultDevCatInfoList.java +++ b/src/main/java/jpos/profile/DefaultDevCatInfoList.java @@ -25,7 +25,7 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien */ -class DefaultDevCatInfoList extends Object implements DevCatInfoList +class DefaultDevCatInfoList implements DevCatInfoList { //------------------------------------------------------------------------- // Public methods @@ -70,7 +70,7 @@ class DefaultDevCatInfoList extends Object implements DevCatInfoList * @author E. Michael Maximilien (maxim@us.ibm.com) * @since 1.3 (SF 2K meeting) */ - class DefaultIterator extends Object implements DevCatInfoList.Iterator + class DefaultIterator implements DevCatInfoList.Iterator { //--------------------------------------------------------------------- // Ctor(s) diff --git a/src/main/java/jpos/profile/DefaultDevCatV.java b/src/main/java/jpos/profile/DefaultDevCatV.java index dae103d..f16511a 100644 --- a/src/main/java/jpos/profile/DefaultDevCatV.java +++ b/src/main/java/jpos/profile/DefaultDevCatV.java @@ -23,7 +23,7 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class DefaultDevCatV extends Object implements DevCatVisitor +public class DefaultDevCatV implements DevCatVisitor { //------------------------------------------------------------------------- // Public methods diff --git a/src/main/java/jpos/profile/DefaultProfile.java b/src/main/java/jpos/profile/DefaultProfile.java index d1f6b90..d54a32b 100644 --- a/src/main/java/jpos/profile/DefaultProfile.java +++ b/src/main/java/jpos/profile/DefaultProfile.java @@ -27,7 +27,7 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -class DefaultProfile extends Object implements Profile, Serializable +class DefaultProfile implements Profile, Serializable { private static final long serialVersionUID = 1478221492781830174L; diff --git a/src/main/java/jpos/profile/DefaultProfileRegistry.java b/src/main/java/jpos/profile/DefaultProfileRegistry.java index f90fbc1..d3ba1f7 100644 --- a/src/main/java/jpos/profile/DefaultProfileRegistry.java +++ b/src/main/java/jpos/profile/DefaultProfileRegistry.java @@ -25,7 +25,7 @@ * @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 { //------------------------------------------------------------------------- // Public methods diff --git a/src/main/java/jpos/profile/DefaultPropInfo.java b/src/main/java/jpos/profile/DefaultPropInfo.java index 9a4aaf0..444f084 100644 --- a/src/main/java/jpos/profile/DefaultPropInfo.java +++ b/src/main/java/jpos/profile/DefaultPropInfo.java @@ -25,7 +25,7 @@ * @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 { private static final long serialVersionUID = -4186241572660113196L; diff --git a/src/main/java/jpos/profile/DefaultPropInfoList.java b/src/main/java/jpos/profile/DefaultPropInfoList.java index 74bfbd9..6343fcd 100644 --- a/src/main/java/jpos/profile/DefaultPropInfoList.java +++ b/src/main/java/jpos/profile/DefaultPropInfoList.java @@ -25,7 +25,7 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -class DefaultPropInfoList extends Object implements PropInfoList +class DefaultPropInfoList implements PropInfoList { //------------------------------------------------------------------------- // Public methods @@ -70,7 +70,7 @@ class DefaultPropInfoList extends Object implements PropInfoList * @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) diff --git a/src/main/java/jpos/profile/DefaultPropValue.java b/src/main/java/jpos/profile/DefaultPropValue.java index 78655dd..964fc72 100644 --- a/src/main/java/jpos/profile/DefaultPropValue.java +++ b/src/main/java/jpos/profile/DefaultPropValue.java @@ -26,7 +26,7 @@ * @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 { private static final long serialVersionUID = 8895132456252900824L; diff --git a/src/main/java/jpos/profile/DefaultPropValueList.java b/src/main/java/jpos/profile/DefaultPropValueList.java index 264e7ee..f2a509d 100644 --- a/src/main/java/jpos/profile/DefaultPropValueList.java +++ b/src/main/java/jpos/profile/DefaultPropValueList.java @@ -25,7 +25,7 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -class DefaultPropValueList extends Object implements PropValueList +class DefaultPropValueList implements PropValueList { //------------------------------------------------------------------------- // Public methods @@ -70,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) diff --git a/src/main/java/jpos/profile/JposDevCats.java b/src/main/java/jpos/profile/JposDevCats.java index 11a0601..40fbe82 100644 --- a/src/main/java/jpos/profile/JposDevCats.java +++ b/src/main/java/jpos/profile/JposDevCats.java @@ -25,7 +25,7 @@ * @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 @@ -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 abstract static class AbstractDevCat extends Object - implements DevCat + public abstract static class AbstractDevCat implements DevCat { //--------------------------------------------------------------------- // Public overriden methods diff --git a/src/main/java/jpos/profile/XercesProfileFactory.java b/src/main/java/jpos/profile/XercesProfileFactory.java index 07db80d..e3372df 100644 --- a/src/main/java/jpos/profile/XercesProfileFactory.java +++ b/src/main/java/jpos/profile/XercesProfileFactory.java @@ -38,7 +38,7 @@ * @since 1.3 (SF 2K meeting) * @author E. Michael Maximilien (maxim@us.ibm.com) */ -public class XercesProfileFactory extends Object implements ProfileFactory +public class XercesProfileFactory implements ProfileFactory { //------------------------------------------------------------------------- // Private methods @@ -227,7 +227,7 @@ 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 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 e8d67c0..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 @@ -436,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) @@ -570,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 2cc2786..b2e0a25 100644 --- a/src/main/java/jpos/util/FileUtil.java +++ b/src/main/java/jpos/util/FileUtil.java @@ -41,7 +41,7 @@ * @version 0.0.1 * @since 2.1.0 */ -public class FileUtil extends Object +public class FileUtil { //------------------------------------------------------------------------- // Ctor(s) diff --git a/src/main/java/jpos/util/JposEntryUtility.java b/src/main/java/jpos/util/JposEntryUtility.java index 824e672..f71a108 100644 --- a/src/main/java/jpos/util/JposEntryUtility.java +++ b/src/main/java/jpos/util/JposEntryUtility.java @@ -32,7 +32,7 @@ * @since 1.3 (Tokyo 2001 meeting) * @version 1.3.0 */ -public class JposEntryUtility extends Object +public class JposEntryUtility { //------------------------------------------------------------------------- // Ctor(s) 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/Sorter.java b/src/main/java/jpos/util/Sorter.java index 90572db..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 diff --git a/src/main/java/jpos/util/XmlHelper.java b/src/main/java/jpos/util/XmlHelper.java index 1dbe0b8..1371e07 100644 --- a/src/main/java/jpos/util/XmlHelper.java +++ b/src/main/java/jpos/util/XmlHelper.java @@ -30,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 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 33ef7ca..50be110 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 diff --git a/src/main/java/jpos/util/tracing/Tracing.java b/src/main/java/jpos/util/tracing/Tracing.java index fb9f694..6e32f25 100644 --- a/src/main/java/jpos/util/tracing/Tracing.java +++ b/src/main/java/jpos/util/tracing/Tracing.java @@ -25,7 +25,7 @@ * to do Tracer.getInstance() everytime you need to access the Tracer. * @author E. Michael Maximilien */ -public class Tracing extends Object +public class Tracing { //------------------------------------------------------------------------- // Ctor From 4cf4ef899c845647ad34c5180c60eaabb6bea1ec Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Wed, 3 Jul 2024 13:44:35 +0200 Subject: [PATCH 31/48] Added change notes related to code improvements done at branch fixes-from-pr7. --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aeda79..ad35c63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log for javapos-config-loader +- 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` + ## 3.1.0 - Added missing devices to XML schema/DTD files and as DevCat interfaces (contribution by [@dougberkland](https://github.com/dougberkland)) From a43f15fce1b06decb47c53737efc6f31d0fd787f Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Wed, 3 Jul 2024 14:41:58 +0200 Subject: [PATCH 32/48] Added change notes related to Xerces removal done at branch xerces-replacement-from-pr7. --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aeda79..4dd8f37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Change Log for javapos-config-loader +- 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` + ## 3.1.0 - Added missing devices to XML schema/DTD files and as DevCat interfaces (contribution by [@dougberkland](https://github.com/dougberkland)) From 996bc63aaa7279bd9de062e486bc8a6dd4cb2d9e Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Wed, 3 Jul 2024 18:15:49 +0200 Subject: [PATCH 33/48] Deleted the conflicting Xerces2RegPopulator.java subsequently which was not staged during previous merge due to an Eclipse Git tooling problem. --- .../simple/xml/Xerces2RegPopulator.java | 527 ------------------ 1 file changed, 527 deletions(-) delete mode 100644 src/main/java/jpos/config/simple/xml/Xerces2RegPopulator.java 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 8e3fdff..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 in case the {@link XMLReader} initialization fails - */ - 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 == null ? "" : 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 == null ? "" : 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( typeString ); - - 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 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 From de7fd8a096b8b7dfd433496ba4249413a2fa367e Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Wed, 3 Jul 2024 18:40:27 +0200 Subject: [PATCH 34/48] Code cleanup: get rid of deprecated method calls which got marked as deprecated and replaced by new type-safe and internal-data-strcuture-protecting calls. That way the last compiler warning in the merged code got resolved. --- src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index 7eac798..798f30a 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -334,7 +334,7 @@ private void load(InputStream inputStream) { } parserFactory.setNamespaceAware(true); jposEntryList.clear(); - getJposEntries().clear(); + clearAllJposEntries(); try { SAXParser parser = parserFactory.newSAXParser(); parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, XML_RESTRICTED_ACCESS_ATTRIBUTE); @@ -351,7 +351,7 @@ private void load(InputStream inputStream) { lastLoadException = e; } for (JposEntry jposEntry : jposEntryList) { - getJposEntries().put(jposEntry.getLogicalName(), jposEntry); + addJposEntry(jposEntry.getLogicalName(), jposEntry); } } From 44f81b8d59aeffce9673c02011a6a019fff6171a Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Wed, 3 Jul 2024 18:50:09 +0200 Subject: [PATCH 35/48] Enabling for webstart environments by solving issue #1 by using standard class-loader instead of system class loader. --- CHANGELOG.md | 1 + src/main/java/jpos/util/XmlHelper.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6341319..bf19fee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - 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 diff --git a/src/main/java/jpos/util/XmlHelper.java b/src/main/java/jpos/util/XmlHelper.java index 1371e07..a5d6393 100644 --- a/src/main/java/jpos/util/XmlHelper.java +++ b/src/main/java/jpos/util/XmlHelper.java @@ -89,7 +89,7 @@ public void checkAndCreateTempDtd() dtdFilePath ); } - is = ClassLoader.getSystemClassLoader().getResourceAsStream( dtdJarFullFileName ); + is = getClass().getClassLoader().getResourceAsStream( dtdJarFullFileName ); tracer.println( "Got DTD InputStream from current ClassLoader" ); From f0d95165945f53d6a271d7c8d7bac7414241884b Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Wed, 3 Jul 2024 18:51:19 +0200 Subject: [PATCH 36/48] Prepares versions for 4.0 releasing. --- CHANGELOG.md | 6 +++++- build.gradle | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf19fee..40b5cb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Change Log for javapos-config-loader +## 4.0.0 + - 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` @@ -10,7 +12,9 @@ - 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 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) diff --git a/build.gradle b/build.gradle index ed1ef7c..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 /////////////////////////////////////////////////////////////////////////////// From 07d1d019a7f173d2e23f28fc4647ce63d229e989 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 5 Jul 2024 10:54:59 +0200 Subject: [PATCH 37/48] Code cleanup: I removed empty comments for serial UIDs as requested by reviewer. They had been added automatically by Eclipse when generating the serial UID to solve compiler warnings. A comment would be meaningsless at the end here... --- src/main/java/jpos/config/JposConfigException.java | 3 --- src/main/java/jpos/config/JposEntryRegistryEvent.java | 3 --- src/main/java/jpos/loader/JposLoaderException.java | 7 ++----- src/main/java/jpos/profile/ProfileException.java | 9 +++------ 4 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/main/java/jpos/config/JposConfigException.java b/src/main/java/jpos/config/JposConfigException.java index c6bfe8e..d4ab2fe 100644 --- a/src/main/java/jpos/config/JposConfigException.java +++ b/src/main/java/jpos/config/JposConfigException.java @@ -28,9 +28,6 @@ */ public class JposConfigException extends JposException { - /** - * - */ private static final long serialVersionUID = 6080819630774250157L; //------------------------------------------------------------------------- diff --git a/src/main/java/jpos/config/JposEntryRegistryEvent.java b/src/main/java/jpos/config/JposEntryRegistryEvent.java index dd09500..eb86838 100644 --- a/src/main/java/jpos/config/JposEntryRegistryEvent.java +++ b/src/main/java/jpos/config/JposEntryRegistryEvent.java @@ -27,9 +27,6 @@ */ public class JposEntryRegistryEvent extends EventObject { - /** - * - */ private static final long serialVersionUID = 8674029321997818382L; /** diff --git a/src/main/java/jpos/loader/JposLoaderException.java b/src/main/java/jpos/loader/JposLoaderException.java index ab2e567..8c0f83e 100644 --- a/src/main/java/jpos/loader/JposLoaderException.java +++ b/src/main/java/jpos/loader/JposLoaderException.java @@ -28,14 +28,11 @@ */ public class JposLoaderException extends JposException { + private static final long serialVersionUID = -2552211746747253072L; + //------------------------------------------------------------------------- // Ctor(s) // - - /** - * - */ - private static final long serialVersionUID = -2552211746747253072L; /** * Creates a JposLoaderException with the description passed diff --git a/src/main/java/jpos/profile/ProfileException.java b/src/main/java/jpos/profile/ProfileException.java index f275c05..c46db4f 100644 --- a/src/main/java/jpos/profile/ProfileException.java +++ b/src/main/java/jpos/profile/ProfileException.java @@ -25,15 +25,12 @@ */ public class ProfileException extends Exception { + private static final long serialVersionUID = -5646225627523470777L; + //------------------------------------------------------------------------- // Ctor(s) // - - /** - * - */ - private static final long serialVersionUID = -5646225627523470777L; - + /** * 1-arg ctor * @param msg the exception's message From bc046075f021336df24f8442d3dbeabf3552761c Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 12 Jul 2024 19:08:33 +0200 Subject: [PATCH 38/48] Replaced legacy trace enabling at tests by new one. --- src/test/java/jpos/config/CompositeRegPopulatorTestCase.java | 2 +- .../java/jpos/config/simple/SimpleRegPopulatorTestCase.java | 4 ++-- src/test/java/jpos/loader/AbstractTestCase.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) 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 4347c04..c545bbb 100644 --- a/src/test/java/jpos/config/simple/SimpleRegPopulatorTestCase.java +++ b/src/test/java/jpos/config/simple/SimpleRegPopulatorTestCase.java @@ -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/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; From 46b614882d9b281d7dcb53f2b02f2979b62a5507 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 12 Jul 2024 19:11:06 +0200 Subject: [PATCH 39/48] Enabled tracing at tests by default. --- src/test/java/jpos/JposTestCase.java | 2 +- src/test/resources/jpos/res/jpos_junit.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/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 From 4c424d40e38271555cd6f3c76c26863ce40c6b93 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 12 Jul 2024 19:13:03 +0200 Subject: [PATCH 40/48] Revert "Enabled tracing at tests by default." This reverts commit 46b614882d9b281d7dcb53f2b02f2979b62a5507. --- src/test/java/jpos/JposTestCase.java | 2 +- src/test/resources/jpos/res/jpos_junit.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/jpos/JposTestCase.java b/src/test/java/jpos/JposTestCase.java index 3e163f9..59f59a3 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 = "ON"; + public static final String JPOS_UTIL_TRACING_VALUE = "OFF"; /** * This method is needed as on different OS the temp directory path may come without diff --git a/src/test/resources/jpos/res/jpos_junit.properties b/src/test/resources/jpos/res/jpos_junit.properties index 739e3ce..f46e012 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.TurnOnAllNamedTracers=ON +jpos.util.tracing=OFF # Use this property to for the JCL to load a specific file (cfg or XML) #jpos.config.populatorFile=jpos1.cfg From 5777a90ae6cc627ee0c6288001828110b1538b2d Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 12 Jul 2024 19:15:21 +0200 Subject: [PATCH 41/48] Enabled tracing on tests by default. --- src/test/java/jpos/JposTestCase.java | 2 +- src/test/java/jpos/util/JposPropertiesTestCase.java | 8 ++++---- src/test/resources/jpos/res/jpos_junit.properties | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) 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/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 From fc0877a9e01a7989c2050d42e902eaf8e8b58afa Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Fri, 12 Jul 2024 19:16:21 +0200 Subject: [PATCH 42/48] Improved tracing to log the valuable information. --- .../config/simple/xml/JavaxRegPopulator.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index 798f30a..340faf4 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -125,6 +125,7 @@ public void save(@SuppressWarnings("rawtypes") Enumeration entries) throws Excep @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); } @@ -142,6 +143,7 @@ public void load() { @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) { @@ -329,6 +331,7 @@ private void load(InputStream inputStream) { 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); } @@ -369,7 +372,6 @@ private class JavaxSaxHandler @Override public void startElement(String uri, String lname, String qname, Attributes attrs) { - tracer.print("StartElement: " + qname); if (theException != null) { tracer.println(": Parse error: " + theException.getMessage()); lastLoadException = theException; @@ -412,7 +414,6 @@ else if (currentEntry != null) { } } } - tracer.println(""); } private void addOptionalProperty(String name, String value) { @@ -421,10 +422,8 @@ private void addOptionalProperty(String name, String value) { @Override public void endElement(String uri, String lname, String qname) { - if (theException == null) - tracer.println("EndElement: " + qname); - else { - tracer.println("EndElement: " + theException.getMessage()); + if (theException != null) { + tracer.println("Parsing XML element " + qname + " failed with: " + theException.getMessage()); theException = null; } if (qname.equals(XML_TAG_JPOSENTRY)) { @@ -455,13 +454,10 @@ public void fatalError(SAXParseException e) { @Override public InputSource resolveEntity(String name, String publicId, String uri, String systemId) { - tracer.println("JposEntityResolver:resolveEntity:publicId=" + - publicId); - - tracer.println("JposEntityResolver:resolveEntity:systemId=" + - 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) From edff5272d85e9dee89dd25a601388982163af1de Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Tue, 16 Jul 2024 15:24:19 +0200 Subject: [PATCH 43/48] Code clean up for some generic type issues. --- .../java/jpos/util/tracing/TracerFactory.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/jpos/util/tracing/TracerFactory.java b/src/main/java/jpos/util/tracing/TracerFactory.java index 50be110..236def8 100644 --- a/src/main/java/jpos/util/tracing/TracerFactory.java +++ b/src/main/java/jpos/util/tracing/TracerFactory.java @@ -81,7 +81,7 @@ public static TracerFactory getInstance() */ public void setOutputFile( File file ) throws IOException { - // + // not implemented yet } /** @@ -109,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 ); @@ -164,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; } From b579f60745d2ef4e47358abaef9ec3613ce3944e Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Tue, 16 Jul 2024 15:20:16 +0200 Subject: [PATCH 44/48] Trim tracer names from jpos.util.tracing.TurnOnNamedTracers definition. --- src/main/java/jpos/util/tracing/TracerFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/jpos/util/tracing/TracerFactory.java b/src/main/java/jpos/util/tracing/TracerFactory.java index 236def8..bbf829a 100644 --- a/src/main/java/jpos/util/tracing/TracerFactory.java +++ b/src/main/java/jpos/util/tracing/TracerFactory.java @@ -231,7 +231,7 @@ private void initTurnedOnTracers( JposProperties props ) props.getStringListProperty( TURN_ON_NAMED_TRACERS_PROP_NAME ); for (String tracerName : turnOnNamedTracersList) { - namedTracerState.put( tracerName, Boolean.TRUE ); + namedTracerState.put( tracerName.trim(), Boolean.TRUE ); } } } From 2307d19f0955cb6e499327c673049d3e95893213 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Thu, 18 Jul 2024 16:01:25 +0200 Subject: [PATCH 45/48] Replaced XercesProfileFactory.java by DefaultProfileFactory.java which is the same implementation, just another name to get rid of "Xerces" in the name. In fact, it was forgotten to remove the old file when renaming it. --- .../jpos/profile/XercesProfileFactory.java | 279 ------------------ 1 file changed, 279 deletions(-) delete mode 100644 src/main/java/jpos/profile/XercesProfileFactory.java diff --git a/src/main/java/jpos/profile/XercesProfileFactory.java b/src/main/java/jpos/profile/XercesProfileFactory.java deleted file mode 100644 index e3372df..0000000 --- a/src/main/java/jpos/profile/XercesProfileFactory.java +++ /dev/null @@ -1,279 +0,0 @@ -package jpos.profile; - -/////////////////////////////////////////////////////////////////////////////// -// -// 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.net.MalformedURLException; - -import javax.xml.parsers.*; - -import org.w3c.dom.*; -import org.xml.sax.*; - -import jpos.util.XmlHelper; -import jpos.util.tracing.Tracer; -import jpos.util.tracing.TracerFactory; - -/** - * Default implementation of the ProfileFactory interface uses the Apache Xerces - * XML parser to create profiles from the XML file passed - * @since 1.3 (SF 2K meeting) - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ -public class XercesProfileFactory implements ProfileFactory -{ - //------------------------------------------------------------------------- - // Private methods - // - - /** - * @return a Profile object created from the Document object - * @param document the XML document object - * @exception jpos.profile.ProfileException if document is not in correct - * format - */ - private Profile extractProfile( Document document ) throws ProfileException - { - Element profileElement = document.getDocumentElement(); - - String name = profileElement.getAttribute( "name" ); - DefaultProfile profile = new DefaultProfile( name ); - - NodeList nodeList = profileElement.getElementsByTagName( "ProfileInfo" ); - - if( nodeList.getLength() != 1 ) - throw new ProfileException( "Profile does not contain 1 ProfileInfo element" ); - - Element profileInfoElement = (Element)nodeList.item( 0 ); - - profile.setVersion( profileInfoElement.getAttribute( "version" ) ); - profile.setVendorName( profileInfoElement.getAttribute( "vendorName" ) ); - - try - { - String vendorUrlString = profileInfoElement.getAttribute( "vendorUrl" ); - profile.setVendorUrl( new URL( vendorUrlString ) ); - } - catch( MalformedURLException e ) - { throw new ProfileException( "ProfileInfo contains an invalid vendorUrl string" ); } - - profile.setDescription( profileInfoElement.getAttribute( "description" ) ); - - return profile; - } - - //------------------------------------------------------------------------- - // Package methods - // - - /** - * Parses the XML file into a valid XML document for the profile DTD - * @param xmlFileName the XML file name - * @exception jpos.profile.ProfileException if the XML file could not be parsed - */ - Document parse( String xmlFileName ) throws ProfileException - { - XmlHelper xmlHelper = new XmlHelper(); - - try - { - xmlHelper.setDtdFileName( PROFILE_DTD_FILE_NAME ); - xmlHelper.setDtdFilePath( PROFILE_DTD_FILE_PATH ); - xmlHelper.checkAndCreateTempDtd(); - - DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - docFactory.setNamespaceAware( true ); - docFactory.setValidating( true ); - - DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - - DefaultErrorHandler errorHandler = this.new DefaultErrorHandler(); - docBuilder.setErrorHandler( errorHandler ); - - Document document = docBuilder.parse( new File( xmlFileName ) ); - - if( !errorHandler.getErrorList().isEmpty() || - !errorHandler.getFatalErrorList().isEmpty() ) - { - String msg = "Error while parsing XML file, set properties"+ - " jpos.tracing = ON in jpos.properties" + - " file for details"; - throw new ProfileException( msg ); - } - - return document; - } - catch( IOException ioe ) - { - String msg = "Error loading XML profile file"; - tracer.println( msg + ": Exception.message = " + ioe.getMessage() ); - throw new ProfileException( msg, ioe ); - } - catch( SAXException se ) - { - String msg = "Error parsing XML profile file"; - tracer.println( msg + ": Exception.message = " + se.getMessage() ); - throw new ProfileException( msg, se ); - } - catch( ParserConfigurationException pce ) - { - String msg = "Error creating XML parser"; - tracer.println( msg + ": Exception.message = " + pce.getMessage() ); - throw new ProfileException( msg, pce ); - } - finally - { xmlHelper.removeTempDtd(); } - } - - /** - * Parses the XML file into a valid XML document for the profile Schemas - * @param xmlFileName the XML file name - * @exception jpos.profile.ProfileException if the XML file could not be parsed - */ - Document parseSchema( String xmlFileName ) throws ProfileException - { - try - { - DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - docFactory.setNamespaceAware( true ); - docFactory.setValidating( true ); - - DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - - DefaultErrorHandler errorHandler = this.new DefaultErrorHandler(); - docBuilder.setErrorHandler( errorHandler ); - - return docBuilder.parse( new File( xmlFileName ) ); - } - catch( IOException ioe ) - { - String msg = "Error loading XML profile file"; - tracer.println( msg + ": Excpetion.message = " + ioe.getMessage() ); - throw new ProfileException( msg, ioe ); - } - catch( SAXException se ) - { - String msg = "Error parsing XML profile file"; - tracer.println( msg + ": Exception.message = " + se.getMessage() ); - - throw new ProfileException( msg, se ); - } - catch( ParserConfigurationException pce ) - { - String msg = "Error creating XML parser"; - tracer.println( msg + ": Exception.message = " + pce.getMessage() ); - throw new ProfileException( msg, pce ); - } - } - - /** - * Loads the Profile specified in the xmlFileName as a Profile object - * @param xmlFileName the XML file name - * @exception jpos.profile.ProfileException if the XML file could not be - * parsed and the profile created - */ - Profile load( String xmlFileName ) throws ProfileException - { - Document document = parse( xmlFileName ); - - return extractProfile( document ); - } - - //------------------------------------------------------------------------- - // Public methods - // - - /** - * @return a Profile object created parsing the XML file provided - * @param xmlProfileFileName the XML profile file - * @exception jpos.profile.ProfileException if there is an error loading the profile - */ - public Profile createProfile( String xmlProfileFileName ) throws ProfileException - { - return load( xmlProfileFileName ); - } - - //------------------------------------------------------------------------- - // Instance variables - // - - private Tracer tracer = TracerFactory.getInstance(). - createTracer( "XercesProfileFactory" ); - - //------------------------------------------------------------------------- - // Inner classes - // - - /** - * ErrorHandler inner class used to capture errors while parsing XML document - * @since 1.3 (Washington DC 2001 meeting) - * @author E. Michael Maximilien (maxim@us.ibm.com) - */ - class DefaultErrorHandler implements org.xml.sax.ErrorHandler - { - //--------------------------------------------------------------------- - // Package methods - // - - List getErrorList() { return errorList; } - - List getWarningList() { return warningList; } - - List getFatalErrorList() { return fatalErrorList; } - - //--------------------------------------------------------------------- - // Public methods - // - - public void warning( SAXParseException e ) throws SAXException - { - tracer.println( "Line " + e.getLineNumber() + ": WARNING SAXParseException.message = " + e.getMessage() ); - warningList.add( e ); - } - - public void error( SAXParseException e ) throws SAXException - { - tracer.println( "Line " + e.getLineNumber() + ": ERROR SAXParseException.message = " + e.getMessage() ); - errorList.add( e ); - } - - public void fatalError( SAXParseException e ) throws SAXException - { - tracer.println( "Line " + e.getLineNumber() + ": FATALERROR SAXParseException.message = " + e.getMessage() ); - fatalErrorList.add( e ); - } - - //--------------------------------------------------------------------- - // Private variables - // - - private final List warningList = new ArrayList<>(); - private final List errorList = new ArrayList<>(); - private final List fatalErrorList = new ArrayList<>(); - } - - //------------------------------------------------------------------------- - // Class constants - // - - public static final String PROFILE_DTD_FILE_NAME = "jcl_profile.dtd"; - public static final String PROFILE_DTD_FILE_PATH = "jpos" + File.separator + "res" + File.separator; -} From d87273a1c6dadd627760e4b7667387a8ea23baa8 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Thu, 18 Jul 2024 18:20:18 +0200 Subject: [PATCH 46/48] load() at composite populators throw a more specific runtime exception in case there is a configuration error which can not be recovered from. --- CHANGELOG.md | 1 + src/main/java/jpos/config/DefaultCompositeRegPopulator.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40b5cb6..e2c055c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 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` diff --git a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java index b0d3341..96096b9 100644 --- a/src/main/java/jpos/config/DefaultCompositeRegPopulator.java +++ b/src/main/java/jpos/config/DefaultCompositeRegPopulator.java @@ -255,7 +255,7 @@ public void load() 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" ); } @SuppressWarnings("unchecked") From f075aaa4a65cb98209fa112d213b4e22e3c41aa6 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Thu, 18 Jul 2024 18:22:36 +0200 Subject: [PATCH 47/48] Code cleanup: made code less complex by refactoring out code to private methods for better readability. --- .../config/simple/xml/JavaxRegPopulator.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java index 340faf4..4b61fac 100644 --- a/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java +++ b/src/main/java/jpos/config/simple/xml/JavaxRegPopulator.java @@ -383,8 +383,6 @@ public void startElement(String uri, String lname, String qname, Attributes attr else if (qname.equals(XML_TAG_JPOSENTRY)) currentEntry = new SimpleEntry(attrs.getValue("logicalName"), JavaxRegPopulator.this); else if (currentEntry != null) { - String temp; - 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)); @@ -399,23 +397,26 @@ else if (currentEntry != null) { 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)) { - temp = attrs.getValue(XML_ATTR_TYPE); - try { - Class type = temp == null ? String.class : Class.forName("java.lang." + temp); - Object obj = null; - 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); - } + 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); } From 24aea5a334e39028e32814c0aa2cc8d84e097c89 Mon Sep 17 00:00:00 2001 From: Denis Kuniss Date: Thu, 18 Jul 2024 18:29:22 +0200 Subject: [PATCH 48/48] Code improvements regarding try-with-resouce and Files.delete() in XMLHelper.java --- src/main/java/jpos/util/XmlHelper.java | 53 ++++++++++---------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/src/main/java/jpos/util/XmlHelper.java b/src/main/java/jpos/util/XmlHelper.java index a5d6393..350fec3 100644 --- a/src/main/java/jpos/util/XmlHelper.java +++ b/src/main/java/jpos/util/XmlHelper.java @@ -77,7 +77,7 @@ public void checkAndCreateTempDtd() File dtdFile = new File( dtdJarFullFileName ); if( dtdFile.exists() ) - return; + return; if( !dtdPath.exists() ) { @@ -94,7 +94,7 @@ public void checkAndCreateTempDtd() tracer.println( "Got DTD InputStream from current ClassLoader" ); if( is != null ) - readAndCreateTempDtdFile( is ); + readAndCreateTempDtdFile( is ); } catch( IOException ioe ) { @@ -121,7 +121,7 @@ public void removeTempDtd() if( createdTempDTD ) { File dtdFile = new File( dtdJarFullFileName ); - dtdFile.delete(); + Files.delete(dtdFile.toPath()); if( createdTempDir ) removeDirs( dtdFilePath ); @@ -148,36 +148,25 @@ 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 ); } /**