2 * Copyright (C) 2001-2006 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * and distribute linked combinations including the two. You must obey the
23 * GNU General Public License in all respects for all of the code used other than
24 * the above mentioned libraries. If you modify this file, you may extend this
25 * exception to your version of the file, but you are not obligated to do so.
26 * If you do not wish to do so, delete this exception statement from your version.
28 package mir.storage.store;
31 * Title: Class StoreIdentifier
32 * Description: StoreIdentifier has two functions.
33 * A) StoreIdenfier holds a reference to a @see StorableObject
34 * or B) StoreIdentifier is used too search for a @see StorableObject
35 * in the @see StoreContainer that matches its
37 * Copyright: Copyright (c) 2002
42 import mir.entity.EntityList;
43 import mir.log.LoggerWrapper;
45 import java.util.Iterator;
48 public class StoreIdentifier {
49 /** todo check if invalidating already to avoid deadlocks what about concurrency? */
51 private static ObjectStore o_store = ObjectStore.getInstance();
53 private StoreContainerType stocType = null;
54 private StorableObject reference = null;
55 private String uniqueIdentifier = null; // id for Entity & sql for EntityList
56 private long timesUsed;
57 private boolean invalidating = false;
59 protected static LoggerWrapper logger = new LoggerWrapper("Database.ObjectStore");
61 public StoreIdentifier(StorableObject reference, int storeType,
62 String uniqueIdentifier) {
64 if (reference instanceof EntityList)
65 theClass = ( (EntityList) reference).getStorage().getEntityClass();
67 theClass = reference.getClass();
68 this.uniqueIdentifier = uniqueIdentifier;
69 this.stocType = StoreContainerType.valueOf(theClass, storeType);
70 this.reference = reference;
73 public StoreIdentifier(StorableObject reference, String uniqueIdentifier) {
74 this(reference, StoreContainerType.STOC_TYPE_ENTITY, uniqueIdentifier);
77 public StoreIdentifier(Class theClass, String uniqueIdentifier) {
78 this(theClass, StoreContainerType.STOC_TYPE_ENTITY, uniqueIdentifier);
81 public StoreIdentifier(Class theClass, int storeType, String uniqueIdentifier) {
82 this.uniqueIdentifier = uniqueIdentifier;
83 this.stocType = StoreContainerType.valueOf(theClass, storeType);
86 public void invalidate() {
87 logger.info("Invalidating: " + toString());
88 // avoid deadlock due to propagation.
91 if (stocType != null &&
92 stocType.getStocType() == StoreContainerType.STOC_TYPE_ENTITY) {
93 logger.info("Propagating invalidation to EntityList for " + toString());
94 // we should invalidate related ENTITY_LIST
95 StoreContainerType entityListStocType =
96 StoreContainerType.valueOf(stocType.getStocClass(),
97 StoreContainerType.STOC_TYPE_ENTITYLIST);
98 o_store.invalidate(entityListStocType);
101 // propagate invalidation to Set
102 Set set = reference.getNotifyOnReleaseSet();
104 for (Iterator it = set.iterator(); it.hasNext(); ) {
105 Object o = it.next();
106 if (o instanceof StoreIdentifier) {
107 logger.info("Propagating invalidation to StoreIdentifier: " + o.toString());
108 // propagate invalidation to a specific StoreIdentifier in cache
109 o_store.invalidate( (StoreIdentifier) o);
111 else if (o instanceof StoreContainerType) {
112 logger.info("Propagating invalidation to StoreContainer: " + o.toString());
113 // propagate invalidation to a whole StoreContainer
114 o_store.invalidate( (StoreContainerType) o);
123 public void release() {
124 this.reference = null;
125 this.uniqueIdentifier = null;
126 this.stocType = null;
129 public StorableObject use() {
135 * Method equals for comparison between two identifier
137 * @return true if yes otherwise false
140 public boolean equals(Object sid) {
141 if (! (sid instanceof StoreIdentifier))
143 if ( ( (StoreIdentifier) sid).getStoreContainerType() == stocType &&
144 ( (StoreIdentifier) sid).getUniqueIdentifier().equals(uniqueIdentifier)) {
150 public StoreContainerType getStoreContainerType() {
154 public String getUniqueIdentifier() {
155 return uniqueIdentifier;
158 public boolean hasReference() {
159 return (reference == null) ? false : true;
162 public String toString() {
163 StringBuffer id = new StringBuffer(uniqueIdentifier);
164 id.append("@storetype: ").append(stocType.toString());
165 if (reference != null)
166 id.append(" (" + timesUsed).append(") times used.");
167 return id.toString();