001 /** 002 * Copyright (c) 2003 Daffodil Software Ltd all rights reserved, 003 * Modifications Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved. 004 * This program is free software; you can redistribute it and/or modify 005 * it under the terms of version 2 of the GNU General Public License as 006 * published by the Free Software Foundation. 007 * There are special exceptions to the terms and conditions of the GPL 008 * as it is applied to this software. See the GNU General Public License for more details. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 021 package org.dbreplicator.replication.schedule; 022 023 import java.sql.*; 024 import java.util.*; 025 026 import org.dbreplicator.replication.*; 027 import org.dbreplicator.replication.DBHandler.AbstractDataBaseHandler; 028 import org.apache.log4j.Logger; 029 /** 030 * 031 * <p>Handles scheduling for DBReplicator</p> 032 * <p>This class Starts the thread for Scheduling.Whenever the replication Server starts 033 * it starts scheduling in seperate thread.This classs is used for:start schedule,edit schedule and remove schedule. 034 * </p> 035 * <p>Copyright: 036 * <p>Company: </p> 037 * @author not attributable 038 * @version 1.0 039 */ 040 public class ScheduleHandler { 041 _Subscription sub; 042 _ReplicationServer repServer; 043 Connection con; 044 AbstractDataBaseHandler dbHandler; 045 ArrayList schedulerList = new ArrayList(); 046 HashMap map = new HashMap(); 047 String subName = null; 048 String scheduleName = null; 049 String scheduleType=null; 050 String publicationServerName = null; 051 String publicationPortNo = null; 052 String recurrenceType = null; 053 String replicationType = null; 054 long scheduleTime = 0; 055 int counter = 0; 056 ScheduledSynchronizer scheduledSynchronizer; 057 Thread t; 058 protected static Logger log =Logger.getLogger(ScheduleHandler.class.getName()); 059 060 public ScheduleHandler(_ReplicationServer repServer0, 061 AbstractDataBaseHandler dbHandler0) throws RepException { 062 log.debug("intializing ScheduleHandler"); 063 repServer = repServer0; 064 dbHandler = dbHandler0; 065 con = repServer.getDefaultConnection(); 066 Statement st = null; 067 ResultSet rs = null; 068 try { 069 070 st = con.createStatement(); 071 rs = st.executeQuery("select * from " + 072 dbHandler.getScheduleTableName()); //select query 073 while (rs.next()) { 074 scheduleName = rs.getString(1); 075 subName = rs.getString(2); 076 scheduleType=rs.getString(3); 077 publicationServerName = rs.getString(4); 078 publicationPortNo = rs.getString(5); 079 recurrenceType = rs.getString(6); 080 replicationType = rs.getString(7); 081 scheduleTime = rs.getLong(8); 082 counter = rs.getInt(9); 083 084 scheduledSynchronizer = new 085 ScheduledSynchronizer(repServer,dbHandler,subName, scheduleName, scheduleType, publicationServerName, 086 publicationPortNo, recurrenceType, 087 replicationType, scheduleTime, counter,map); 088 t = new Thread(scheduledSynchronizer, 089 (scheduleName)); 090 map.put(scheduleName, t); 091 log.info("Schedule "+scheduleName+" going to start"); 092 t.start(); 093 } 094 }catch (SQLException ex) { 095 log.debug(ex); 096 } 097 catch(RepException ex1){ 098 log.debug(ex1,ex1); 099 throw ex1; 100 } 101 catch (Exception ex) { 102 log.debug(ex); 103 } finally { 104 try { 105 if (st != null) { 106 st.close(); 107 } 108 if (rs != null) { 109 rs.close(); 110 } 111 } 112 catch (SQLException ex2) { 113 } 114 } 115 } 116 /** 117 * startSchedule is called after saving Schedule information in Rep_ScheduleTable and when user edits a schedule 118 * @param subName0 String 119 * @param scheduleName0 String 120 * @param remoteServerName String 121 * @param remotePortNo String 122 * @param recurrenceType0 String 123 * @param repType String 124 * @param schTime long 125 * @param counter0 int 126 * @throws RepException 127 */ 128 public void startSchedule(String subName0, String scheduleName0,String scheduleType0, 129 String remoteServerName, String remotePortNo, 130 String recurrenceType0, String repType, long schTime, 131 int counter0) throws RepException { 132 subName = subName0; 133 scheduleName = scheduleName0; 134 scheduleType= scheduleType0; 135 subName = subName0; 136 scheduleName = scheduleName0; 137 publicationServerName = remoteServerName; 138 publicationPortNo = remotePortNo; 139 recurrenceType = recurrenceType0; 140 replicationType = repType; 141 scheduleTime = schTime; 142 counter = counter0; 143 scheduledSynchronizer = new 144 ScheduledSynchronizer(repServer,dbHandler,subName, scheduleName,scheduleType, publicationServerName, 145 publicationPortNo, recurrenceType, repType, 146 scheduleTime, counter,map); 147 148 t = new Thread(scheduledSynchronizer, 149 (scheduleName)); 150 map.put(scheduleName, t); 151 log.info("Schedule "+scheduleName+" going to start"); 152 t.start(); 153 } 154 /** 155 * This method drop the schedule by deleting the schedule information from schedule table and 156 * stopping the Schedule thread 157 * @param subName0 String 158 * @throws RepException 159 */ 160 public void dropSchedule(String subName0) throws 161 RepException { 162 Statement st =null; 163 ResultSet rs =null; 164 try { 165 subName = subName0; 166 st = con.createStatement(); 167 StringBuffer select =new StringBuffer(); 168 select.append("select " + RepConstants.schedule_Name + " from ") 169 .append(dbHandler.getScheduleTableName()) 170 .append(" where ").append(RepConstants.subscription_subName1) 171 .append(" = '").append(subName).append("'"); 172 rs = st.executeQuery(select.toString()); 173 if(!(rs.next())){ 174 throw new RepException("REP203", new Object[] {scheduleName}); 175 } 176 scheduleName = rs.getString(1); 177 Thread scheduleThread = (Thread) map.get(scheduleName); 178 if(scheduleThread!=null){ 179 scheduledSynchronizer.stopSchedule=true; 180 // Thread thread =(Thread) map.remove(scheduleName); 181 // System.out.println("thread before stopping::" + thread); 182 // thread.stop(); 183 // System.out.println("thread after stopping::" + thread); 184 } 185 StringBuffer query=new StringBuffer(); 186 query=query.append("delete from " ).append(dbHandler.getScheduleTableName()) 187 .append(" where ").append(RepConstants.subscription_subName1) 188 .append(" = '").append(subName).append( "'"); 189 st.executeUpdate(query.toString()); 190 log.info("remove schedule query "+query.toString()); 191 Utility.createTransactionLogFile =true; 192 } 193 catch (SQLException ex) { 194 // ex.printStackTrace(); 195 // RepConstants.writeERROR_FILE(ex); 196 throw new RepException("REP204", new Object[] {scheduleName, 197 ex.getMessage()}); 198 }catch(RepException ex1){ 199 throw ex1; 200 } 201 catch (Exception ex2) { 202 RepConstants.writeERROR_FILE(ex2); 203 } finally { 204 try { 205 if (st != null) 206 st.close(); 207 if(rs!=null) 208 rs.close(); 209 } 210 catch (SQLException ex3) { 211 } 212 } 213 } 214 /** 215 * Whenever user edits the schedule information,this methods first stops the running thread then update the schedule information in 216 * Rep_ScheduleTable then start the schedule again by giving call to startSchedule() method. 217 * (only publisher server name and publisher port no. is allowed to be edited) 218 * 219 * @param scheduleName0 String 220 * @param subName0 String 221 * @param newPubServerName String 222 * @param newPubPortNo String 223 * @throws RepException 224 */ 225 public void editSchedule(String scheduleName0, String subName0, 226 String newPubServerName, String newPubPortNo) throws 227 RepException { 228 Statement st =null; 229 ResultSet rs =null; 230 try { 231 subName = subName0; 232 scheduleName = scheduleName0; 233 st = con.createStatement(); 234 Thread scheduleThread = (Thread) map.get(scheduleName); 235 if(scheduleThread!=null){ 236 scheduledSynchronizer.stopSchedule=true; 237 // Thread thread =(Thread) map.remove(scheduleName); 238 // thread.stop(); 239 } 240 StringBuffer query=new StringBuffer(); 241 query=query.append("Update ").append(dbHandler.getScheduleTableName()) 242 .append(" set ").append(RepConstants.publication_portNo) 243 .append(" = '" ).append(newPubPortNo).append("',").append(RepConstants.publication_serverName3) 244 .append(" = '").append(newPubServerName) 245 .append("' where " ) 246 .append(RepConstants.subscription_subName1) 247 .append(" = '").append(subName).append("' and ").append( RepConstants.schedule_Name) 248 .append(" = '").append(scheduleName).append("'"); 249 int updateQuery=st.executeUpdate(query.toString()); 250 log.info("edit schedule query "+query.toString()); 251 if(updateQuery==0){ 252 throw new RepException("REP221", new Object[] {scheduleName}); 253 } 254 255 StringBuffer query1=new StringBuffer(); 256 query1=query1.append("select * from ").append(dbHandler.getScheduleTableName()) 257 .append(" where ").append(RepConstants.schedule_Name ) 258 .append(" = '").append(scheduleName).append( "' and " ) 259 .append(RepConstants.subscription_subName1 ) 260 .append(" = '").append(subName).append( "'"); 261 262 rs = st.executeQuery(query1.toString()); //select query 263 while (rs.next()) { 264 scheduleName = rs.getString(1); 265 subName= rs.getString(2); 266 scheduleType=rs.getString(3); 267 publicationServerName = rs.getString(4); 268 publicationPortNo = rs.getString(5); 269 recurrenceType = rs.getString(6); 270 replicationType = rs.getString(7); 271 scheduleTime = rs.getLong(8); 272 counter = rs.getInt(9); 273 } 274 startSchedule(subName, scheduleName, scheduleType,publicationServerName, 275 publicationPortNo, recurrenceType, replicationType, 276 scheduleTime, counter); 277 } 278 catch (SQLException ex) { 279 // RepConstants.writeERROR_FILE(ex); 280 throw new RepException("REP219",new Object[] {scheduleName,subName}); 281 }catch(RepException ex1){ 282 throw ex1; 283 }catch(Exception ex){ 284 RepConstants.writeERROR_FILE(ex); 285 } finally { 286 try { 287 if (st != null) 288 st.close(); 289 if(rs!=null) 290 rs.close(); 291 } 292 catch (SQLException ex2) { 293 } 294 } 295 } 296 297 }

