KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > business > UpdateThread


1 /*
2  * snapper
3  *
4  * Enhydra super-servlet business object
5  *
6  */

7
8 package org.enhydra.snapper.business;
9
10 import java.io.File JavaDoc;
11 import java.sql.Timestamp JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import org.enhydra.dods.DODS;
15 import org.enhydra.dods.DODSException;
16 import org.enhydra.snapper.SnapperAdmin;
17 import org.enhydra.snapper.SnapperManager;
18 import org.enhydra.snapper.api.Reader;
19 import org.enhydra.snapper.spec.Update;
20 import org.enhydra.snapper.spec.Indexed;
21 import org.enhydra.snapper.spec.IndexedFactory;
22 import org.enhydra.snapper.spec.IndexerBaseSpec;
23 import org.enhydra.snapper.spec.NotIndexed;
24 import org.enhydra.snapper.spec.NotIndexedFactory;
25 import org.enhydra.snapper.spec.Path;
26 import org.enhydra.snapper.spec.PathFactory;
27 import org.enhydra.snapper.spec.ReaderFactory;
28 import org.enhydra.snapper.spec.Site;
29 import org.enhydra.snapper.spec.SiteFactory;
30 import org.enhydra.snapper.spec.UpdaterSpec;
31 import org.enhydra.snapper.spec.UpdaterSpecFactory;
32
33 import com.lutris.appserver.server.sql.DBTransaction;
34 import com.lutris.appserver.server.sql.DatabaseManager;
35
36 // Enhydra SuperServlet specification imports
37

38 // Standard imports
39
//import java.text.DateFormat;
40

41 public class UpdateThread implements Update, Runnable JavaDoc {
42     String JavaDoc siteID, siteName;
43
44     Site site = null;
45
46     Path[] pathArray = null;
47
48     UpdaterSpec updater = null;
49
50     IndexerBaseSpec indexerBase = null;
51
52     String JavaDoc indexdir = "";
53
54     DBTransaction dbTransaction = null;
55
56     private boolean doStop, indexed;
57
58     private DatabaseManager dbm;
59
60     public void setDoStop(boolean n) throws Exception JavaDoc {
61         doStop = n;
62         updater.setDoStop(n);
63         site.setLOCKED(null);
64         site.save();
65         dbTransaction.commit();
66         SnapperManager.getInstance().removeThread(siteID);
67         //dbTransaction.release();
68
updater = null;
69
70     }
71
72     public void run() {
73         try {
74             try {
75                 dbTransaction = dbm.createTransaction();
76             } catch (Exception JavaDoc ex) {
77                 System.out.println("Could not init transaction");
78             }
79
80             try {
81                 site = (SiteFactory.getSite("org.enhydra.snapper.business.SiteImpl")).findSiteByID(siteID,
82                                 dbTransaction);
83                 siteName = site.getName();
84                 updater = UpdaterSpecFactory.getUpdater("org.enhydra.snapper.business.Updater");
85                 updater.setDBM(dbm);
86                 Path pt = PathFactory.getPath("org.enhydra.snapper.business.PathImpl");
87                 pathArray = pt.getListForID(dbTransaction, site.getID());
88
89             } catch (Exception JavaDoc ex) {
90             }
91
92             Timestamp JavaDoc sts = new Timestamp JavaDoc(System.currentTimeMillis());
93
94             try {
95                 SnapperManager.getInstance().getLoggingManager().info("Updating site " + site.getName() + " started");
96
97             } catch (Exception JavaDoc e) {
98                 System.out.println("Updating site " + siteName + " started");
99
100             }
101
102             try {
103                 site.setLOCKED("true");
104                 site.save();
105                 dbTransaction.commit();
106             } catch (Exception JavaDoc e) {
107                 SnapperManager.getInstance().getLoggingManager().error("Could not lock site");
108             }
109
110             try {
111                 updater.setMeta(site.getMETADB(), site.getMETATABLE(), site.getMETAFILE(), site.getMETAKEY(), site
112                                 .getMETAVALUE());
113                 updater.updateIndex(site, pathArray);
114             } catch (Exception JavaDoc ex) {
115                 SnapperManager.getInstance().getLoggingManager().error("Could not update");
116             }
117
118             Timestamp JavaDoc ts = new Timestamp JavaDoc(System.currentTimeMillis());
119
120             try {
121                 site.setLOCKED(null);
122                 site.setLASTUPDATE(ts);
123                 Reader reader = ReaderFactory.createReader("org.enhydra.snapper.wrapper.lucene.LuceneReader");
124                 if (site.getINDEXDIR().length() == 0)
125                     reader.setUpReader(SnapperAdmin.getIndexDir() + File.separator + site.getName());
126                 else
127                     reader.setUpReader(site.getINDEXDIR());
128                 site.setFILENR(reader.getSize());
129                 site.save();
130                 SnapperManager.getInstance().getIndexerFactory().removeIndexer();
131
132                 Indexed in = IndexedFactory.getIndexed("org.enhydra.snapper.business.IndexedImpl");
133                 in.createNew(dbTransaction);
134                 in.setName(site.getName());
135                 in.setLASTSTART(sts);
136                 in.setLASTTYPE("UPDATE");
137                 in.setTime(ts);
138
139                 in.setDoc(String.valueOf(reader.getSize()));
140                 in.save();
141                 reader.closeReader();
142                 reader = null;
143                 dbTransaction.commit();
144
145                 NotIndexed ni = NotIndexedFactory.getNotIndexed("org.enhydra.snapper.business.NotIndexedImpl");
146
147                 Vector JavaDoc vni = updater.getNotIndexed();
148                 for (int i = 0; i < vni.size(); i++) {
149                     ni.createNew(dbTransaction);
150                     ni.setIndexed(in.getID());
151                     ni.setLink(vni.elementAt(i).toString());
152                     ni.save();
153                 }
154                 dbTransaction.commit();
155             } catch (Exception JavaDoc e) {
156
157                 SnapperManager.getInstance().getLoggingManager().debug("Could not save NotIndexed");
158             }
159
160             SnapperManager.getInstance().getLoggingManager().info("Updating site " + siteName + " finished");
161             SnapperManager.getInstance().getLoggingManager().info(
162                             "Total time = " + (ts.getTime() - sts.getTime()) + " miliseconds");
163             SnapperManager.getInstance().removeThread(this.siteID);
164             
165         } finally {
166             try{
167                 if (dbTransaction!=null)
168                     dbTransaction.release();
169             }catch (Exception JavaDoc e) {
170                 e.printStackTrace();
171             }
172            
173             try {DODS.unregister(Thread.currentThread());
174             } catch (DODSException e) {
175                 e.printStackTrace();
176             }
177
178         }
179     }
180
181     public void stop() {
182     };
183
184     public UpdateThread() {
185     }
186
187     public void update(String JavaDoc siteOID, DatabaseManager dbm) {
188         this.siteID = siteOID;
189         this.dbm = dbm;
190     }
191
192 }
Popular Tags