View Javadoc

1   package org.eclipse.jetty.nosql;
2   //========================================================================
3   //Copyright (c) 2011 Intalio, Inc.
4   //------------------------------------------------------------------------
5   //All rights reserved. This program and the accompanying materials
6   //are made available under the terms of the Eclipse Public License v1.0
7   //and Apache License v2.0 which accompanies this distribution.
8   //The Eclipse Public License is available at
9   //http://www.eclipse.org/legal/epl-v10.html
10  //The Apache License v2.0 is available at
11  //http://www.opensource.org/licenses/apache2.0.php
12  //You may elect to redistribute this code under either of these licenses.
13  //========================================================================
14  
15  import java.util.HashSet;
16  import java.util.Set;
17  import java.util.concurrent.atomic.AtomicInteger;
18  
19  import javax.servlet.http.HttpServletRequest;
20  
21  import org.eclipse.jetty.server.session.AbstractSession;
22  import org.eclipse.jetty.util.log.Log;
23  import org.eclipse.jetty.util.log.Logger;
24  
25  
26  /* ------------------------------------------------------------ */
27  public class NoSqlSession extends AbstractSession
28  {
29      private final static Logger __log = Log.getLogger("org.eclipse.jetty.server.session");
30  
31      private final NoSqlSessionManager _manager;
32      private Set<String> _dirty;
33      private final AtomicInteger _active = new AtomicInteger();
34      private Object _version;
35      private long _lastSync;
36  
37      /* ------------------------------------------------------------ */
38      public NoSqlSession(NoSqlSessionManager manager, HttpServletRequest request)
39      {
40          super(manager, request);
41          _manager=manager;
42          save(true);
43          _active.incrementAndGet();
44      }
45      
46      /* ------------------------------------------------------------ */
47      public NoSqlSession(NoSqlSessionManager manager, long created, long accessed, String clusterId, Object version)
48      {
49          super(manager, created,accessed,clusterId);
50          _manager=manager;
51          _version=version;
52      }
53      
54      /* ------------------------------------------------------------ */
55      @Override
56      public Object doPutOrRemove(String name, Object value)
57      {
58          synchronized (this)
59          {
60              if (_dirty==null)
61                  _dirty=new HashSet<String>();
62              _dirty.add(name);
63              Object old = super.doPutOrRemove(name,value);
64              if (_manager.getSavePeriod()==-2)
65                  save(true);
66              return old;
67          }
68      }
69  
70      /* ------------------------------------------------------------ */
71      @Override
72      protected void checkValid() throws IllegalStateException
73      {
74          super.checkValid();
75      }
76  
77      /* ------------------------------------------------------------ */
78      @Override
79      protected boolean access(long time)
80      {
81          __log.debug("NoSqlSession:access:active "+_active);
82          if (_active.incrementAndGet()==1)
83          {
84              long period=_manager.getStalePeriod()*1000L;
85              if (period==0)
86                  refresh();
87              else if (period>0)
88              {
89                  long stale=time-_lastSync;
90                  __log.debug("NoSqlSession:access:stale "+stale);
91                  if (stale>period)
92                      refresh();
93              }
94          }
95  
96          return super.access(time);
97      }
98  
99      /* ------------------------------------------------------------ */
100     @Override
101     protected void complete()
102     {
103         super.complete();
104         if(_active.decrementAndGet()==0)
105         {
106             switch(_manager.getSavePeriod())
107             {
108                 case 0: 
109                     save(isValid());
110                     break;
111                 case 1:
112                     if (isDirty())
113                         save(isValid());
114                     break;
115 
116             }
117         }
118     }
119 
120     /* ------------------------------------------------------------ */
121     @Override
122     protected void doInvalidate() throws IllegalStateException
123     {
124         super.doInvalidate();
125         save(false);
126     }
127     
128     /* ------------------------------------------------------------ */
129     protected void save(boolean activateAfterSave)
130     {
131         synchronized (this)
132         {
133             _version=_manager.save(this,_version,activateAfterSave);
134             _lastSync=getAccessed();
135         }
136     }
137 
138 
139     /* ------------------------------------------------------------ */
140     protected void refresh()
141     {
142         synchronized (this)
143         {
144             _version=_manager.refresh(this,_version);
145         }
146     }
147 
148     /* ------------------------------------------------------------ */
149     public boolean isDirty()
150     {
151         synchronized (this)
152         {
153             return _dirty!=null && !_dirty.isEmpty();
154         }
155     }
156     
157     /* ------------------------------------------------------------ */
158     public Set<String> takeDirty()
159     {
160         synchronized (this)
161         {
162             Set<String> dirty=_dirty;
163             if (dirty==null)
164                 dirty= new HashSet<String>();
165             else
166                 _dirty=null;
167             return dirty;
168         }
169     }
170 
171     /* ------------------------------------------------------------ */
172     public Object getVersion()
173     {
174     	return _version;
175     }
176     
177 }