In the Netscape Extension Builder sample directory is a subdirectory named Auction, which contains a Java extension that demonstrates the use of state and session management. The Auction extension is a complete example.
The sample application simulates an online auction. On the auction home page you can perform any of the following tasks:
To manage state and session data, perform the following steps:
Implement the IState2 Interface
You add this code in the service module's manager class.
Implement the ISession2 Interface
You add this code in the class representing a user.
Implement the IState2 Interface
The manager class is CAuctionModule and is represented by this file:
sample\auction\java\Auction\myext\CAuctionModule.java
The complete contents of this file, including additional comments, are as follows:
//
// This file is initially generated by KIDL - Edit as
// necessary to complete
//
package Auction.myext;
import com.kivasoft.*;
import com.kivasoft.util.*;
import com.kivasoft.types.*;
import Auction.*;
public class CAuctionModule extends
com.kivasoft.bind.BinderBase
implements com.kivasoft.IModule, Auction.IAuctionModule
{
public com.kivasoft.IContext m_Context;
public static final String m_extensionNodeName =
"NASExtensionNode";
public static final String m_auctionNodeName =
"AuctionExtensionNode";
private IState2 m_auctionState;
private IAuctionPool m_auctionPool;
public CAuctionModule()
{
super();
m_auctionState = null;
m_auctionPool = null;
}
public void finalize()
{
super.finalize();
//***
//*** Add your own code to the destructor here.
//***
}
// use init to set up the state information
public synchronized int init(com.kivasoft.IObject obj) {
int res;
res = super.init(obj);
com.kivasoft.util.Util.dictionaryPut(obj, "Auction.IAuctionModule",
this);
m_Context=(com.kivasoft.IContext) obj;
IModuleState2 stateModule = (IModuleState2)
((IDictionary)m_Context).get("com.kivasoft.IState2Module");
if (stateModule != null)
// get a root node
{
IState2 extensionState =
stateModule.getStateTree(m_extensionNodeName,
GXSTATE.GXSTATE_LOCAL);
if (extensionState != null)
// create an application-specific node
{
IState2 auctionState =
extensionState.createStateChild(m_auctionNodeName,
0, GXSTATE.GXSTATE_LOCAL);
if (auctionState != null)
// populate the state data with merchandise names
// and minimum bid prices
{
IValList merchandiseList =
auctionState.getStateContents();
merchandiseList.setValInt(new
String("15 Inch Monitor"), 250);
merchandiseList.setValInt(new
String("PCS Phone"), 190);
merchandiseList.setValInt(new
String("GPS System"), 1200);
merchandiseList.setValInt(new
String("Web Browser"), 0);
merchandiseList.setValInt(new
String("Pebble Beach Vacation"), 1500);
merchandiseList.setValInt(new
String("DVD Player"), 150);
merchandiseList.setValInt(new
String("Digital Camera"), 300);
auctionState.setStateContents(merchandiseList);
auctionState.saveState(GXSTATE.GXSTATE_LOCAL);
m_auctionState = auctionState;
}
}
}
return res;
}
// return the state interface
public IState2 getState()
{
return m_auctionState;
}
// Helper functions
// Method bodies for interface: IAuctionModule
public Auction.IAuctionPool createAuctionPool()
{
// one pool only
if (m_auctionPool != null)
return m_auctionPool;
else
{
CAuctionPool auctionPool = new CAuctionPool(this);
return auctionPool;
}
}
public Auction.IParticipant getParticipant(
java.lang.String UserId)
{
CParticipant user = new CParticipant(this, UserId);
return user;
}
// if a bid is made, update the state data for the
// corresponding merchandise
public void bid(Auction.IBid bidorder)
{
IValList merchandiseList = getState().getStateContents();
merchandiseList.setValInt(bidorder.getName(),
bidorder.getPrice());
getState().setStateContents(merchandiseList);
getState().saveState(GXSTATE.GXSTATE_LOCAL);
}
// remainder of this file is automatically generated
public static final String
appNameLocal="CAuctionModuleL";
public static final String
appNameDistributed="CAuctionModuleD";
public static final String
appNameCluster="CAuctionModuleC";
public ISession2 getThreadSession(int sessionType)
{
String appName;
ISession2 session=null;
int dwflags;
if ((sessionType & GXSESSION.GXSESSION_LOCAL) > 0)
{
appName = appNameLocal;
dwflags = GXSESSION.GXSESSION_LOCAL;
}
else if ((sessionType & GXSESSION.GXSESSION_CLUSTER) > 0)
{
appName = appNameCluster;
dwflags = GXSESSION.GXSESSION_CLUSTER;
}
else
{
appName = appNameDistributed;
dwflags = GXSESSION.GXSESSION_DISTRIB;
}
IModuleExtensionData extData = (IModuleExtensionData)
((IDictionary)m_Context).get
("com.kivasoft.IModuleExtensionData");
if(extData!=null) {
String sessionID = extData.getSessionID(null);
IModuleSession modSession = (IModuleSession)
((IDictionary)m_Context).get
("com.kivasoft.IModuleSession");
ISessionGroup sessionGroup =
modSession.getSessionGroup(appName);
session = sessionGroup.getSession(sessionID, null, 1);
if (session == null) {
// sync with application's session
int tmpflags = extData.getSessionFlags(null);
if ((tmpflags & GXSESSION.GXSESSION_TIMEOUT_CREATE)
> 0)
dwflags |= GXSESSION.GXSESSION_TIMEOUT_CREATE;
else
dwflags |= GXSESSION.GXSESSION_TIMEOUT_ABSOLUTE;
if ((tmpflags & GXSESSION.GXSESSION_PERSISTENT) > 0)
dwflags |= GXSESSION.GXSESSION_PERSISTENT;
int timeout = extData.getSessionTimeout(null);
session = sessionGroup.createSession(dwflags,
timeout, null, sessionID);
}
return session;
}
return null;
}
}
Implement the ISession2 Interface
In this example, the Participant class corresponds to an individual user of the auction application. The Participant class is represented by this file:
sample\auction\java\Auction\myext\CParticipant.java
The complete contents of this file, including additional comments, are as follows:
//
// This file is initially generated by KIDL - Edit as
// necessary to complete
//
package Auction.myext;
import com.kivasoft.*;
import com.kivasoft.types.*;
public class CParticipant
implements Auction.IParticipant
{
public Auction.myext.CAuctionModule m_Module;
private String m_userId;
public CParticipant(Auction.myext.CAuctionModule module,
String userId)
{
m_Module=module;
m_userId = userId;
}
public void finalize()
{
//***
//*** Add your own code to the destructor here.
//***
}
// Method bodies for interface: IParticipant
// Get a list of your bids
public com.kivasoft.IValList getBiddingList()
{
ISession2 session = m_Module.getThreadSession
(GXSESSION.GXSESSION_DISTRIB);
IValList list = session.getSessionData();
return list;
}
// Put an IBid object into a session
public int submitBid(
Auction.IBid bid)
{
ISession2 session = m_Module.getThreadSession
(GXSESSION.GXSESSION_DISTRIB);
IValList list = session.getSessionData();
// Put data into an IValList.
// For this example, assume the name of
// each merchandise item is unique
list.setValInt(bid.getName(), bid.getPrice());
// Assign the IValList to the session
session.setSessionData(list);
// Save the session data
session.saveSession(GXSESSION.GXSESSION_DISTRIB);
// Submit the bid to the Manager class
m_Module.bid(bid);
return 0;
}
public Auction.IBid newBid(
java.lang.String name,
int price)
{
CBid bid = new CBid(m_userId, name, price);
return bid;
}
}