KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > metadata > PropertyCache


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.vfs.metadata;
20
21 import java.util.*;
22
23 import org.openharmonise.commons.xml.namespace.*;
24 import org.openharmonise.vfs.*;
25 import org.openharmonise.vfs.context.*;
26 import org.openharmonise.vfs.metadata.range.*;
27 import org.openharmonise.vfs.metadata.value.*;
28 import org.openharmonise.vfs.servers.ServerList;
29
30
31 /**
32  * Properties are cached so that they are not constantly fetched from
33  * the Harmonise server. When a property is requested from the cache and
34  * there is a version identifier is specified it is compared with the
35  * version identifier of the property in the cache. This way updates to
36  * properties will be fetched into the cache even if there is a previous
37  * version held. Properties can be requested either by the path to their
38  * virtual file or by their fully qualified (i.e. namespace and name) name.
39  *
40  * @author Matthew Large
41  * @version $Revision: 1.2 $
42  *
43  */

44 public class PropertyCache implements ContextListener {
45
46     /**
47      * Instance of the property cache, following the Singleton pattern.
48      */

49     private static PropertyCache m_instance = null;
50
51     /**
52      * Property lookup.
53      */

54     private ResourceLookup m_properties = new ResourceLookup(11);
55     
56     /**
57      * Property group lookup.
58      */

59     private ResourceLookup m_propertyGroups = new ResourceLookup(11);
60
61     /**
62      *
63      */

64     private PropertyCache() {
65         super();
66         ContextHandler.getInstance().addListener(ContextType.CONTEXT_METADATA_DEFINITION_CHANGED, this);
67         this.setup();
68     }
69     
70     private void setup() {
71         Property stringProp = new Property();
72         stringProp.setNamespace(NamespaceType.OHRM.getURI());
73         stringProp.setHREF("/default/harmonise-path");
74         stringProp.setDisplayName("Harmonise Path");
75         stringProp.setName("harmonise-path");
76         stringProp.setSummary("Harmonise path for this resource, used as a unique reference to this resource by site developers.");
77         StringRange longStringRange = new StringRange();
78         longStringRange.setMaxLength(500);
79         stringProp.setRange(longStringRange);
80         this.addProperty(NamespaceType.OHRM.getURI(), "harmonise-path", stringProp);
81         
82         stringProp = new Property();
83         stringProp.setNamespace(NamespaceType.OHRM.getURI());
84         stringProp.setHREF("/default/harmonise-id");
85         stringProp.setDisplayName("Harmonise ID");
86         stringProp.setName("harmonise-id");
87         stringProp.setSummary("Harmonise ID for this resource, used as a unique reference to this resource by site developers.");
88         longStringRange = new StringRange();
89         longStringRange.setMaxLength(500);
90         stringProp.setRange(longStringRange);
91         this.addProperty(NamespaceType.OHRM.getURI(), "harmonise-id", stringProp);
92     }
93     
94     /**
95      * Returns the instance of the property cache. Follows the Singleton
96      * pattern.
97      *
98      * @return The property cache
99      */

100     public static PropertyCache getInstance() {
101         if(m_instance==null) {
102             m_instance = new PropertyCache();
103         }
104         return m_instance;
105     }
106
107     /**
108      * Loads stored cache information from an XML file.
109      *
110      * @param sFilePath Path to XML file
111      */

112     public void load(String JavaDoc sFilePath) {
113         Property domainProp = new Property();
114         domainProp.setName("domain");
115         domainProp.setNamespace(NamespaceType.DAV.getURI());
116         domainProp.setRange(new DomainRange());
117         domainProp.setHREF("/Domain");
118         String JavaDoc sQName = domainProp.getNamespace() + "#" + domainProp.getName();
119         this.m_properties.put(sQName, domainProp.getHREF(), domainProp);
120         
121         
122         Property rangeProp = new Property();
123         rangeProp.setName("range");
124         rangeProp.setNamespace(NamespaceType.DAV.getURI());
125         rangeProp.setRange(new RangeRange());
126         rangeProp.setHREF("/Range");
127         sQName = rangeProp.getNamespace() + "#" + rangeProp.getName();
128         this.m_properties.put(sQName, rangeProp.getHREF(), rangeProp);
129         
130     }
131     
132     public void addProperty(String JavaDoc sNamespace, String JavaDoc sName, Property prop) {
133         this.m_properties.put(sNamespace+"#"+sName, null, prop);
134     }
135     
136     /**
137      * Saves cache information to an XML file.
138      *
139      * @param sFilePath Full path to XML file
140      */

141     public void save(String JavaDoc sFilePath) {
142         
143     }
144     
145     /**
146      * Returns a property.
147      *
148      * @param sHREF Full path to virtual file for property
149      * @param sVersion Version identifier of property
150      * @return Property or null if not found
151      */

152     public Property getPropertyByPath(String JavaDoc sHREF, String JavaDoc sVersion) {
153         this.checkDefinition(sHREF, sVersion);
154         return (Property)this.m_properties.getByHREF(sHREF);
155     }
156     
157     /**
158      * Returns a property.
159      *
160      * @param sHREF Full path to virtual file for property.
161      * @return Property or null if not found
162      */

163     public Property getPropertyByPath(String JavaDoc sHREF) {
164         this.checkDefinition(sHREF);
165         return (Property)this.m_properties.getByHREF(sHREF);
166     }
167     
168     /**
169      * Returns a property.
170      *
171      * @param sNamespace Namespace of property
172      * @param sName Name of property
173      * @param sVersion Version identifier of property
174      * @return Property or null if not found
175      */

176     public Property getPropertyByName(String JavaDoc sNamespace, String JavaDoc sName, String JavaDoc sVersion) {
177         String JavaDoc sQName = sNamespace.trim() + "#" + sName.trim();
178         String JavaDoc sHref = this.m_properties.getHref(sQName);
179         if(sHref!=null) {
180             this.checkDefinition(sHref, sVersion);
181         }
182         Property prop = (Property)this.m_properties.getByQName(sQName);
183         if(prop==null) {
184             prop = new Property(sNamespace, sName, "/default");
185         }
186         return prop;
187     }
188     
189     /**
190      * Returns a property.
191      *
192      * @param sNamespace Namespace of property
193      * @param sName Name of property
194      * @return Property or null if not found
195      */

196     public Property getPropertyByName(String JavaDoc sNamespace, String JavaDoc sName) {
197         String JavaDoc sQName = sNamespace.trim() + "#" + sName.trim();
198         String JavaDoc sHref = this.m_properties.getHref(sQName);
199         if(sHref!=null) {
200             this.checkDefinition(sHref);
201         }
202         Property prop = (Property)this.m_properties.getByQName(sQName);
203         if(prop==null) {
204             prop = new Property(sNamespace, sName, "/default");
205         }
206         return prop;
207     }
208     
209     /**
210      * Checks if the currently cached property is valid. If it is not
211      * valid a new version is fetched.
212      *
213      * @param sHREF Full path to virtual file for property
214      */

215     private void checkDefinition(String JavaDoc sHREF) {
216         this.checkDefinition(sHREF, null);
217     }
218     
219     /**
220      * Checks if the currently cached property is valid. If it is not
221      * valid a new version is fetched.
222      *
223      * @param sHREF Full path to virtual file for property
224      * @param sVersion Version identifier of property
225      */

226     private void checkDefinition(String JavaDoc sHREF, String JavaDoc sVersion) {
227         if(sHREF.equals("/webdav/Metadata/Properties/MattPropCol/mattRemoveCompoundProp")) {
228             System.out.println("Checking prop");
229         }
230         Property prop = (Property)this.m_properties.getByHREF(sHREF);
231         if(prop!=null && sVersion!=null && prop.getVersion()!=null) {
232             if(!prop.getVersion().equals(sVersion)) {
233                 prop = this.getPropertyFromVFS(sHREF);
234                 if(prop!=null) {
235                     this.m_properties.put(prop.getNamespace()+"#"+prop.getName(), sHREF, prop);
236                 }
237             }
238         } else if(prop==null) {
239             prop = this.getPropertyFromVFS(sHREF);
240             if(prop!=null) {
241                 this.m_properties.put(prop.getNamespace()+"#"+prop.getName(), sHREF, prop);
242             }
243         } else {
244             // TODO Don't know what to do here.....???
245
}
246     }
247     
248     /**
249      * Returns a property from the Harmonise server virtual file system.
250      *
251      * @param sHREF Full path to the virtual file for property
252      * @return Property or null if not found
253      */

254     private Property getPropertyFromVFS(String JavaDoc sHREF) {
255         Property prop = null;
256         
257         AbstractVersioningVFS vfs = (AbstractVersioningVFS) ServerList.getInstance().getHarmoniseServer().getVFS();
258         
259         VersionedVirtualFile vfFile = (VersionedVirtualFile)vfs.getPropertyVirtualFile(sHREF);
260         
261         if(vfFile!=null) {
262             prop = new Property(NamespaceType.OHRM.getURI(), vfFile.getFileName(), vfFile.getFullPath());
263             prop.setDisplayName( vfFile.getVFS().getVirtualFileSystemView().getDisplayName(vfFile) );
264             prop.setSummary( vfFile.getVFS().getVirtualFileSystemView().getSummary(vfFile) );
265             
266             PropertyInstance range = vfFile.getProperty(NamespaceType.DAV.getURI(), "range");
267             PropertyInstance domain = vfFile.getProperty(NamespaceType.DAV.getURI(), "domain");
268             
269             if(range!=null) {
270                 List rangeValues = range.getValues();
271                 if(rangeValues.size()>0) {
272                     RangeValue rangeValue = (RangeValue) rangeValues.get(0);
273                     prop.setRange(rangeValue.getRange());
274                 }
275             }
276             
277             if(domain!=null) {
278                 List domainValues = domain.getValues();
279                 Iterator itor = domainValues.iterator();
280                 while (itor.hasNext()) {
281                     DomainValue domainValue = (DomainValue) itor.next();
282                     Domain newDomain = new Domain();
283                 
284                     newDomain.addPath( domainValue.getPath() );
285                     newDomain.setResourceType( domainValue.getResourceType() );
286                     newDomain.setMaxOccurs( domainValue.getMaxOccurs() );
287                     newDomain.setMinOccurs( domainValue.getMinOccurs() );
288                     Iterator itor2 = domainValue.getContentTypes().iterator();
289                     while (itor2.hasNext()) {
290                         String JavaDoc sContentType = (String JavaDoc) itor2.next();
291                         newDomain.addContentType(sContentType);
292                     }
293                 
294                     prop.addDomain(newDomain);
295                 }
296             }
297         }
298         
299         return prop;
300     }
301     
302     /**
303      * Returns a property group.
304      *
305      * @param sHREF Full path to virtual file for property group
306      * @return Property group or null if not found
307      */

308     public PropertyGroup getPropertyGroup(String JavaDoc sHREF) {
309         AbstractVirtualFileSystem vfs = (AbstractVirtualFileSystem) ServerList.getInstance().getHarmoniseServer().getVFS();
310         
311         VersionedVirtualFile vfDir = (VersionedVirtualFile) vfs.getVirtualFile(sHREF).getResource();
312         PropertyGroup group = null;
313         if(vfDir!=null) {
314             List vfChildren = vfDir.getChildren();
315             group = new PropertyGroup();
316             group.setChildrenHREFs(vfChildren);
317         }
318          
319         return group;
320     }
321     
322     public String JavaDoc toString() {
323         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
324         
325         Iterator itor = this.m_properties.getValues().iterator();
326         while(itor.hasNext()) {
327             sBuff.append( ((Property)itor.next()).toString() ).append("\n");
328         }
329         
330         return sBuff.toString();
331     }
332
333     /* (non-Javadoc)
334      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
335      */

336     public void contextMessage(ContextEvent ce) {
337         if(ce.getPath()!=null) {
338             this.m_properties.removeByHREF(ce.getPath());
339         }
340     }
341
342 }
343
Popular Tags