KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > persistence > lib > BasicWorkingSet


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.perseus.persistence.lib;
19
20 import org.objectweb.perseus.persistence.api.ConnectionHolder;
21 import org.objectweb.perseus.persistence.api.TransactionalWorkingSet;
22 import org.objectweb.perseus.persistence.api.State;
23 import org.objectweb.perseus.persistence.api.PersistenceException;
24 import org.objectweb.util.monolog.api.Logger;
25 import org.objectweb.util.monolog.api.BasicLevel;
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.LinkedHashMap JavaDoc;
31
32 /**
33  * It is an implementation of the WorkingSet interface. This implementation is
34  * a Fractal component which needs a MonologFactory.
35  * @author S.Chassande-Barrioz, Y.Bersihand
36  */

37 public class BasicWorkingSet
38         implements TransactionalWorkingSet, BindingController {
39
40     protected Logger logger = null;
41     protected LinkedHashMap JavaDoc oid2state;
42     protected byte status;
43     protected Object JavaDoc userObject;
44     protected ConnectionHolder connectionHolder;
45     protected boolean retainValues = true;
46     protected boolean restoreValues = false;
47     protected boolean rollBackOnly = false;
48
49     public BasicWorkingSet() {
50         status = CTX_ACTIVE;
51         oid2state = new LinkedHashMap JavaDoc();
52         userObject = null;
53         connectionHolder = null;
54     }
55
56     // IMPLEMENTATION OF THE UserBindingController INTERFACE //
57
//-------------------------------------------------------//
58

59     public String JavaDoc[] listFc() {
60         return new String JavaDoc[] { };
61     }
62
63     public Object JavaDoc lookupFc(String JavaDoc c) {
64         return null;
65     }
66
67     public void bindFc(String JavaDoc c, Object JavaDoc s) {
68         if ("logger".equals(c)) {
69             logger = (Logger) s;
70         }
71     }
72
73     public void unbindFc(String JavaDoc c) {
74     }
75
76     // IMPLEMENTATION OF THE WorkingSet INTERFACE //
77
//--------------------------------------------//
78

79     public byte getStatus() {
80         return status;
81     }
82
83     public void setStatus(byte status) throws PersistenceException {
84         this.status = status;
85     }
86
87     /**
88      * @return the entry attached to the transaction weither its identifier
89      */

90     public State lookup(Object JavaDoc oid) {
91         return (State) oid2state.get(oid);
92     }
93
94     /**
95      * Attaches an entry to the transaction.
96      * @param state the state which must be attached to the transaction
97      * @param mode see WorkingSet.READ_INTENTION WorkingSet.WRITE_INTENTION WorkingSet.UNKNOWN_INTENTION
98      * @return the old state value in the cache
99      */

100     public State bind(State state, Object JavaDoc oid, byte mode) {
101         Object JavaDoc old;
102         synchronized(oid2state) {
103             old = oid2state.put(oid, state);
104         }
105         if (logger.isLoggable(BasicLevel.DEBUG)) {
106             if (old == null) {
107                 logger.log(BasicLevel.DEBUG,
108                         "Bind (" + oid + ", " + state + ") to the context");
109             } else {
110                 logger.log(BasicLevel.DEBUG,
111                         "Change the state in the context"
112                         + "\n-oid=" + oid
113                         + "\n-old state=" + old
114                         + "\n- new state=" + state);
115             }
116         }
117         return (State)old;
118     }
119
120     /**
121      * Dettaches an entry from the transaction.
122      * @param oid is the identifier of entry which must be detattached from the transaction
123      */

124     public boolean unbind(Object JavaDoc oid) {
125         if (logger.isLoggable(BasicLevel.DEBUG))
126             logger.log(BasicLevel.DEBUG, "UnBind " + oid + " from the context");
127         synchronized(oid2state) {
128             return (oid2state.remove(oid) == null);
129         }
130     }
131
132     /**
133      * Removes all entries of the transaction.
134      */

135     public void clear() {
136         synchronized(oid2state) {
137             oid2state.clear();
138         }
139     }
140
141     /**
142      * @return the collection of entry attached to the transaction.
143      */

144     public Set entries() {
145         synchronized(oid2state) {
146             return new HashSet JavaDoc(oid2state.values());
147         }
148     }
149
150     public Set oids() {
151         return oid2state.keySet();
152     }
153
154     public Object JavaDoc getUserObject() {
155         return userObject;
156     }
157
158     public ConnectionHolder getConnectionHolder() {
159         return connectionHolder;
160     }
161
162     public boolean getWSRetainValues() {
163         return retainValues;
164     }
165
166     public void setWSRetainValues(boolean val) {
167         retainValues = val;
168     }
169
170     public boolean getWSRestoreValues() {
171         return restoreValues;
172     }
173
174     public void setWSRestoreValues(boolean val) {
175         restoreValues = val;
176     }
177     
178     public boolean getWSRollBackOnly() {
179         return rollBackOnly;
180     }
181     public void setWSRollBackOnly(boolean val) {
182         rollBackOnly = val;
183     }
184 }
185
Popular Tags