001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq.sm.jdbc;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.sql.Connection;
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.sql.Statement;
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.HashSet;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Map;
038: import java.util.Properties;
039:
040: import javax.jms.InvalidClientIDException;
041: import javax.jms.JMSException;
042: import javax.jms.JMSSecurityException;
043: import javax.management.ObjectName;
044: import javax.naming.InitialContext;
045: import javax.sql.DataSource;
046: import javax.transaction.Status;
047: import javax.transaction.Transaction;
048: import javax.transaction.TransactionManager;
049:
050: import org.jboss.logging.Logger;
051: import org.jboss.mq.DurableSubscriptionID;
052: import org.jboss.mq.SpyJMSException;
053: import org.jboss.mq.SpyTopic;
054: import org.jboss.mq.sm.AbstractStateManager;
055: import org.jboss.mq.sm.StateManager;
056: import org.jboss.tm.TransactionManagerService;
057:
058: /**
059: * A state manager which does not create the table schema in a tx
060: *
061: * Based on http://jira.jboss.com/jira/browse/JBAS-4260
062: *
063: * @author Luc Texier (ltexier@redhat.com)
064: * @version $Revision$
065: */
066: public class MSSQLJDBCStateManager extends JDBCStateManager {
067: static final Logger log = Logger
068: .getLogger(MSSQLJDBCStateManager.class);
069:
070: protected void initDB() throws Exception {
071: CREATE_USER_TABLE = sqlProperties.getProperty(
072: "CREATE_USER_TABLE", CREATE_USER_TABLE);
073: CREATE_ROLE_TABLE = sqlProperties.getProperty(
074: "CREATE_ROLE_TABLE", CREATE_ROLE_TABLE);
075: CREATE_SUBSCRIPTION_TABLE = sqlProperties.getProperty(
076: "CREATE_SUBSCRIPTION_TABLE", CREATE_SUBSCRIPTION_TABLE);
077: GET_SUBSCRIPTION = sqlProperties.getProperty(
078: "GET_SUBSCRIPTION", GET_SUBSCRIPTION);
079: GET_SUBSCRIPTIONS_FOR_TOPIC = sqlProperties.getProperty(
080: "GET_SUBSCRIPTIONS_FOR_TOPIC",
081: GET_SUBSCRIPTIONS_FOR_TOPIC);
082: LOCK_SUBSCRIPTION = sqlProperties.getProperty(
083: "LOCK_SUBSCRIPTION", LOCK_SUBSCRIPTION);
084: INSERT_SUBSCRIPTION = sqlProperties.getProperty(
085: "INSERT_SUBSCRIPTION", INSERT_SUBSCRIPTION);
086: UPDATE_SUBSCRIPTION = sqlProperties.getProperty(
087: "UPDATE_SUBSCRIPTION", UPDATE_SUBSCRIPTION);
088: REMOVE_SUBSCRIPTION = sqlProperties.getProperty(
089: "REMOVE_SUBSCRIPTION", REMOVE_SUBSCRIPTION);
090: GET_USER_BY_CLIENTID = sqlProperties.getProperty(
091: "GET_USER_BY_CLIENTID", GET_USER_BY_CLIENTID);
092: GET_USER = sqlProperties.getProperty("GET_USER", GET_USER);
093:
094: // Read the queries to populate the tables with initial data
095: for (Iterator i = sqlProperties.entrySet().iterator(); i
096: .hasNext();) {
097: Map.Entry entry = (Map.Entry) i.next();
098: String key = (String) entry.getKey();
099: if (key.startsWith("POPULATE.TABLES."))
100: POPULATE_TABLES.add(entry.getValue());
101: }
102:
103: String createString = sqlProperties
104: .getProperty("CREATE_TABLES_ON_START_UP");
105: if (createString == null)
106: createString = sqlProperties
107: .getProperty("CREATE_TABLES_ON_STARTUP");
108: if (createString == null)
109: createTables = true;
110: else
111: createTables = createString.trim().equalsIgnoreCase("true");
112:
113: if (createTables) {
114: Connection connection = null;
115: connection = dataSource.getConnection();
116:
117: try {
118: PreparedStatement statement;
119: try {
120: statement = connection
121: .prepareStatement(CREATE_USER_TABLE);
122: statement.executeUpdate();
123: } catch (SQLException ignored) {
124: log.trace("Error creating table: "
125: + CREATE_USER_TABLE, ignored);
126: }
127: try {
128: statement = connection
129: .prepareStatement(CREATE_ROLE_TABLE);
130: statement.executeUpdate();
131: } catch (SQLException ignored) {
132: log.trace("Error creating table: "
133: + CREATE_ROLE_TABLE, ignored);
134: }
135: try {
136: statement = connection
137: .prepareStatement(CREATE_SUBSCRIPTION_TABLE);
138: statement.executeUpdate();
139: } catch (SQLException ignored) {
140: log.trace("Error creating table: "
141: + CREATE_SUBSCRIPTION_TABLE, ignored);
142: }
143:
144: Iterator iter = POPULATE_TABLES.iterator();
145: String nextQry = null;
146: while (iter.hasNext()) {
147: try {
148: nextQry = (String) iter.next();
149: statement = connection
150: .prepareStatement(nextQry);
151: statement.execute();
152: } catch (SQLException ignored) {
153: log.trace(
154: "Error populating tables: " + nextQry,
155: ignored);
156: }
157: }
158: } finally {
159: try {
160: if (connection != null)
161: connection.close();
162:
163: } catch (SQLException sqle) {
164: log.trace("Error when closing connection " + sqle);
165: }
166: }
167: }
168: }
169:
170: }
|