View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.om.registry.base;
18  
19  import org.apache.jetspeed.om.registry.RegistryEntry;
20  import org.apache.jetspeed.om.registry.InvalidEntryException;
21  import java.util.Map;
22  import java.util.TreeMap;
23  import java.util.Iterator;
24  import java.util.Enumeration;
25  import java.util.Vector;
26  
27  /***
28   * Provides base functionality within a Registry.
29   *
30   * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
31   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
32   * @version $Id: BaseRegistry.java,v 1.7 2004/02/23 03:08:26 jford Exp $
33   */
34  public class BaseRegistry implements LocalRegistry
35  {
36      protected static final boolean DEBUG = false;
37  
38      protected Map entries = new TreeMap();
39  
40      /*** @see Registry#getEntryCount */
41      public int getEntryCount()
42      {
43          return this.entries.size();
44      }
45  
46      /*** @see Registry#getEntry */
47      public RegistryEntry getEntry( String name ) throws InvalidEntryException
48      {
49  
50          RegistryEntry entry = null;
51  
52          if (name != null)
53          {
54              entry = (RegistryEntry)this.entries.get( name ) ;
55          }
56  
57          if (entry == null)
58          {
59              throw new InvalidEntryException( InvalidEntryException.ENTRY_DOES_NOT_EXIST+" "+name );
60          }
61  
62          return entry;
63      }
64  
65      /***
66      @see Registry#setEntry
67      */
68      public void setEntry( RegistryEntry entry ) throws InvalidEntryException
69      {
70          synchronized (this)
71          {
72  
73              if ( this.hasEntry( entry.getName() ) == false )
74              {
75                  throw new InvalidEntryException( InvalidEntryException.ENTRY_DOES_NOT_EXIST+" "+entry.getName());
76              }
77  
78              this.entries.put( entry.getName(), entry );
79          }
80      }
81  
82      /***
83      @see Registry#addEntry
84      */
85      public void addEntry( RegistryEntry entry ) throws InvalidEntryException
86      {
87  
88          synchronized (this)
89          {
90              if ( this.hasEntry( entry.getName() ) )
91              {
92                  throw new InvalidEntryException( InvalidEntryException.ENTRY_ALREADY_PRESENT );
93              }
94  
95              this.entries.put( entry.getName(), entry );
96          }
97      }
98  
99      /***
100        @see Registry#hasEntry
101     */
102     public boolean hasEntry( String name )
103     {
104         return this.entries.containsKey( name );
105     }
106 
107     /***
108     @see Registry#removeEntry
109     */
110     public void removeEntry( String name )
111     {
112         synchronized(this)
113         {
114             this.entries.remove( name );
115         }
116     }
117 
118     /***
119     @see Registry#removeEntry
120     */
121 
122     public void removeEntry( RegistryEntry entry )
123     {
124         synchronized(this)
125         {
126             this.entries.remove( entry.getName() );
127         }
128     }
129 
130     /***
131        @see Registry#getEntries
132      */
133     public Enumeration getEntries()
134     {
135         Vector v = null;
136 
137         synchronized (this)
138         {
139             // this is ne
140             v = new Vector(this.entries.values());
141         }
142 
143         return v.elements();
144     }
145 
146     /***
147        @see Registry#listEntryNames
148      */
149     public Iterator listEntryNames()
150     {
151         return entries.keySet().iterator();
152     }
153 
154     /***
155        @see Registry#toArray
156      */
157     public RegistryEntry[] toArray()
158     {
159 
160         Enumeration enum = getEntries();
161         Vector v = new Vector();
162 
163         while( enum.hasMoreElements() )
164         {
165             v.addElement( enum.nextElement() );
166         }
167 
168         RegistryEntry[] entries = new RegistryEntry[ v.size() ];
169         v.copyInto( entries );
170         return entries;
171 
172     }
173 
174     /***
175      * Creates a new RegistryEntry instance compatible with the current
176      * Registry instance implementation
177      *
178      * @return the newly created RegistryEntry
179      */
180     public RegistryEntry createEntry()
181     {
182         return new BaseRegistryEntry();
183     }
184 
185 
186     // RegistryService specific methods
187 
188     /***
189      * This method is used  to only set the entry in the local
190      * memory cache of the registry without any coherency check with
191      * persistent storage
192      *
193      * @param entry the RegistryEntry to store
194      */
195     public void setLocalEntry( RegistryEntry entry ) throws InvalidEntryException
196     {
197         synchronized (this)
198         {
199 
200             if ( this.hasEntry( entry.getName() ) == false )
201             {
202                 throw new InvalidEntryException( InvalidEntryException.ENTRY_DOES_NOT_EXIST+" "+entry.getName());
203             }
204 
205             this.entries.put( entry.getName(), entry );
206         }
207     }
208 
209     /***
210      * This method is used to only add the entry in the local
211      * memory cache of the registry without any coherency check with
212      * persistent storage
213      *
214      * @param entry the RegistryEntry to store
215      */
216     public void addLocalEntry( RegistryEntry entry ) throws InvalidEntryException
217     {
218 
219         synchronized (this)
220         {
221             if ( this.hasEntry( entry.getName() ) )
222             {
223                 throw new InvalidEntryException( InvalidEntryException.ENTRY_ALREADY_PRESENT );
224             }
225 
226             this.entries.put( entry.getName(), entry );
227         }
228     }
229 
230     /***
231      * This method is used to only remove the entry from the local
232      * memory cache of the registry without any coherency check with
233      * persistent storage
234      *
235      * @param name the name of the RegistryEntry to remove
236      */
237     public void removeLocalEntry( String name )
238     {
239         synchronized(this)
240         {
241             this.entries.remove( name );
242         }
243     }
244 
245     /***
246      * This method is used to only remove the entry from the local
247      * memory cache of the registry without any coherency check with
248      * persistent storage
249      *
250      * @param entry the RegistryEntry to remove
251      */
252     public void removeLocalEntry( RegistryEntry entry )
253     {
254         synchronized(this)
255         {
256             this.entries.remove( entry.getName() );
257         }
258     }
259 
260 }