KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > facility > naming > polymorphid > PolymorphIdBinderInfo


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
24 package org.objectweb.jorm.facility.naming.polymorphid;
25
26 import org.objectweb.jorm.api.PAccessor;
27 import org.objectweb.jorm.api.PBinding;
28 import org.objectweb.jorm.api.PClassMapping;
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.api.PMapper;
31 import org.objectweb.jorm.api.PBindingCtrl;
32 import org.objectweb.jorm.api.PStateGraph;
33 import org.objectweb.jorm.api.PExceptionProtocol;
34 import org.objectweb.jorm.api.PExceptionNoDSI;
35 import org.objectweb.jorm.type.api.PType;
36 import org.objectweb.jorm.facility.naming.generator.LongGen;
37 import org.objectweb.jorm.naming.api.PName;
38 import org.objectweb.jorm.naming.api.PExceptionNaming;
39 import org.objectweb.jorm.naming.api.PExceptionExistingName;
40 import org.objectweb.perseus.cache.api.CacheEntry;
41 import org.objectweb.perseus.cache.api.CacheException;
42
43 /**
44  * Implements a binder that manages polymorphic names. Such names are composed
45  * of the identifier of the persistent class to which they belong (see classId
46  * below), and of an object identifier within this binder, which is generated
47  * by this binder. The classId information is persistent; thus, this class is
48  * extended by ClassIdBinding generated by JORM using "ClassId.pd".
49  * @author P. Dechamboux
50  */

