1 23 24 package com.sun.enterprise.admin.common.domains.registry; 25 26 import java.io.Serializable ; 27 import java.util.HashMap ; 28 import java.io.File ; 29 import java.util.Iterator ; 30 import java.util.Set ; 31 import java.util.TreeMap ; 32 import java.util.NoSuchElementException ; 33 34 35 class Registry implements Serializable , Cloneable , DomainRegistryI 36 { 37 TreeMap roots = new TreeMap (); 38 HashMap entries = new HashMap (); 39 40 public boolean isRegistered(String name){ 41 return roots.containsKey(name); 42 } 43 44 45 public void registerDomain(DomainEntry de) throws DomainRegistryException{ 46 if (isRegistered(de.getName())) { 47 throw new AlreadyRegisteredException(de.getName()); 48 } 49 if (containsRoot(de.getRoot())){ 50 throw new InvalidRootException("The root \""+de.getRoot()+"\" is already registered"); 51 } 52 roots.put(de.getName(), de.getRoot()); 53 entries.put(de.getName(), de); 54 } 55 56 public void unregisterDomain(String domain_name) throws DomainRegistryException{ 57 if (!isRegistered(domain_name)){ 58 throw new UnregisteredDomainException(domain_name); 59 } 60 delete(domain_name); 61 } 62 63 public void unregisterDomain(DomainEntry de) throws DomainRegistryException{ 64 unregisterDomain(de.getName()); 65 } 66 public void reregisterDomain(DomainEntry de) throws DomainRegistryException { 67 if (isRegistered(de.getName())) { 68 if (!roots.get(de.getName()).equals(de.getRoot())){ 69 throw new InvalidRootException("The given root ("+de.getRoot()+") of domain "+de.getName()+" doesn't match the already registered root for this domain"); 70 } 71 } else { 72 if (containsRoot(de.getRoot())){ 73 throw new InvalidRootException("The given root ("+de.getRoot()+") of domain "+de.getName()+" is already registered with a different domain"); 74 } 75 }; 76 77 entries.put(de.getName(), de); 78 } 79 80 public Iterator iterator() throws DomainRegistryException{ 81 return new RegistryIterator(this); 82 } 83 84 public boolean containsDomain(DomainEntry de) throws DomainRegistryException{ 85 return entries.values().contains(de); 86 } 87 public DomainEntry getDomain(String name) throws DomainRegistryException { 88 return (DomainEntry) entries.get(name); 89 } 90 public int size(){ 91 return roots.size(); 92 } 93 94 private boolean containsRoot(File root){ 95 return roots.containsValue(root); 96 } 97 98 private void delete(String name){ 99 roots.remove(name); 100 entries.remove(name); 101 } 102 103 104 105 protected Object clone(){ 106 try { 107 Registry lhs = (Registry) super.clone(); 108 lhs.roots = (TreeMap ) this.roots.clone(); 109 lhs.entries = (HashMap ) this.entries.clone(); 110 return lhs; 111 } 112 catch (CloneNotSupportedException cne){ 113 return null; 114 } 115 } 116 117 118 class RegistryIterator implements Iterator 119 { 120 Registry registry; 121 Iterator iterator; 122 123 RegistryIterator(Registry r){ 124 registry = (Registry) r.clone(); 125 iterator = registry.roots.keySet().iterator(); 126 } 127 public boolean hasNext(){ 128 return iterator.hasNext(); 129 } 130 public Object next() throws NoSuchElementException { 131 return entries.get((String ) iterator.next()); 132 } 133 public void remove() throws UnsupportedOperationException { 134 throw new UnsupportedOperationException (); 135 } 136 } 137 138 } 139 140 | Popular Tags |