KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > httpsession > beanimpl > ejb > ClusteredHTTPSessionBeanImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.ha.httpsession.beanimpl.ejb;
9
10
11 import java.rmi.RemoteException JavaDoc;
12
13 import javax.ejb.EJBException JavaDoc;
14
15 import org.jboss.ha.httpsession.interfaces.SerializableHttpSession;
16 import org.jboss.invocation.MarshalledValue;
17
18 /**
19  * Core implementation of methods for the bean.
20  *
21  * @see org.jboss.ha.httpsession.beanimpl.interfaces.ClusteredHTTPSession
22  * @see org.jboss.ha.httpsession.beanimpl.ejb.ClusteredHTTPSessionBeanAbstract
23  *
24  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
25  * @version $Revision: 1.4 $
26  */

27 public abstract class ClusteredHTTPSessionBeanImpl extends ClusteredHTTPSessionBeanAbstract
28 {
29
30    // Constants -----------------------------------------------------
31

32    // Attributes ----------------------------------------------------
33

34    protected SerializableHttpSession tmpSession = null;
35    protected boolean isModified = false;
36
37    // Static --------------------------------------------------------
38

39    // Constructors --------------------------------------------------
40

41    // Public --------------------------------------------------------
42

43    // ClusteredHTTPSessionBeanAbstract overrides ---------------------------------------------------
44

45    public void ejbStore () throws EJBException JavaDoc, RemoteException JavaDoc
46    {
47       if (tmpSession != null)
48       {
49          // the tmpSession has been assigned. Furthermore, if ejbStore is called
50
// it means that isModified==true => we need to rebuild a serialized representation
51
//
52
serializeSession();
53       }
54    }
55
56    public void ejbLoad () throws EJBException JavaDoc, RemoteException JavaDoc
57    {
58       // the tmp value is no more valid: a new serialized representation is just loaded.
59
// it will be transformed only if explicitly asked.
60
//
61
tmpSession = null;
62       isModified = false;
63    }
64
65    public SerializableHttpSession getSession ()
66    {
67       if (tmpSession == null)
68       {
69          // this is the first access to the object representation.
70
// we use a lazy scheme => we unserialize now
71
unserializeSession ();
72       }
73       return this.tmpSession;
74    }
75
76    public void setSession (SerializableHttpSession session)
77    {
78       if (tmpSession == null)
79          isModified = true;
80       else
81          isModified = session.areAttributesModified (tmpSession);
82
83       // in any case, we update the "time" attributes
84
//
85
this.setCreationTime (session.getContentCreationTime ());
86       this.setLastAccessedTime (session.getContentLastAccessTime ());
87
88       // in any cases, we assign the new session: this is because the session
89
// may have internal data that is not used for the isModified comparison
90
// (such as last access time). Consequently, if we use a load-balancer with
91
// sticky sessions, the values will be kept in cache correctly whereas if we
92
// don't have sticky session, these values will only be clustered-saved if
93
// an attributed is modified!
94
//
95
this.tmpSession = session;
96    }
97
98     // Optimisation: called by the CMP engine
99
//
100
public boolean isModified () { return this.isModified; }
101
102    // Package protected ---------------------------------------------
103

104    // Protected -----------------------------------------------------
105

106    protected void serializeSession() throws EJBException JavaDoc
107    {
108       try
109       {
110          this.setSerializedSession (new MarshalledValue (this.tmpSession));
111       }
112       catch (Exception JavaDoc e)
113       {
114          throw new EJBException JavaDoc (e.toString ());
115       }
116    }
117
118    protected void unserializeSession() throws EJBException JavaDoc
119    {
120       try
121       {
122          MarshalledValue mo = (MarshalledValue)this.getSerializedSession ();
123          if (mo != null)
124             this.tmpSession = (SerializableHttpSession)(mo.get ());
125       }
126       catch (Exception JavaDoc e)
127       {
128          throw new EJBException JavaDoc (e.toString ());
129       }
130    }
131
132    // Private -------------------------------------------------------
133

134    // Inner classes -------------------------------------------------
135

136 }
137
Popular Tags