KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > settings > ScheduledRequest


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 2004 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.settings;
21
22 import java.io.IOException JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import org.openide.filesystems.FileLock;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileSystem;
28 import org.openide.util.Exceptions;
29 import org.openide.util.RequestProcessor;
30 import org.openide.util.RequestProcessor.*;
31
32 /** Provides support for async storing of a setting objects.
33  * @author Jan Pokorsky
34  */

35 public final class ScheduledRequest implements Runnable JavaDoc {
36     private final static RequestProcessor PROCESSOR =
37         new RequestProcessor("Settings Processor"); //NOI18N
38
private final static int DELAY = 2000;
39     
40     private Object JavaDoc inst;
41     private Task task;
42     private FileLock lock;
43     private final FileObject fobj;
44     private final FileSystem.AtomicAction run;
45     private boolean running = false;
46     /** counter of scheduled tasks */
47     private int counter = 0;
48     
49     /** Creates a new instance of ScheduledRequest
50      * @param fobj file containing a persistent setting object
51      * @param run impl performing the storing task
52      */

53     public ScheduledRequest(FileObject fobj, FileSystem.AtomicAction run) {
54         this.fobj = fobj;
55         this.run = run;
56     }
57     
58     /** Is the request being executed? */
59     public boolean isRunning() {
60         return running;
61     }
62     
63     /** Provide a file lock; can be null if it is not invoked from the impl of
64      * run.run() (see {@link #ScheduledRequest}). Do not release the lock,
65      * it is the ScheduledRequest responsibility!
66      */

67     public FileLock getFileLock() {
68         return lock;
69     }
70     
71     /** Schedule the request, lock the file and keep the obj reference.
72      * @param obj a setting object. ScheduledRequest prevents object to be GCed
73      * before running the request task
74      */

75     public synchronized void schedule(Object JavaDoc obj) {
76         schedule(obj, DELAY);
77     }
78     
79     private void schedule(Object JavaDoc obj, int delay) {
80         if (obj == null) return;
81         if (inst != null && inst != obj)
82             throw new IllegalStateException JavaDoc("Inconsistant state! File: " + fobj); //NOI18N
83

84         try {
85             if (lock == null) lock = fobj.lock();
86         } catch (IOException JavaDoc ex) {
87             Logger.getLogger(ScheduledRequest.class.getName()).log(Level.WARNING, null, ex);
88             return;
89         }
90         
91         counter++;
92         if (task == null) {
93             task = PROCESSOR.post(this, delay);
94         } else {
95             task.schedule(delay);
96         }
97         
98         // prevent object to be GCed before running the task
99
this.inst = obj;
100     }
101     
102     /** Try to cancel the scheduled request if exists and perform the request task.
103      * @exception IOException if the storing fails
104      */

105     public synchronized void runAndWait() throws IOException JavaDoc {
106         if (task != null) {
107             // if anything was scheduled cancel it
108
task.cancel();
109             counter = 0;
110             releaseResources();
111         }
112         lock = fobj.lock();
113         performRequest();
114     }
115     
116     /** force to finish the scheduled request */
117     public void forceToFinish() {
118         Task t = null;
119         synchronized (this) {
120             if (task != null) {
121                 t = task;
122                 task.schedule(0);
123             }
124         }
125         if (t != null) t.waitFinished();
126     }
127     
128     /** cancel the scheduled request task */
129     public void cancel() {
130         RequestProcessor.Task t = task;
131         if (t == null) return ;
132         
133         if (isRunning()) {
134             t.waitFinished();
135         } else {
136             synchronized (this) {
137                 t.cancel();
138                 counter = 0;
139                 releaseResources();
140             }
141         }
142     }
143     
144     public void run() {
145         synchronized (this) {
146             counter = 0;
147         }
148         
149         try {
150             performRequest();
151         } catch (IOException JavaDoc ex) {
152             Exceptions.attachLocalizedMessage(ex, fobj.toString());
153             Logger.getLogger(ScheduledRequest.class.getName()).log(Level.WARNING, null, ex);
154         }
155     }
156     
157     /** perform the storing task, release the file lock and reset
158      * ScheduledRequest */

159     private void performRequest() throws IOException JavaDoc {
160         boolean isAlreadyRunning = false;
161         Task runningTask;
162         
163         synchronized (this) {
164             isAlreadyRunning = running;
165             runningTask = task;
166             running = true;
167         }
168         
169         if (isAlreadyRunning) {
170             runningTask.waitFinished();
171             return;
172         }
173         
174         try {
175             run.run();
176         } finally {
177             synchronized (this) {
178                 running = false;
179                 if (task == null || counter == 0) {
180                     releaseResources();
181                 }
182             }
183         }
184     }
185     
186     private void releaseResources() {
187         if (lock != null) {
188             lock.releaseLock();
189             lock = null;
190         }
191         inst = null;
192         task = null;
193     }
194     
195 }
196
Popular Tags