KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > make > DependencyContainer


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.make;
31
32 import com.caucho.loader.DynamicClassLoader;
33 import com.caucho.util.Alarm;
34 import com.caucho.util.Log;
35 import com.caucho.vfs.Dependency;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Contains a set of dependencies.
42  */

43 public class DependencyContainer implements Dependency
44 {
45   private static Logger JavaDoc _log;
46   
47   private ArrayList JavaDoc<Dependency> _dependencyList = new ArrayList JavaDoc<Dependency>();
48
49   // Marks if the last check returned modified
50
private boolean _isModified;
51
52   // The interval for checking for a dependency.
53
private long _checkInterval = 2000L;
54
55   // When the dependency check last occurred
56
private long _lastCheckTime = 0;
57
58   private volatile boolean _isChecking;
59
60   public DependencyContainer()
61   {
62     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
63
64     _checkInterval = DynamicClassLoader.getGlobalDependencyCheckInterval();
65     
66     for (; loader != null; loader = loader.getParent()) {
67       if (loader instanceof DynamicClassLoader) {
68     _checkInterval = ((DynamicClassLoader) loader).getDependencyCheckInterval();
69     break;
70       }
71     }
72   }
73   
74   /**
75    * Adds a dependency.
76    */

77   public DependencyContainer add(Dependency dependency)
78   {
79     if (dependency == this)
80       throw new IllegalArgumentException JavaDoc("Can't add self as a dependency.");
81     
82     if (! _dependencyList.contains(dependency))
83       _dependencyList.add(dependency);
84
85     // server/1d0w
86
// XXX: _lastCheckTime = 0;
87

88     return this;
89   }
90   
91   /**
92    * Removes a dependency.
93    */

94   public DependencyContainer remove(Dependency dependency)
95   {
96     if (dependency == this)
97       throw new IllegalArgumentException JavaDoc("Can't remove self as a dependency.");
98     
99     _dependencyList.remove(dependency);
100
101     return this;
102   }
103
104   /**
105    * Sets the check modification check interval in milliseconds.
106    * Negative values mean never check. 0 means always check.
107    *
108    * @param checkInterval how often the dependency should be checked
109    */

110   public void setCheckInterval(long checkInterval)
111   {
112     if (checkInterval < 0 || checkInterval > Long.MAX_VALUE / 2)
113       _checkInterval = Long.MAX_VALUE / 2;
114     else
115       _checkInterval = checkInterval;
116
117     _lastCheckTime = 0;
118   }
119
120   /**
121    * Gets the check modification check interval.
122    * Negative values mean never check. 0 means always check.
123    */

124   public long getCheckInterval()
125   {
126     return _checkInterval;
127   }
128
129   /**
130    * Sets the modified.
131    */

132   public void setModified(boolean isModified)
133   {
134     _isModified = isModified;
135     _lastCheckTime = 0;
136   }
137       
138   /**
139    * Resets the check interval.
140    */

141   public void resetDependencyCheckInterval()
142   {
143     _lastCheckTime = 0;
144   }
145
146   /**
147    * Clears the modified flag and sets the last check time to now.
148    */

149   public void clearModified()
150   {
151     _isModified = false;
152     _lastCheckTime = Alarm.getCurrentTime();
153   }
154
155   /**
156    * Returns true if the underlying dependencies have changed.
157    */

158   public boolean isModified()
159   {
160     synchronized (this) {
161       if (_isChecking || _isModified) {
162     return _isModified;
163       }
164
165       _isChecking = true;
166     }
167
168     try {
169       long now = Alarm.getCurrentTime();
170
171       if (now < _lastCheckTime + _checkInterval)
172     return _isModified;
173
174       _lastCheckTime = now;
175
176       for (int i = _dependencyList.size() - 1; i >= 0; i--) {
177     Dependency dependency = _dependencyList.get(i);
178     
179     if (dependency.isModified()) {
180       log().fine(dependency + " is modified");
181
182       _isModified = true;
183         
184       return _isModified;
185     }
186       }
187
188       // _isModified = false;
189

190       return _isModified;
191     } finally {
192       _isChecking = false;
193     }
194   }
195
196   /**
197    * Returns true if the underlying dependencies have changed, forcing a check.
198    */

199   public boolean isModifiedNow()
200   {
201     _lastCheckTime = 0;
202
203     return isModified();
204   }
205
206   private Logger JavaDoc log()
207   {
208     if (_log == null)
209       _log = Log.open(DependencyContainer.class);
210
211     return _log;
212   }
213   
214   public String JavaDoc toString()
215   {
216     return "DependencyContainer" + _dependencyList;
217   }
218 }
219
Popular Tags