📄 simpleregpopulator.java
字号:
zos.flush();
zos.close();
}
/**
* Tries to save the entries in the file where they were loaded
* @param entries an Enumeration of JposEntry objects
* @since 1.2 (NY 2K meeting)
* @throws java.lang.Exception if any problems occurs while saving
*/
protected void saveSerFile( Enumeration entries ) throws Exception
{
saveJposEntries( entries, new FileOutputStream( serFileName ) );
}
/**
* Save the JposEntry object to the OutputStream as serialized objects
* @param entries an enumeration of JposEntry objects
* @param os the OuputStream to save to
* @since 1.2 (NY 2K meeting)
* @throws java.lang.Exception if any error occurs while saving
*/
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 );
}
oos.close();
}
/**
* @return an ObjectInputStream of the first serFileName found. The algorithm is:
* 1) Goes through the CLASSPATH and get the first serialized file name in the paths in order
* 2) If no simple ser file is found then looks in the JAR files in order
* @since 1.2 (NY 2K meeting)
*/
protected ObjectInputStream findSerOIS()
{
Vector classpathJarFiles = new Vector();
//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
ObjectInputStream ois = findSerOISInClasspath( classpathJarFiles );
//If no serialized file found in the directories of the path in the CLASSPATH then
//try to open each JAR/Zip file and see if they contain a serialized file
if( ois == null )
ois = findSerOISInJar( classpathJarFiles );
return ois;
}
/**
* Finds the first serialized JposEntry file in directory of each classpath
* <b>NOTE:</b>Decorated the FileInputStream with a BufferedInputStream to
* improve load time...
* @param jarZipFilesVector a vector of JAR/Zip file names
* @since 1.2 (NY 2K meeting)
*/
protected ObjectInputStream findSerOISInClasspath( Vector jarZipFilesVector )
{
ObjectInputStream ois = null;
String classpath = System.getProperty( "java.class.path" );
String pathSeparator = System.getProperty( "path.separator" );
String fileSeparator = System.getProperty( "file.separator" );
String path = "";
//Searches for the serialized JposEntry file
for( StringTokenizer st = new StringTokenizer( classpath, pathSeparator, false );
st.hasMoreTokens(); )
{
try
{
path = st.nextToken().trim();
if( path.equals("") ) continue;
if( path.length() > 4 && ( path.endsWith( ".zip" ) || path.endsWith( ".jar" ) ) )
jarZipFilesVector.addElement( path );
else
{
absoluteFileName = path + fileSeparator + serFileName;
ois = new ObjectInputStream( new BufferedInputStream( new FileInputStream( absoluteFileName ) ) );
serFile = new File( absoluteFileName );
serInZipFile = false;
break;
}
}
catch( Exception e ) { continue; }
}
return ois;
}
/**
* Finds the first serialized JposEntry file in the JAR files
* @param jarFilesVector a vector of JAR/Zip file names
* @since 1.2 (NY 2K meeting)
*/
protected ObjectInputStream findSerOISInJar( Vector jarFilesVector )
{
ObjectInputStream ois = null;
for( int i = 0; i < jarFilesVector.size(); ++i )
{
String jarFileName = (String)jarFilesVector.elementAt( i );
try
{
ZipFile zipFile = new ZipFile( jarFileName );
Enumeration zipEntries = zipFile.entries();
while( zipEntries.hasMoreElements() )
{
ZipEntry zipEntry = (ZipEntry)zipEntries.nextElement();
String entryName = zipEntry.getName();
if( entryName.endsWith( serFileName ) )
{
ois = new ObjectInputStream( zipFile.getInputStream( zipEntry ) );
zipSerFile = zipFile;
serInZipFile = true;
break;
}
}
}
catch( Exception e ) {}
if( ois != null ) break;
}
return ois;
}
/**
* Searches the current class path for the serialized file and un-serializes the JposEntry objects
* @return an Enumeration of JposEntry objects un-serializes from the file specified
* @param is the InputStream from which to read the serialized entries from
* @since 1.2 (NY 2K meeting)
*/
protected Enumeration readJposEntries( InputStream is )
{
Vector entries = new Vector();
try
{
//Added in 1.3 (SF-2K meeting) (not elegant since need to do
//a instanceof operation but works :-)
ObjectInputStream in = null;
if( is instanceof ObjectInputStream )
in = (ObjectInputStream)is;
else
if( is != null )
in = new ObjectInputStream( is );
if( in == null )
tracer.println( "Can't find serialized JposEntry file: " +
serFileName );
else
while( true )
entries.addElement( in.readObject() );
serFileName = absoluteFileName;
}
catch( EOFException eofe ) {}
catch( Exception e )
{ tracer.println( "ERROR while reading serialized JposEntry file: " +
serFileName + " Exception.message=" +
e.getMessage() ); }
return entries.elements();
}
/**
* @return an Enumeration of JposEntry objects
* @since 1.2 (NY 2K meeting)
*/
protected Enumeration readJposEntries()
{
Enumeration entries = null;
if( isPopulatorFileDefined() )
try { entries = readJposEntries( getPopulatorFileIS() ); }
catch( Exception e )
{ entries = ( new Vector() ).elements(); }
else
entries = readJposEntries( findSerOIS() );
return entries;
}
/**
*
* @param entries an enumeration of JposEntry objects
* @since 1.2 (NY 2K meeting)
* @throws java.lang.Exception if any error occurs while saving
*/
protected void saveJposEntries( Enumeration entries ) throws Exception
{
if( isPopulatorFileDefined() )
saveJposEntries( entries, getPopulatorFileOS() );
else
{
if( serInZipFile )
saveSerInZipFile( entries );
else
saveSerFile( entries );
}
}
//--------------------------------------------------------------------------
// Instance variables
//
private File serFile = null;
private ZipFile zipSerFile = null;
private boolean serInZipFile = false;
private String absoluteFileName = "";
private String serFileName = DEFAULT_JPOS_SER_FILE_NAME;
private Tracer tracer = TracerFactory.getInstance().
createTracer( "SimpleRegPopulator" );
//--------------------------------------------------------------------------
// Class constants
//
/**
* The default serialized JposEntry file name
* @since 1.2 (NY 2K meeting)
*/
public static final String DEFAULT_JPOS_SER_FILE_NAME = "jpos.cfg";
/**
* A tempory file name used for temporary storage
* @since 1.2 (NY 2K meeting)
*/
public static final String TEMP_SER_FILE_NAME = "__jpos_temp.cfg";
/**
* The default name for the SimpleRegPopulator
* @since 1.3 (Washington DC 2001 meeting)
*/
public static final String SIMPLE_REG_POPULATOR_NAME_STRING = "JCL Serialized Entries Populator";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -