KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > pm > lib > SpeedoProxyManagerSwitch


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27 package org.objectweb.speedo.pm.lib;
28
29 import org.objectweb.speedo.pm.api.ProxyManagerSwitch;
30 import org.objectweb.speedo.pm.api.ProxyManager;
31 import org.objectweb.speedo.pm.api.ProxyManagerFactory;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Collections JavaDoc;
38
39 /**
40  * This class is an implementation of the ProxyManagerSwitch based on the use
41  * of a ThreadLocal field. This field contains an instance of ProxyManager of an
42  * ArrayList of ProxyManager. A SpeedoProxyManagerSwitch is a fractal
43  * component which have no bindings.
44  *
45  * @author S.Chassande-Barrioz
46  */

47 public class SpeedoProxyManagerSwitch implements ProxyManagerSwitch {
48
49     protected ThreadLocal JavaDoc pms = new ThreadLocal JavaDoc();
50
51     public final static String JavaDoc BIND_ERROR_MSG = "Impossible to associated to ProxyManager linked to the same PersistenceManagerFactory in the same context (thread)";
52
53     // IMPLEMENTATION OF THE ProxyManagerSwitch INTERFACE //
54
//----------------------------------------------------//
55

56     /**
57      * @param pmf is persistent manager factory which manages the returned
58      * proxy manager.
59      * @return the ProxyManager managed by the given persistence manager factory
60      * and bound to current the context, or the null value if there is no
61      * ProxyManager.
62      */

63     public ProxyManager lookup(ProxyManagerFactory pmf) {
64         Object JavaDoc o = pms.get();
65         if (o == null) {
66             return null;
67         } else if (o instanceof ProxyManager) {
68             if (((ProxyManager) o).getPersistenceManagerFactory() == pmf)
69                 return (ProxyManager) o;
70         } else {
71             for (Iterator JavaDoc it = ((List JavaDoc) o).iterator(); it.hasNext();) {
72                 ProxyManager pm = (ProxyManager) it.next();
73                 if (pm.getPersistenceManagerFactory() == pmf)
74                     return pm;
75             }
76         }
77         return null;
78     }
79
80     /**
81      * It assignes a ProxyManager to the current context.
82      * @param pm is the ProxyManager
83      */

84     public void bind(ProxyManager pm) {
85         Object JavaDoc o = pms.get();
86         if (o == null) {
87             pms.set(pm);
88         } else if (o instanceof ProxyManager) {
89             // There is one ProxyManager, then check if the old and the new
90
// proxy manager are manager by the same PMF.
91
ProxyManager pm1 = (ProxyManager) o;
92             if (pm1.getPersistenceManagerFactory()
93                     == pm.getPersistenceManagerFactory()) {
94                 pms.set(pm);
95             } else {
96                 // Put the old and the new in a List
97
ArrayList JavaDoc al = new ArrayList JavaDoc(3);
98                 al.add(pm1);
99                 al.add(pm);
100                 pms.set(al);
101             }
102         } else {
103             // The element is a list of ProxyManager instances, then check if
104
// there is a proxy manager linked to the same PersistenceManagerFactory
105
List JavaDoc l = (List JavaDoc) o;
106             for (Iterator JavaDoc it = l.iterator(); it.hasNext();) {
107                 if (((ProxyManager) it.next()).getPersistenceManagerFactory()
108                         == pm.getPersistenceManagerFactory()) {
109                     it.remove();
110                 }
111             }
112             //No element with the same pmf, then add the proxy manager
113
((List JavaDoc) o).add(pm);
114         }
115     }
116
117     /**
118      * It clears the list of ProxyManager for the current context.
119      */

120     public void clear() {
121         pms.set(null);
122     }
123
124     /**
125      * It clears a ProxyManager for the current context.
126      */

127     public boolean unbind(ProxyManager pm) {
128         Object JavaDoc o = pms.get();
129         if (o == null) {
130             return false;
131         } else if (o instanceof ProxyManager) {
132             if (o == pm) {
133                 pms.set(null);
134                 return true;
135             }
136         } else if (o instanceof Collection JavaDoc) {
137             return ((Collection JavaDoc) o).remove(pm);
138         }
139         return false;
140     }
141
142     public boolean unbind(ProxyManagerFactory pmf) {
143         Object JavaDoc o = pms.get();
144         if (o == null) {
145             return false;
146         } else if (o instanceof ProxyManager) {
147             if (((ProxyManager) o).getPersistenceManagerFactory() == pmf) {
148                 pms.set(null);
149                 return true;
150             }
151         } else { // The element is a list of ProxyManager instances
152
for (Iterator JavaDoc it = ((List JavaDoc) o).iterator(); it.hasNext();) {
153                 if (((ProxyManager) it.next()).getPersistenceManagerFactory()
154                         == pmf) {
155                     it.remove();
156                     return true;
157                 }
158             }
159         }
160         return false;
161     }
162
163     /**
164      * @return all ProxyManager instances bound with the current context
165      */

166     public Collection JavaDoc entries() {
167         Object JavaDoc o = pms.get();
168         if (o instanceof ProxyManager)
169             return Collections.singletonList(o);
170         else
171             return (Collection JavaDoc) o;
172     }
173 }
174
Popular Tags