KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > source > ModelSourceFile


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.server.source;
7
8 import java.util.* ;
9 //import org.apache.commons.logging.* ;
10
import com.hp.hpl.jena.rdf.model.* ;
11 import org.joseki.server.SourceController ;
12 import org.joseki.util.cache.*;
13
14
15 /** An attached model that manages a file holding the model.
16  * Files are read in and managed in a cache. Each file source
17  * has a system resource manager, the SourceManager
18  *
19  * @author Andy Seaborne
20  * @version $Id: ModelSourceFile.java,v 1.7 2004/04/30 14:13:14 andy_seaborne Exp $
21  */

22
23 public class ModelSourceFile extends ModelSourceCom
24 {
25     static FileModelFactory factory = new FileModelFactory() ;
26     
27     // TODO Make the cache size configurable.
28
static Cache cache = new Cache(25, factory, new CachePolicyLRU()) ;
29
30     // For all items: map serverURI (external relative URI) to the source
31
// controller so the static FileModelFactory can do its thing.
32

33     // [Alternative: use an indirect cache : cache keeps record of which only N
34
// are in use at any one time.]
35

36     static Map controllers = new HashMap() ;
37     
38     
39     public ModelSourceFile(SourceController controller, String JavaDoc serverURI)
40     {
41         super(controller, serverURI) ;
42         controllers.put(serverURI, controller) ;
43     }
44     
45     public Model getModel()
46     {
47         return (Model)cache.get(getServerURI()) ;
48     }
49
50     static class FileModelFactory implements CacheItemFactory
51     {
52         //static Log logger = LogFactory.getLog(FileModelFactory.class) ;
53

54         // CacheItemFactory interface
55

56         /* (non-Javadoc)
57          * @see org.joseki.util.cache.CacheItemFactory#create(java.lang.Object)
58          */

59         public Object JavaDoc create(Object JavaDoc key)
60         {
61             // Find the controller for this item by key
62
return getSourceController(key).buildSource() ;
63         }
64     
65         /* (non-Javadoc)
66          * @see org.joseki.util.cache.CacheItemFactory#destroy(java.lang.Object, java.lang.Object)
67          */

68         public void destroy(Object JavaDoc key, Object JavaDoc obj)
69         {
70             getSourceController(key).releaseSource() ;
71         }
72         
73         SourceController getSourceController(Object JavaDoc key)
74         {
75             return (SourceController)controllers.get(key) ;
76         }
77     }
78 }
79
80 /*
81  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
82  * All rights reserved.
83  *
84  * Redistribution and use in source and binary forms, with or without
85  * modification, are permitted provided that the following conditions
86  * are met:
87  * 1. Redistributions of source code must retain the above copyright
88  * notice, this list of conditions and the following disclaimer.
89  * 2. Redistributions in binary form must reproduce the above copyright
90  * notice, this list of conditions and the following disclaimer in the
91  * documentation and/or other materials provided with the distribution.
92  * 3. The name of the author may not be used to endorse or promote products
93  * derived from this software without specific prior written permission.
94  *
95  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
96  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
97  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
98  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
99  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
101  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
102  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
103  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
104  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
105  */

106
Popular Tags