1 64 65 package com.jcorporate.expresso.services.test; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.dbobj.Schema; 69 import com.jcorporate.expresso.kernel.ConsoleInstallLog; 70 import org.apache.log4j.Logger; 71 72 import java.util.HashMap ; 73 import java.util.TreeSet ; 74 75 76 86 public class SchemaManager { 87 static HashMap references = new HashMap (); 88 static TreeSet readOnlySet = new TreeSet (); 89 static Logger log = Logger.getLogger(SchemaManager.class); 90 91 public SchemaManager() { 92 } 93 94 99 synchronized static public void addReference(Schema s) { 100 SchemaManager.addReference(s, false); 101 } 102 103 111 synchronized static public void addReference(Schema s, 112 boolean forceNoCreate) { 113 if (log.isDebugEnabled()) { 114 log.debug("Adding Reference To Schema: " + s.getClass().getName()); 115 } 116 if (readOnlySet.contains(s.getClass().getName())) { 117 readOnlySet.remove(s.getClass().getName()); 118 } 119 if (references.containsKey(s)) { 120 Integer i = (Integer ) references.get(s); 121 122 if (i == null) { 123 log.error("Got null reference count, when schema exists in the " + 124 " hash map"); 125 references.put(s, new Integer (1)); 126 } else { 127 Integer i2 = new Integer (i.intValue() + 1); 128 129 if (log.isDebugEnabled()) { 130 log.debug("Previous Reference Count: " + i.toString()); 131 } 132 133 references.put(s, i2); 134 } 135 } else { 136 if (log.isDebugEnabled()) { 137 log.debug("Inserting new reference"); 138 } 139 140 references.put(s, new Integer (1)); 141 142 if (forceNoCreate == false) { 143 try { 144 145 if (SchemaCreator.ensureSchemaExists(s, new ConsoleInstallLog()) == true) { 146 147 log.debug("Schema " + s.getClass().getName() + 153 " didn't have a reference but already exists."); 154 references.put(s, new Integer (2)); 155 } 156 } catch (DBException e) { 157 log.error("Error Creating Schema", e); 158 } 159 } 160 } 161 } 162 163 173 synchronized static public void addReadOnlyReference(Schema s) { 174 boolean canBeReadOnly = false; 175 176 if (!references.containsKey(s)) { 177 canBeReadOnly = true; 178 } else if (readOnlySet.contains(s.getClass().getName())) { 179 canBeReadOnly = true; 180 } 181 182 addReference(s); 183 readOnlySet.add(s.getClass().getName()); 184 } 185 186 192 synchronized static public void removeReference(Schema s) { 193 if (log.isDebugEnabled()) { 194 log.debug("Removing Reference To Schema: " + 195 s.getClass().getName()); 196 } 197 if (references.containsKey(s)) { 198 Integer i = (Integer ) references.get(s); 199 200 if (i.intValue() == 1) { 201 references.remove(s); 202 203 if (!readOnlySet.contains(s.getClass().getName())) { 204 if (log.isDebugEnabled()) { 205 log.debug("Reference going to zero. Removing Schema"); 206 } 207 208 SchemaDeleter.deleteSchema(TestSystemInitializer.getTestContext(), s); 209 } else { 210 readOnlySet.remove(s.getClass().getName()); 211 212 if (log.isDebugEnabled()) { 213 log.debug("Reference to zero, but read only item. Not removing schema"); 214 } 215 } 216 } else { 217 Integer i2 = new Integer (i.intValue() - 1); 218 references.put(s, i2); 219 } 220 } else { 221 log.error("Schema : " + s.getClass().getName() + 222 "not found in reference list"); 223 224 return; 225 } 226 } 227 228 233 synchronized static public void forceSchemaDelete(Schema s) { 234 readOnlySet.remove(s.getClass().getName()); 235 } 236 237 synchronized static public void forceAllSchemaDelete() { 238 readOnlySet.clear(); 239 } 240 241 } | Popular Tags |