2 * Copyright (C) 2001, 2002 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 the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mir.storage.store;
35 * Title: Class StoreIdentifier
36 * Description: StoreIdentifier has two functions.
37 * A) StoreIdenfier holds a reference to a @see StorableObject
38 * or B) StoreIdentifier is used too search for a @see StorableObject
39 * in the @see StoreContainer that matches its
41 * Copyright: Copyright (c) 2002
47 import mir.misc.Logfile;
50 public class StoreIdentifier {
52 /** @todo check if invalidating already to avoid deadlocks
53 * what about concurrency? */
55 private static Logfile storeLog;
56 private static ObjectStore o_store = ObjectStore.getInstance();
58 private StoreContainerType stocType=null;
59 private StorableObject reference=null;
60 private String uniqueIdentifier=null; // id for Entity & sql for EntityList
61 private long timesUsed;
62 private boolean invalidating=false;
64 /** @todo initialize logfile */
66 private StoreIdentifier() {}
68 public StoreIdentifier(StorableObject reference, int storeType, String uniqueIdentifier) {
70 if (reference instanceof EntityList)
71 theClass=((EntityList)reference).getStorage().getEntityClass();
73 theClass=reference.getClass();
74 this.uniqueIdentifier=uniqueIdentifier;
75 this.stocType = StoreContainerType.valueOf(theClass, storeType);
76 this.reference=reference;
79 public StoreIdentifier(StorableObject reference, String uniqueIdentifier) {
80 this(reference, StoreContainerType.STOC_TYPE_ENTITY, uniqueIdentifier);
83 public StoreIdentifier(Class theClass, String uniqueIdentifier) {
84 this(theClass, StoreContainerType.STOC_TYPE_ENTITY,uniqueIdentifier);
87 public StoreIdentifier(Class theClass, int storeType, String uniqueIdentifier) {
88 this.uniqueIdentifier=uniqueIdentifier;
89 this.stocType = StoreContainerType.valueOf(theClass, storeType);
97 public void invalidate() {
98 System.out.println("Invalidating: " + toString());
99 // avoid deadlock due to propagation.
102 if ( stocType!=null &&
103 stocType.getStocType()==StoreContainerType.STOC_TYPE_ENTITY )
105 System.out.println("Propagating invalidation to EntityList for " + toString());
106 // we should invalidate related ENTITY_LIST
107 StoreContainerType entityListStocType =
108 StoreContainerType.valueOf( stocType.getStocClass(),
109 StoreContainerType.STOC_TYPE_ENTITYLIST );
110 o_store.invalidate(entityListStocType);
113 // propagate invalidation to Set
114 Set set = reference.getNotifyOnReleaseSet();
116 for (Iterator it = set.iterator(); it.hasNext(); ) {
117 Object o = it.next();
118 if ( o instanceof StoreIdentifier ) {
119 System.out.println("Propagating invalidation to StoreIdentifier: " + o.toString());
120 // propagate invalidation to a specific StoreIdentifier in cache
121 o_store.invalidate((StoreIdentifier)o);
122 } else if ( o instanceof StoreContainerType ) {
123 System.out.println("Propagating invalidation to StoreContainer: " + o.toString());
124 // propagate invalidation to a whole StoreContainer
125 o_store.invalidate((StoreContainerType)o);
134 public void release() {
136 this.uniqueIdentifier=null;
140 public StorableObject use() {
146 * Method equals for comparison between two identifier
148 * @return true if yes otherwise false
151 public boolean equals(Object sid) {
152 if ( !(sid instanceof StoreIdentifier) ) return false;
153 if ( ((StoreIdentifier)sid).getStoreContainerType()==stocType &&
154 ((StoreIdentifier)sid).getUniqueIdentifier().equals(uniqueIdentifier) ) {
160 public StoreContainerType getStoreContainerType() { return stocType; }
161 public String getUniqueIdentifier() { return uniqueIdentifier; }
162 public boolean hasReference() { return (reference==null) ? false:true; }
164 public String toString() {
165 StringBuffer id = new StringBuffer(uniqueIdentifier);
166 id.append("@storetype: ").append(stocType.toString());
167 if (reference != null) id.append(" ("+timesUsed).append(") times used.");
168 return id.toString();