001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.legacy.persistence;
024:
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: /**
030: *
031: * @author Pavel Vlasov
032: * @version $Revision: 1.1 $
033: */
034: public class CompositeStorage implements Storage {
035: private List storages = new ArrayList();
036:
037: private class StorageEntry {
038: String key;
039: Storage storage;
040:
041: public boolean equals(Object obj) {
042: return obj instanceof StorageEntry
043: && ((StorageEntry) obj).key.equals(key);
044: }
045: }
046:
047: public void addStorage(String key, Storage storage) {
048: if (key == null) {
049: throw new NullPointerException("key==null");
050: }
051:
052: if (storage == null) {
053: throw new NullPointerException("storage==null");
054: }
055:
056: StorageEntry se = new StorageEntry();
057: se.key = key;
058: se.storage = storage;
059: storages.remove(se);
060: storages.add(se);
061: }
062:
063: public String put(Object o) throws PersistenceException {
064: Iterator it = storages.iterator();
065: while (it.hasNext()) {
066: StorageEntry se = (StorageEntry) it.next();
067: String key = se.storage.put(o);
068: if (key != null) {
069: return se.key + ":" + key;
070: }
071: }
072: return null;
073: }
074:
075: public Object get(String key) throws PersistenceException {
076: int idx = key.indexOf(':');
077: if (idx == -1) {
078: throw new PersistenceException("Invalid key format");
079: } else {
080: String skey = key.substring(0, idx);
081: Iterator it = storages.iterator();
082: while (it.hasNext()) {
083: StorageEntry se = (StorageEntry) it.next();
084: if (skey.equals(se.key)) {
085: return se.storage.get(key.substring(idx + 1));
086: }
087: }
088: }
089: return null;
090: }
091:
092: public void remove(String key) throws PersistenceException {
093: int idx = key.indexOf(':');
094: if (idx == -1) {
095: throw new PersistenceException("Invalid key format");
096: } else {
097: String skey = key.substring(0, idx);
098: Iterator it = storages.iterator();
099: while (it.hasNext()) {
100: StorageEntry se = (StorageEntry) it.next();
101: if (skey.equals(se.key)) {
102: se.storage.remove(key.substring(idx + 1));
103: return;
104: }
105: }
106: }
107: }
108:
109: public Storage getStorage(Class storageClass) {
110: Iterator it = storages.iterator();
111: while (it.hasNext()) {
112: StorageEntry se = (StorageEntry) it.next();
113: if (storageClass.isInstance(se.storage)) {
114: return se.storage;
115: }
116: }
117: return null;
118: }
119:
120: public Storage getStorage(String storageKey) {
121: Iterator it = storages.iterator();
122: while (it.hasNext()) {
123: StorageEntry se = (StorageEntry) it.next();
124: if (storageKey.equals(se.key)) {
125: return se.storage;
126: }
127: }
128: return null;
129: }
130: }
|