KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > fos > lib > FosPMappingStructuresManager


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.mapper.fos.lib;
25
26 import org.objectweb.jorm.api.PMappingStructuresManager;
27 import org.objectweb.jorm.api.PMapper;
28 import org.objectweb.jorm.api.PException;
29 import org.objectweb.jorm.api.PExceptionProtocol;
30 import org.objectweb.jorm.api.PMapCluster;
31 import org.objectweb.util.monolog.api.Logger;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Collection JavaDoc;
36
37 /**
38  * Defines the way to manage relational schema for the FOS mappers.
39  * @author P. Dechamboux
40  */

41 public class FosPMappingStructuresManager implements PMappingStructuresManager {
42     private PMapper pMapper = null;
43     private ArrayList JavaDoc clusterList = new ArrayList JavaDoc();
44     protected Logger logger = null;
45
46
47     public synchronized void declareClass(String JavaDoc jcname) {
48         Iterator JavaDoc it = clusterList.iterator();
49         FosPMapCluster cl;
50         while (it.hasNext()) {
51             cl = (FosPMapCluster) it.next();
52             if (cl.containClass(jcname)) {
53                 return;
54             }
55         }
56         cl = new FosPMapCluster(jcname, this);
57         clusterList.add(cl);
58     }
59
60     /**
61      * Closes a mapper connection.
62      * @param conn The mapper connection.
63      * @throws org.objectweb.jorm.api.PException
64      */

65     void closeConnection(Object JavaDoc conn) throws PException {
66         pMapper.closeConnection(conn);
67     }
68
69     /**
70      * Gets a connection from the mapper.
71      * @return The allocated connection.
72      * @throws org.objectweb.jorm.api.PException
73      */

74     Object JavaDoc getConnection() throws PException {
75         Object JavaDoc conn = pMapper.getConnection();
76         if (conn == null)
77             throw new PExceptionProtocol("Impossible to initialize the mapping structures without connection");
78         return conn;
79     }
80
81     /**
82      * Retrieves or creates an FosPMapCluster associated with this JORM class.
83      * @param jcname The name of this JORM class.
84      * @return The associated FosPMapCluster.
85      */

86     private FosPMapCluster getAlwaysPMapCluster(String JavaDoc jcname,
87                                                 String JavaDoc dirname)
88             throws PException {
89         FosPMapCluster cl = null;
90         Iterator JavaDoc it = clusterList.iterator();
91         while (it.hasNext()) {
92             cl = (FosPMapCluster) it.next();
93             if (cl.containDirectory(dirname)) {
94                 break;
95             }
96             cl = null;
97         }
98         if (cl == null) {
99             // no map cluster use dirname
100
cl = (FosPMapCluster) getPMapCluster(jcname);
101             if (cl == null) {
102                 // no map cluster for jcname : creates a new one
103
cl = new FosPMapCluster(jcname, dirname, this);
104                 clusterList.add(cl);
105             }
106             // there is already a map cluster for jcname
107
} else {
108             // there is already a map cluster for dirname
109
FosPMapCluster cl2 = (FosPMapCluster) getPMapCluster(jcname);
110             if ((cl != cl2) && (cl2 != null)) {
111                 // there is also a map cluster for jcname : merge both into cl
112
cl.merge(cl2);
113                 cl.addDirName(dirname);
114                 clusterList.remove(cl2);
115             } else {
116                 // no map cluster for jcname : declare jcname into cl
117
cl.addDependency(jcname);
118             }
119         }
120         return cl;
121     }
122
123     public void addDependency(String JavaDoc jcname1, String JavaDoc jcname2) throws PException {
124 //TODO:
125
}
126
127     public void classDefined(String JavaDoc jcname) throws PException {
128 //TODO:
129
}
130
131     /**
132      * Adds a table column into these RDB mapping structures.
133      * @param jcname The JORM class name.
134      * @param dirname The table name.
135      * @throws PException
136      */

137     synchronized public void addDirName(String JavaDoc jcname, String JavaDoc dirname)
138             throws PException {
139         FosPMapCluster cl = getAlwaysPMapCluster(jcname, dirname);
140         cl.addDirName(dirname);
141     }
142
143     // IMPLEMENTATION OF METHODS FROM THE PMappingStructuresManager INTERFACE
144

145     /**
146      * Asks for the map cluster associated with a JORM class.
147      * @return The relevant map cluster.
148      * @throws PException This class has not been mapped.
149      */

150     public PMapCluster getPMapCluster(String JavaDoc jcname) throws PException {
151         Iterator JavaDoc it = clusterList.iterator();
152         while (it.hasNext()) {
153             FosPMapCluster cl = (FosPMapCluster) it.next();
154             if (cl.containClass(jcname)) {
155                 return cl;
156             }
157         }
158         return null;
159     }
160
161     /**
162      * Asks for all the map clusters defined within this mapper.
163      * @return The Iterator over the collection of map clusters.
164      */

165     public Collection JavaDoc getPMapClusters() {
166         return clusterList;
167     }
168
169     /**
170      * Assigns a mapper to this schema manager.
171      * @param pm The concerned mapper.
172      */

173     public void setPMapper(PMapper pm) {
174         pMapper = pm;
175     }
176
177     /**
178      * Assigns a logger to this schema manager.
179      * @param l The concerned logger.
180      */

181     public void setLogger(Logger l) {
182         logger = l;
183     }
184 }
185
Popular Tags