c9d509409a477a7bd80f1ad090be4a6b4d41f4ed
[mir.git] / source / mir / storage / store / StoreIdentifier.java
1 package mir.storage.store;
2
3 /**
4  * Title:         Class StoreIdentifier
5  * Description:   StoreIdentifier has two functions.
6  *                A) StoreIdenfier holds a reference to a @see StorableObject
7  *                or B) StoreIdentifier is used too search for a @see StorableObject
8  *                in the @see StoreContainer that matches its
9  *
10  * Copyright:     Copyright (c) 2002
11  * Company:       indy
12  * @author        rk
13  * @version 1.0
14  */
15 import java.util.*;
16 import mir.misc.Logfile;
17 import mir.entity.*;
18
19 public class StoreIdentifier {
20
21         /** @todo check if invalidating already to avoid deadlocks
22          *  what about concurrency? */
23
24         private static Logfile      storeLog;
25   private static ObjectStore  o_store = ObjectStore.getInstance();
26
27         private StoreContainerType  stocType=null;
28         private StorableObject      reference=null;
29         private String              uniqueIdentifier=null; // id for Entity & sql for EntityList
30         private long                timesUsed;
31         private boolean             invalidating=false;
32
33         /** @todo initialize logfile  */
34
35         private StoreIdentifier() {}
36
37         public StoreIdentifier(StorableObject reference, int storeType, String uniqueIdentifier) {
38     Class theClass;
39     if (reference instanceof EntityList)
40       theClass=((EntityList)reference).getStorage().getEntityClass();
41     else
42       theClass=reference.getClass();
43     this.uniqueIdentifier=uniqueIdentifier;
44                 this.stocType = StoreContainerType.valueOf(theClass, storeType);
45                 this.reference=reference;
46         }
47
48   public StoreIdentifier(StorableObject reference, String uniqueIdentifier) {
49     this(reference, StoreContainerType.STOC_TYPE_ENTITY, uniqueIdentifier);
50   }
51
52   public StoreIdentifier(Class theClass, String uniqueIdentifier) {
53     this(theClass, StoreContainerType.STOC_TYPE_ENTITY,uniqueIdentifier);
54   }
55
56   public StoreIdentifier(Class theClass, int storeType, String uniqueIdentifier) {
57     this.uniqueIdentifier=uniqueIdentifier;
58                 this.stocType = StoreContainerType.valueOf(theClass, storeType);
59   }
60         /**
61          *  Method:       ivalidate
62          *  Description:
63          *
64          *  @return
65          */
66         public void invalidate() {
67     System.out.println("Invalidating: " + toString());
68                 // avoid deadlock due to propagation.
69                 if (!invalidating) {
70                         invalidating=true;
71       if ( stocType!=null &&
72            stocType.getStocType()==StoreContainerType.STOC_TYPE_ENTITY )
73       {
74         System.out.println("Propagating invalidation to EntityList for " + toString());
75         // we should invalidate related ENTITY_LIST
76         StoreContainerType entityListStocType =
77             StoreContainerType.valueOf( stocType.getStocClass(),
78                                         StoreContainerType.STOC_TYPE_ENTITYLIST );
79         o_store.invalidate(entityListStocType);
80       }
81
82       // propagate invalidation to Set
83                         Set set = reference.getNotifyOnReleaseSet();
84       if (set!=null) {
85         for (Iterator it = set.iterator(); it.hasNext(); ) {
86           Object o = it.next();
87           if ( o instanceof StoreIdentifier ) {
88             System.out.println("Propagating invalidation to StoreIdentifier: " + o.toString());
89             // propagate invalidation to a specific StoreIdentifier in cache
90             o_store.invalidate((StoreIdentifier)o);
91           } else if ( o instanceof StoreContainerType ) {
92             System.out.println("Propagating invalidation to StoreContainer: " + o.toString());
93             // propagate invalidation to a whole StoreContainer
94             o_store.invalidate((StoreContainerType)o);
95           }
96
97         }
98       }
99                         release();
100                 }
101         }
102
103         public void release() {
104                 this.reference=null;
105                 this.uniqueIdentifier=null;
106                 this.stocType=null;
107         }
108
109         public StorableObject use() {
110                 timesUsed++;
111                 return reference;
112         }
113
114         /**
115          *  Method equals for comparison between two identifier
116          *
117          *  @return true if yes otherwise false
118          *
119          */
120         public boolean equals(Object sid) {
121     if ( !(sid instanceof StoreIdentifier) ) return false;
122     if ( ((StoreIdentifier)sid).getStoreContainerType()==stocType &&
123          ((StoreIdentifier)sid).getUniqueIdentifier().equals(uniqueIdentifier) ) {
124       return true;
125     }
126                 return false;
127         }
128
129         public StoreContainerType getStoreContainerType() { return stocType; }
130         public String getUniqueIdentifier() { return uniqueIdentifier; }
131         public boolean hasReference() { return (reference==null) ? false:true; }
132
133         public String toString() {
134     StringBuffer id = new StringBuffer(uniqueIdentifier);
135     id.append("@storetype: ").append(stocType.toString());
136     if (reference != null) id.append(" ("+timesUsed).append(") times used.");
137                 return id.toString();
138         }
139
140
141 }