1 18 package org.enhydra.util.jivan; 19 20 import java.io.File ; 21 import java.net.URL ; 22 import java.util.HashMap ; 23 24 import org.jivan.apache.xerces.xni.parser.XMLInputSource; 25 import org.jivan.html.document.DocumentFactory; 26 import org.jivan.html.document.DocumentManager; 27 28 import com.lutris.logging.LogChannel; 29 import com.lutris.logging.Logger; 30 31 46 public class JivanFactory extends DocumentFactory { 47 48 51 private boolean isReload = false; 52 53 56 private LogChannel logChan; 57 58 62 private HashMap resourceTimestamps = null; 63 64 71 public JivanFactory(boolean reload, LogChannel logChan) { 72 super(); 73 this.isReload = reload; 74 this.logChan = logChan; 75 if(reload) 76 this.resourceTimestamps = new HashMap (); 77 78 logChan.write(Logger.DEBUG,"JivanFactory is initialised with AutoReload status: " + reload); 79 } 80 81 85 public LogChannel getLogger() { 86 return this.logChan; 87 } 88 89 90 111 public DocumentManager docManFor(String resourceId, XMLInputSource inp) { 112 this.logChan.write(Logger.DEBUG, "-------------------------------------------------------"); 113 this.logChan.write(Logger.DEBUG, "resourceId: " + resourceId); 114 this.logChan.write(Logger.DEBUG, "XMLInputSource - BaseSystemId: " + inp.getBaseSystemId()); 115 this.logChan.write(Logger.DEBUG, "XMLInputSource - Encoding: " + inp.getEncoding()); 116 this.logChan.write(Logger.DEBUG, "XMLInputSource - PublicId: " + inp.getPublicId()); 117 this.logChan.write(Logger.DEBUG, "XMLInputSource - SystemId: " + inp.getSystemId()); 118 resourceId = resourceId.intern(); 119 synchronized (resourceId) { 120 if (isReload) { 121 if (reloadChecking(resourceId, inp)) { 124 super.mapMasterDoc.remove(resourceId); 125 super.mapSysemtId2Thread.remove(resourceId); 126 } 127 128 } 129 } 130 return super.docManFor(resourceId, inp); 131 } 132 133 134 142 private boolean reloadChecking(String resourceId, XMLInputSource inp) { 143 144 if(inp.getCharacterStream() != null) 145 return true; 146 else if(inp.getByteStream() != null) 147 return true; 148 149 String systemId = inp.getSystemId(); 150 if (systemId != null) { 151 try { 152 URL url = new URL (systemId); 154 File fResource = new File (url.getFile()); 155 if (fResource.exists()) 156 return timestampCheck(resourceId, fResource); 157 else { 158 fResource = new File (systemId); 160 if (fResource.exists()) 161 return timestampCheck(resourceId, fResource); 162 else 163 return true; 164 } 165 } 166 catch (Exception ex) { 167 return true; 168 } 169 } 170 else 171 return true; 172 173 } 174 175 183 private boolean timestampCheck(String resourceId, File f) { 184 Long oldTime = (Long )this.resourceTimestamps.get(resourceId); 185 if (oldTime == null) { 186 resourceTimestamps.put(resourceId, new Long (f.lastModified())); 187 return true; 188 } 189 else if (oldTime.longValue() < f.lastModified()) { 190 resourceTimestamps.put(resourceId, new Long (f.lastModified())); 191 return true; 192 } 193 else 194 return false; 195 196 } 197 198 } | Popular Tags |