KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.io.File JavaDoc;
30 import java.lang.UnsupportedOperationException JavaDoc;
31 import java.util.NoSuchElementException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.EOFException JavaDoc;
34
35 /**
36    This class provides a registry for domains, abstracting the
37    persistent storage and coordination activities required to permit
38    uniqueness invariants to be maintained between domains no matter
39    which JVM they are being referred from.
40    <p>
41    The directory in which the persistent store is kept is denoted by
42    the <code>System</code> property
43    <code>com.sun.aas.admin.configRoot</code>. The name of the
44    file is <code>domains.ser</code>
45    <p>
46    The principal invariants this registry maintains is that no two
47    domains shall have the same name, and no two domains shall have the
48    same location.
49    <p>
50    The registry can be considered to be a table whose keys are domain
51    name and domain location, and whose values are the remote contact
52    information by which the adminstration server within that domain
53    can be contacted remotely.
54    <p>
55    A row in this table is abstracted into the {@link DomainEntry} class.
56    *
57    * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
58    * @version $Revision: 1.3 $
59    */

60 public class DomainRegistry implements DomainRegistryI
61 {
62
63         /**
64          * Return an instance of the DomainRegistry
65          * @return a <code>DomainRegistry</code> value
66          @throws DomainRegistryException if there was a problem
67          instantiating the class
68          */

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 JavaDoc 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 JavaDoc name) throws DomainRegistryException {
125       refreshRegistry();
126       return registry.getDomain(name);
127     }
128
129   public Iterator JavaDoc 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 JavaDoc {
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 JavaDoc te){
153         throw new DomainRegistryException("problem reading from store", te);
154       }
155     }
156   }
157
158   private Registry getRegistryFromStore() throws IOException JavaDoc, TimeoutException, ClassNotFoundException JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc e){
194       throw new DomainRegistryException("couldn't initialize registry. Error message: "+ e.getMessage());
195     }
196   }
197
198 /*
199   PersistentStore getPS(){
200     return (PersistentStore) store;
201   }
202  */

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