reintroduced StringUtil.regexpReplace
[mir.git] / source / mir / storage / store / StoreIdentifier.java
1 /*
2  * Copyright (C) 2001-2006 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  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.
27  */
28 package mir.storage.store;
29
30 /**
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
36  *
37  * Copyright:     Copyright (c) 2002
38  * Company:       indy
39  * @author        rk
40  * @version 1.0
41  */
42 import mir.entity.EntityList;
43 import mir.log.LoggerWrapper;
44
45 import java.util.Iterator;
46 import java.util.Set;
47
48 public class StoreIdentifier {
49   /** todo check if invalidating already to avoid deadlocks what about concurrency? */
50
51   private static ObjectStore o_store = ObjectStore.getInstance();
52
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;
58
59   protected static LoggerWrapper logger = new LoggerWrapper("Database.ObjectStore");
60
61   public StoreIdentifier(StorableObject reference, int storeType,
62                          String uniqueIdentifier) {
63     Class theClass;
64     if (reference instanceof EntityList)
65       theClass = ( (EntityList) reference).getStorage().getEntityClass();
66     else
67       theClass = reference.getClass();
68     this.uniqueIdentifier = uniqueIdentifier;
69     this.stocType = StoreContainerType.valueOf(theClass, storeType);
70     this.reference = reference;
71   }
72
73   public StoreIdentifier(StorableObject reference, String uniqueIdentifier) {
74     this(reference, StoreContainerType.STOC_TYPE_ENTITY, uniqueIdentifier);
75   }
76
77   public StoreIdentifier(Class theClass, String uniqueIdentifier) {
78     this(theClass, StoreContainerType.STOC_TYPE_ENTITY, uniqueIdentifier);
79   }
80
81   public StoreIdentifier(Class theClass, int storeType, String uniqueIdentifier) {
82     this.uniqueIdentifier = uniqueIdentifier;
83     this.stocType = StoreContainerType.valueOf(theClass, storeType);
84   }
85
86   public void invalidate() {
87     logger.info("Invalidating: " + toString());
88     // avoid deadlock due to propagation.
89     if (!invalidating) {
90       invalidating = true;
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);
99       }
100
101       // propagate invalidation to Set
102       Set set = reference.getNotifyOnReleaseSet();
103       if (set != null) {
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);
110           }
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);
115           }
116
117         }
118       }
119       release();
120     }
121   }
122
123   public void release() {
124     this.reference = null;
125     this.uniqueIdentifier = null;
126     this.stocType = null;
127   }
128
129   public StorableObject use() {
130     timesUsed++;
131     return reference;
132   }
133
134   /**
135    *  Method equals for comparison between two identifier
136    *
137    *  @return true if yes otherwise false
138    *
139    */
140   public boolean equals(Object sid) {
141     if (! (sid instanceof StoreIdentifier))
142       return false;
143     if ( ( (StoreIdentifier) sid).getStoreContainerType() == stocType &&
144         ( (StoreIdentifier) sid).getUniqueIdentifier().equals(uniqueIdentifier)) {
145       return true;
146     }
147     return false;
148   }
149
150   public StoreContainerType getStoreContainerType() {
151     return stocType;
152   }
153
154   public String getUniqueIdentifier() {
155     return uniqueIdentifier;
156   }
157
158   public boolean hasReference() {
159     return (reference == null) ? false : true;
160   }
161
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();
168   }
169 }