KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > NamespaceAccessTokenImpl


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/NamespaceAccessTokenImpl.java,v 1.28 2004/08/05 15:42:32 unico Exp $
3  * $Revision: 1.28 $
4  * $Date: 2004/08/05 15:42:32 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.common;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 import javax.transaction.HeuristicMixedException JavaDoc;
31 import javax.transaction.HeuristicRollbackException JavaDoc;
32 import javax.transaction.NotSupportedException JavaDoc;
33 import javax.transaction.RollbackException JavaDoc;
34 import javax.transaction.SystemException JavaDoc;
35 import javax.transaction.TransactionManager JavaDoc;
36 import javax.xml.parsers.SAXParser JavaDoc;
37 import javax.xml.parsers.SAXParserFactory JavaDoc;
38
39 import org.apache.slide.content.Content;
40 import org.apache.slide.content.ContentImpl;
41 import org.apache.slide.event.EventDispatcher;
42 import org.apache.slide.event.TransactionEvent;
43 import org.apache.slide.event.VetoException;
44 import org.apache.slide.lock.Lock;
45 import org.apache.slide.lock.LockImpl;
46 import org.apache.slide.macro.Macro;
47 import org.apache.slide.macro.MacroImpl;
48 import org.apache.slide.search.Search;
49 import org.apache.slide.search.SearchImpl;
50 import org.apache.slide.security.ACLSecurityImpl;
51 import org.apache.slide.security.Security;
52 import org.apache.slide.security.SecurityImpl;
53 import org.apache.slide.security.SecurityImplAllGrant;
54 import org.apache.slide.structure.Structure;
55 import org.apache.slide.structure.StructureImpl;
56 import org.apache.slide.util.conf.Configuration;
57 import org.apache.slide.util.conf.ConfigurationElement;
58 import org.apache.slide.util.conf.ConfigurationException;
59 import org.apache.slide.util.conf.Populate;
60 import org.apache.slide.util.logger.Logger;
61 import org.xml.sax.InputSource JavaDoc;
62 import org.xml.sax.SAXException JavaDoc;
63
64
65 /**
66  * Namespace access token implementation.
67  *
68  * @version $Revision: 1.28 $
69  */

