View Javadoc

1   /***
2    *
3    * Copyright 2003-2005 Core Developers Network Ltd.
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.codehaus.wadi.core.motable;
18  
19  import java.util.concurrent.locks.ReadWriteLock;
20  import java.util.concurrent.locks.ReentrantReadWriteLock;
21  
22  import org.codehaus.wadi.core.eviction.SimpleEvictable;
23  import org.codehaus.wadi.core.eviction.SimpleEvictableMemento;
24  
25  /***
26   * Implement all of Motable except for the Bytes field. This is the field most likely to have different representations.
27   *
28   * @author <a href="mailto:jules@coredevelopers.net">Jules Gosnell</a>
29   * @version $Revision: 2244 $
30   */
31  public abstract class AbstractMotable extends SimpleEvictable implements Motable {
32      protected ReadWriteLock readWriteLock;
33  
34      protected AbstractMotable() {
35          readWriteLock = newReadWriteLock();
36      }
37  
38      @Override
39      protected SimpleEvictableMemento newMemento() {
40          return new AbstractMotableMemento();
41      }
42      
43      public AbstractMotableMemento getAbstractMotableMemento() {
44          return (AbstractMotableMemento) memento;
45      }
46      
47      public ReadWriteLock getReadWriteLock() {
48          return readWriteLock;
49      }
50      
51      public synchronized void init(long creationTime, long lastAccessedTime, int maxInactiveInterval, String name) {
52          init(creationTime, lastAccessedTime, maxInactiveInterval);
53          getAbstractMotableMemento().setName(name);
54      }
55  
56      public synchronized void rehydrate(long creationTime, long lastAccessedTime, int maxInactiveInterval, String name, byte[] body)
57              throws RehydrationException {
58          initExisting(creationTime, lastAccessedTime, maxInactiveInterval, name, body);
59      }
60  
61      public synchronized void restore(long creationTime, long lastAccessedTime, int maxInactiveInterval, String name, byte[] body)
62              throws RehydrationException {
63          initExisting(creationTime, lastAccessedTime, maxInactiveInterval, name, body);
64      }
65  
66      public synchronized void copy(Motable motable) throws Exception {
67          super.copy(motable);
68          getAbstractMotableMemento().setName(motable.getName());
69          getAbstractMotableMemento().setNewSession(false);
70          setBodyAsByteArray(motable.getBodyAsByteArray());
71      }
72  
73      public synchronized void mote(Motable recipient) throws Exception {
74          recipient.copy(this);
75          destroyForMotion();
76      }
77  
78      public synchronized String getName() {
79          return getAbstractMotableMemento().getName();
80      }
81      
82      public synchronized boolean isNew() {
83          return getAbstractMotableMemento().isNewSession();
84      }
85  
86      public synchronized void destroy() throws Exception {
87          super.destroy();
88          getAbstractMotableMemento().setNewSession(false);
89      }
90      
91      @Override
92      protected void onDeserialization() {
93          readWriteLock = newReadWriteLock();
94      }
95      
96      protected ReadWriteLock newReadWriteLock() {
97          return new ReentrantReadWriteLock();
98      }
99  
100     protected synchronized void initExisting(long creationTime,
101             long lastAccessedTime,
102             int maxInactiveInterval,
103             String name,
104             byte[] body) throws RehydrationException {
105         getAbstractMotableMemento().setNewSession(false);
106         init(creationTime, lastAccessedTime, maxInactiveInterval, name);
107         try {
108             setBodyAsByteArray(body);
109         } catch (Exception e) {
110             throw new RehydrationException(e);
111         }
112     }
113 
114     protected void destroyForMotion() throws Exception {
115         super.destroy();
116     }
117     
118 }
119 
120