coherence/io/pof/SystemPofContext.hpp

00001 /*
00002 * SystemPofContext.hpp
00003 *
00004 * Copyright 2001-2008 by Oracle. 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_SYSTEM_POF_CONTEXT_HPP
00017 #define COH_SYSTEM_POF_CONTEXT_HPP
00018 
00019 #include "coherence/lang.ns"
00020 
00021 #include "coherence/io/ReadBuffer.hpp"
00022 #include "coherence/io/WriteBuffer.hpp"
00023 #include "coherence/io/pof/PofContext.hpp"
00024 #include "coherence/io/pof/PofSerializer.hpp"
00025 #include "coherence/io/pof/PortableObjectSerializer.hpp"
00026 #include "coherence/io/pof/TypedSerializer.hpp"
00027 #include "coherence/util/LongArray.hpp"
00028 #include "coherence/util/Map.hpp"
00029 #include "coherence/util/ThreadGate.hpp"
00030 
00031 COH_OPEN_NAMESPACE3(coherence,io,pof)
00032 
00033 using coherence::io::ReadBuffer;
00034 using coherence::io::WriteBuffer;
00035 using coherence::util::LongArray;
00036 using coherence::util::Map;
00037 using coherence::util::ThreadGate;
00038 
00039 
00040 /**
00041 * System-wide {@link PofContext} implementation that allows POF user types to
00042 * be registered programatically.
00043 *
00044 * @author jh  2008.02.21
00045 */
00046 class COH_EXPORT SystemPofContext
00047     : public class_spec<SystemPofContext,
00048         extends<Object>,
00049         implements<PofContext> >
00050     {
00051     friend class factory<SystemPofContext>;
00052 
00053     // ----- constructors ---------------------------------------------------
00054 
00055     private:
00056         /**
00057         * @internal
00058         */
00059         using this_spec::create;
00060 
00061         /**
00062         * @internal
00063         */
00064         SystemPofContext();
00065 
00066 
00067     // ----- SystemPofContext interface -------------------------------------
00068 
00069     public:
00070         /**
00071         * Associate a user type with a type identifier and
00072         * {@link PofSerializer}.
00073         *
00074         * @param nTypeId      the type identifier of the specified user type;
00075         *                     must be greater or equal to 0
00076         * @param vClass       the user type to register with this PofContext;
00077         *                     must not be NULL
00078         * @param vSerializer  the PofSerializer that will be used to
00079         *                     serialize and deserialize objects of the
00080         *                     specified class; must not be NULL
00081         *
00082         * @throws coherence::lang::IllegalArgumentException on invalid type
00083         *         identifer, class, or PofSerializer
00084         */
00085         virtual void registerUserType(int32_t nTypeId, Class::View vClass,
00086                 PofSerializer::View vSerializer);
00087 
00088         /**
00089         * Unregister a user type previously registered using the specified
00090         * type identifier.
00091         *
00092         * @param nTypeId  the type identifier of the user type to unregister
00093         *
00094         * @throws coherence::lang::IllegalArgumentException  if the specified
00095         *         user type identifier is unknown to this PofContext
00096         */
00097         virtual void unregisterUserType(int32_t nTypeId);
00098 
00099 
00100     // ----- PofContext interface -------------------------------------------
00101 
00102     public:
00103         /**
00104         * {@inheritDoc}
00105         */
00106         virtual PofSerializer::View getPofSerializer(int32_t nTypeId) const;
00107 
00108         /**
00109         * {@inheritDoc}
00110         */
00111         virtual int32_t getUserTypeIdentifier(Object::View v) const;
00112 
00113         /**
00114         * {@inheritDoc}
00115         */
00116         virtual int32_t getUserTypeIdentifier(Class::View vClass) const;
00117 
00118         /**
00119         * {@inheritDoc}
00120         */
00121         virtual int32_t getUserTypeIdentifier(String::View vsClass) const;
00122 
00123         /**
00124         * {@inheritDoc}
00125         */
00126         virtual String::View getClassName(int32_t nTypeId) const;
00127 
00128         /**
00129         * {@inheritDoc}
00130         */
00131         virtual Class::View getClass(int32_t nTypeId) const;
00132 
00133         /**
00134         * {@inheritDoc}
00135         */
00136         virtual bool isUserType(Object::View v) const;
00137 
00138         /**
00139         * {@inheritDoc}
00140         */
00141         virtual bool isUserType(Class::View vClass) const;
00142 
00143         /**
00144         * {@inheritDoc}
00145         */
00146         virtual bool isUserType(String::View vsClass) const;
00147 
00148 
00149     // ----- Serializer interface -------------------------------------------
00150 
00151     public:
00152         /**
00153         * {@inheritDoc}
00154         */
00155         virtual void serialize(WriteBuffer::BufferOutput::Handle hOut,
00156                 Object::View v) const;
00157 
00158         /**
00159         * {@inheritDoc}
00160         */
00161         virtual Object::Holder deserialize(
00162                 ReadBuffer::BufferInput::Handle hIn) const;
00163 
00164 
00165     // ----- Object interface -----------------------------------------------
00166 
00167     public:
00168         /**
00169         * {@inheritDoc}
00170         */
00171         virtual void toStream(std::ostream& out) const;
00172 
00173 
00174     // ----- internal methods -----------------------------------------------
00175 
00176     protected:
00177         /**
00178         * Ensure that the given user type identifier is valid.
00179         *
00180         * @param nTypeId  the user type identifier to validate
00181         *
00182         * @throws coherence::lang::IllegalArgumentException if the given user
00183         *         type identifier is negative
00184         */
00185         virtual void validateTypeId(int32_t nTypeId) const;
00186 
00187 
00188     // ----- data members ---------------------------------------------------
00189 
00190     protected:
00191         /**
00192         * A LongArray of user types, indexed by type identifier.
00193         */
00194         const LongArray::Handle m_hlaClass;
00195 
00196         /**
00197         * A LongArray of PofSerializer objects, indexed by type identifier.
00198         */
00199         const LongArray::Handle m_hlaSerializer;
00200 
00201         /**
00202         * A Map that contains mappings from a registered user type to type
00203         * identifier.
00204         */
00205         const Map::Handle m_hMapTypeId;
00206 
00207         /**
00208         * ThreadGate used to synchronize access to this PofContext.
00209         */
00210         mutable ThreadGate::Handle m_hGate;
00211 
00212 
00213     // ----- constants ------------------------------------------------------
00214 
00215     public:
00216         /**
00217         * Return the SystemPofContext singleton instance.
00218         *
00219         * @return the global SystemPofContext
00220         */
00221         static Handle getInstance();
00222     };
00223 
00224 
00225 // ----- helper macros ------------------------------------------------------
00226 
00227 /**
00228 * Register a PofSerializer with the system PofContext. The call will also
00229 * register the class with the SystemClassLoader.
00230 *
00231 * @param POF_TYPE_ID     the POF type identifier
00232 * @param CLASS           the associated class to register
00233 * @param POF_SERIALIZER  the PofSerializer instance to register
00234 */
00235 #define COH_REGISTER_POF_SERIALIZER(POF_TYPE_ID, CLASS, POF_SERIALIZER) \
00236     COH_STATIC_INIT(coherence::io::pof::SystemPofContext::getInstance()-> \
00237         registerUserType(POF_TYPE_ID, \
00238             coherence::lang::SystemClassLoader::getInstance()-> \
00239             registerClass(CLASS), POF_SERIALIZER))
00240 
00241 /**
00242 * Register a PortableObject implementation with the system ClassLoader and
00243 * PofContext.
00244 *
00245 * @param POF_TYPE_ID  the POF type identifier
00246 * @param TYPE         the class type to register
00247 */
00248 #define COH_REGISTER_PORTABLE_CLASS(POF_TYPE_ID, TYPE) \
00249     COH_REGISTER_POF_SERIALIZER(POF_TYPE_ID, \
00250         coherence::lang::TypedClass<TYPE >::create(), \
00251         coherence::io::pof::PortableObjectSerializer::create(POF_TYPE_ID))
00252 
00253 /**
00254 * Register a Managed object implementation with the system ClassLoader and
00255 * PofContext.
00256 *
00257 * @param POF_TYPE_ID  the POF type identifier
00258 * @param TYPE         the class type to register
00259 */
00260 #define COH_REGISTER_MANAGED_CLASS(POF_TYPE_ID, TYPE) \
00261     COH_REGISTER_POF_SERIALIZER(POF_TYPE_ID, \
00262         coherence::lang::TypedClass<coherence::lang::Managed<TYPE > >::create(), \
00263         coherence::io::pof::TypedSerializer<coherence::lang::Managed<TYPE > >::create())
00264 
00265 COH_CLOSE_NAMESPACE3
00266 
00267 #endif // COH_SYSTEM_POF_CONTEXT_HPP
Copyright (c) 2000-2008 Oracle. All rights reserved.