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 class Proxy
00090         {
00091                 abstract Object create (IReader reader);
00092         }
00093 
00094         /***********************************************************************
00095         
00096                 Simple registrar entry
00097 
00098         ***********************************************************************/
00099 
00100         private 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 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 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                 if (guid in registry)
00214                     return registry [guid];
00215                 return null;
00216         }
00217 
00218         /***********************************************************************
00219         
00220                 Add the provided object to the registry. Note that one
00221                 cannot change a registration once it is placed. Neither
00222                 can one remove registered item. This is done to avoid 
00223                 major issues when trying to synchronize servers across
00224                 a farm, which may still have live instances of "old"
00225                 objects waiting to be passed around the cluster. New
00226                 versions of an object should be given a distinct guid
00227                 from the prior version; appending an incremental number 
00228                 may well be sufficient for your needs.
00229 
00230         ***********************************************************************/
00231 
00232         static void enroll (IPickleFactory object)
00233         {
00234                 enroll (new PickleProxy(object), object.getGuid);
00235         }
00236 
00237         /***********************************************************************
00238         
00239                 Add the provided function to the registry. Note that one
00240                 cannot change a registration once it is placed. Neither
00241                 can one remove registered item. This is done to avoid 
00242                 major issues when trying to synchronize servers across
00243                 a farm, which may still have live instances of "old"
00244                 objects waiting to be passed around the cluster. New
00245                 versions of an object should be given a distinct guid
00246                 from the prior version; appending an incremental number 
00247                 may well be sufficient for your needs.
00248 
00249         ***********************************************************************/
00250 
00251         static void enroll (SimpleProxyFnc sp, char[] guid)
00252         {
00253                 enroll (new SimpleProxy(sp), guid);
00254         }
00255 
00256         /***********************************************************************
00257         
00258                 Add the provided function to the registry. Note that one
00259                 cannot change a registration once it is placed. Neither
00260                 can one remove registered item. This is done to avoid 
00261                 major issues when trying to synchronize servers across
00262                 a farm, which may still have live instances of "old"
00263                 objects waiting to be passed around the cluster. New
00264                 versions of an object should be given a distinct guid
00265                 from the prior version; appending an incremental number 
00266                 may well be sufficient for your needs.
00267 
00268         ***********************************************************************/
00269 
00270         static void enroll (GuidProxyFnc gp, char[] guid)
00271         {
00272                 enroll (new GuidProxy(gp, guid), guid);
00273         }
00274 
00275         /***********************************************************************
00276         
00277                 Create a new instance of a registered class from the content
00278                 made available via the given reader. The factory is located
00279                 using the provided guid, which must match an enrolled class.
00280 
00281                 Note that only the factory lookup is synchronized, and not 
00282                 the instance construction itself.
00283 
00284         ***********************************************************************/
00285 
00286         static Object create (IReader reader, char[] guid)
00287         {
00288                 // locate the appropriate Proxy. 
00289                 Proxy p = lookup (guid);
00290                 if (p)
00291                     return p.create (reader);
00292 
00293                 throw new PickleException ("Attempt to unpickle via unregistered guid '"~guid~"'");
00294         }
00295 }
00296 

Generated on Fri May 27 18:11:56 2005 for Mango by  doxygen 1.4.0