Baffling generics error message.
I am puzzled. I have serialisation code similar to this that works
fine, but this one is giving me the following warning:
[javac] E:\com\mindprod\bulk\CachedMailProber.java:107: warning:
[unchecked]
unchecked cast
[javac] found : java.lang.Object
[javac] required:
java.util.HashMap<java.lang.String,com.mindprod.bulk.Domai
nStatus>
[javac] domainCache = (HashMap<String,
DomainStatus>)ois.readObj
ect();
[javac]
I have put <><><><><> above the line in question.
package com.mindprod.bulk;
import static com.mindprod.bulk.CustConfig.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
/**
* A persistent cache of DomainStatuses, e.g. where a given domain has
a working
* mailserver. Has provision to cull deadwood. Uses serialisation. The
idea is
* to avoid the overhead of probing a mailserver For every email
validation.
*
* @since 2002 April 13
*/
public class CachedMailProber
{
// ------------------------------ FIELDS
------------------------------
/**
* collection of previously probed Domains along with their
statuses.
*/
static private HashMap<String, DomainStatus> domainCache;
// -------------------------- PUBLIC STATIC METHODS
--------------------------
/**
* save the domainCache in serialised form.
*/
public static void flush()
{
try
{
// C U L L
/* get rid of old junk just before we save */
for ( Iterator iter =
domainCache.values().iterator(); iter.hasNext(); )
{
DomainStatus d = (DomainStatus) iter.next();
if ( d.shouldCull() )
{
iter.remove();// avoid
ConcurrentModificationException
}
}// end for
// O P E N
FileOutputStream fos =
new FileOutputStream( CUST_ABBREVIATION +
"domaincache.ser",
false
/* append */ );
ObjectOutputStream oos = new ObjectOutputStream( fos );
// W R I T E
oos.writeObject( domainCache );
// C L O S E
oos.close();
}
catch ( IOException e )
{
System.out
.println( "Troubles writing out the
domaincache.ser file" );
e.printStackTrace();
}
}// end close
// -------------------------- STATIC METHODS
--------------------------
static
{
/* load up serialised domainCache from before */
fireup();
}
/**
* Called when class is shut down to save the domainCache in
serialised
* form.
*/
private static void close()
{
flush();
domainCache = null;
}
/**
* Called when class is loaded to reconstitute the serialised
domainCache.
* Caled automatically when class is loaded.
*/
private static void fireup()
{
try
{
// O P E N
FileInputStream fis =
new FileInputStream( CUST_ABBREVIATION
+ "domaincache.ser" );
ObjectInputStream ois = new ObjectInputStream( fis );
// R E A D
<><><><><>
domainCache = (HashMap<String,
DomainStatus>)ois.readObject();
// C L O S E
ois.close();
}
catch ( Exception e )
{
System.out
.println(
"Troubles restoring the domaincache.ser
file. Not to worry, starting afresh." );
domainCache = new HashMap<String, DomainStatus>( 2053,
0.75f );
}
/* seed the special exceptions. Might be already there, no
problem */
for ( String treatAsGoodDomain : TREAT_AS_GOOD_DOMAINS )
{
domainCache.put( treatAsGoodDomain,
new DomainStatus( treatAsGoodDomain, true
) );
}
for ( String treatAsBadDomain : TREAT_AS_BAD_DOMAINS )
{
domainCache.put( treatAsBadDomain,
new DomainStatus( treatAsBadDomain, false
) );
}
}// end fireup
/**
* Test the mailserver status of a domain.
*
* @param domain name of domain, (will be automatically trimmed
and
* converted internally to lower case), whose status
we are
* interested in e.g. mindprod.com or
oberon.ark.com, not the
* mailserver name, not an email address.
*
* @return true if that domain existed in DNS records at the last
probe, and
* one of whose mailservers responded at least one in
recent probes.
* Reprobes as necessary.
*/
private static boolean isMailServerForDomainAlive( String domain )
{
if ( domain == null )
{
return false;
}
domain = domain.trim().toLowerCase();
DomainStatus d = domainCache.get( domain );
if ( d == null )
{
/* not in domainCache yet, put it there. */
d = new DomainStatus( domain );
domainCache.put( domain, d );
}
return d.isGood();
}
/**
* Tests if mailserver corresponding to that email is up and
running.
*
* @param email email address you are proposing to send to.
*
* @return true if that the corresponding mailserver is working,
or was
* working recently.
*/
static boolean isMailServerForEmailAlive( String email )
{
if ( email == null )
{
return false;
}
email = email.trim().toLowerCase();
int lastAt = email.lastIndexOf( '@' );
if ( lastAt < 0 )
{
return false;
}
String domain = email.substring( lastAt + 1 );
return isMailServerForDomainAlive( domain );
}
// --------------------------- main() method
---------------------------
/**
* test driver
*
* @param args sample email addresses to test for working
mailservers
*/
public static void main( String[] args )
{
for ( String email : args )
{
System.out
.println( email
+ " "
+ isMailServerForEmailAlive( email ) );
}
close();
}
}
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com