KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > portal > expire > FileWatcher


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 at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * 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 and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.portal.expire;
18
19 //jetspeed stuff
20
import org.apache.jetspeed.cache.disk.DiskCacheEntry;
21 import org.apache.jetspeed.cache.disk.DiskCacheUtils;
22 import org.apache.jetspeed.cache.disk.JetspeedDiskCache;
23 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24 import org.apache.jetspeed.services.logging.JetspeedLogger;
25
26 //java stuff
27
import java.io.IOException JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30
31 /**
32 A generic class for watching a file and determining if it has changed.
33
34 @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 */

37 public class FileWatcher implements Serializable JavaDoc
38 {
39
40     /**
41      * Static initialization of the logger for this class
42      */

43     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(FileWatcher.class.getName());
44     
45     //BEGIN Exception index
46

47     public static final String JavaDoc EXCEPTION_URL_NOT_NULL =
48         "URL can NOT be null here.";
49     
50     public static final String JavaDoc EXCEPTION_URL_NOT_IN_CACHE =
51         "The URL you specified within the disk cache does not exist: ";
52     
53     
54     //END Exception index
55

56     /**
57     The URL on which this FileWatchExpire is based.
58     */

59     private String JavaDoc url = "";
60
61     /**
62     The last time this files URL has been modified on disk.
63     */

64     private long lastModified = 0;
65
66     /**
67     The object that relies on this FileWatcher
68     */

69     private String JavaDoc parent = "";
70
71     /**
72     Create a FileWatcher with no parent info.
73     
74     @see #FileWatcher( String, String )
75     
76     @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     */

79     public FileWatcher( String JavaDoc url ) throws IOException JavaDoc {
80         this( url, null );
81     }
82                             
83     /**
84     Create a new FileWatcher to watch the given URL.
85     
86     @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     */

89     public FileWatcher( String JavaDoc url,
90                         String JavaDoc parent ) throws IOException JavaDoc {
91         
92         if ( url == null ) {
93             throw new IOException JavaDoc( EXCEPTION_URL_NOT_NULL );
94         }
95
96         if ( DiskCacheUtils.isRemote( url ) &&
97              DiskCacheUtils.isCached( url ) == false ) {
98             
99             throw new IOException JavaDoc( EXCEPTION_URL_NOT_IN_CACHE + url );
100         }
101
102         //Try to set last modified when creating FileWatcher objet
103
try {
104             this.lastModified = JetspeedDiskCache.getInstance().getEntry( url ).
105                                                             getLastModified();
106         } catch (Throwable JavaDoc e)
107         {
108             logger.error( "Unable to set last modified on url " + url, e );
109         }
110         
111         this.url = url;
112         this.parent = parent;
113     }
114     
115     /**
116     Return true if the URL on which this is based has changed.
117
118     @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     */

121     public boolean hasChanged() {
122
123         try {
124
125             //initially set the lastModified data
126
if ( this.lastModified == 0 ) {
127             
128                 DiskCacheEntry entry = JetspeedDiskCache.getInstance().getEntry( url );
129                 
130                 this.lastModified = entry.getLastModified();
131
132                 return false;
133             
134             }
135
136             //the recent modification... if there was one. otherwise it will
137
// == this.lastModified
138
long recent = JetspeedDiskCache.getInstance()
139                 .getEntry( url ).getLastModified();
140
141             // 0 means always modified
142
if ( recent == 0 ||
143                 this.lastModified < recent ) {
144                    
145                 if ( logger.isInfoEnabled() )
146                 {
147                     String JavaDoc message = "";
148
149                     if ( this.parent != null ) {
150                         message = this.parent + ": ";
151                     }
152
153                     message += "REFRESH: Expiring Portlet because it's URL has been modified on disk -> " + url;
154
155                     logger.info( message );
156                 }
157                 return true;
158             }
159             
160         } catch ( IOException JavaDoc e ) {
161             logger.error("Exception", e );
162             return false;
163         }
164         
165         //default should be to not expire. This is set if the URL is null
166
return false;
167         
168     }
169     
170 }
171
Popular Tags