1/*2 * Copyright 2000-2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.portal.expire;
1819//jetspeed stuff20import org.apache.jetspeed.cache.disk.DiskCacheEntry;
21import org.apache.jetspeed.cache.disk.DiskCacheUtils;
22import org.apache.jetspeed.cache.disk.JetspeedDiskCache;
23import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24import org.apache.jetspeed.services.logging.JetspeedLogger;
2526//java stuff27import java.io.IOException;
28import java.io.Serializable;
293031/***32A generic class for watching a file and determining if it has changed.3334@author <a href="mailto:burton@apache.org">Kevin A. Burton</a>35@version $Id: FileWatcher.java,v 1.16 2004/02/23 03:24:40 jford Exp $36*/37publicclassFileWatcher implements Serializable
38 {
3940/***41 * Static initialization of the logger for this class42 */43privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(FileWatcher.class.getName());
4445//BEGIN Exception index4647publicstaticfinal String EXCEPTION_URL_NOT_NULL =
48"URL can NOT be null here.";
4950publicstaticfinal String EXCEPTION_URL_NOT_IN_CACHE =
51"The URL you specified within the disk cache does not exist: ";
525354//END Exception index 5556/***57 The URL on which this FileWatchExpire is based.58 */59private String url = "";
6061/***62 The last time this files URL has been modified on disk.63 */64privatelong lastModified = 0;
6566/***67 The object that relies on this FileWatcher68 */69private String parent = "";
7071/***72 Create a FileWatcher with no parent info.7374 @see #FileWatcher( String, String )7576 @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>77 @version $Id: FileWatcher.java,v 1.16 2004/02/23 03:24:40 jford Exp $78 */79publicFileWatcher( String url ) throws IOException {
80this( url, null );
81 }
8283/***84 Create a new FileWatcher to watch the given URL.8586 @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>87 @version $Id: FileWatcher.java,v 1.16 2004/02/23 03:24:40 jford Exp $88 */89publicFileWatcher( String url,
90 String parent ) throws IOException {
9192if ( url == null ) {
93thrownew IOException( EXCEPTION_URL_NOT_NULL );
94 }
9596if ( DiskCacheUtils.isRemote( url ) &&
97 DiskCacheUtils.isCached( url ) == false ) {
9899thrownew IOException( EXCEPTION_URL_NOT_IN_CACHE + url );
100 }
101102//Try to set last modified when creating FileWatcher objet103try {
104this.lastModified = JetspeedDiskCache.getInstance().getEntry( url ).
105 getLastModified();
106 } catch (Throwable e)
107 {
108 logger.error( "Unable to set last modified on url " + url, e );
109 }
110111this.url = url;
112this.parent = parent;
113 }
114115/***116 Return true if the URL on which this is based has changed.117118 @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>119 @version $Id: FileWatcher.java,v 1.16 2004/02/23 03:24:40 jford Exp $120 */121publicboolean hasChanged() {
122123try {
124125//initially set the lastModified data126if ( this.lastModified == 0 ) {
127128DiskCacheEntry entry = JetspeedDiskCache.getInstance().getEntry( url );
129130this.lastModified = entry.getLastModified();
131132return false;
133134 }
135136//the recent modification... if there was one. otherwise it will 137// == this.lastModified138long recent = JetspeedDiskCache.getInstance()
139 .getEntry( url ).getLastModified();
140141// 0 means always modified142if ( recent == 0 ||
143this.lastModified < recent ) {
144145if ( logger.isInfoEnabled() )
146 {
147 String message = "";
148149if ( this.parent != null ) {
150 message = this.parent + ": ";
151 }
152153 message += "REFRESH: Expiring Portlet because it's URL has been modified on disk -> " + url;
154155 logger.info( message );
156 }
157returntrue;
158 }
159160 } catch ( IOException e ) {
161 logger.error("Exception", e );
162return false;
163 }
164165//default should be to not expire. This is set if the URL is null166return false;
167168 }
169170 }