51 public class PolymorphIdBinderInfo
52         extends PolymorphIdBinder
53         implements PAccessor, ClassIdAccessor, IdClassAccessor {
54     public static final String JavaDoc CLASSIDN =
55             "org.objectweb.jorm.facility.naming.polymorphid.ClassId";
56     public static final String JavaDoc IDCLASSN =
57             "org.objectweb.jorm.facility.naming.polymorphid.IdClass";
58     Long JavaDoc classId;
59     String JavaDoc className;
60     private PolymorphIdMgr mgr;
61     private LongGen objectIdGen;
62
63     /**
64      * Initializes this new binder. It either loads its persistent information
65      * if it has already been created, or it creates all this persistent
66      * information and immediatly stores them.
67      */

68     void init(PMapper m, String JavaDoc cn, PolymorphIdMgr mgr) throws PException {
69         Object JavaDoc conn = mgr.getPMapper().getConnection();
70         try {
71             init(m, cn, mgr, conn);
72         } catch (PException e) {
73             throw e;
74         } finally {
75             mgr.getPMapper().closeConnection(conn);
76         }
77     }
78
79     /**
80      * Initializes this new binder. It either loads its persistent information
81      * if it has already been created, or it creates all this persistent
82      * information and immediatly stores them.
83      */

84     void init(PMapper m, String JavaDoc cn, PolymorphIdMgr mgr, Object JavaDoc conn) throws PException {
85         this.mgr = mgr;
86         className = cn;
87         PClassMapping pcm = m.lookup(CLASSIDN);
88         PBinding binding = pcm.createPBinding();
89         objectIdGen = mgr.getLongGenMgr().getLongGen(className);
90         binding.bind(pcm.getPBinder().decodeString(className));
91         Object JavaDoc connection = conn;
92         if (connection == null)
93             connection = mgr.getPMapper().getConnection();
94         try {
95             binding.read(connection, this);
96         } catch (PExceptionNoDSI e) {
97             binding.export(connection, className);
98             classId = new Long JavaDoc(mgr.newClassId(connection));
99             binding.write(connection, this);
100             PClassMapping pcm2 = m.lookup(IDCLASSN);
101             PBinding binding2 = pcm2.createPBinding();
102             binding2.bind(pcm2.getPBinder().decodeLong(classId.longValue()));
103             if (!binding2.exist(connection)) {
104                 binding2.export(connection, classId);
105                 binding2.write(connection, this);
106             }
107             binding2.unbind();
108         }
109         binding.unbind();
110     }
111
112     /**
113      * It initializes the Binder with a specified identifier. The conn is used
114      * to search the class name associated to this id. If the class name is not
115      * found a PExceptionNaming is thrown.
116      * @param m is the mapper in which the class IdClass is mapped
117      * @param id is the class identifier
118      * @param mgr is the PolymorphIdMgr
119      * @param conn is the connection to access to the data support which stores
120      * the naming meta data.
121      * @throws PExceptionNaming when the identifier is associated to any
122      * clas name. And throws a PException if erros occurs during the loading
123      * of the IdClass object.
124      */

125     void init(PMapper m, long id, PolymorphIdMgr mgr, Object JavaDoc conn)
126             throws PException {
127         Object JavaDoc connection = conn;
128         if (connection == null)
129             connection = mgr.getPMapper().getConnection();
130         try {
131             this.mgr = mgr;
132             PClassMapping pcm = m.lookup(IDCLASSN);
133             PBinding binding = pcm.createPBinding();
134             binding.bind(pcm.getPBinder().decodeLong(id));
135             try {
136                 binding.read(connection, this);
137             } catch (PExceptionNoDSI e) {
138                 throw new PExceptionNaming(e, "The binder " + id + " does not exist");
139             } finally {
140                 binding.unbind();
141             }
142             objectIdGen = mgr.getLongGenMgr().getLongGen(className);
143         } catch (PException e) {
144             throw e;
145         } finally {
146             mgr.getPMapper().closeConnection(connection);
147         }
148     }
149
150
151     // IMPLEMENTATION OF METHODS FROM THE PNameManager INTERFACE
152

153     public String JavaDoc getClassName() {
154         return className;
155     }
156
157     public void setPType(PType pt) {
158         super.setPType(pt);
159         mgr.binderTypeDef(this);
160     }
161
162     public PName export(Object JavaDoc conn, Object JavaDoc binding) throws PException {
163         PBindingCtrl pb = (PBindingCtrl) binding;
164         byte nextstate = PStateGraph.nextStatePBinding(pb.getStatus(),
165                                                        PBinding.ACTION_EXPORT);
166         if (nextstate == PBinding.LIFECYCLE_ERROR)
167             throw new PExceptionProtocol("Unauthorized operation");
168         // Performs the transition
169
PName res = new PolymorphIdPName(classId.longValue(),
170                                          objectIdGen.genId(conn), this);
171         if (bindingcache != null) {
172             synchronized (bindingcache) {
173                 if (bindingcache.lookup(res) != null) {
174                     throw new PExceptionExistingName(
175                             "[" + getClassName() + "]: an object has been already export with the same identifier");
176                 }
177                 try {
178                     CacheEntry ce = bindingcache.bind(res, binding);
179                     bindingcache.fix(ce);
180                 } catch (CacheException e) {
181                     throw new PException(e, "[" + getClassName() + "]: problem with cache management");
182                 }
183             }
184         }
185         pb.setPName(res);
186         pb.setStatus(nextstate);
187         return res;
188     }
189
190     public PName export(Object JavaDoc conn, Object JavaDoc binding, Object JavaDoc ctxt)
191             throws PException {
192         throw new PExceptionNaming("Supports only auto-generated names (no need of a user context).");
193     }
194
195     // IMPLEMENTATION OF METHODS FROM THE PAccessor INTERFACE
196

197     public Object JavaDoc getMemoryInstance() {
198         return this;
199     }
200
201     // IMPLEMENTATION OF METHODS FROM THE PAccessorClassId INTERFACE
202

203     //Accessors to the classId field
204
public void paSetClassId(long val) throws PException {
205         classId = new Long JavaDoc(val);
206     }
207
208     public long paGetClassId() throws PException {
209         return classId.longValue();
210     }
211
212     // IMPLEMENTATION OF METHODS FROM THE PAccessorIdClass INTERFACE
213

214     //Accessors to the classId field
215
public void paSetClassName(String JavaDoc val) throws PException {
216         className = val;
217     }
218
219     public String JavaDoc paGetClassName() throws PException {
220         return className;
221     }
222 }
223
Popular Tags