KickJava   Java API By Example, From Geeks To Geeks.

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


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.PException;
27 import org.objectweb.jorm.lib.AbstractPMapcluster;
28 import org.objectweb.perseus.fos.api.FosAccess;
29 import org.objectweb.perseus.fos.api.FosException;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * Defines the PMapCluster used to manage the directory tree for the FOS
36  * mappers.
37  * @author P. Dechamboux
38  */

39 public class FosPMapCluster extends AbstractPMapcluster {
40     private ArrayList JavaDoc directories;
41     private FosPMappingStructuresManager manager;
42
43     /**
44      * Constructs a RdbPMapCluster for an abstract class
45      * @param jcname The first JORM class name to be assigned to this cluster.
46      * @param man The manager associated to these mapping structures.
47      */

48     FosPMapCluster(String JavaDoc jcname, FosPMappingStructuresManager man) {
49         super();
50         jcNames.add(jcname);
51         directories = new ArrayList JavaDoc(1);
52         manager = man;
53     }
54
55     /**
56      * Constructs a RdbPMapCluster.
57      * @param jcname The first JORM class name to be assigned to this cluster.
58      * @param basedir The directory to be used for this class.
59      * @param man The manager associated to these mapping structures.
60      */

61     FosPMapCluster(String JavaDoc jcname, String JavaDoc basedir, FosPMappingStructuresManager man) {
62         this(jcname, man);
63         directories.add(basedir);
64     }
65
66     /**
67      * Adds a new JORM class name to this cluster.
68      * @param jcname The JORM class name.
69      */

70     void addClass(String JavaDoc jcname) {
71         addDependency(jcname);
72     }
73
74     /**
75      * Defines a new directory into this map cluster if it does not already
76      * exist.
77      * @param dirname The directory name.
78      * @throws PException Thrown if the column is redefined with incompatible
79      * information.
80      */

81     void addDirName(String JavaDoc dirname) throws PException {
82         String JavaDoc curd = null;
83         Iterator JavaDoc it = directories.iterator();
84         while (it.hasNext()) {
85             curd = (String JavaDoc) it.next();
86             if (curd.equals(dirname)) {
87                 break;
88             }
89             curd = null;
90         }
91         if (curd == null) {
92             // no such directory defined yet: create a new one
93
directories.add(dirname);
94         }
95     }
96
97     /**
98      * Looks for this specified directory to be a sub-directory of the ones
99      * defined into this map cluster.
100      * @param dirname The table name.
101      * @throws PException Thrown if the column is redefined with incompatible
102      * information.
103      */

104     boolean containDirectory(String JavaDoc dirname) throws PException {
105         String JavaDoc curd = null;
106         Iterator JavaDoc it = directories.iterator();
107         while (it.hasNext()) {
108             curd = (String JavaDoc) it.next();
109             if ((dirname.length() >= curd.length()) && curd.equals(dirname.substring(0, curd.length()))) {
110                 return true;
111             }
112             if ((dirname.length() < curd.length()) && dirname.equals(curd.substring(0, dirname.length()))) {
113                 return true;
114             }
115             curd = null;
116         }
117         return false;
118     }
119
120     /**
121      * Merges map cluster passed in parameter with this one. Clusters are merged
122      * either because they share a directory.
123      * @param cl
124      */

125     void merge(FosPMapCluster cl) {
126         jcNames.addAll(cl.jcNames);
127         directories.addAll(cl.directories);
128     }
129
130     // IMPLEMENTATION OF METHODS FROM THE PMapCluster INTERFACE
131

132     /**
133      * Creates the mapping structures defined by this map cluster. Nothing to do
134      * with FOS.
135      * @throws PException Thrown if it cannot be performed (especially
136      * if all or some of these mapping structures already exist.
137      */

138     public void createMappingStructures(boolean force) throws PException {
139         if (structuresActive) {
140             throw new PException("Cannot change mapping structures while they are under use");
141         }
142     }
143
144     /**
145      * Deletes the data that have been inserted into the mapping structures
146      * defined by this map cluster.
147      * @throws PException Thrown when the data store cannot perform this
148      * operation.
149      */

150     public void deleteData() throws PException {
151         Object JavaDoc conn = manager.getConnection();
152         String JavaDoc curd = null;
153         try {
154             Iterator JavaDoc it = directories.iterator();
155             while (it.hasNext()) {
156                 curd = (String JavaDoc) it.next();
157                 if (((FosAccess) conn).existDir(curd)) {
158                     Iterator JavaDoc it2 = ((FosAccess) conn).scan(curd);
159                     while (it2.hasNext()) {
160                         String JavaDoc oid = (String JavaDoc) it2.next();
161                         ((FosAccess) conn).delete(curd, oid);
162                     }
163                 }
164             }
165         } catch (FosException e) {
166             throw new PException(e, "PB while deteling data from FOS directory " + curd);
167         } finally {
168             manager.closeConnection(conn);
169         }
170     }
171
172     /**
173      * Deletes all or part of the mapping structures defined by this map
174      * cluster when they exist. If they contain some data, they are also
175      * deleted.
176      * @throws PException Thrown when the data store cannot perform this
177      * operation.
178      */

179     public void deleteMappingStructures() throws PException {
180         if (structuresActive) {
181             throw new PException("Cannot change mapping structures while they are under use");
182         }
183         Object JavaDoc conn = manager.getConnection();
184         String JavaDoc curd = null;
185         try {
186             Iterator JavaDoc it = directories.iterator();
187             while (it.hasNext()) {
188                 curd = (String JavaDoc) it.next();
189                 if (((FosAccess) conn).existDir(curd)) {
190                     ((FosAccess) conn).deleteDir(curd);
191                 }
192             }
193         } catch (FosException e) {
194             throw new PException(e, "PB while deteling FOS directory " + curd);
195         } finally {
196             manager.closeConnection(conn);
197         }
198     }
199 }
200
Popular Tags