first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[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
18 public class StoreIdentifier {
19
20         /** @todo check if invalidating already to avoid deadlocks
21          *  what about concurrency? */
22
23         private static Logfile      storeLog;
24   private static ObjectStore  o_store = ObjectStore.getInstance();
25
26         private StoreContainerType  stocType=null;
27         private StorableObject      reference=null;
28         private String              uniqueIdentifier=null; // id for Entity & sql for EntityList
29         private long                timesUsed;
30         private boolean             invalidating=false;
31
32         /** @todo initialize logfile  */
33
34         private StoreIdentifier() {}
35
36         public StoreIdentifier(StorableObject reference, int storeType, String uniqueIdentifier) {
37     this(reference.getClass(), uniqueIdentifier);
38                 this.reference=reference;
39         }
40
41   public StoreIdentifier(Class theClass, String uniqueIdentifier) {
42     this(theClass, StoreContainerType.STOC_TYPE_ENTITY,uniqueIdentifier);
43   }
44
45   public StoreIdentifier(Class theClass, int storeType, String uniqueIdentifier) {
46     this.uniqueIdentifier=uniqueIdentifier;
47                 this.stocType = StoreContainerType.valueOf(theClass, storeType);
48   }
49         /**
50          *  Method:       ivalidate
51          *  Description:
52          *
53          *  @return
54          */
55         public void invalidate() {
56     System.out.println("Invalidating: " + toString());
57                 // avoid deadlock due to propagation.
58                 if (!invalidating) {
59                         invalidating=true;
60       if ( stocType!=null &&
61            stocType.getStocType()==StoreContainerType.STOC_TYPE_ENTITY )
62       {
63         System.out.println("Propagating invalidation to EntityList for " + toString());
64         // we should invalidate related ENTITY_LIST
65         StoreContainerType entityListStocType =
66             StoreContainerType.valueOf( stocType.getStocClass(),
67                                         StoreContainerType.STOC_TYPE_ENTITYLIST );
68         o_store.invalidate(entityListStocType);
69       }
70
71       // propagate invalidation to Set
72                         Set set = reference.getNotifyOnReleaseSet();
73       if (set!=null) {
74         for (Iterator it = set.iterator(); it.hasNext(); ) {
75           Object o = it.next();
76           if ( o instanceof StoreIdentifier ) {
77             System.out.println("Propagating invalidation to StoreIdentifier: " + o.toString());
78             // propagate invalidation to a specific StoreIdentifier in cache
79             o_store.invalidate((StoreIdentifier)o);
80           } else if ( o instanceof StoreContainerType ) {
81             System.out.println("Propagating invalidation to StoreContainer: " + o.toString());
82             // propagate invalidation to a whole StoreContainer
83             o_store.invalidate((StoreContainerType)o);
84           }
85
86         }
87       }
88                         release();
89                 }
90         }
91
92         public void release() {
93                 this.reference=null;
94                 this.uniqueIdentifier=null;
95                 this.stocType=null;
96         }
97
98         public StorableObject use() {
99                 timesUsed++;
100                 return reference;
101         }
102
103         /**
104          *  Method equals for comparison between two identifier
105          *
106          *  @return true if yes otherwise false
107          *
108          */
109         public boolean equals(Object sid) {
110     if ( !(sid instanceof StoreIdentifier) ) return false;
111     if ( ((StoreIdentifier)sid).getStoreContainerType()==stocType &&
112          ((StoreIdentifier)sid).getUniqueIdentifier().equals(uniqueIdentifier) )
113                                 return true;
114                 return false;
115         }
116
117         public StoreContainerType getStoreContainerType() { return stocType; }
118         public String getUniqueIdentifier() { return uniqueIdentifier; }
119         public boolean hasReference() { return (reference==null) ? false:true; }
120
121         public String toString() {
122     StringBuffer id = new StringBuffer(uniqueIdentifier);
123     id.append("@storetype: ").append(stocType.toString());
124     if (reference != null) id.append(" ("+timesUsed).append(") times used.");
125                 return id.toString();
126         }
127
128
129 }