KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > storage > raf > log > asynraf > LogDemon


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.storage.raf.log.asynraf;
25
26 import org.objectweb.jalisto.se.exception.JalistoException;
27 import org.objectweb.jalisto.se.impl.trace.Trace;
28
29 import java.util.List JavaDoc;
30
31 public class LogDemon implements Runnable JavaDoc {
32     public LogDemon(AsynrafLogger minilog, PhysicalFileAccessLogAsynrafImpl physical) {
33         this.minilog = minilog;
34         this.physical = physical;
35         trace = physical.getTrace();
36     }
37
38     public void run() {
39         while (!mustClose) {
40             try {
41                 Thread.yield();
42                 if (isWorking) {
43                     List JavaDoc list = minilog.getSortedInfos();
44                     if (!list.isEmpty()) {
45                         while (!list.isEmpty()) {
46                             LogInfo info = (LogInfo) list.remove(0);
47                             if ((!list.isEmpty()) && (info.toString().equals(list.get(0).toString()))) {
48                                 throw new JalistoException("pbm during test");
49                             }
50                             short type = info.getActionType();
51                             if (type == LogInfo.INSERT_TYPE) {
52                                 byte[] datas = minilog.readBytes(info.getValueAddress(),
53                                                                  (int) info.getValueLength());
54                                 physical.internalInsert(info.getIfa(), datas);
55                             } else if (type == LogInfo.UPDATE_TYPE) {
56                                 byte[] datas = minilog.readBytes(info.getValueAddress(),
57                                                                  (int) info.getValueLength());
58                                 physical.internalUpdateFileObject(info.getIfa(), datas);
59                             } else if (type == LogInfo.DELETE_TYPE) {
60                                 physical.internalDeleteFileObject(info.getIfa());
61                             } else {
62                                 throw new JalistoException("unknow action type : " + type);
63                             }
64                             minilog.removeInfo(info);
65                         }
66                         minilog.setLength(2);
67                         minilog.writeStatus(AsynrafLogger.NOBODY_WORKING);
68                         isWorking = false;
69                     } else {
70                         if (isWorking) {
71                             isWorking = false;
72                         }
73                     }
74                 }
75             } catch (Exception JavaDoc e) {
76                 isWorking = false;
77                 throw new JalistoException(e);
78             }
79         }
80     }
81
82     public boolean isWorking() {
83         return isWorking;
84     }
85
86     public void setWorking() {
87         isWorking = true;
88     }
89
90     public void open() {
91         mustClose = false;
92         demon = new Thread JavaDoc(this);
93         demon.start();
94     }
95
96     public void close() {
97         mustClose = true;
98         while (demon.isAlive()) {
99             Thread.yield();
100         }
101         demon = null;
102     }
103
104     private PhysicalFileAccessLogAsynrafImpl physical;
105     private AsynrafLogger minilog;
106     private Trace trace;
107     private boolean mustClose = false;
108     private boolean isWorking = false;
109     private Thread JavaDoc demon;
110 }
111
Popular Tags