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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.om.registry.base;
1819import java.io.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.ObjectInputStream;
22import java.io.ObjectOutputStream;
2324import org.apache.jetspeed.om.registry.RegistryEntry;
25import org.apache.jetspeed.om.registry.InvalidEntryException;
26import org.apache.jetspeed.om.registry.RegistryException;
27import org.apache.jetspeed.om.registry.SecurityEntry;
28import org.apache.jetspeed.services.Registry;
29import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30import org.apache.jetspeed.services.logging.JetspeedLogger;
3132/***33 * Extends BaseRegistry implementation to override object creation34 * method and ensure Registry object is synchronized with its35 * persistence backend by delegating actual addition/deletion of objects36 * to the registry service.37 * <p>To avoid loops, a RegistryService implementation using this class38 * nees to call the addLocalEntry/removeLocalEntry methods to modify39 * 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 */44publicclassBaseSecurityRegistryextendsBaseRegistry45 {
4647/***48 * Static initialization of the logger for this class49 */50privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseSecurityRegistry.class.getName());
5152/***53 @see Registry#setEntry54 */55publicvoid setEntry( RegistryEntry entry ) throws InvalidEntryException
56 {
57// Delegate to the RegistryService to ensure correct handling of58// persistence if using file fragments5960try61 {
62 Registry.addEntry(Registry.SECURITY, entry);
63 }
64catch (RegistryException e)
65 {
66 logger.error("Exception", e);
67 }
68 }
6970/***71 @see Registry#addEntry72 */73publicvoid addEntry( RegistryEntry entry ) throws InvalidEntryException
74 {
75// Delegate to the RegistryService to ensure correct handling of76// persistence if using file fragments7778try79 {
80 Registry.addEntry(Registry.SECURITY, entry);
81 }
82catch (RegistryException e)
83 {
84 logger.error("Exception", e);
85 }
86 }
8788/***89 @see Registry#removeEntry90 */91publicvoid removeEntry( String name )
92 {
93// Delegate to the RegistryService to ensure correct handling of94// persistence if using file fragments9596 Registry.removeEntry(Registry.SECURITY, name);
97 }
9899/***100 @see Registry#removeEntry101 */102publicvoid removeEntry( RegistryEntry entry )
103 {
104// Delegate to the RegistryService to ensure correct handling of105// persistence if using file fragments106107if (entry != null)
108 {
109 Registry.removeEntry(Registry.SECURITY, entry.getName());
110 }
111 }
112113/***114 * Creates a new RegistryEntry instance compatible with the current115 * Registry instance implementation116 *117 * @return the newly created RegistryEntry118 */119publicRegistryEntry createEntry()
120 {
121returnnewBaseSecurityEntry();
122 }
123124/***125 * returns a security entry from the registry based on the name provided126 * @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 */130publicSecurityEntry getSecurityEntry(String name)
131 {
132try133 {
134return (SecurityEntry) this.getEntry(name);
135 }
136catch (InvalidEntryException e)
137 {
138 logger.error("Exception", e);
139 }
140141returnnull;
142 }
143144/***145 * @return SecurityEntry a new SecurityEntry instance146 */147publicSecurityEntry createSecurityEntry()
148 {
149return (SecurityEntry) this.createEntry();
150 }
151152/***153 * Makes an exact copy of the named role, but changing the nmae attribute154 * to the value of <code>newName</code>155 * @param String original Name of the entry we want to clone156 * @param String newName Name to give the cloned entry157 * @return SecurityEntry The cloned entry.158 */159publicSecurityEntry cloneSecurityEntry(String original, String newName)
160 {
161SecurityEntry baseEntry = getSecurityEntry(original);
162if (baseEntry != null)
163 {
164SecurityEntry newEntry = cloneEntry(baseEntry);
165 newEntry.setName(newName);
166return newEntry;
167 }
168169returnnull;
170 }
171172173/***174 * Makes an indentical copy of the SecurityEntry provided using 175 * serialize/de-serialize logic to make a clean reference176 * @param SecurityEntry secEntry the entry to clone177 * @return SecurityEntry the cloned entry.178 */179privatestaticSecurityEntry cloneEntry(SecurityEntry secEntry)
180 {
181SecurityEntry clonedEntry = null;
182try183 {
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 }
194catch (Exception exception)
195 {
196// nothing197 }
198return clonedEntry;
199 }
200 }