KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > lib > AbstractPMapcluster


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.jorm.lib;
19
20 import org.objectweb.jorm.api.PMapCluster;
21 import org.objectweb.jorm.api.PException;
22
23 import java.util.Set JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26
27 /**
28  * Abstract common implementation of a PMapCluster. This implementation
29  * manages the dependencies (the list of classes of the cluster).
30  *
31  * @author S.Chassande-Barrioz
32  */

33 public abstract class AbstractPMapcluster implements PMapCluster {
34
35     /**
36      * The names of the Jorm classes defined and managed by this PMapCluster
37      */

38     protected Set JavaDoc jcNames;
39
40     /**
41      * The names of the Jorm classes not yet defined but to manage by this
42      * PMapCluster. An empty list mean the cluster is complete and can be
43      * started.
44      */

45     protected Set JavaDoc unresolvedDependencies;
46
47     /**
48      * Indicates if the cluster is active:
49      * - data structure created
50      * - cluster started
51      */

52     protected boolean structuresActive;
53
54     public AbstractPMapcluster() {
55         jcNames = new HashSet JavaDoc(1);
56         unresolvedDependencies = new HashSet JavaDoc(1);
57         structuresActive = false;
58     }
59
60     /**
61      * Retrieves an Iterator over the collection of all JORM classes whose
62      * mapping structures have to be controlled altogether.
63      * @return The Iterator over this collection.
64      */

65     public Collection JavaDoc getClusterClasses() {
66         if (unresolvedDependencies.isEmpty()) {
67             return jcNames;
68         } else {
69             Set JavaDoc res = new HashSet JavaDoc(jcNames);
70             res.addAll(unresolvedDependencies);
71             return res;
72         }
73     }
74
75     public boolean isDefined() {
76         return unresolvedDependencies.isEmpty();
77     }
78
79     public Set JavaDoc getUnResolvedDependencies() {
80         return unresolvedDependencies;
81     }
82
83     public void addDependency(String JavaDoc jcname) {
84         if (!jcNames.contains(jcname) && !unresolvedDependencies.contains(jcname)) {
85             unresolvedDependencies.add(jcname);
86         }
87     }
88
89     public void classDefined(String JavaDoc jcname) {
90         if (!jcNames.contains(jcname)) {
91             jcNames.add(jcname);
92             unresolvedDependencies.remove(jcname);
93         }
94     }
95
96     /**
97      * Looks for this JORM class name into this map cluster.
98      * @param jcname The JORM class name.
99      * @return true if this JORM class name is already assigned to this cluster.
100      */

101     public boolean containClass(String JavaDoc jcname) {
102         return jcNames.contains(jcname);
103     }
104
105     /**
106      * Starts this map cluster. The following operation cannot be executed
107      * anymore: createMappingStructures, deleteMappingStructures, and
108      * updateMappingStructures.
109      * @throws org.objectweb.jorm.api.PException
110      */

111     public void start() throws PException {
112         if (!structuresActive) {
113             throw new PException("Cannot start map cluster: not yet inactive");
114         }
115         if (!isDefined()) {
116             throw new PException("Cannot start map cluster: the following " +
117                                  "dependencies have not been resolved: "
118                                  + unresolvedDependencies);
119         }
120         structuresActive = true;
121     }
122
123     /**
124      * Stops this map cluster.
125      * @throws org.objectweb.jorm.api.PException
126      */

127     public void stop() throws PException {
128         if (structuresActive) {
129             throw new PException("Cannot stop map cluster: not yet active");
130         }
131         structuresActive = false;
132     }
133
134     /**
135      * Aligns existing mapping structures to the ones define by this map
136      * cluster. This operation allows schema evolution on existing data store.
137      * It is thus usually difficult to support in the general case.
138      * @throws org.objectweb.jorm.api.PException Thrown when the data store cannot perform this
139      * operation.
140      * @throws UnsupportedOperationException Thrown if this operation is not
141      * supported by the mapping.
142      */

143     public void updateMappingStructures()
144             throws PException, UnsupportedOperationException JavaDoc {
145         if (structuresActive) {
146             throw new PException("Cannot change mapping structures while they are under use");
147         }
148         throw new UnsupportedOperationException JavaDoc();
149     }
150 }
151
Popular Tags