KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > util > jivan > JivanFactory


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Developer of the Enhydra Application Server is Together Austria.
15  * All Rights Reserved.
16  *
17  */

18 package org.enhydra.util.jivan;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.HashMap JavaDoc;
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 /**
32  * <p>Title: JivanFactory</p>
33  * <p>Description: JivanFactory extends the capabilities of DocumentFactory
34  * class which is originaly supplied by Jivan project. The enhacements are the
35  * possibilites of logging and autoreloading. To invoke autoreloading, in the
36  * application configuration file, the parameter 'Server.Jivan.AutoReload'
37  * should be set to 'true'. Autoreloading parameter force Jivan DocumentFactory
38  * object to read neededy resource again from its source. Note that
39  * autoreloading slows down the performance, and it should be used in testing
40  * purpose. The default value for autoreloading is false.</p>
41  * <p>Copyright: Copyright (c) 2004</p>
42  * <p>Company: Together</p>
43  * @author Vladimir Radisic
44  * @version 1.0
45  */

46 public class JivanFactory extends DocumentFactory {
47
48   /**
49    * Indicates autoreloading status (true = on, false = off)
50    */

51   private boolean isReload = false;
52
53   /**
54    * Logging object
55    */

56   private LogChannel logChan;
57
58   /**
59    * Storage for resource timestamps. The timestamps is used only in autoreload
60    * mode of Jivanfactory work.
61    */

62   private HashMap JavaDoc resourceTimestamps = null;
63
64   /**
65    * Constructs JivanFactory with autoreloading indicator, and given Log4j
66    * logging object. This constructor initialise DocumentManager singleton
67    * object if it is not initialised yet.
68    * @param reload autoreloading status indicator (true = on, false = off).
69    * @param logChan logging object
70    */

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 JavaDoc();
77
78     logChan.write(Logger.DEBUG,"JivanFactory is initialised with AutoReload status: " + reload);
79   }
80
81   /**
82    * Returns logging object which can be used in applications.
83    * @return logging object
84    */

85     public LogChannel getLogger() {
86         return this.logChan;
87     }
88
89
90   /**
91    * Overrides DocumentFactory method in order to implement autoreloading
92    * possibility. It retrieves a DocumentManager for the specific 'resourceId'.
93    * Also, if DocumentManager for the corresponding 'resourceId' does not
94    * exists, it reads given XMLInputSource object and creates DocumentManager.<BR>
95    * <BR>
96    * Other docManFor() methods also call this method. docManFor() with single
97    * argument of String type, calls this method with self created XMLInputSource
98    * object by using its 'systemId' argument, which represents the location of
99    * the resource.<BR>
100    * <BR>
101    * For example the systemId can look like:<BR>
102    * <BR>
103    * file:///C:/projects/testjivan/Welcome.html<BR>
104    * <BR>
105    * @param resourceId unique resource identifier which is used as key for
106    * retrieving corresponding DocumentManager.
107    * @param inp XMLInputSource object which is taken as resource in case when
108    * there is no corresponding DocumentManager for given 'resourceId'.
109    * @return DocumentManager which corresponds to given 'resourceId'.
110    */

111   public DocumentManager docManFor(String JavaDoc 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 // Object will be removed from HashMaps only if its timestamp is larger than
122
// timestamp for particular resource
123
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   /**
135    * Checks is the resource autoreloading necessery or not. Checking is
136    * performed according to stored resource timestamp.
137    * @param resourceId resourceId unique resource identification
138    * @param inp XMLInputSource object which corresponds to given 'resourceId'.
139    * @return true if stored timestamp is older 'then' resource timestamp, or
140    * 'false' otherwise.
141    */

142   private boolean reloadChecking(String JavaDoc resourceId, XMLInputSource inp) {
143
144     if(inp.getCharacterStream() != null)
145       return true;
146     else if(inp.getByteStream() != null)
147       return true;
148
149     String JavaDoc systemId = inp.getSystemId();
150     if (systemId != null) {
151       try {
152         // try with URL if systemId is something like: file:///C:/projects/Welcome.html
153
URL JavaDoc url = new URL JavaDoc(systemId);
154         File JavaDoc fResource = new File JavaDoc(url.getFile());
155         if (fResource.exists())
156           return timestampCheck(resourceId, fResource);
157         else {
158           // try withFile if systemId is something like: C:/projects/Welcome.html
159
fResource = new File JavaDoc(systemId);
160           if (fResource.exists())
161             return timestampCheck(resourceId, fResource);
162           else
163             return true;
164         }
165       }
166       catch (Exception JavaDoc ex) {
167         return true;
168       }
169     }
170     else
171       return true;
172
173   }
174
175   /**
176    * Check the timestamp for given File resource. If corresponding timestamp
177    * does not exist it will be added to timestamp storage for later use.
178    * @param resourceId unique resource identification
179    * @param f file resource
180    * @return true if stored timestamp is older 'then' resource timestamp, or
181    * 'false' otherwise.
182    */

183   private boolean timestampCheck(String JavaDoc resourceId, File JavaDoc f) {
184     Long JavaDoc oldTime = (Long JavaDoc)this.resourceTimestamps.get(resourceId);
185     if (oldTime == null) {
186       resourceTimestamps.put(resourceId, new Long JavaDoc(f.lastModified()));
187       return true;
188     }
189     else if (oldTime.longValue() < f.lastModified()) {
190       resourceTimestamps.put(resourceId, new Long JavaDoc(f.lastModified()));
191       return true;
192     }
193     else
194       return false;
195
196   }
197
198 }
Popular Tags