>> The problem is I need to use this in java not in servlet so I cannot
store
>> it in context.
You should be able to create a singleton class - or a holder/cache class
where you can put your list. Then the data will be shareable throughout a
single JVM.
Here's a code snip for an abstract class of "Reference Code" - where
children classes are basically classes of key/value pairs that are to be
shared (retrieved from the database once - and used throughout the
application). For example - a list of states and state codes in the US.
(Since the number of states in the US doesn't change very often - you can
read them once and cache them). Notice the inner class is static... The
class ReferenceList (not included) subclasses ArrayList.
==============
import java.util.ArrayList;
import java.util.Date;
import java.lang.reflect.*;
import java.util.*;
/**
* Reference Code - a combination of code/description.
* This class provides caching mechanism to maintain & contain lists of
* ReferenceCode values; assuming the subclasses will know how to build the
list.
*
* Replaces obsolute class ONLReference.
*
* Class objectives:
* Client side caching (since codes are relative static) reducing RIM
lookup.
* Subclass codes - so each class can have own cache and cache-age
specifications.
* All fetching should be done by this class (simplifying API client
needs to use).
* All fetched generated lists should be ordered alphabetically by key
* or table designated sequence order and key; and have the ability
* to return a re-ordered list ordered by value.
* The Return List should support interfaces like enumeration, iterator,
and SortedSet).
*
* @todo - Synchronization - to ensure no multple user access problems.
*/
public abstract class ReferenceCode implements java.util.Map.Entry,
java.io.Serializable {
private String code;
private String description;
protected ReferenceCode() {} // just for testing. Delete.
public ReferenceCode(String aCode, String aDescription) {
code = aCode;
description = aDescription;
}
public static /* abstract */ ReferenceCode getInstance(String aCode,
String aDescription) {
return null;
}
// code - description approach.
public String code() { return code; }
public String description() { return description; }
public String toString() { return this.getClass() +" "+ this.code() +" "+
this.description(); }
// getXXX - for that standard.
public String getCode() { return this.code(); }
public String getDescription() { return this.description(); };
// To support interface...
public Object getKey() { return this.getCode(); }
public Object getValue() { return this.getDescription(); }
public Object setValue(Object o) {
// ReferenceCode rtnValue = new
ReferenceCode(this.code(),this.description());
this.code = ((ReferenceCode)o).getCode();
this.description = ((ReferenceCode)o).getDescription();
return (Object) null; // should return the old object.
}
// exists for Interface.
public boolean equals(Object o) { return equals(this,o); }
public static boolean equals(Object o1, Object o2) {
ReferenceCode e1 = (ReferenceCode) o1;
ReferenceCode e2 = (ReferenceCode) o2;
return
(e1.getKey()==null ?
e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
(e1.getValue()==null ?
e2.getValue()==null : e1.getValue().equals(e2.getValue()));
}
public int compare(Object o) { return compare(this,o); }
public static int compare(Object o1,Object o2) {
ReferenceCode e1 = (ReferenceCode) o1;
ReferenceCode e2 = (ReferenceCode) o2;
int rtnValue = ((String) e1.getKey()).compareTo(e2.getKey());
if ( rtnValue == 0 ) rtnValue =
((String)e1.getValue()).compareTo(e2.getValue());
return rtnValue;
}
/**
* ONLReference "interface" - for backward compability.
* @deprecated
* @see #code(), description()
*/
public String getKeyName() { return code(); };
public String getKeyValue() { return description(); };
//
==========================================================================
// Caching ...
protected static class Cache {
private ReferenceList list = null;
private Date cacheTimestamp = null;
private int maxCacheAgeInMilliSeconds = 5 * 60 * 1000; // 5 minute for
testing
// = 24 * 60 * 60* 1000; // One day.
private Method constructListMethod = null;
public Cache() { }
public Cache(int maxAgeInSeconds ) { this.maxCacheAgeInMilliSeconds =
maxAgeInSeconds * 1000; }
public synchronized void setCache(ReferenceList aList) {
if ( aList != null ) {
this.list = aList;
this.cacheTimestamp = new Date();
}
}
public ReferenceList list() {
if ( this.constructListMethod == null ) return this.list; // Cannot
rebuild cache.
else return list(this.constructListMethod);
}
public ReferenceList list(Method aMethod) {
if ( this.list == null || this.isCacheStale() ) {
try { // Create ReferenceList from subclass method.
setCache((ReferenceList) aMethod.invoke(null,null)); }
catch (Exception ex) { ex.printStackTrace(); }
}
return this.list;
}
public void expireCache() { this.cacheTimestamp = null; }
private boolean isCacheStale() {
return ( list == null
|| cacheTimestamp == null
|| ( cacheTimestamp.getTime() + maxCacheAgeInMilliSeconds ) < (new
Date()).getTime() );
}
}
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html