1 23 24 package com.sun.enterprise.admin.common.domains.registry; 25 26 import java.util.Iterator ; 27 import java.util.HashMap ; 28 import java.io.Serializable ; 29 import java.io.File ; 30 import java.lang.UnsupportedOperationException ; 31 import java.util.NoSuchElementException ; 32 import java.io.IOException ; 33 import java.io.EOFException ; 34 35 60 public class DomainRegistry implements DomainRegistryI 61 { 62 63 69 synchronized static public DomainRegistry newInstance() throws DomainRegistryException { 70 if (instance == null){ 71 instance = new DomainRegistry(); 72 instance.init(); 73 } 74 return instance; 75 } 76 77 public synchronized void registerDomain(DomainEntry de) throws DomainRegistryException { 78 prepareForUpdate(); 79 try { 80 registry.registerDomain(de); 81 } 82 catch (DomainRegistryException e){ 83 store.unlock(); 84 throw e; 85 } 86 saveRegistry(); 87 } 88 89 public void unregisterDomain(String domain_name) throws DomainRegistryException { 90 prepareForUpdate(); 91 try{ 92 registry.unregisterDomain(domain_name); 93 } 94 catch (DomainRegistryException e){ 95 store.unlock(); 96 throw e; 97 } 98 saveRegistry(); 99 100 } 101 102 public void unregisterDomain(DomainEntry de) throws DomainRegistryException { 103 this.unregisterDomain(de.getName()); 104 } 105 106 public void reregisterDomain(DomainEntry de) throws DomainRegistryException { 107 prepareForUpdate(); 108 try { 109 registry.reregisterDomain(de); 110 } 111 catch (DomainRegistryException e){ 112 store.unlock(); 113 throw e; 114 } 115 saveRegistry(); 116 117 } 118 119 public boolean containsDomain(DomainEntry de) throws DomainRegistryException { 120 refreshRegistry(); 121 return registry.containsDomain(de); 122 } 123 124 public DomainEntry getDomain(String name) throws DomainRegistryException { 125 refreshRegistry(); 126 return registry.getDomain(name); 127 } 128 129 public Iterator iterator() throws DomainRegistryException { 130 refreshRegistry(); 131 return registry.iterator(); 132 } 133 134 public int size() throws DomainRegistryException { 135 refreshRegistry(); 136 return registry.size(); 137 } 138 139 140 void reset() throws IOException { 141 store.unlock(); 142 registry = null; 143 store = null; 144 instance = null; 145 } 146 147 private void refreshRegistry() throws DomainRegistryException { 148 if (lastModified < store.lastModified()){ 149 try { 150 registry = getRegistryFromStore(); 151 } 152 catch (Exception te){ 153 throw new DomainRegistryException("problem reading from store", te); 154 } 155 } 156 } 157 158 private Registry getRegistryFromStore() throws IOException , TimeoutException, ClassNotFoundException { 159 Registry br; 160 br = (Registry) store.readObject(); 161 return (br != null ? br : new Registry()); 162 } 163 164 165 private void prepareForUpdate() throws DomainRegistryException { 166 try { 167 store.lock(); 168 } 169 catch (Exception e){ 170 throw new DomainRegistryException("problem locking store ", e); 171 } 172 refreshRegistry(); 173 } 174 175 private void saveRegistry() throws DomainRegistryException { 176 try { 177 store.writeObject(registry); 178 store.unlock(); 179 lastModified = store.lastModified(); 180 } 181 catch (Exception e){ 182 e.printStackTrace(); 183 throw new DomainRegistryException("couldn't save registry", e); 184 } 185 } 186 187 188 private void init() throws DomainRegistryException { 189 try { 190 store = LockingStoreFactory.getInstance(); 191 registry = getRegistryFromStore(); 192 } 193 catch (Exception e){ 194 throw new DomainRegistryException("couldn't initialize registry. Error message: "+ e.getMessage()); 195 } 196 } 197 198 203 204 private Registry registry; 205 private LockingStore store; 206 private static DomainRegistry instance = null; 207 private long lastModified = 0; 208 209 private DomainRegistry(){} 210 211 } 212 | Popular Tags |