KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > web > IssuezillaUpdater


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.exceptions.web;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
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 /**
32  *
33  * @author Jan Horvath
34  */

35 public class IssuezillaUpdater extends Thread JavaDoc {
36     
37     public static final int MAX_RETRY = 10;
38     
39     public static final int DELAY = 45000; // time in ms
40

41     EntityManagerFactory emf = Persistence.createEntityManagerFactory("StrutsExceptionsPU");
42     
43     private HashMap JavaDoc<Integer JavaDoc, Integer JavaDoc> issues;
44     
45     private boolean run = true;
46     
47     /** Creates a new instance of IssuezillaUpdater */
48     public IssuezillaUpdater() {
49         issues = new HashMap JavaDoc();
50     }
51     
52     public synchronized void addIssue(Integer JavaDoc id) {
53         System.err.println("*** add:" + id);
54         if (!issues.containsKey(id)) {
55             issues.put(id, new Integer JavaDoc(0));
56         }
57     }
58     
59     public synchronized void removeIssue(Integer JavaDoc id) {
60         if (issues.containsKey(id)) {
61             issues.remove(id);
62         }
63     }
64     
65     public synchronized Collection JavaDoc getIssues() {
66         return Collections.unmodifiableCollection(issues.keySet());
67     }
68     
69     public synchronized void checkIssue(Integer JavaDoc 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 JavaDoc(count));
76         }
77     }
78     
79     public void stopUpdating() {
80         run = false;
81     }
82     
83     public void run() {
84         while (run) {
85             Collection JavaDoc<Integer JavaDoc> col = getIssues();
86             for(Integer JavaDoc 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 JavaDoc 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 JavaDoc e) {
102             }
103         }
104     }
105     
106     public void merge(Object JavaDoc object) {
107         EntityManager em = getEM();
108         try {
109             em.getTransaction().begin();
110             em.merge(object);
111             em.getTransaction().commit();
112         } catch (Exception JavaDoc 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 JavaDoc getEntity(Class JavaDoc entity, Object JavaDoc key) {
127         Object JavaDoc result = null;
128         EntityManager em = getEM();
129         try {
130             result = em.find(entity, key);
131         } catch(Exception JavaDoc e) {
132             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e);
133             throw new RuntimeException JavaDoc(e);
134         } finally {
135             em.close();
136         }
137         return result;
138     }
139 }
140
Popular Tags