00001 /* 00002 * RunAsBlock.hpp 00003 * 00004 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 00005 * 00006 * Oracle is a registered trademarks of Oracle Corporation and/or its 00007 * affiliates. 00008 * 00009 * This software is the confidential and proprietary information of Oracle 00010 * Corporation. You shall not disclose such confidential and proprietary 00011 * information and shall use it only in accordance with the terms of the 00012 * license agreement you entered into with Oracle. 00013 * 00014 * This notice may not be removed or altered. 00015 */ 00016 #ifndef COH_RUN_AS_BLOCK_HPP 00017 #define COH_RUN_AS_BLOCK_HPP 00018 00019 #include "coherence/lang.ns" 00020 00021 #include "coherence/security/auth/Subject.hpp" 00022 00023 COH_OPEN_NAMESPACE2(coherence,security) 00024 00025 COH_OPEN_NAMESPACE(auth) 00026 class Subject; 00027 COH_CLOSE_NAMESPACE 00028 00029 using coherence::security::auth::Subject; 00030 00031 /** 00032 * The RunAsBlock class allows for easy creation of scoped authorization code 00033 * blocks based on a Subject. The RunAsBlock object will 00034 * push the Subject onto a thread-local "current subject" upon entering the 00035 * block, and pop the subject upon exiting the code block. 00036 * 00037 * A more friendly form is to use the COH_RUN_AS macro. Example usage: 00038 * 00039 * COH_RUN_AS(vSubject) 00040 * { 00041 * // code here will considered to be run by the supplied subject 00042 * // ... 00043 * // ... 00044 * } // subject will be reverted 00045 * 00046 * @author mf 2008.08.22 00047 */ 00048 class COH_EXPORT RunAsBlock 00049 { 00050 // ----- constructors --------------------------------------------------- 00051 00052 public: 00053 /** 00054 * Construct a RunAsBlock object. 00055 * 00056 * This will automatically set the thread's Subject. 00057 */ 00058 RunAsBlock(TypedHandle<const Subject> vSubject); 00059 00060 /** 00061 * Copy constructor, for use by COH_RUN_AS macro. 00062 * 00063 * The new block takes over the ownership of the pop. 00064 */ 00065 RunAsBlock(const RunAsBlock& that); 00066 00067 /** 00068 * Destroy a RunAsBlock object. 00069 * 00070 * This will automatically pop the Subject. 00071 */ 00072 ~RunAsBlock(); 00073 00074 00075 // ----- operators ------------------------------------------------------ 00076 00077 public: 00078 /* 00079 * Boolean conversion for use in COH_RUN_AS macro. 00080 * 00081 * @return false always 00082 */ 00083 operator bool() const; 00084 00085 private: 00086 /** 00087 * Blocked assignment operator. 00088 */ 00089 const RunAsBlock& operator=(const RunAsBlock&); 00090 00091 /** 00092 * Blocked dynamic allocation. 00093 */ 00094 static void* operator new(size_t); 00095 00096 00097 // ----- data members --------------------------------------------------- 00098 00099 private: 00100 /** 00101 * Subject associated with the block. 00102 */ 00103 mutable TypedHandle<const Subject> m_vSubject; // on stack 00104 }; 00105 00106 COH_CLOSE_NAMESPACE2 00107 00108 00109 /** 00110 * Macro for making more readable run-as code blocks See the documentation of 00111 * RunAsBlock for a usage example. 00112 * 00113 * @see coherence::security::RunAsBlock 00114 */ 00115 #define COH_RUN_AS(VSUBJECT) \ 00116 if (coherence::security::RunAsBlock COH_UNIQUE_IDENTIFIER(_coh_runas_) \ 00117 = coherence::security::RunAsBlock(VSUBJECT)) \ 00118 { \ 00119 COH_THROW(coherence::lang::IllegalStateException::create()); \ 00120 } \ 00121 else 00122 00123 #endif // COH_RUN_AS_BLOCK_HPP