KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > domains > registry > Registry


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.common.domains.registry;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.io.File JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.TreeMap JavaDoc;
32 import java.util.NoSuchElementException JavaDoc;
33
34
35 class Registry implements Serializable JavaDoc, Cloneable JavaDoc, DomainRegistryI
36 {
37   TreeMap JavaDoc roots = new TreeMap JavaDoc();
38   HashMap JavaDoc entries = new HashMap JavaDoc();
39
40   public boolean isRegistered(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc root){
95     return roots.containsValue(root);
96   }
97
98   private void delete(String JavaDoc name){
99     roots.remove(name);
100     entries.remove(name);
101   }
102
103     
104
105   protected Object JavaDoc clone(){
106     try {
107       Registry lhs = (Registry) super.clone();
108       lhs.roots = (TreeMap JavaDoc) this.roots.clone();
109       lhs.entries = (HashMap JavaDoc) this.entries.clone();
110       return lhs;
111     }
112     catch (CloneNotSupportedException JavaDoc cne){
113       return null;
114     }
115   }
116
117   
118   class RegistryIterator implements Iterator JavaDoc
119   {
120     Registry registry;
121     Iterator JavaDoc 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 JavaDoc next() throws NoSuchElementException JavaDoc{
131       return entries.get((String JavaDoc) iterator.next());
132     }
133     public void remove() throws UnsupportedOperationException JavaDoc{
134       throw new UnsupportedOperationException JavaDoc();
135     }
136   }
137
138 }
139
140
Popular Tags