KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > metadata > UserTableManager


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * UserTableManager.java
25  *
26  * Created on 29 août 2002, 14:55
27  */

28
29 package org.xquark.mapper.metadata;
30
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Statement JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import org.xquark.mapper.RepositoryException;
38 import org.xquark.mapper.dbms.Sequence;
39 import org.xquark.mapper.dbms.TableInfo;
40 import org.xquark.mapper.dbms.TableSpec;
41
42 /**
43  * User table dictionary.
44  * This object manages concurrency problems using a lock-optimistic algorithm.
45  *
46  */

47 public class UserTableManager
48 {
49     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
50     private static final String JavaDoc RCSName = "$Name: $";
51     
52     private Repository rep;
53     private Sequence userTablesSeq;
54     private Map JavaDoc tableIDs = new HashMap JavaDoc();
55     private String JavaDoc selectStmt;
56     private String JavaDoc insertStmt;
57     
58     /** Creates a new instance of UserTableManager */
59     public UserTableManager(Repository rep)
60     throws RepositoryException
61     {
62         this.rep = rep;
63         userTablesSeq = new Sequence(rep.getTableInfo(TableSpec.TYPE_TABLE_SEQ), (short)1);
64         initStatements(rep);
65         refresh();
66     }
67     
68     private void initStatements(Repository rep)
69     {
70         TableInfo userTablesTable = rep.getTableInfo(TableSpec.TYPE_TABLES);
71         selectStmt = userTablesTable.getSelectAllStatement();
72         insertStmt = "INSERT INTO " + userTablesTable.getName() + " VALUES(";
73     }
74     
75     private synchronized void refresh() throws RepositoryException
76     {
77         tableIDs.clear();
78         Statement JavaDoc stmt = null;
79         ResultSet JavaDoc rs = null;
80         try
81         {
82             stmt = rep.getMetadataConnection().getConnection().createStatement();
83             rs = stmt.executeQuery(selectStmt);
84             while (rs.next())
85                 tableIDs.put(rs.getString(1), new Short JavaDoc(rs.getShort(2)));
86         }
87         catch (SQLException JavaDoc e)
88         {
89             throw new RepositoryException(RepositoryException.DB_ERROR,
90             "Could not read the user tables system table", e);
91         }
92         finally
93         {
94             try
95             {
96                 if (rs != null)
97                     rs.close();
98                 if (stmt != null)
99                     stmt.close();
100             }
101             catch (SQLException JavaDoc e)
102             {
103                 // no op
104
}
105         }
106     }
107     
108     public synchronized short getTableID(String JavaDoc tableName)
109     throws RepositoryException
110     {
111         short ret = -1;
112         
113         /* First look in the map */
114         ret = getID(tableName);
115         if (ret == -1)
116         {
117             // First refresh the cache (for the case another instance has already created table)
118
refresh();
119             // look again in the map
120
ret = getID(tableName);
121             if (ret == -1)
122             {
123                 // allocate a new ID
124
short newTableID = -1;
125                 newTableID = (short)userTablesSeq.nextValue(rep.getMetadataConnection());
126                 // save it
127
try
128                 {
129                     rep.getMetadataConnection().executeUpdate(insertStmt + "'" + tableName + "'," + newTableID + ')');
130                     tableIDs.put(tableName, new Short JavaDoc(newTableID));
131                     ret = newTableID;
132                 }
133                 catch (SQLException JavaDoc e)
134                 {
135                     // collision
136
refresh();
137                     ret = getID(tableName);
138                     if (ret == -1)
139                         throw new RepositoryException(RepositoryException.DB_CONSISTENCY_ERROR,
140                         "Could not insert a row in the user tables system table", e);
141                 }
142             }
143         }
144         
145         return ret;
146     }
147     
148     private short getID(String JavaDoc tableName)
149     {
150         short ret = -1;
151         Short JavaDoc mapID = (Short JavaDoc)tableIDs.get(tableName);
152         if (mapID != null)
153             ret = mapID.shortValue();
154         return ret;
155     }
156 }
157
Popular Tags