Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

MetadataContainer.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): Emmanuel Cecchet.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.sql.metadata;
00026 
00027 import java.util.HashMap;
00028 import java.util.Iterator;
00029 
00030 import org.objectweb.cjdbc.common.log.Trace;
00031 
00032 /**
00033  * a MetadataContainer is basically a hashtable of jdbc metadata. We may want to
00034  * override a few options from the usual Hashtable so I've put it in a separate
00035  * class.
00036  * 
00037  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00038  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00039  * @version 1.0
00040  */
00041 public final class MetadataContainer extends HashMap
00042 {
00043   private final String line = System.getProperty("line.separator");
00044   private String       url;
00045 
00046   /**
00047    * Creates a new <code>MetadataContainer</code> object
00048    * 
00049    * @param url which url is this container pointed to
00050    */
00051   public MetadataContainer(String url)
00052   {
00053     this.url = url;
00054   }
00055 
00056   /**
00057    * Check to see if this container metadata is compatible with another
00058    * container. A general boolean is returned, but all incompatible values are
00059    * logged as warning in the given parameter
00060    * 
00061    * @param container the container to check compatibility with
00062    * @param logger the logger, if null, echo on stderr
00063    * @return true if all metadata are identical.
00064    */
00065   public boolean isCompatible(MetadataContainer container, Trace logger)
00066   {
00067     if (keySet() == null)
00068       return container.keySet() == null;
00069     Iterator keys = keySet().iterator();
00070     boolean isCompatible = true;
00071     String key;
00072     Object value1;
00073     Object value2;
00074     String log;
00075     while (keys.hasNext())
00076     {
00077       key = (String) keys.next();
00078       value1 = get(key);
00079       value2 = container.get(key);
00080       if (!value1.equals(value2))
00081       {
00082         isCompatible = false;
00083         log = "Metadata key [" + key + "] is not compatible. (Backends are: ["
00084             + url + "] and [" + container.getUrl() + "] ; Values are:["
00085             + value1 + "] and [" + value2 + "])";
00086         if (logger != null)
00087           logger.warn(log);
00088         else
00089           System.err.println(log);
00090       }
00091     }
00092     return isCompatible;
00093   }
00094 
00095   /**
00096    * @see java.lang.Object#toString()
00097    */
00098   public String toString()
00099   {
00100     if (keySet() == null)
00101       return "no metadata";
00102     StringBuffer buffer = new StringBuffer();
00103     Iterator keys = keySet().iterator();
00104     String element;
00105     while (keys.hasNext())
00106     {
00107       element = (String) keys.next();
00108       buffer.append(element + " : " + this.get(element) + line);
00109     }
00110     return buffer.toString();
00111   }
00112 
00113   /**
00114    * Returns the url value.
00115    * 
00116    * @return Returns the url.
00117    */
00118   public String getUrl()
00119   {
00120     return url;
00121   }
00122   
00123   /**
00124    * Get the metadata container key for the given method.
00125    * 
00126    * @param methodName method invoked to generate the key in the container
00127    * @param parametersType parameters type of invoked method
00128    * @param arguments arguments used to invoke the method
00129    */
00130   public static String getContainerKey(String methodName,
00131       Class[] parametersType, Object[] arguments)
00132   {
00133     if (parametersType == null)
00134     { // Function without parameters, just store the function name as the key
00135       return methodName;
00136     }
00137     else
00138     { // Include all argument in function name
00139       StringBuffer key = new StringBuffer(methodName);
00140       key.append('(');
00141       for (int i = 0; i < parametersType.length; i++)
00142       {
00143         Class c = parametersType[i];
00144         if (c != null)
00145           key.append(c.getName());
00146         else
00147           key.append("null");
00148         key.append('=');
00149         Object arg = arguments[i];
00150         if (arg != null)
00151           key.append(arg.toString());
00152         else
00153           key.append("null");
00154         key.append(',');
00155       }
00156       // Replace last comma with )
00157       key.setCharAt(key.length() - 1, ')');
00158       return key.toString();
00159     }
00160   }
00161 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1