Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

PickleRegistry.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file PickleRegistry.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032 
00033         @version        Initial version, April 2004      
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.io.PickleRegistry;
00040 
00041 private import  mango.io.Exception;
00042         
00043 public  import  mango.io.model.IReader,
00044                 mango.io.model.IPickle;
00045 
00046 /*******************************************************************************
00047 
00048         Bare framework for registering and creating serializable objects.
00049         Such objects are intended to be transported across a local network
00050         and re-instantiated at some destination node. 
00051 
00052         Each IPickle exposes the means to write, or freeze, its content. An
00053         IPickleFactory provides the means to create a new instance of itself
00054         populated with thawed data. Frozen objects are uniquely identified 
00055         by a guid exposed via the interface. Responsibility of maintaining
00056         uniqueness across said identifiers lies in the hands of the developer.
00057 
00058         See PickleReader for an example of how this is expected to operate.
00059 
00060 *******************************************************************************/
00061 
00062 class PickleRegistry
00063 {
00064         private static Proxy[char[]] registry;
00065 
00066         /***********************************************************************
00067         
00068         ***********************************************************************/
00069 
00070         alias Object function (IReader reader) SimpleProxyFnc;
00071         alias Object function (IReader reader, char[] guid) GuidProxyFnc;
00072 
00073         /***********************************************************************
00074         
00075                 This is a singleton: the constructor should not be exposed
00076 
00077         ***********************************************************************/
00078 
00079         private this ()
00080         {
00081         }
00082 
00083         /***********************************************************************
00084         
00085                 Base registrar entry
00086 
00087         ***********************************************************************/
00088 
00089         private static class Proxy
00090         {
00091                 abstract Object create (IReader reader);
00092         }
00093 
00094         /***********************************************************************
00095         
00096                 Simple registrar entry
00097 
00098         ***********************************************************************/
00099 
00100         private static class SimpleProxy : Proxy
00101         {
00102                 SimpleProxyFnc  fnc;
00103 
00104                 /***************************************************************
00105         
00106                 ***************************************************************/
00107               
00108                 this (SimpleProxyFnc fnc)
00109                 {
00110                         this.fnc = fnc;
00111                 }
00112 
00113                 /***************************************************************
00114         
00115                 ***************************************************************/
00116               
00117                 Object create (IReader reader)
00118                 {       
00119                         return fnc (reader);
00120                 }
00121         }
00122 
00123         /***********************************************************************
00124         
00125                 Guid registrar entry
00126 
00127         ***********************************************************************/
00128 
00129         private static class GuidProxy : Proxy
00130         {
00131                 GuidProxyFnc    fnc;
00132                 char[]          guid;
00133 
00134                 /***************************************************************
00135         
00136                 ***************************************************************/
00137               
00138                 this (GuidProxyFnc fnc, char[] guid)
00139                 {
00140                         this.fnc = fnc;
00141                         this.guid = guid;
00142                 }
00143 
00144                 /***************************************************************
00145         
00146                 ***************************************************************/
00147               
00148                 Object create (IReader reader)
00149                 {       
00150                         return fnc (reader, guid);
00151                 }
00152         }
00153 
00154         /***********************************************************************
00155         
00156                 IPickleFactory registrar entry
00157 
00158         ***********************************************************************/
00159 
00160         private static class PickleProxy : Proxy
00161         {
00162                 IPickleFactory  factory;
00163 
00164                 /***************************************************************
00165         
00166                 ***************************************************************/
00167               
00168                 this (IPickleFactory factory)
00169                 {
00170                         this.factory = factory;
00171                 }
00172 
00173                 /***************************************************************
00174         
00175                 ***************************************************************/
00176               
00177                 Object create (IReader reader)
00178                 {       
00179                         return factory.create (reader);
00180                 }
00181         }
00182 
00183         /***********************************************************************
00184         
00185                 Add the provided class to the registry. Note that one
00186                 cannot change a registration once it is placed. Neither
00187                 can one remove registered item. This is done to avoid 
00188                 major issues when trying to synchronize servers across
00189                 a farm, which may still have live instances of "old"
00190                 objects waiting to be passed around the cluster. New
00191                 versions of an object should be given a distinct guid
00192                 from the prior version; appending an incremental number 
00193                 may well be sufficient for your needs.
00194 
00195         ***********************************************************************/
00196 
00197         private static synchronized void enroll (Proxy proxy, char[] guid)
00198         {
00199                 if (guid in registry)
00200                     throw new PickleException ("Invalid attempt to re-register a guid");
00201         
00202                 registry[guid] = proxy;
00203         }
00204 
00205         /***********************************************************************
00206         
00207                 do a synchronized Proxy lookup of the guid
00208 
00209         ***********************************************************************/
00210 
00211         private final static synchronized Proxy lookup (char[] guid)
00212         {
00213                 Proxy* p = guid in registry;
00214                 if (p)
00215                     return *p;
00216                 return null;
00217         }
00218 
00219         /***********************************************************************
00220         
00221                 Add the provided object to the registry. Note that one
00222                 cannot change a registration once it is placed. Neither
00223                 can one remove registered item. This is done to avoid 
00224                 major issues when trying to synchronize servers across
00225                 a farm, which may still have live instances of "old"
00226                 objects waiting to be passed around the cluster. New
00227                 versions of an object should be given a distinct guid
00228                 from the prior version; appending an incremental number 
00229                 may well be sufficient for your needs.
00230 
00231         ***********************************************************************/
00232 
00233         static void enroll (IPickleFactory object)
00234         {
00235                 enroll (new PickleProxy(object), object.getGuid);
00236         }
00237 
00238         /***********************************************************************
00239         
00240                 Add the provided function to the registry. Note that one
00241                 cannot change a registration once it is placed. Neither
00242                 can one remove registered item. This is done to avoid 
00243                 major issues when trying to synchronize servers across
00244                 a farm, which may still have live instances of "old"
00245                 objects waiting to be passed around the cluster. New
00246                 versions of an object should be given a distinct guid
00247                 from the prior version; appending an incremental number 
00248                 may well be sufficient for your needs.
00249 
00250         ***********************************************************************/
00251 
00252         static void enroll (SimpleProxyFnc sp, char[] guid)
00253         {
00254                 enroll (new SimpleProxy(sp), guid);
00255         }
00256 
00257         /***********************************************************************
00258         
00259                 Add the provided function to the registry. Note that one
00260                 cannot change a registration once it is placed. Neither
00261                 can one remove registered item. This is done to avoid 
00262                 major issues when trying to synchronize servers across
00263                 a farm, which may still have live instances of "old"
00264                 objects waiting to be passed around the cluster. New
00265                 versions of an object should be given a distinct guid
00266                 from the prior version; appending an incremental number 
00267                 may well be sufficient for your needs.
00268 
00269         ***********************************************************************/
00270 
00271         static void enroll (GuidProxyFnc gp, char[] guid)
00272         {
00273                 enroll (new GuidProxy(gp, guid), guid);
00274         }
00275 
00276         /***********************************************************************
00277         
00278                 Create a new instance of a registered class from the content
00279                 made available via the given reader. The factory is located
00280                 using the provided guid, which must match an enrolled class.
00281 
00282                 Note that only the factory lookup is synchronized, and not 
00283                 the instance construction itself.
00284 
00285         ***********************************************************************/
00286 
00287         static Object create (IReader reader, char[] guid)
00288         {
00289                 // locate the appropriate Proxy. 
00290                 Proxy p = lookup (guid);
00291                 if (p)
00292                     return p.create (reader);
00293 
00294                 throw new PickleException ("Attempt to unpickle via unregistered guid '"~guid~"'");
00295         }
00296 }
00297 

Generated on Sat Dec 24 17:28:33 2005 for Mango by  doxygen 1.4.0