You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by to...@apache.org on 2004/05/27 23:02:46 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata ConnectionRepository.java
tomdz 2004/05/27 14:02:46
Modified: src/java/org/apache/ojb/broker/metadata
ConnectionRepository.java
Log:
Added convenience method to create a connection descriptor from a jdbc url at runtime (including some guessing of the platform)
Revision Changes Path
1.15 +96 -13 db-ojb/src/java/org/apache/ojb/broker/metadata/ConnectionRepository.java
Index: ConnectionRepository.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ConnectionRepository.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- ConnectionRepository.java 4 Apr 2004 23:53:34 -0000 1.14
+++ ConnectionRepository.java 27 May 2004 21:02:46 -0000 1.15
@@ -1,18 +1,5 @@
package org.apache.ojb.broker.metadata;
-import org.apache.commons.lang.SerializationUtils;
-import org.apache.commons.lang.SystemUtils;
-import org.apache.ojb.broker.PBKey;
-import org.apache.ojb.broker.util.logging.Logger;
-import org.apache.ojb.broker.util.logging.LoggerFactory;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.List;
-
/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,11 +14,51 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
+import org.apache.commons.lang.SerializationUtils;
+import org.apache.commons.lang.SystemUtils;
+import org.apache.ojb.broker.PBKey;
+import org.apache.ojb.broker.util.logging.Logger;
+import org.apache.ojb.broker.util.logging.LoggerFactory;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+
public class ConnectionRepository implements Serializable, XmlCapable
{
private static final long serialVersionUID = -5581126412817848887L;
private static Logger log = LoggerFactory.getLogger(ConnectionRepository.class);
+ /** Maps the sub-protocl part of a jdbc connection url to a OJB platform name */
+ private static HashMap jdbcSubProtocolToPlatform = new HashMap();
+
+ static
+ {
+ jdbcSubProtocolToPlatform.put("db2", "Db2");
+ jdbcSubProtocolToPlatform.put("firebirdsql", "Firebird");
+ jdbcSubProtocolToPlatform.put("hsqldb", "Hsqldb");
+ jdbcSubProtocolToPlatform.put("informix-sqli", "Informix");
+ // this might also mean Sapdb but we have no means to distinguish them
+ jdbcSubProtocolToPlatform.put("sapdb", "MaxDB");
+ // could be a different database but they usually have their own drivers and don't
+ // need the jdbc-odbc bridge so we guess it is Access
+ jdbcSubProtocolToPlatform.put("odbc", "MsAccess");
+ jdbcSubProtocolToPlatform.put("inetdae7a", "MsSQLServer");
+ jdbcSubProtocolToPlatform.put("inetdae7", "MsSQLServer");
+ jdbcSubProtocolToPlatform.put("inetdae6", "MsSQLServer");
+ jdbcSubProtocolToPlatform.put("inetdae", "MsSQLServer");
+ jdbcSubProtocolToPlatform.put("inetpool", "MsSQLServer");
+ jdbcSubProtocolToPlatform.put("mysql", "MySQL");
+ jdbcSubProtocolToPlatform.put("oracle", "Oracle");
+ jdbcSubProtocolToPlatform.put("inetora", "Oracle");
+ jdbcSubProtocolToPlatform.put("postgresql", "PostgreSQL");
+ jdbcSubProtocolToPlatform.put("sybase", "Sybase");
+ }
+
private HashMap jcdMap;
private Hashtable jcdAliasToPBKeyMap;
@@ -125,6 +152,62 @@
}
}
+ /**
+ * Creates and adds a new connection descriptor for the given JDBC connection url.
+ * This method tries to guess the platform to be used, but it should be checked
+ * afterwards nonetheless using the {@link JdbcConnectionDescriptor#getDbms()} method.
+ * For properties that are not part of the url, the following standard values are
+ * explicitly set:
+ * <ul>
+ * <li>jdbc level = 2.0</li>
+ * </ul>
+ *
+ * @param jcdAlias The connection alias for the created connection; if 'default' is used,
+ * then the new descriptor will become the default connection descriptor
+ * @param jdbcDriver The fully qualified jdbc driver name
+ * @param jdbcConnectionUrl The connection url of the form '[protocol]:[sub protocol]:{database-specific path]'
+ * where protocol is usually 'jdbc'
+ * @param username The user name (can be <code>null</code>)
+ * @param password The password (can be <code>null</code>)
+ * @return The created connection descriptor
+ * @see JdbcConnectionDescriptor#getDbms()
+ */
+ public JdbcConnectionDescriptor addDescriptor(String jcdAlias, String jdbcDriver, String jdbcConnectionUrl, String username, String password)
+ {
+ JdbcConnectionDescriptor jcd = new JdbcConnectionDescriptor();
+
+ int pos = jdbcConnectionUrl.indexOf(':');
+ int lastPos;
+
+ jcd.setProtocol(jdbcConnectionUrl.substring(0, pos));
+ lastPos = pos;
+ pos = jdbcConnectionUrl.indexOf(':', lastPos + 1);
+
+ String subProtocol = jdbcConnectionUrl.substring(lastPos + 1, pos);
+
+ jcd.setSubProtocol(subProtocol);
+ jcd.setDbAlias(jdbcConnectionUrl.substring(pos + 1));
+
+ String platform = (String)jdbcSubProtocolToPlatform.get(subProtocol);
+
+ jcd.setDbms(platform);
+ jcd.setJcdAlias(jcdAlias);
+ jcd.setJdbcLevel(2.0);
+ jcd.setDriver(jdbcDriver);
+ if ("default".equals(jcdAlias))
+ {
+ jcd.setDefaultConnection(true);
+ }
+
+ if (username != null)
+ {
+ jcd.setUserName(username);
+ jcd.setPassWord(password);
+ }
+ addDescriptor(jcd);
+ return jcd;
+ }
+
/**
* Remove a descriptor.
* @param validKey This could be the {@link JdbcConnectionDescriptor}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org