KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > sql > metadata > MetadataContainer


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Nicolas Modrzyk.
21  * Contributor(s): Emmanuel Cecchet.
22  */

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

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

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

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

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

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

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