KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > gnat > touchTag


1 /*
2  * Copyright 1999,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.taglibs.gnat;
18
19 import javax.servlet.ServletContext JavaDoc;
20 import javax.servlet.jsp.*;
21 import javax.servlet.jsp.tagext.*;
22 import java.lang.reflect.Method JavaDoc;
23 import java.io.*;
24 import java.text.*;
25 import java.util.*;
26
27 /**
28  * Touch a file - corresponds to the Unix touch command.
29  *
30  * <p>If the file to touch doesn't exist, an empty one is
31  * created. Setting the modification time of files is not supported in
32  * JDK 1.1.
33  *
34  * @author <a HREF="mailto:stefan.bodewig@megabit.net">Stefan Bodewig</a>
35  * @author <a HREF="mailto:ssstirling@mediaone.net">Scott Stirling</a>
36  */

37
38 public class touchTag extends TagSupport
39 {
40     private File _file;
41     private long millis=0;
42     private String JavaDoc file="";
43     private String JavaDoc datetime = null;
44     private ServletContext JavaDoc ctx; // for logging
45
private ResourceBundle gnatRB = ListResourceBundle.getBundle("org.apache.taglibs.gnat.util.GnatTagStrings");
46     private ResourceBundle gnatERB = ListResourceBundle.getBundle("org.apache.taglibs.gnat.util.GnatExceptionStrings");
47         
48     private static Method JavaDoc setLastModified = null;
49     private static Object JavaDoc lockReflection = new Object JavaDoc();
50     
51     /**
52      * The name of the file to touch.
53      */

54     public void setFile(String JavaDoc file) { this.file = file; }
55
56     /**
57      * Milliseconds since 01/01/1970 00:00 am.
58      */

59     public void setMillis(long millis) { this.millis = millis; }
60
61     /**
62      * Date in the format MM/DD/YYYY HH:MM AM_PM.
63      */

64     public void setDatetime(String JavaDoc datetime) { this.datetime = datetime; }
65     
66     
67     public int doStartTag() throws JspException
68     {
69         ctx = pageContext.getServletContext();
70         _file = new File(file);
71         
72         return SKIP_BODY;
73     }
74     
75     public int doEndTag() throws JspException
76     {
77         if (datetime != null)
78         {
79             DateFormat df = DateFormat.getDateTimeInstance( DateFormat.SHORT,
80                                                             DateFormat.SHORT,
81                                                             Locale.getDefault() );
82             try
83             {
84                 setMillis( df.parse(datetime).getTime() );
85             }
86             catch (ParseException pe)
87             {
88                 throw new JspTagException( gnatRB.getString("touch.tag") + pe.getMessage() );
89             }
90             touch();
91             return EVAL_PAGE;
92         }
93         else
94         {
95             touch();
96             return EVAL_PAGE;
97         }
98     }
99
100     /**
101      * Does the actual work.
102      */

103     private void touch() throws JspTagException
104     {
105         if ( !_file.exists() ) {
106             try
107             {
108                 FileOutputStream fos = new FileOutputStream(_file);
109                 fos.write(new byte[0]);
110                 fos.close();
111             }
112             catch (IOException ioe)
113             {
114                 throw new JspTagException( gnatRB.getString("touch.tag") + ": " +
115                                            gnatERB.getString("touch.no.create") +
116                                            ioe.getMessage() );
117             }
118             ctx.log( gnatRB.getString("touch.success") + file );
119         }
120
121         if (setLastModified == null)
122         {
123             synchronized (lockReflection)
124             {
125                 if (setLastModified == null)
126                 {
127                     try
128                     {
129                         setLastModified =
130                             java.io.File JavaDoc.class.getMethod( "setLastModified",
131                                                           new Class JavaDoc[] { Long.TYPE } );
132                     }
133                     catch(NoSuchMethodException JavaDoc nsme)
134                     {
135                         throw new JspTagException( gnatRB.getString("touch.tag") +
136                                                    gnatERB.getString("touch.nsme") +
137                                                    nsme.getMessage() );
138                     }
139                 }
140             }
141         }
142
143         Long JavaDoc[] times = new Long JavaDoc[1];
144         if (millis < 0)
145         {
146             times[0] = new Long JavaDoc( System.currentTimeMillis() );
147         }
148         else
149         {
150             times[0] = new Long JavaDoc(millis);
151         }
152         try
153         {
154             ctx.log( gnatRB.getString("touch.log.mod.time") + file );
155             setLastModified.invoke( _file, times );
156         }
157         catch (Throwable JavaDoc other)
158         {
159             throw new JspTagException( gnatRB.getString("touch.tag") +
160                                        gnatERB.getString("touch.throwable") +
161                                        file );
162         }
163     }
164    
165     public String JavaDoc getFile() { return file; }
166     
167     public String JavaDoc getDatetime() { return this.datetime; }
168     
169     public long getMillis() { return this.millis; }
170 }
171
Popular Tags