001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Nicolas Modrzyk.
021: * Contributor(s): Emmanuel Cecchet.
022: */package org.continuent.sequoia.common.sql.metadata;
023:
024: import java.util.HashMap;
025: import java.util.Iterator;
026:
027: import org.continuent.sequoia.common.log.Trace;
028:
029: /**
030: * A MetadataContainer is basically a hashtable of jdbc metadata. We may want to
031: * override a few options from the usual Hashtable so I've put it in a separate
032: * class.
033: *
034: * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
035: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
036: * @version 1.0
037: */
038: public final class MetadataContainer extends HashMap {
039: private static final long serialVersionUID = 564436668119294938L;
040:
041: private final String line = System.getProperty("line.separator");
042: private String url;
043:
044: /**
045: * Creates a new <code>MetadataContainer</code> object
046: *
047: * @param url which url is this container pointed to
048: */
049: public MetadataContainer(String url) {
050: this .url = url;
051: }
052:
053: /**
054: * Check to see if two metadata sets are identical. All incompatible values
055: * are logged as warning into the logger given.
056: *
057: * @param container the container to check compatibility with
058: * @param logger the logger, if null, echo on stderr
059: * @return true if all metadata are identical.
060: */
061: public boolean isCompatible(MetadataContainer container,
062: Trace logger) {
063: if (keySet() == null)
064: return container.keySet() == null;
065: Iterator keys = keySet().iterator();
066: boolean isCompatible = true;
067: String key;
068: Object value1;
069: Object value2;
070: String log;
071: while (keys.hasNext()) {
072: key = (String) keys.next();
073: value1 = get(key);
074: value2 = container.get(key);
075: if (!value1.equals(value2)) {
076: isCompatible = false;
077: log = "Metadata key [" + key
078: + "] is not compatible. (Backends are: [" + url
079: + "] and [" + container.getUrl()
080: + "] ; Values are:[" + value1 + "] and ["
081: + value2 + "])";
082: if (logger != null)
083: logger.warn(log);
084: else
085: System.err.println(log);
086: }
087: }
088: return isCompatible;
089: }
090:
091: /**
092: * @see java.lang.Object#toString()
093: */
094: public String toString() {
095: if (keySet() == null)
096: return "no metadata";
097: StringBuffer buffer = new StringBuffer();
098: Iterator keys = keySet().iterator();
099: String element;
100: while (keys.hasNext()) {
101: element = (String) keys.next();
102: buffer.append(element + " : " + this .get(element) + line);
103: }
104: return buffer.toString();
105: }
106:
107: /**
108: * Returns the url value.
109: *
110: * @return Returns the url.
111: */
112: public String getUrl() {
113: return url;
114: }
115:
116: /**
117: * Get the metadata container key for the given query. Serializes the method
118: * call into a "getXXX(Y,Z,...)" String (with name, signature and arguments).
119: *
120: * @param methodName method invoked to generate the key in the container
121: * @param parametersType parameters type of invoked method
122: * @param arguments arguments used to invoke the method
123: * @return container key for the given method call
124: */
125: public static String getContainerKey(String methodName,
126: Class[] parametersType, Object[] arguments) {
127: if (parametersType == null) { // Function without parameters, just store the function name as the key
128: return methodName;
129: } else { // Include all argument in function name
130: StringBuffer key = new StringBuffer(methodName);
131: key.append('(');
132: for (int i = 0; i < parametersType.length; i++) {
133: Class c = parametersType[i];
134: if (c != null)
135: key.append(c.getName());
136: else
137: key.append("null");
138: key.append('=');
139: Object arg = arguments[i];
140: if (arg != null)
141: key.append(arg.toString());
142: else
143: key.append("null");
144: key.append(',');
145: }
146: // Replace last comma with )
147: key.setCharAt(key.length() - 1, ')');
148: return key.toString();
149: }
150: }
151: }
|