001/* 002 * Copyright (c) 2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-05 22:21:26 -0700 (Wed, 05 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029package org.kepler.workflowscheduler.gui; 030 031import java.io.StringReader; 032import java.util.Vector; 033import java.util.concurrent.locks.Lock; 034 035import javax.swing.JOptionPane; 036import javax.swing.SwingWorker; 037 038import org.apache.commons.logging.Log; 039import org.apache.commons.logging.LogFactory; 040import org.kepler.module.workflowschedulergui.Initialize; 041import org.xml.sax.ContentHandler; 042import org.xml.sax.ErrorHandler; 043import org.xml.sax.InputSource; 044import org.xml.sax.XMLReader; 045import org.xml.sax.helpers.XMLReaderFactory; 046 047import edu.ucsb.nceas.schedulerclient.WorkflowSchedulerClient; 048import edu.ucsb.nceas.workflowscheduler.scheduler.WorkflowSchedulerException; 049 050/** 051 * This class represents a worker to search existing schedules from remote 052 * server in a in a dedicated thread. 053 * @author tao 054 * 055 */ 056public class SearchScheduleSwingWorker extends SwingWorker 057{ 058 private static final Log log = LogFactory.getLog(SearchScheduleSwingWorker.class.getName()); 059 public static final String PARSERNAME = "org.apache.xerces.parsers.SAXParser"; 060 061 private String workflowLSID = null; 062 private String karLSID = null; 063 //private String workflowName = null; 064 //private String karLSID = null; 065 private Schedule[] schedules = null; 066 private ScheduleChangeController scheduleChangeController= null; 067 private Lock existingScheduleTablelock = null; 068 069 /** 070 * Constructor 071 * @param workflowLSID the workflow id will be search 072 */ 073 public SearchScheduleSwingWorker(ScheduleChangeController controller, 074 String workflowLSID, String karLSID, Lock existingScheduleTablelock) throws Exception 075 { 076 super(); 077 if(workflowLSID == null || workflowLSID.trim().equals("")) 078 { 079 throw new Exception("The workflow id for seaching existing schedules couldn't be blank "); 080 } 081 if(karLSID == null || karLSID.trim().equals("")) 082 { 083 throw new Exception("The kar id for seaching existing schedules couldn't be blank "); 084 } 085 this.scheduleChangeController = controller; 086 this.karLSID = karLSID; 087 this.existingScheduleTablelock = existingScheduleTablelock; 088 this.workflowLSID = workflowLSID; 089 //this.workflowName = workflowName; 090 //this.karLSID = karLSID; 091 } 092 093 094 095 /** 096 * Compute the value to be returned by the <code>get</code> method. 097 */ 098 public Object doInBackground() 099 { 100 if(existingScheduleTablelock != null) 101 { 102 existingScheduleTablelock.lock(); 103 } 104 searchSchedules(); 105 if(existingScheduleTablelock != null) 106 { 107 existingScheduleTablelock.unlock(); 108 } 109 return null; 110 } 111 112 /* 113 * Search existing schedules from scheduler server and update the 114 * existing table 115 */ 116 private void searchSchedules() 117 { 118 String schedulerURL = Initialize.getSchedulerURL(); 119 String sessionID = "unknown"; 120 String authenticationServiceURL = null; 121 try 122 { 123 if(schedulerURL == null || schedulerURL.trim().equals("")) 124 { 125 throw new WorkflowSchedulerException("Kepler couldn't find the workflow scheduler url in the configuration file."); 126 } 127 WorkflowSchedulerClient client = new WorkflowSchedulerClient(schedulerURL, sessionID, authenticationServiceURL); 128 String result = client.getJobs(workflowLSID, karLSID); 129 //System.out.println("search result is:\n"+result); 130 parse(result); 131 if(scheduleChangeController != null) 132 { 133 scheduleChangeController.update(schedules); 134 scheduleChangeController.complete(); 135 } 136 137 } 138 catch(WorkflowSchedulerException e) 139 { 140 JOptionPane.showMessageDialog(null, "Search existing schedule failed:\n"+ 141 e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 142 if(scheduleChangeController != null) 143 { 144 scheduleChangeController.stopProgressBar(); 145 } 146 147 } 148 catch(Exception e) 149 { 150 log.error(e.getMessage()); 151 if(scheduleChangeController != null) 152 { 153 scheduleChangeController.stopProgressBar(); 154 } 155 } 156 157 158 } 159 160 /* 161 * Parse the result xml by sax parser 162 */ 163 private void parse(String result) 164 { 165 if(result != null && !result.trim().equals("")) 166 { 167 StringReader xmlReader = new StringReader(result); 168 try 169 { 170 XMLReader parser = null; 171 parser = XMLReaderFactory.createXMLReader(PARSERNAME); 172 QueryingSchedulerServerResultParser handler = new 173 QueryingSchedulerServerResultParser(); 174 parser.setContentHandler((ContentHandler) handler); 175 parser.setErrorHandler((ErrorHandler) handler); 176 parser.parse(new InputSource(xmlReader)); 177 Vector<Schedule> schedulesVector = handler.getSchedules(); 178 if(schedulesVector != null && schedulesVector.size() >0) 179 { 180 //System.out.println("The schedules vector size is "+ schedulesVector.size()); 181 schedules = new Schedule[schedulesVector.size()]; 182 schedulesVector.toArray(schedules); 183 //System.out.println("The schedules array size is "+schedules.length); 184 } 185 else 186 { 187 //System.out.println("The result schedules vector is null"); 188 } 189 190 } 191 catch(Exception e) 192 { 193 log.error("Couldn't parse the return document -"+e.getMessage()); 194 } 195 } 196 } 197 198}