1 19 package org.netbeans.modules.exceptions.web; 20 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.persistence.EntityManager; 27 import javax.persistence.EntityManagerFactory; 28 import javax.persistence.Persistence; 29 import org.netbeans.modules.exceptions.entity.Issue; 30 31 35 public class IssuezillaUpdater extends Thread { 36 37 public static final int MAX_RETRY = 10; 38 39 public static final int DELAY = 45000; 41 EntityManagerFactory emf = Persistence.createEntityManagerFactory("StrutsExceptionsPU"); 42 43 private HashMap <Integer , Integer > issues; 44 45 private boolean run = true; 46 47 48 public IssuezillaUpdater() { 49 issues = new HashMap (); 50 } 51 52 public synchronized void addIssue(Integer id) { 53 System.err.println("*** add:" + id); 54 if (!issues.containsKey(id)) { 55 issues.put(id, new Integer (0)); 56 } 57 } 58 59 public synchronized void removeIssue(Integer id) { 60 if (issues.containsKey(id)) { 61 issues.remove(id); 62 } 63 } 64 65 public synchronized Collection getIssues() { 66 return Collections.unmodifiableCollection(issues.keySet()); 67 } 68 69 public synchronized void checkIssue(Integer id) { 70 int count = issues.get(id).intValue(); 71 if (count > MAX_RETRY) { 72 issues.remove(id); 73 } else { 74 count++; 75 issues.put(id, new Integer (count)); 76 } 77 } 78 79 public void stopUpdating() { 80 run = false; 81 } 82 83 public void run() { 84 while (run) { 85 Collection <Integer > col = getIssues(); 86 for(Integer id:col) { 87 System.err.println("*** " + id); 88 checkIssue(id); 89 Issue issue = (Issue) getEntity(Issue.class, id); 90 if (issue.getIssuezillaid().intValue() == 0) { 91 Integer iz = HttpUtils.checkIssuezillaId(issue.getId()); 92 if (iz != null) { 93 removeIssue(id); 94 issue.setIssuezillaid(iz); 95 merge(issue); 96 } 97 } 98 } 99 try { 100 Thread.sleep(DELAY); 101 } catch (java.lang.InterruptedException e) { 102 } 103 } 104 } 105 106 public void merge(Object object) { 107 EntityManager em = getEM(); 108 try { 109 em.getTransaction().begin(); 110 em.merge(object); 111 em.getTransaction().commit(); 112 } catch (Exception e) { 113 java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, 114 "exception caught", e); 115 em.getTransaction().rollback(); 116 } finally { 117 em.close(); 118 } 119 } 120 121 private EntityManager getEM() { 122 EntityManager em = emf.createEntityManager(); 123 return em; 124 } 125 126 protected Object getEntity(Class entity, Object key) { 127 Object result = null; 128 EntityManager em = getEM(); 129 try { 130 result = em.find(entity, key); 131 } catch(Exception e) { 132 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e); 133 throw new RuntimeException (e); 134 } finally { 135 em.close(); 136 } 137 return result; 138 } 139 } 140 | Popular Tags |