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 java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.ObjectInputStream;
22  import java.io.ObjectOutputStream;
23  
24  import org.apache.jetspeed.om.registry.RegistryEntry;
25  import org.apache.jetspeed.om.registry.InvalidEntryException;
26  import org.apache.jetspeed.om.registry.RegistryException;
27  import org.apache.jetspeed.om.registry.SecurityEntry;
28  import org.apache.jetspeed.services.Registry;
29  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30  import org.apache.jetspeed.services.logging.JetspeedLogger;
31  
32  /***
33   * Extends BaseRegistry implementation to override object creation
34   * method and ensure Registry object is synchronized with its
35   * persistence backend by delegating actual addition/deletion of objects
36   * to the registry service.
37   * <p>To avoid loops, a RegistryService implementation using this class
38   * nees to call the addLocalEntry/removeLocalEntry methods to modify
39   * the in memory state of this Registry</p>
40   *
41   * @author <a href="mailto:raphael@apache.org">RaphaŽl Luta</a>
42   * @version $Id: BaseSecurityRegistry.java,v 1.6 2004/02/23 03:08:26 jford Exp $
43   */
44  public class BaseSecurityRegistry extends BaseRegistry
45  {
46  
47      /***
48       * Static initialization of the logger for this class
49       */    
50      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseSecurityRegistry.class.getName());
51      
52      /***
53      @see Registry#setEntry
54      */
55      public void setEntry( RegistryEntry entry ) throws InvalidEntryException
56      {
57  		// Delegate to the RegistryService to ensure correct handling of
58  		// persistence if using file fragments
59  
60  		try
61  		{
62  			Registry.addEntry(Registry.SECURITY, entry);
63  		}
64  		catch (RegistryException e)
65  		{
66  			logger.error("Exception", e);
67  		}
68      }
69  
70      /***
71      @see Registry#addEntry
72      */
73      public void addEntry( RegistryEntry entry ) throws InvalidEntryException
74      {
75  		// Delegate to the RegistryService to ensure correct handling of
76  		// persistence if using file fragments
77  
78  		try
79  		{
80  			Registry.addEntry(Registry.SECURITY, entry);
81  		}
82  		catch (RegistryException e)
83  		{
84  			logger.error("Exception", e);
85  		}
86      }
87  
88      /***
89      @see Registry#removeEntry
90      */
91      public void removeEntry( String name )
92      {
93  		// Delegate to the RegistryService to ensure correct handling of
94  		// persistence if using file fragments
95  
96  		Registry.removeEntry(Registry.SECURITY, name);
97      }
98  
99      /***
100     @see Registry#removeEntry
101     */
102     public void removeEntry( RegistryEntry entry )
103     {
104 		// Delegate to the RegistryService to ensure correct handling of
105 		// persistence if using file fragments
106 
107 		if (entry != null)
108 		{
109 			Registry.removeEntry(Registry.SECURITY, entry.getName());
110 		}
111     }
112 
113     /***
114      * Creates a new RegistryEntry instance compatible with the current
115      * Registry instance implementation
116      *
117      * @return the newly created RegistryEntry
118      */
119     public RegistryEntry createEntry()
120     {
121 		return new BaseSecurityEntry();
122 	}
123 	
124 	/***
125      * returns a security entry from the registry based on the name provided
126      * @param String name Name of security entry we want.
127      * @return SecurityEntry SecurityEntry matching the <code>name</code>
128      * argument or null if no such entry exists.
129      */
130     public SecurityEntry getSecurityEntry(String name)
131     {
132         try
133         {
134             return (SecurityEntry) this.getEntry(name);
135         }
136         catch (InvalidEntryException e)
137         {
138             logger.error("Exception", e);
139         }
140 
141         return null;
142     }
143 
144     /***
145      * @return SecurityEntry a new SecurityEntry instance
146      */
147     public SecurityEntry createSecurityEntry()
148     {
149         return (SecurityEntry) this.createEntry();
150     }
151 
152     /***
153      * Makes an exact copy of the named role, but changing the nmae attribute
154      * to the value of <code>newName</code>
155      * @param String original Name of the entry we want to clone
156      * @param String newName Name to give the cloned entry
157      * @return SecurityEntry The cloned entry.
158      */
159     public SecurityEntry cloneSecurityEntry(String original, String newName)
160     {
161         SecurityEntry baseEntry = getSecurityEntry(original);
162         if (baseEntry != null)
163         {
164             SecurityEntry newEntry = cloneEntry(baseEntry);
165             newEntry.setName(newName);
166             return newEntry;
167         }
168 
169         return null;
170     }
171     
172 
173     /***
174      * Makes an indentical copy of the SecurityEntry provided using 
175      * serialize/de-serialize logic to make a  clean reference
176      * @param SecurityEntry secEntry the entry to clone
177      * @return SecurityEntry the cloned entry.
178      */
179     private static SecurityEntry cloneEntry(SecurityEntry secEntry)
180     {
181         SecurityEntry clonedEntry = null;
182         try
183         {
184             ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(100);
185             ObjectOutputStream objectoutputstream = new ObjectOutputStream(bytearrayoutputstream);
186             objectoutputstream.writeObject(secEntry);
187             byte abyte0[] = bytearrayoutputstream.toByteArray();
188             objectoutputstream.close();
189             ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
190             ObjectInputStream objectinputstream = new ObjectInputStream(bytearrayinputstream);
191             clonedEntry = (SecurityEntry) objectinputstream.readObject();
192             objectinputstream.close();
193         }
194         catch (Exception exception)
195         {
196             // nothing
197         }
198         return clonedEntry;
199     }
200 }