KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > naming > lib > BasicPBinder


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
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  * Contact: jorm-team@objectweb.org
21  *
22  */

23 package org.objectweb.jorm.naming.lib;
24
25 import org.objectweb.jorm.naming.api.PBinder;
26 import org.objectweb.jorm.naming.api.PName;
27 import org.objectweb.jorm.naming.api.PExceptionNaming;
28 import org.objectweb.jorm.api.PBinding;
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.api.PExceptionProtocol;
31 import org.objectweb.jorm.api.PBindingCtrl;
32 import org.objectweb.jorm.api.PStateGraph;
33 import org.objectweb.jorm.api.PClassMapping;
34 import org.objectweb.perseus.cache.api.CacheEntry;
35 import org.objectweb.perseus.cache.api.CacheException;
36 import org.objectweb.perseus.cache.api.FixableCacheEntry;
37 import org.objectweb.perseus.cache.api.CacheManager;
38
39 /**
40  * It defines the a basic PBinder which able to be also a PNamingContext.
41  *
42  * @author S.Chassande-Barrioz
43  */

44 public abstract class BasicPBinder
45         extends BasicPNamingContext
46         implements PBinder {
47
48     protected CacheManager cache;
49     private PClassMapping pcm;
50     protected PName nullPName;
51
52     public BasicPBinder() {
53     }
54
55     public BasicPBinder(PName nullPName) {
56         this.nullPName = nullPName;
57     }
58
59     public String JavaDoc getClassName() {
60         return ptype.getJormName();
61     }
62
63
64     // IMPLEMENTATION OF THE METHOD OF PBinder INTERFACE //
65
//---------------------------------------------------//
66

67     public PBinding lookup(PName pn) throws PException {
68         if (pn == null) {
69             throw new PExceptionNaming("[" + getClassName() + "]: this pname is null");
70         }
71         if (pn.isNull()) {
72             return null;
73         }
74         if (cache == null) {
75             throw new PExceptionProtocol("No internal cache: should be managed externally!");
76         }
77         CacheEntry ce = cache.lookup(pn);
78         if (ce != null) {
79             return (PBinding) ce.getCeObject();
80         }
81         return null;
82     }
83
84     public void bind(PName pn, PBindingCtrl pb) throws PException {
85         if (pn == null)
86             throw new PExceptionNaming("[" + getClassName() + "]: this pname is null");
87         if (pb == null)
88             throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is null");
89         if (!pn.getClass().equals(getNull().getClass()))
90             throw new PExceptionNaming("Can only bind " + getNull().getClass() + ", found: " + pn);
91         if (!pn.getPNameManager().equals(this))
92             throw new PExceptionNaming("[" + getClassName() + "]: this pname is not valid in this binder ("
93                                        + ", \nthis:" + this
94                                        + ", \nthis.type:" + this.getPType().getJormName()
95                                        + ", \nthis.pcm:" + this.getBinderClassMapping()
96                                        + ", \npn.pnc: " + pn.getPNameManager()
97                                        + ", \npn.pnc.type: " + pn.getPNameManager().getPType().getJormName()
98                                        + ", \npn.pnc.pcm: " + ((PBinder) pn.getPNameManager()).getBinderClassMapping()
99                                        + ", \npn:" + pn + ")");
100         if (!pb.getPClassMapping().equals(pcm))
101             throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is not valid in this binder");
102         if (pn.isNull())
103             throw new PExceptionNaming("[" + getClassName() + "]: this PName represent the null value");
104         byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(),
105                                                        PBinding.ACTION_BIND);
106         // Performs the transition
107
if (cache != null) {
108             synchronized (cache) {
109                 try {
110                     cache.fix(cache.bind(pn, pb));
111                 } catch (CacheException e) {
112                     throw new PException(e, "[" + getClassName() + "]: problem with cache management");
113                 }
114             }
115         }
116         pb.setPName(pn);
117         pb.setStatus(nextstate);
118     }
119
120     public PClassMapping getBinderClassMapping() {
121         return pcm;
122     }
123
124     public void setPClassMapping(PClassMapping pcm) {
125         this.pcm = pcm;
126     }
127
128     public void unbind(PBindingCtrl pb) throws PException {
129         byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(),
130                                                        PBinding.ACTION_UNBIND);
131         // If the state does not change, do nothing
132
if (nextstate == pb.getStatus())
133             return;
134         // Perform the transition
135
if (pb == null)
136             throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is null");
137         PName pn = pb.getPName();
138         if (pn == null)
139             throw new PExceptionNaming("[" + getClassName() + "]: this PBinding is not bounded with a pname");
140         if (!pb.getPClassMapping().equals(pcm))
141             throw new PExceptionNaming("[" + getClassName() + "]: this pbinding is not valid in this binder");
142         if (cache != null) {
143             synchronized (cache) {
144                 try {
145                     if (((FixableCacheEntry) cache).getCeFixCount() == 0) {
146                         // the cache replacement has decided to remove this entry
147
pb.setPName(null);
148                         pb.setStatus(nextstate);
149                     } else {
150                         // this entry becomes eligeable for replacement
151
CacheEntry ce = cache.lookup(pn);
152                         if (ce != null) {
153                             cache.unfix(ce);
154                         }
155                     }
156                 } catch (CacheException e) {
157                     throw new PException(e, "[" + getClassName() + "]: problem with cache management");
158                 }
159             }
160         } else {
161             pb.setPName(null);
162             pb.setStatus(nextstate);
163         }
164     }
165
166     public CacheManager getCacheManager() {
167         return cache;
168     }
169
170     public void setCacheManager(CacheManager cm) throws PException {
171         cache = cm;
172     }
173
174     public PName getNull() {
175         return nullPName;
176     }
177
178     public void setNullPName(Object JavaDoc o) throws PException {
179     }
180 }
181
Popular Tags