1 16 17 package org.apache.taglibs.gnat; 18 19 import javax.servlet.ServletContext ; 20 import javax.servlet.jsp.*; 21 import javax.servlet.jsp.tagext.*; 22 import java.lang.reflect.Method ; 23 import java.io.*; 24 import java.text.*; 25 import java.util.*; 26 27 37 38 public class touchTag extends TagSupport 39 { 40 private File _file; 41 private long millis=0; 42 private String file=""; 43 private String datetime = null; 44 private ServletContext ctx; 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 setLastModified = null; 49 private static Object lockReflection = new Object (); 50 51 54 public void setFile(String file) { this.file = file; } 55 56 59 public void setMillis(long millis) { this.millis = millis; } 60 61 64 public void setDatetime(String 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 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 .class.getMethod( "setLastModified", 131 new Class [] { Long.TYPE } ); 132 } 133 catch(NoSuchMethodException 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 [] times = new Long [1]; 144 if (millis < 0) 145 { 146 times[0] = new Long ( System.currentTimeMillis() ); 147 } 148 else 149 { 150 times[0] = new Long (millis); 151 } 152 try 153 { 154 ctx.log( gnatRB.getString("touch.log.mod.time") + file ); 155 setLastModified.invoke( _file, times ); 156 } 157 catch (Throwable other) 158 { 159 throw new JspTagException( gnatRB.getString("touch.tag") + 160 gnatERB.getString("touch.throwable") + 161 file ); 162 } 163 } 164 165 public String getFile() { return file; } 166 167 public String getDatetime() { return this.datetime; } 168 169 public long getMillis() { return this.millis; } 170 } 171 | Popular Tags |