KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > storage > MemorySnipStorage


1 /*
2 * This file is part of "SnipSnap Wiki/Weblog".
3 *
4 * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5 * All Rights Reserved.
6 *
7 * Please visit http://snipsnap.org/ for updates and contact.
8 *
9 * --LICENSE NOTICE--
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * --LICENSE NOTICE--
24 */

25
26 package org.snipsnap.snip.storage;
27
28 import org.snipsnap.snip.Snip;
29 import org.snipsnap.util.PartialSearcher;
30 import org.snipsnap.util.ApplicationAwareMap;
31 import org.snipsnap.app.Application;
32 import org.snipsnap.app.ApplicationManager;
33 import org.snipsnap.app.ApplicationStorage;
34
35 import java.sql.Timestamp JavaDoc;
36 import java.util.*;
37
38 /**
39  * Wrapper with finders for in-memory searching. Can
40  * be used with JDBC...Storage when all Snips are
41  * kept in memory.
42  *
43  //@TODO replace with dynamic proxy.
44  * @author Stephan J. Schmidt
45  * @version $Id: MemorySnipStorage.java 1606 2004-05-17 10:56:18Z leo $
46  */

47
48 public class MemorySnipStorage implements SnipStorage {
49   public static final String JavaDoc NOT_SUPPORTED_EXCEPTION_MSG =
50       "Method not supported, do not call MemorySnipStorage directly";
51
52   private SnipStorage storage;
53
54   private ApplicationAwareMap cache;
55
56   // @TODO Keep list sorted with comparator
57
// This is used to keep an list of all Snips
58
// for faster storageAll() operations
59
private Map allList;
60
61   public MemorySnipStorage(SnipStorage storage, ApplicationManager manager) {
62     this.storage = storage;
63
64 // if (!(storage instanceof CacheableStorage)) {
65
// //@TODO optimize with array
66
// Iterator iterator = allList.iterator();
67
// while (iterator.hasNext()) {
68
// Snip snip = (Snip) iterator.next();
69
// map.put(snip.getName().toUpperCase(), snip);
70
// }
71
// }
72

73     cache = new ApplicationAwareMap(HashMap.class, PartialSearcher.class);
74
75     if (storage instanceof CacheableStorage) {
76       ((CacheableStorage) storage).setCache(cache);
77     }
78
79     // hash of list of snips
80
// applicationOid -> [snip, snip, ...]
81
allList = new HashMap();
82
83     //This should also fill the cache
84
Iterator iterator = manager.getApplications().iterator();
85     while (iterator.hasNext()) {
86       String JavaDoc oid = ((Properties)iterator.next()).getProperty(ApplicationStorage.OID);
87       List instanceList = storage.storageAll(oid);
88       allList.put(oid, instanceList);
89     }
90   }
91
92   // Basic manipulation methods Load,Store,Create,Remove
93
public Snip[] match(String JavaDoc pattern) {
94     return ((PartialSearcher) cache.getMap()).match(pattern.toUpperCase());
95   }
96
97   public Snip[] match(String JavaDoc start, String JavaDoc end) {
98     return ((PartialSearcher) cache.getMap()).match(start.toUpperCase(), end.toUpperCase());
99   }
100
101   public Snip storageLoad(String JavaDoc name) {
102     return (Snip) cache.getMap().get(name.toUpperCase());
103   }
104
105   public Object JavaDoc loadObject(String JavaDoc name) {
106     return (Snip) cache.getMap().get(name.toUpperCase());
107   }
108
109   public void storageStore(Snip snip) {
110     storage.storageStore(snip);
111   }
112
113   public Snip storageCreate(String JavaDoc name, String JavaDoc content) {
114     Snip snip = storage.storageCreate(name, content);
115
116     // TODO fix this, the allList is not necessarily correctly initialized!
117
String JavaDoc applicationOid = snip.getApplication();
118
119     List allSnips = (List) allList.get(applicationOid);
120     if(null == allSnips) {
121       allSnips = storage.storageAll(applicationOid);
122       allList.put(applicationOid, allSnips);
123     }
124     allSnips.add(snip);
125     cache.getMap(applicationOid).put(snip.getName().toUpperCase(), snip);
126     return snip;
127   }
128
129   public void storageRemove(Snip snip) {
130     String JavaDoc applicationOid = snip.getApplication();
131     storage.storageRemove(snip);
132     List allSnips = (List) allList.get(applicationOid);
133     if (null != allSnips) {
134       allSnips.remove(snip);
135     } else {
136       System.err.println("WARNING: access to unknown oid: "+snip.getApplication());
137     }
138     cache.getMap(applicationOid).remove(snip.getName().toUpperCase());
139   }
140
141   public int storageCount() {
142     String JavaDoc application = (String JavaDoc) Application.get().getObject(Application.OID);
143     List allSnips = (List) allList.get(application);
144     return allSnips != null ? allSnips.size() : 0;
145   }
146
147   public List storageAll(String JavaDoc applicationOid) {
148     List all = (List) allList.get(applicationOid);
149     return all;
150   }
151
152   public List storageAll() {
153     String JavaDoc applicationOid = (String JavaDoc) Application.get().getObject(Application.OID);
154     return storageAll(applicationOid);
155   }
156
157   // Finder methods
158
// Those should not be called
159
// MemorySnipStorage should be wrapped with
160
// a Query Storage (e.g. QuerySnipStorage or
161
// a future XPathSnipStorage)
162
public class MethodNotSupportedException extends RuntimeException JavaDoc {
163     public MethodNotSupportedException(String JavaDoc s) {
164       super(s);
165     }
166   };
167
168   public List storageByHotness(int size) {
169     throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG);
170   }
171
172   public List storageByUser(String JavaDoc login) {
173     throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG);
174   }
175
176   public List storageByDateSince(Timestamp JavaDoc date) {
177     throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG);
178   }
179
180   public List storageByRecent(String JavaDoc applicationOid, int size) {
181     throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG);
182   }
183
184   public List storageByComments(Snip parent) {
185     throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG);
186   }
187
188   public List storageByParent(Snip parent) {
189     throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG);
190   }
191
192   public List storageByParentNameOrder(Snip parent, int count) {
193     throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG);
194   }
195
196   public List storageByParentModifiedOrder(Snip parent, int count) {
197     throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG);
198   }
199
200   public List storageByDateInName(String JavaDoc nameSpace, String JavaDoc start, String JavaDoc end) {
201     throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG);
202   }
203 }
204
Popular Tags