📄 simpleregpopulator.java
字号:
package jpos.config.simple;
///////////////////////////////////////////////////////////////////////////////
//
// 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://oss.software.ibm.com/developerworks/opensource/license-cpl.html
//
///////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.net.URL;
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
* serialized set of entries
* <p>
* <b>NOTE</b>: 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
* </p>
* @see jpos.util.JposProperties#JPOS_REG_POPULATOR_CLASS_PROP_NAME
* @since 1.2 (NY 2K 99 meeting)
* @author E. Michael Maximilien (maxim@us.ibm.com)
*/
public class SimpleRegPopulator extends AbstractRegPopulator
{
//-------------------------------------------------------------------------
// Ctor(s)
//
/**
* Default ctor
* @since 1.2 (NY 2K meeting)
*/
public SimpleRegPopulator()
{ super( SimpleRegPopulator.class.getName() ); }
/**
* 1-arg ctor that takes the unique ID string
* @param s the unique ID string
* @since 1.3 (Washington DC 2001 meeting)
*/
public SimpleRegPopulator( 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 SimpleRegPopulator.class.getName(); }
/**
* 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
{
saveJposEntries( entries );
}
/**
* 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 void save( Enumeration entries, String fileName ) throws Exception
{
File file = new File( fileName );
FileOutputStream fos = new FileOutputStream( file );
saveJposEntries( entries, fos );
fos.close();
}
/**
* Tell the populator to load the entries
* @since 1.2 (NY 2K meeting)
*/
public void load()
{
getJposEntries().clear();
Enumeration entries = readJposEntries();
while( entries.hasMoreElements() )
{
try
{
JposEntry entry = (JposEntry)entries.nextElement();
String logicalName = logicalName = (String)entry.
getPropertyValue( JposEntry.LOGICAL_NAME_PROP_NAME );
if( logicalName != null )
getJposEntries().put( logicalName, entry );
lastLoadException = null;
}
catch( Exception e )
{
lastLoadException = e;
tracer.println( "Error loading serialized JposEntry file: " +
"Exception.message= " + e.getMessage() );
}
}
}
/**
* Loads the entries specified in the fileName
* @param fileName the entries file name
* @since 1.3 (SF 2K meeting)
*/
public void load( String fileName )
{
try
{
getJposEntries().clear();
Enumeration entries = readJposEntries( new FileInputStream( fileName ) );
while( entries.hasMoreElements() )
{
JposEntry entry = (JposEntry)entries.nextElement();
String logicalName = (String)entry.
getPropertyValue( JposEntry.LOGICAL_NAME_PROP_NAME );
if( logicalName != null )
getJposEntries().put( logicalName, entry );
}
lastLoadException = null;
}
catch( Exception e )
{
lastLoadException = e;
tracer.println( "Error loading serialized JposEntry file: " +
"Exception.message=" + e.getMessage() );
}
}
/**
* @return the URL pointing to the entries file loaded or saved
* @since 1.2 (NY 2K meeting)
*/
public URL getEntriesURL()
{
URL url = null;
if( serInZipFile )
url = createURLFromFile( zipSerFile );
else
url = createURLFromFile( serFile );
return url;
}
/**
* @return the name of this populator. This should be a short descriptive name
* @since 1.3 (Washington DC 2001 meeting)
*/
public String getName() { return SIMPLE_REG_POPULATOR_NAME_STRING; }
//--------------------------------------------------------------------------
// Protected methods
//
/**
* Tries to save the entries as a ZipEntry in the ZipFile
* @param entries an Enumeration of JposEntry objects
* NOTE: if the the serialized entries is in a Sip/JAR file then if must be an
* entry in the "root" of the Sip/JAR file...
* Also when saving in a Zip/JAR file could get an error because the Zip/JAR file
* is being used by a process in Win32 environment
* @since 1.2 (NY 2K meeting)
* @throws java.lang.Exception if any problems occurs while saving
*/
protected void saveSerInZipFile( Enumeration entries ) throws Exception
{
ZipOutputStream zos = new ZipOutputStream( new
FileOutputStream( zipSerFile.getName() + ".temp.jar" ) );
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
{
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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -