KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > BasicDependencyContainer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.vfs;
31
32 import com.caucho.util.Alarm;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 /**
38  * Contains a set of dependencies.
39  */

40 public class BasicDependencyContainer implements Dependency
41 {
42   private static Logger JavaDoc _log;
43   
44   private ArrayList JavaDoc<Dependency> _dependencyList = new ArrayList JavaDoc<Dependency>();
45
46   // Marks if the last check returned modified
47
private boolean _isModified;
48
49   // The interval for checking for a dependency.
50
private long _checkInterval = 2000L;
51
52   // When the dependency check last occurred
53
private long _lastCheckTime = 0;
54
55   private volatile boolean _isChecking;
56   
57   /**
58    * Adds a dependency.
59    */

60   public BasicDependencyContainer add(Dependency dependency)
61   {
62     if (dependency == this)
63       throw new IllegalArgumentException JavaDoc("Can't add self as a dependency.");
64     
65     if (! _dependencyList.contains(dependency))
66       _dependencyList.add(dependency);
67
68     // server/1d0w
69
// XXX: _lastCheckTime = 0;
70

71     return this;
72   }
73   
74   /**
75    * Removes a dependency.
76    */

77   public BasicDependencyContainer remove(Dependency dependency)
78   {
79     if (dependency == this)
80       throw new IllegalArgumentException JavaDoc("Can't remove self as a dependency.");
81     
82     _dependencyList.remove(dependency);
83
84     return this;
85   }
86
87   /**
88    * Sets the check modification check interval in milliseconds.
89    * Negative values mean never check. 0 means always check.
90    *
91    * @param checkInterval how often the dependency should be checked
92    */

93   public void setCheckInterval(long checkInterval)
94   {
95     if (checkInterval < 0 || checkInterval > Long.MAX_VALUE / 2)
96       _checkInterval = Long.MAX_VALUE / 2;
97     else
98       _checkInterval = checkInterval;
99
100     _lastCheckTime = 0;
101   }
102
103   /**
104    * Gets the check modification check interval.
105    * Negative values mean never check. 0 means always check.
106    */

107   public long getCheckInterval()
108   {
109     return _checkInterval;
110   }
111
112   /**
113    * Sets the modified.
114    */

115   public void setModified(boolean isModified)
116   {
117     _isModified = isModified;
118     _lastCheckTime = 0;
119   }
120       
121   /**
122    * Resets the check interval.
123    */

124   public void resetDependencyCheckInterval()
125   {
126     _lastCheckTime = 0;
127   }
128
129   /**
130    * Clears the modified flag and sets the last check time to now.
131    */

132   public void clearModified()
133   {
134     _isModified = false;
135     _lastCheckTime = Alarm.getCurrentTime();
136   }
137
138   /**
139    * Returns true if the underlying dependencies have changed.
140    */

141   public boolean isModified()
142   {
143     synchronized (this) {
144       if (_isChecking || _isModified) {
145     return _isModified;
146       }
147
148       _isChecking = true;
149     }
150
151     try {
152       long now = Alarm.getCurrentTime();
153
154       if (now < _lastCheckTime + _checkInterval)
155     return _isModified;
156
157       _lastCheckTime = now;
158
159       for (int i = _dependencyList.size() - 1; i >= 0; i--) {
160     Dependency dependency = _dependencyList.get(i);
161     
162     if (dependency.isModified()) {
163       log().fine(dependency + " is modified");
164
165       _isModified = true;
166         
167       return _isModified;
168     }
169       }
170
171       // _isModified = false;
172

173       return _isModified;
174     } finally {
175       _isChecking = false;
176     }
177   }
178
179   /**
180    * Returns true if the underlying dependencies have changed, forcing a check.
181    */

182   public boolean isModifiedNow()
183   {
184     _lastCheckTime = 0;
185
186     return isModified();
187   }
188
189   private Logger JavaDoc log()
190   {
191     if (_log == null)
192       _log = Logger.getLogger(BasicDependencyContainer.class.getName());
193
194     return _log;
195   }
196   
197   public String JavaDoc toString()
198   {
199     return "BasicDependencyContainer" + _dependencyList;
200   }
201 }
202
Popular Tags