View Javadoc
1 /* 2 * PROJECT : DAR Runtime and Tools 3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org 4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE 5 * Version 2.1, February 1999 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package org.enableit.db; 22 23 import java.sql.Connection; 24 import java.sql.SQLWarning; 25 import java.util.Hashtable; 26 import java.util.List; 27 import java.util.TreeMap; 28 29 import org.apache.log4j.Logger; 30 31 32 /*** 33 * Implements a Persistence Manager whose persistent store is the 34 * Database managed by this library. Similar concept to JDO. 35 * 36 * @deprecated Experimental class now evolved into the ShapeShifter classes in 37 * package org.enableit.db.ss 38 * @version __VERSION__ 39 * @author __AUTHOR__ 40 */ 41 public class DBPersistenceManager { 42 /* 43 * Properties 44 */ 45 46 /*** 47 * The Log4J <code>Logger</code> doing the logging. 48 */ 49 private static Logger logger = Logger.getLogger(DBPersistenceManager.class); 50 51 /*** 52 * CVS info about this class and its current version 53 */ 54 public static final String ABOUT = "$Revision: 1.7 $"; 55 56 /* 57 * Constructors 58 */ 59 60 /*** 61 * Default Constructor 62 */ 63 public DBPersistenceManager() { 64 } 65 66 /* 67 * PersistenceManager methods 68 */ 69 70 /*** 71 * Stores the state of the object received, keyed by the domain and keys table 72 */ 73 public void store(String domain, Hashtable keys, Object obj) { 74 logger.info("METHOD_ENTRY"); 75 76 try { 77 Connection conn = ConnectionFactory.getConnection(); 78 79 // It should be impossible to store two versions of the same MBean, so delete any existing 80 String sql = "DELETE FROM MBean WHERE mbeanName = '" + domain + ":" 81 + keys.toString() + "'"; 82 83 DatabaseProxy.executeUpdate(conn, sql); 84 85 // Need an id 86 int id = DBUtils.getNextIdUsingSQL("MBean"); 87 88 sql = "insert MBean (mbeanId, mbeanName, mbeanObject) values(?, ?, ?)"; 89 90 java.sql.PreparedStatement ps = conn.prepareStatement(sql); 91 92 ps.setInt(1, id); 93 ps.setObject(2, domain + ":" + keys.toString()); 94 ps.setObject(3, (Object) obj); 95 96 logger.warn("Executing: " + ps.toString()); 97 98 // Do it 99 ps.executeUpdate(); 100 101 // Print any warnings chained to statement object 102 SQLWarning warning = ps.getWarnings(); 103 104 while (warning != null) { 105 logger.warn("<SQL Warning :>" + warning.toString()); 106 warning = warning.getNextWarning(); 107 } 108 109 // Cleanup 110 conn.close(); 111 conn = null; 112 } catch (Exception e) { 113 logger.error(e.getMessage(), e); 114 } 115 116 logger.info("METHOD_EXIT"); 117 } 118 119 /*** 120 * Loads the state of the object received, keyed by the domain and keys table 121 */ 122 public Hashtable load(String domain, Hashtable keys) { 123 logger.info("METHOD_ENTRY"); 124 125 Hashtable attributes = null; 126 127 try { 128 Connection conn = ConnectionFactory.getConnection(); 129 130 String sql = "SELECT mbeanObject "; 131 132 sql += "FROM MBean "; 133 sql += ("WHERE mbeanName = '" + domain + ":" + keys.toString() 134 + "'"); 135 logger.warn("SQL_VERBOSE executing: " + sql); 136 137 List results = (List) GenericDBProxy.executeQuery(sql, 138 DatabaseProxy.LIST); 139 140 if (results.size() != 1) { 141 throw new Exception(); 142 } 143 144 attributes = (Hashtable) ((TreeMap) results.get(0)).get( 145 "mbeanObject"); 146 } catch (Exception e) { 147 logger.error(e.getMessage(), e); 148 } 149 150 logger.info("METHOD_EXIT"); 151 152 return attributes; 153 } 154 }

This page was automatically generated by Maven