Mir goes GPL
[mir.git] / source / mir / storage / store / StoreIdentifier.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
30  */
31
32 package mir.storage.store;
33
34 /**
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
40  *
41  * Copyright:     Copyright (c) 2002
42  * Company:       indy
43  * @author        rk
44  * @version 1.0
45  */
46 import java.util.*;
47 import mir.misc.Logfile;
48 import mir.entity.*;
49
50 public class StoreIdentifier {
51
52         /** @todo check if invalidating already to avoid deadlocks
53          *  what about concurrency? */
54
55         private static Logfile      storeLog;
56   private static ObjectStore  o_store = ObjectStore.getInstance();
57
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;
63
64         /** @todo initialize logfile  */
65
66         private StoreIdentifier() {}
67
68         public StoreIdentifier(StorableObject reference, int storeType, String uniqueIdentifier) {
69     Class theClass;
70     if (reference instanceof EntityList)
71       theClass=((EntityList)reference).getStorage().getEntityClass();
72     else
73       theClass=reference.getClass();
74     this.uniqueIdentifier=uniqueIdentifier;
75                 this.stocType = StoreContainerType.valueOf(theClass, storeType);
76                 this.reference=reference;
77         }
78
79   public StoreIdentifier(StorableObject reference, String uniqueIdentifier) {
80     this(reference, StoreContainerType.STOC_TYPE_ENTITY, uniqueIdentifier);
81   }
82
83   public StoreIdentifier(Class theClass, String uniqueIdentifier) {
84     this(theClass, StoreContainerType.STOC_TYPE_ENTITY,uniqueIdentifier);
85   }
86
87   public StoreIdentifier(Class theClass, int storeType, String uniqueIdentifier) {
88     this.uniqueIdentifier=uniqueIdentifier;
89                 this.stocType = StoreContainerType.valueOf(theClass, storeType);
90   }
91         /**
92          *  Method:       ivalidate
93          *  Description:
94          *
95          *  @return
96          */
97         public void invalidate() {
98     System.out.println("Invalidating: " + toString());
99                 // avoid deadlock due to propagation.
100                 if (!invalidating) {
101                         invalidating=true;
102       if ( stocType!=null &&
103            stocType.getStocType()==StoreContainerType.STOC_TYPE_ENTITY )
104       {
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);
111       }
112
113       // propagate invalidation to Set
114                         Set set = reference.getNotifyOnReleaseSet();
115       if (set!=null) {
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);
126           }
127
128         }
129       }
130                         release();
131                 }
132         }
133
134         public void release() {
135                 this.reference=null;
136                 this.uniqueIdentifier=null;
137                 this.stocType=null;
138         }
139
140         public StorableObject use() {
141                 timesUsed++;
142                 return reference;
143         }
144
145         /**
146          *  Method equals for comparison between two identifier
147          *
148          *  @return true if yes otherwise false
149          *
150          */
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) ) {
155       return true;
156     }
157                 return false;
158         }
159
160         public StoreContainerType getStoreContainerType() { return stocType; }
161         public String getUniqueIdentifier() { return uniqueIdentifier; }
162         public boolean hasReference() { return (reference==null) ? false:true; }
163
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();
169         }
170
171
172 }