70 public final class NamespaceAccessTokenImpl implements NamespaceAccessToken {
71     
72     
73     // ------------------------------------------------------------ Constructor
74

75     private static String JavaDoc ACL_SEMANTICS = "acl_semantics";
76     private static String JavaDoc ALL_GRANT_BEFORE_DENY = "all-grant-before-any-deny";
77     private static String JavaDoc LEGACY_ALL_GRANT_BEFORE_DENY = "legacy-all-grant-before-any-deny";
78     
79     /**
80      * Constructor.
81      *
82      * @param namespace Namespace which can be accessed through this token
83      */

84     NamespaceAccessTokenImpl(Namespace namespace) {
85         this.namespace = namespace;
86         NamespaceConfig config = namespace.getConfig();
87         if (config != null) {
88             String JavaDoc acl_semantics = config.getParameter(ACL_SEMANTICS);
89             if ((acl_semantics != null) && (acl_semantics.equals(LEGACY_ALL_GRANT_BEFORE_DENY ))) {
90                 securityHelper = new SecurityImpl(namespace, namespace.getConfig());
91             } else if((acl_semantics != null) && (acl_semantics.equals(ALL_GRANT_BEFORE_DENY ))) {
92                 securityHelper = new SecurityImplAllGrant(namespace, namespace.getConfig());
93             } else if (acl_semantics != null) {
94                 try {
95                     securityHelper = (Security) Class.forName(acl_semantics).newInstance();
96                     if (securityHelper != null) {
97                         securityHelper.init(namespace, namespace.getConfig());
98                     }
99                 }catch (Exception JavaDoc e) {
100                     e.printStackTrace();
101                 }
102             }
103         }
104         if (securityHelper == null) {
105             securityHelper = new ACLSecurityImpl(namespace, namespace.getConfig());
106         }
107         
108         lockHelper =
109             new LockImpl(namespace, namespace.getConfig(), securityHelper);
110         
111         structureHelper =
112             new StructureImpl(namespace, namespace.getConfig(),
113                               securityHelper, lockHelper);
114         contentHelper =
115             new ContentImpl(namespace, namespace.getConfig(), securityHelper,
116                             structureHelper, lockHelper);
117         searchHelper =
118             new SearchImpl (namespace, namespace.getConfig(),
119                             structureHelper, contentHelper);
120         
121         macroHelper =
122             new MacroImpl(namespace, namespace.getConfig(), securityHelper,
123                           contentHelper, structureHelper, lockHelper);
124     }
125     
126     
127     // ----------------------------------------------------- Instance Variables
128

129     
130     /**
131      * Default namespace associated with this token.
132      */

133     Namespace namespace;
134     
135     
136     /**
137      * Structure helper functions.
138      */

139     private Structure structureHelper;
140     
141     
142     /**
143      * Content helper functions.
144      */

145     private Content contentHelper;
146     
147     
148     /**
149      * Lock helper functions.
150      */

151     private Lock lockHelper;
152     
153     
154     /**
155      * Search helper functions.
156      */

157     private Search searchHelper;
158     
159     
160     /**
161      * Security helper functions.
162      */

163     private Security securityHelper;
164     
165     
166     /**
167      * Macro helper functions.
168      */

169     private Macro macroHelper;
170     
171     
172     // ------------------------------------------------------------- Properties
173

174     
175     /**
176      * Get the structure helper.
177      *
178      * @return Structure helper
179      */

180     public Structure getStructureHelper() {
181         return structureHelper;
182     }
183     
184     
185     /**
186      * Get the content helper.
187      *
188      * @return Content helper
189      */

190     public Content getContentHelper() {
191         return contentHelper;
192     }
193     
194     
195     /**
196      * Get the lock helper.
197      *
198      * @return Lock Lock helper
199      */

200     public Lock getLockHelper() {
201         return lockHelper;
202     }
203     
204     
205     /**
206      * Get the search helper.
207      *
208      * @return Search Search helper
209      */

210     public Search getSearchHelper() {
211         return searchHelper;
212     }
213     
214     
215     /**
216      * Get the security helper.
217      *
218      * @return Security Security helper
219      */

220     public Security getSecurityHelper() {
221         return securityHelper;
222     }
223     
224     
225     /**
226      * Get the macro helper.
227      *
228      * @return Macro Macro helper
229      */

230     public Macro getMacroHelper() {
231         return macroHelper;
232     }
233     
234     
235     /**
236      * Retrive the namespace configuration.
237      *
238      * @return NamespaceConfig Namespace configuration
239      */

240     public NamespaceConfig getNamespaceConfig() {
241         return namespace.getConfig();
242     }
243     
244     
245     /**
246      * Get namespace logger.
247      *
248      * @return The logger associated with the namespace.
249      */

250     public Logger getLogger() {
251         return namespace.getApplicationLogger();
252     }
253     
254     
255     // ------------------------------------------- NamespaceAccessToken Methods
256

257     
258     /**
259      * Import data from Avalon configuration object.
260      *
261      * @param token SlideToken, used for access to the namespace
262      * @param dataConfiguration Configuration object
263      * @exception ConfigurationException Something went wrong during the
264      * reading of the XML
265      * @exception UnknownObjectClassException Object class not found
266      * @exception ServiceAccessException Error accessing service
267      */

268     public void importData(SlideToken token, Configuration dataConfiguration)
269         throws ConfigurationException, UnknownObjectClassException,
270         ServiceAccessException {
271         
272         XMLUnmarshaller.unmarshal(this, token, dataConfiguration);
273         
274     }
275     
276     
277     /**
278      * Import data from reader.
279      *
280      * @param token SlideToken, used for access to the namespace
281      * @param reader Reader
282      * @exception ConfigurationException Something went wrong during the
283      * reading of the XML
284      * @exception UnknownObjectClassException Object class not found
285      * @exception ServiceAccessException Error accessing service
286      */

287     public void importData(SlideToken token, Reader JavaDoc reader)
288         throws ConfigurationException, UnknownObjectClassException,
289         ServiceAccessException, SAXException JavaDoc, IOException JavaDoc {
290         
291         try {
292             
293             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
294             factory.setNamespaceAware(false);
295             factory.setValidating(false);
296             SAXParser JavaDoc parser = factory.newSAXParser();
297             
298             Populate pop = new Populate();
299             Configuration configuration = new ConfigurationElement
300                 (pop.load(new InputSource JavaDoc(reader), parser.getXMLReader()));
301             
302             importData(token, configuration);
303             
304         } catch (javax.xml.parsers.FactoryConfigurationError JavaDoc e) {
305             throw new SlideRuntimeException(e.getMessage());
306         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
307             throw new SlideRuntimeException(e.getMessage());
308         }/* catch (Exception e) {
309          throw new ConfigurationException(e.getMessage());
310          }*/

311         
312     }
313     
314     
315     /**
316      * Saves Slide Data to XML.
317      *
318      * @param writer Writer
319      * @exception SlideException
320      */

321     public void exportData(SlideToken token, Writer JavaDoc writer)
322         throws SlideException {
323         exportData(token, writer, "/");
324     }
325     
326     
327     /**
328      * Saves Slide Data to XML.
329      *
330      * @param writer Writer
331      * @param startNode Start generating XML from this node of the Slide tree
332      * @exception SlideException
333      */

334     public void exportData(SlideToken token, Writer JavaDoc writer,
335                            String JavaDoc startNode)
336         throws SlideException {
337         
338         XMLMarshaller.marshal(this, token, writer, startNode);
339         
340     }
341     
342     
343     /**
344      * Disconnect.
345      */

346     public void disconnect() {
347         try {
348             namespace.disconnectServices();
349         } catch(SlideException e) {
350             e.printStackTrace();
351         }
352     }
353     
354     
355     /**
356      * Get namespace name.
357      *
358      * @return String namespace name.
359      */

360     public String JavaDoc getName() {
361         return namespace.getName();
362     }
363     
364     /**
365      * Builds a new uri object to access this namespace.
366      *
367      * @param token SlideToken
368      * @param uri Requested Uri
369      * @return Uri
370      */

371     public Uri getUri(SlideToken token, String JavaDoc uri) {
372         return namespace.getUri(token, uri);
373     }
374     
375     
376     // ------------------------------------------------ UserTransaction Methods
377

378     
379     /**
380      * Create a new transaction and associate it with the current thread.
381      *
382      * @exception NotSupportedException Thrown if the thread is already
383      * associated with a transaction and the Transaction Manager
384      * implementation does not support nested transactions.
385      * @exception SystemException Thrown if the transaction manager encounters
386      * an unexpected error condition.
387      */

388     public void begin()
389         throws NotSupportedException JavaDoc, SystemException JavaDoc {
390         if ( TransactionEvent.BEGIN.isEnabled() ) EventDispatcher.getInstance().fireEvent(TransactionEvent.BEGIN, new TransactionEvent(this));
391         namespace.getTransactionManager().begin();
392     }
393     
394     
395     /**
396      * Complete the transaction associated with the current thread. When this
397      * method completes, the thread becomes associated with no transaction.
398      *
399      * @exception RollbackException Thrown to indicate that the transaction
400      * has been rolled back rather than committed.
401      * @exception HeuristicMixedException Thrown to indicate that a heuristic
402      * decision was made and that some relevant updates have been committed
403      * while others have been rolled back.
404      * @exception HeuristicRollbackException Thrown to indicate that a
405      * heuristic decision was made and that some relevant updates have been
406      * rolled back.
407      * @exception SecurityException Thrown to indicate that the thread is not
408      * allowed to commit the transaction.
409      * @exception IllegalStateException Thrown if the current thread is not
410      * associated with a transaction.
411      * @exception SystemException Thrown if the transaction manager encounters
412      * an unexpected error condition.
413      */

414     public void commit()
415         throws RollbackException JavaDoc, HeuristicMixedException JavaDoc,
416         HeuristicRollbackException JavaDoc, SecurityException JavaDoc, IllegalStateException JavaDoc,
417         SystemException JavaDoc {
418         try {
419             if ( TransactionEvent.COMMIT.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(TransactionEvent.COMMIT, new TransactionEvent(this));
420         } catch ( VetoException e ) {
421             throw new SystemException JavaDoc(e.getMessage());
422         }
423         namespace.getTransactionManager().commit();
424         if ( TransactionEvent.COMMITED.isEnabled() ) EventDispatcher.getInstance().fireEvent(TransactionEvent.COMMITED, new TransactionEvent(this));
425     }
426     
427     
428     /**
429      * Roll back the transaction associated with the current thread. When
430      * this method completes, the thread becomes associated with no
431      * transaction.
432      *
433      * @exception SecurityException Thrown to indicate that the thread is not
434      * allowed to commit the transaction.
435      * @exception IllegalStateException Thrown if the current thread is not
436      * associated with a transaction.
437      * @exception SystemException Thrown if the transaction manager encounters
438      * an unexpected error condition.
439      */

440     public void rollback()
441         throws SecurityException JavaDoc, IllegalStateException JavaDoc, SystemException JavaDoc {
442         if ( TransactionEvent.ROLLBACK.isEnabled() ) EventDispatcher.getInstance().fireEvent(TransactionEvent.ROLLBACK, new TransactionEvent(this));
443         namespace.getTransactionManager().rollback();
444     }
445     
446     
447     /**
448      * Modify the transaction associated with the current thread such that
449      * the only possible outcome of the transaction is to roll back the
450      * transaction.
451      *
452      * @exception IllegalStateException Thrown if the current thread is not
453      * associated with a transaction.
454      * @exception SystemException Thrown if the transaction manager encounters
455      * an unexpected error condition.
456      */

457     public void setRollbackOnly()
458         throws IllegalStateException JavaDoc, SystemException JavaDoc {
459         namespace.getTransactionManager().setRollbackOnly();
460     }
461     
462     
463     /**
464      * Obtain the status of the transaction associated with the current thread.
465      *
466      * @exception SystemException Thrown if the transaction manager encounters
467      * an unexpected error condition.
468      * @return The transaction status. If no transaction is associated with
469      * the current thread, this method returns the Status.NoTransaction value.
470      */

471     public int getStatus()
472         throws SystemException JavaDoc {
473         return namespace.getTransactionManager().getStatus();
474     }
475     
476     
477     /**
478      * Modify the value of the timeout value that is associated with the
479      * transactions started by the current thread with the begin method.
480      * <p>
481      * If an application has not called this method, the transaction service
482      * uses some default value for the transaction timeout.
483      *
484      * @param seconds The value of the timeout in seconds. If the value is
485      * zero, the transaction service restores the default value.
486      * @exception SystemException Thrown if the transaction manager encounters
487      * an unexpected error condition.
488      */

489     public void setTransactionTimeout(int seconds)
490         throws SystemException JavaDoc {
491         namespace.getTransactionManager().setTransactionTimeout(seconds);
492     }
493     
494     public TransactionManager JavaDoc getTransactionManager() {
495         return namespace.getTransactionManager();
496     }
497     
498 }
499
500
501
Popular Tags