001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration.bean;
023:
024: import org.jboss.portal.migration.helper.DialectFactory;
025:
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.sql.DataSource;
029: import java.lang.reflect.Method;
030: import java.sql.Connection;
031: import java.sql.DatabaseMetaData;
032:
033: /**
034: * Bean representation of fromDS and toDS.
035: *
036: * @author <a href="mailto:roy@jboss.org">Roy Russo</a>
037: */
038: public class DataSourceDS {
039: public boolean connectexists;
040: public String datasource22;
041: public String datasource24;
042: public String dialect22;
043: public String dialect24;
044:
045: public String getDialect22() {
046: return dialect22;
047: }
048:
049: public void setDialect22(String dialect22) {
050: this .dialect22 = dialect22;
051: }
052:
053: public String getDialect24() {
054: return dialect24;
055: }
056:
057: public void setDialect24(String dialect24) {
058: this .dialect24 = dialect24;
059: }
060:
061: public String getDatasource24() {
062: return datasource24;
063: }
064:
065: public void setDatasource24(String datasource24) {
066: this .datasource24 = datasource24;
067: }
068:
069: public String getDatasource22() {
070: return datasource22;
071: }
072:
073: public void setDatasource22(String datasource22) {
074: this .datasource22 = datasource22;
075: }
076:
077: public boolean isConnectexists() {
078: return connectexists;
079: }
080:
081: public void setConnectexists(boolean connectexists) {
082: this .connectexists = connectexists;
083: }
084:
085: public String check22() throws Exception {
086: this .checkConnection("2_2", this .datasource22);
087: return "step2";
088: }
089:
090: public String check24() throws Exception {
091: this .checkConnection("2_4", this .datasource24);
092: return "step3";
093: }
094:
095: /**
096: * Generic connection check and dialect setter based on given jndi name.
097: *
098: * @param version
099: * @param DSName
100: * @throws Exception
101: */
102: private void checkConnection(String version, String DSName)
103: throws Exception {
104: Connection conn = null;
105: try {
106: Context ctx = new InitialContext();
107: if (ctx == null) {
108: throw new Exception();
109: }
110:
111: DataSource ds = (DataSource) ctx.lookup("java:/" + DSName);
112:
113: if (ds != null) {
114: conn = ds.getConnection();
115: if (conn == null) {
116: throw new Exception();
117: } else {
118: setConnectexists(true);
119:
120: DatabaseMetaData meta = conn.getMetaData();
121: String databaseName = meta.getDatabaseProductName();
122: int databaseMajorVersion = getDatabaseMajorVersion(meta);
123: String dialectName = DialectFactory
124: .determineDialect(databaseName,
125: databaseMajorVersion);
126:
127: if ("2_2".equals(version)) {
128: setDialect22(dialectName);
129: } else if ("2_4".equals(version)) {
130: setDialect24(dialectName);
131: }
132: }
133: }
134: } catch (Exception e) {
135: setConnectexists(false);
136: } finally {
137: try {
138: conn.close();
139: } catch (Exception e) {
140: }
141: }
142: }
143:
144: private int getDatabaseMajorVersion(DatabaseMetaData meta) {
145: try {
146: Method gdbmvMethod = DatabaseMetaData.class.getMethod(
147: "getDatabaseMajorVersion", null);
148: return ((Integer) gdbmvMethod.invoke(meta, null))
149: .intValue();
150: } catch (NoSuchMethodException nsme) {
151: return 0;
152: } catch (Throwable t) {
153: return 0;
154: }
155: }
156:
157: }
|