KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > sql > metadata > MetadataContainer


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): Emmanuel Cecchet.
23  */

24
25 package org.objectweb.cjdbc.common.sql.metadata;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.objectweb.cjdbc.common.log.Trace;
31
32 /**
33  * A MetadataContainer is basically a hashtable of jdbc metadata. We may want to
34  * override a few options from the usual Hashtable so I've put it in a separate
35  * class.
36  *
37  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
38  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
39  * @version 1.0
40  */

41 public final class MetadataContainer extends HashMap JavaDoc
42 {
43   private static final long serialVersionUID = 564436668119294938L;
44
45   private final String JavaDoc line = System
46                                                  .getProperty("line.separator");
47   private String JavaDoc url;
48
49   /**
50    * Creates a new <code>MetadataContainer</code> object
51    *
52    * @param url which url is this container pointed to
53    */

54   public MetadataContainer(String JavaDoc url)
55   {
56     this.url = url;
57   }
58
59   /**
60    * Check to see if two metadata sets are identical. All incompatible values
61    * are logged as warning into the logger given.
62    *
63    * @param container the container to check compatibility with
64    * @param logger the logger, if null, echo on stderr
65    * @return true if all metadata are identical.
66    */

67   public boolean isCompatible(MetadataContainer container, Trace logger)
68   {
69     if (keySet() == null)
70       return container.keySet() == null;
71     Iterator JavaDoc keys = keySet().iterator();
72     boolean isCompatible = true;
73     String JavaDoc key;
74     Object JavaDoc value1;
75     Object JavaDoc value2;
76     String JavaDoc log;
77     while (keys.hasNext())
78     {
79       key = (String JavaDoc) keys.next();
80       value1 = get(key);
81       value2 = container.get(key);
82       if (!value1.equals(value2))
83       {
84         isCompatible = false;
85         log = "Metadata key [" + key + "] is not compatible. (Backends are: ["
86             + url + "] and [" + container.getUrl() + "] ; Values are:["
87             + value1 + "] and [" + value2 + "])";
88         if (logger != null)
89           logger.warn(log);
90         else
91           System.err.println(log);
92       }
93     }
94     return isCompatible;
95   }
96
97   /**
98    * @see java.lang.Object#toString()
99    */

100   public String JavaDoc toString()
101   {
102     if (keySet() == null)
103       return "no metadata";
104     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
105     Iterator JavaDoc keys = keySet().iterator();
106     String JavaDoc element;
107     while (keys.hasNext())
108     {
109       element = (String JavaDoc) keys.next();
110       buffer.append(element + " : " + this.get(element) + line);
111     }
112     return buffer.toString();
113   }
114
115   /**
116    * Returns the url value.
117    *
118    * @return Returns the url.
119    */

120   public String JavaDoc getUrl()
121   {
122     return url;
123   }
124
125   /**
126    * Get the metadata container key for the given query. Serializes the method
127    * call into a "getXXX(Y,Z,...)" String (with name, signature and arguments).
128    *
129    * @param methodName method invoked to generate the key in the container
130    * @param parametersType parameters type of invoked method
131    * @param arguments arguments used to invoke the method
132    * @return container key for the given method call
133    */

134   public static String JavaDoc getContainerKey(String JavaDoc methodName,
135       Class JavaDoc[] parametersType, Object JavaDoc[] arguments)
136   {
137     if (parametersType == null)
138     { // Function without parameters, just store the function name as the key
139
return methodName;
140     }
141     else
142     { // Include all argument in function name
143
StringBuffer JavaDoc key = new StringBuffer JavaDoc(methodName);
144       key.append('(');
145       for (int i = 0; i < parametersType.length; i++)
146       {
147         Class JavaDoc c = parametersType[i];
148         if (c != null)
149           key.append(c.getName());
150         else
151           key.append("null");
152         key.append('=');
153         Object JavaDoc arg = arguments[i];
154         if (arg != null)
155           key.append(arg.toString());
156         else
157           key.append("null");
158         key.append(',');
159       }
160       // Replace last comma with )
161
key.setCharAt(key.length() - 1, ')');
162       return key.toString();
163     }
164   }
165 }
Popular Tags