Show
Ignore:
Timestamp:
11/23/08 02:34:34 (4 years ago)
Author:
lmajano
Message:

Preliminary getModel() implemented.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • coldbox/trunk/system/plugins/beanFactory.cfc

    r1880 r1891  
    6565                        } 
    6666                </cfscript> 
     67        </cffunction> 
     68         
     69        <!--- Get Model ---> 
     70        <cffunction name="getModel" access="public" returntype="any" hint="Create or retrieve model objects by convention" output="false" > 
     71                <!--- ************************************************************* ---> 
     72                <cfargument name="name"                                 required="true"  type="string" hint="The name of the model to retrieve"> 
     73                <cfargument name="useSetterInjection"   required="false" type="boolean" default="false" hint="Whether to use setter injection alongside the annotations property injection. cfproperty injection takes precedence."> 
     74                <cfargument name="onDICompleteUDF"              required="false" type="string"  default="onDIComplete" hint="After Dependencies are injected, this method will look for this UDF and call it if it exists. The default value is onDIComplete"> 
     75                <cfargument name="debugMode"                    required="false" type="boolean" default="false" hint="Debugging Mode or not"> 
     76                <!--- ************************************************************* ---> 
     77                <cfscript> 
     78                        var oModel = 0; 
     79                        var ModelsPath = getSetting("ModelsPath"); 
     80                        var ModelsInvocationPath = getSetting("ModelsInvocationPath"); 
     81                        var checkPath = ModelsPath & "/" & replace(arguments.name,".","/","all") & ".cfc"; 
     82                        var modelClassPath = ModelsInvocationPath & "." & arguments.name; 
     83                        var md = 0; 
     84                </cfscript> 
     85                 
     86                 
     87                <!--- Verify if model exists in cache ---> 
     88                <cfif ( getColdboxOCM().lookup(arguments.name) )> 
     89                        <cfset oModel = getColdBoxOCM().get(arguments.name)> 
     90                <!--- Else Create It ---> 
     91                <cfelseif ( fileExists(checkPath) )> 
     92                        <cflock name="beanfactory.createmodel.#arguments.name#" type="exclusive" timeout="20" throwontimeout="true"> 
     93                                <cfscript> 
     94                                if( fileExists(checkPath) ){ 
     95                                        /* Create the model object */ 
     96                                        oModel = createObject("component", modelClassPath); 
     97                                        /* Verify Init() and execute */ 
     98                                        if( structKeyExists(oModel,"init") ){ 
     99                                                oModel.init(); 
     100                                        } 
     101                                        /* Caching Metadata */ 
     102                                        md = getMetadata(oModel); 
     103                                        if( not structKeyExists(md,"cache") or not isBoolean(md.cache) ){ 
     104                                                md.cache = false; 
     105                                        } 
     106                                        /* Are we Caching? */ 
     107                                        if( md.cache ){ 
     108                                                /* Prepare Timeouts and info. */ 
     109                                                if( not structKeyExists(md,"cachetimeout") or not isNumeric(md.cacheTimeout) ){ 
     110                                                        md.cacheTimeout = ""; 
     111                                                } 
     112                                                if( not structKeyExists(md,"cacheLastAccessTimeout") or not isNumeric(md.cacheLastAccessTimeout) ){ 
     113                                                        md.cacheLastAccessTimeout = ""; 
     114                                                } 
     115                                                /* Cache This Puppy. */ 
     116                                                getColdBoxOCM().set(arguments.name,oModel,md.cacheTimeout,md.CacheLastAccessTimeout); 
     117                                        } 
     118                                         
     119                                        /* Autowire Dependencies */ 
     120                                        autowire(target=oModel, 
     121                                                         useSetterInjection=arguments.useSetterInjection, 
     122                                                         annotationCheck=false, 
     123                                                         onDICompleteUDF=arguments.onDICompleteUDF, 
     124                                                         debugMode=arguments.debugmode); 
     125                                } 
     126                                </cfscript> 
     127                        </cflock> 
     128                <cfelse> 
     129                        <cfset throw("Model Not Found","The model path is not valid: #checkPath#","plugin.beanFactory.modelNotFoundException")> 
     130                </cfif> 
     131                 
     132                <cfreturn oModel> 
    67133        </cffunction> 
    68134 
     
    254320                        /* Dependencies Length */ 
    255321                        dependenciesLength = arrayLen(targetDIEntry.dependencies); 
    256                          
    257322                        /* References */ 
    258323                        oMethodInjector = getPlugin("methodInjector"); 
    259                         oIOC = getPlugin("ioc"); 
    260                         oOCM = getColdboxOCM(); 
    261                          
    262324                        /* Let's inject our mixins */ 
    263325                        oMethodInjector.start(targetObject); 
    264                          
    265326                        /* Loop over dependencies and inject. */ 
    266327                        for(x=1; x lte dependenciesLength; x=x+1){ 
     
    270331                                /* Determine Type of Injection */ 
    271332                                if( thisDependency.type eq "ioc" ){ 
    272                                         /* Verify that bean exists in the IOC container. */ 
    273                                         if( oIOC.getIOCFactory().containsBean(thisDependency.name) ){ 
    274                                                  
    275                                                 /* Inject dependency */ 
    276                                                 injectBean(targetBean=targetObject, 
    277                                                                    beanName=thisDependency.name, 
    278                                                                    beanObject=oIOC.getBean(thisDependency.name), 
    279                                                                    scope=thisDependency.scope); 
    280                                                  
    281                                                 /* Debug Mode Check */ 
    282                                                 if( arguments.debugMode ){ 
    283                                                         getPlugin("logger").logEntry("information","Dependency: #thisDependency.toString()# --> injected into #targetCacheKey#."); 
    284                                                 } 
    285                                         } 
    286                                         else if( arguments.debugMode ){ 
    287                                                 getPlugin("logger").logEntry("warning","Dependency: #thisDependency.toString()# --> not found in factory"); 
    288                                         } 
    289                                 }//end if IOC Injection 
    290                                 //Else its OCM injection 
     333                                        injectIOC(thisDependency,targetObject,arguments.debugMode); 
     334                                } 
     335                                else if (thisDependency.type eq "ocm"){ 
     336                                        injectOCM(thisDependency,targetObject,arguments.debugMode); 
     337                                } 
    291338                                else{ 
    292                                         /* Verify that bean exists in the Cache container. */ 
    293                                         if( oOCM.lookup(thisDependency.name) ){ 
    294                                                  
    295                                                 /* Inject dependency */ 
    296                                                 injectBean(targetBean=targetObject, 
    297                                                                    beanName=thisDependency.name, 
    298                                                                    beanObject=oOCM.get(thisDependency.name), 
    299                                                                    scope=thisDependency.scope); 
    300                                                  
    301                                                 /* Debug Mode Check */ 
    302                                                 if( arguments.debugMode ){ 
    303                                                         getPlugin("logger").logEntry("information","Dependency: #thisDependency.toString()# --> injected into #targetCacheKey#."); 
    304                                                 } 
    305                                         } 
    306                                         else if( arguments.debugMode ){ 
    307                                                 getPlugin("logger").logEntry("warning","Dependency: #thisDependency.toString()# --> not found in factory"); 
    308                                         } 
    309                                 }//end if OCM injection                                                  
     339                                        /* Try to inject coldbox dependencies */ 
     340                                        injectColdboxDSL(thisDependency,targetObject,arguments.debugMode); 
     341                                }                                                        
    310342                                 
    311343                        }//end for loop of dependencies. 
    312                          
    313344                        /* Process After ID Complete */ 
    314345                        processAfterCompleteDI(targetObject,onDICompleteUDF); 
    315                          
    316346                        /* Let's cleanup our mixins */ 
    317347                        getPlugin("methodInjector").stop(targetObject); 
     
    321351        </cffunction> 
    322352         
     353         
    323354<!------------------------------------------- PRIVATE -------------------------------------------> 
    324  
     355         
     356        <!--- injectColdboxDSL ---> 
     357        <cffunction name="injectColdboxDSL" access="private" returntype="void" hint="Inject dependencies using the coldbox dependency DSL" output="false" > 
     358                <!--- ************************************************************* ---> 
     359                <cfargument name="Definition"   required="true" type="any" hint="The dependency definition structure"> 
     360                <cfargument name="targetObject" required="true" type="any" hint="The target object"> 
     361                <cfargument name="debugMode"    required="true" type="boolean" hint="The debug mode"> 
     362                <!--- ************************************************************* ---> 
     363                <cfscript> 
     364                        var thisDependency = arguments.Definition; 
     365                        var thisType = thisDependency.type; 
     366                        var thisTypeLen = listLen(thisType,":"); 
     367                        var thisLocationType = ""; 
     368                        var thisLocationKey = ""; 
     369                        var locatedDependency = "_NOT_FOUND_"; 
     370                         
     371                        /* 1 stage dependency */ 
     372                        if( thisTypeLen eq 1 ){ 
     373                                /* Coldbox Reference is the only one available */ 
     374                                locatedDependency = getController(); 
     375                        } 
     376                        /* 2 stage dependencies */ 
     377                        else if(thisTypeLen eq 2){ 
     378                                /* configBean or mailsettingsbean */ 
     379                                thisLocationType = listLast(thisType,":"); 
     380                                if( thisLocationType eq "configbean" ){ 
     381                                        locatedDependency = getSettingsBean(); 
     382                                }        
     383                                else if( thisLocationType eq "mailsettingsbean" ){ 
     384                                        locatedDependency = getMailSettings(); 
     385                                } 
     386                        } 
     387                        /* 3 stage dependencies */ 
     388                        else if(thisTypeLen eq 3){ 
     389                                thisLocationType = getToken(thisType,2,":"); 
     390                                thisLocationKey = getToken(thisType,3,":"); 
     391                                /* Fork on types */ 
     392                                if( thisLocationType eq "setting" ){ 
     393                                        locatedDependency = getSetting(thisLocationKey); 
     394                                } 
     395                                else if( thisLocationType eq "plugin" ){ 
     396                                        locatedDependency = getPlugin(thisLocationKey); 
     397                                } 
     398                                else if( thisLocationType eq "myplugin" ){ 
     399                                        locatedDependency = getMyPlugin(thisLocationKey); 
     400                                } 
     401                                else if( thisLocationType eq "datasource" ){ 
     402                                        locatedDependency = getDatasource(thisLocationKey); 
     403                                } 
     404                                else if( thisLocationType eq "model" ){ 
     405                                        locatedDependency = getModel(thisLocationKey); 
     406                                } 
     407                        }//end 3 stage DSL 
     408                         
     409                        /* Verify injetion */ 
     410                        if( isSimpleValue(locatedDependency) AND locatedDependency EQ "_NOT_FOUND_" ){ 
     411                                /* Only log if debugmode, else no injection */ 
     412                                if( arguments.debugMode ){ 
     413                                        getPlugin("logger").logEntry("warning","Dependency: #thisDependency.toString()# --> not found in factory"); 
     414                                } 
     415                        } 
     416                        else{ 
     417                                /* Inject Dependency */ 
     418                                injectBean(targetBean=arguments.targetObject, 
     419                                                   beanName=thisDependency.name, 
     420                                                   beanObject=locatedDependency, 
     421                                                   scope=thisDependency.scope); 
     422                                /* Debug Mode Check */ 
     423                                if( arguments.debugMode ){ 
     424                                        getPlugin("logger").logEntry("information","Dependency: #thisDependency.toString()# --> injected into #getMetadata(arguments.targetObject).name#."); 
     425                                } 
     426                        } 
     427                </cfscript> 
     428        </cffunction> 
     429 
     430        <!--- injectIOC ---> 
     431        <cffunction name="injectIOC" access="private" returntype="void" hint="Inject a bean with IOC dependencies" output="false" > 
     432                <!--- ************************************************************* ---> 
     433                <cfargument name="Definition"   required="true" type="any" hint="The dependency definition structure"> 
     434                <cfargument name="targetObject" required="true" type="any" hint="The target object"> 
     435                <cfargument name="debugMode"    required="true" type="boolean" hint="The debug mode"> 
     436                <!--- ************************************************************* ---> 
     437                <cfscript> 
     438                        var oIOC = getPlugin("ioc"); 
     439                        var thisDependency = arguments.Definition; 
     440                        /* Verify that bean exists in the IOC container. */ 
     441                        if( oIOC.getIOCFactory().containsBean(thisDependency.name) ){ 
     442                                /* Inject dependency */ 
     443                                injectBean(targetBean=arguments.targetObject, 
     444                                                   beanName=thisDependency.name, 
     445                                                   beanObject=oIOC.getBean(thisDependency.name), 
     446                                                   scope=thisDependency.scope); 
     447                                 
     448                                /* Debug Mode Check */ 
     449                                if( arguments.debugMode ){ 
     450                                        getPlugin("logger").logEntry("information","Dependency: #thisDependency.toString()# --> injected into #getMetadata(arguments.targetObject).name#."); 
     451                                } 
     452                        } 
     453                        else if( arguments.debugMode ){ 
     454                                getPlugin("logger").logEntry("warning","Dependency: #thisDependency.toString()# --> not found in factory"); 
     455                        } 
     456                </cfscript> 
     457        </cffunction> 
     458         
     459        <!--- injectOCM ---> 
     460        <cffunction name="injectOCM" access="private" returntype="void" hint="Inject a bean with OCM dependencies" output="false" > 
     461                <!--- ************************************************************* ---> 
     462                <cfargument name="Definition"   required="true" type="any" hint="The dependency definition structure"> 
     463                <cfargument name="targetObject" required="true" type="any" hint="The target object"> 
     464                <cfargument name="debugMode"    required="true" type="boolean" hint="The debug mode"> 
     465                <!--- ************************************************************* ---> 
     466                <cfscript> 
     467                        var oOCM = getColdboxOCM(); 
     468                        var thisDependency = arguments.Definition; 
     469                        /* Verify that bean exists in the Cache container. */ 
     470                        if( oOCM.lookup(thisDependency.name) ){ 
     471                                 
     472                                /* Inject dependency */ 
     473                                injectBean(targetBean=arguments.targetObject, 
     474                                                   beanName=thisDependency.name, 
     475                                                   beanObject=oOCM.get(thisDependency.name), 
     476                                                   scope=thisDependency.scope); 
     477                                 
     478                                /* Debug Mode Check */ 
     479                                if( arguments.debugMode ){ 
     480                                        getPlugin("logger").logEntry("information","Dependency: #thisDependency.toString()# --> injected into #getMetadata(arguments.targetObject).name#."); 
     481                                } 
     482                        } 
     483                        else if( arguments.debugMode ){ 
     484                                getPlugin("logger").logEntry("warning","Dependency: #thisDependency.toString()# --> not found in factory"); 
     485                        } 
     486                </cfscript> 
     487        </cffunction> 
     488         
    325489        <!--- Get an object's dependencies via metadata ---> 
    326490        <cffunction name="parseMetadata" returntype="array" access="private" output="false" hint="I get a components dependencies via searching for 'setters'"> 
     
    336500                        var entry = structnew(); 
    337501                        var cbox_reserved_functions = "setSetting,setDebugMode,setNextEvent,setNextRoute,setController,settingExists,setPluginName,setPluginVersion,setPluginDescription,setProperty,setproperties"; 
     502                        var foundDependencies = ""; 
     503                         
     504                        /* Look for Object's attributes, and override if found. */ 
     505                        if( structKeyExists(md,"autowire_stoprecursion") ){ 
     506                                arguments.stopRecursion = md["autowire_stoprecursion"]; 
     507                        } 
     508                        if( structKeyExists(md,"autowire_setterinjection") and isBoolean(md["autowire_setterinjection"]) ){ 
     509                                arguments.useSetterInjection = md["autowire_setterinjection"]; 
     510                        } 
    338511                         
    339512                        /* Look For cfProperties */ 
    340513                        if( structKeyExists(md,"properties") and ArrayLen(md.properties) gt 0){ 
    341514                                for(x=1; x lte ArrayLen(md.properties); x=x+1 ){ 
    342                                         /* Check if type is ioc */ 
    343                                         if( structKeyExists(md.properties[x],"type") and (md.properties[x].type eq "ioc" or md.properties[x].type eq "ocm") ){ 
     515                                        /* Check types */ 
     516                                        if( structKeyExists(md.properties[x],"type") AND  
     517                                           (md.properties[x].type eq "ioc" OR md.properties[x].type eq "ocm" OR findNoCase("coldbox",md.properties[x].type)) ){ 
    344518                                                /* New MD Entry */ 
    345519                                                entry = structnew(); 
     
    349523                                                }                
    350524                                                /* Setup Entry */ 
    351                                                 entry.name      = replace(md.properties[x].name,".","_","all"); 
     525                                                entry.name      = md.properties[x].name; 
    352526                                                entry.scope = md.properties[x].scope; 
    353527                                                entry.type      = md.properties[x].type; 
     528                                                 
     529                                                /* Add to found list */ 
     530                                                listAppend(foundDependencies,entry.name); 
    354531                                                 
    355532                                                /* Add Property Dependency */ 
     
    364541                                for(x=1; x lte ArrayLen(md.functions); x=x+1 ){ 
    365542                                        /* Verify we have a setter */ 
    366                                         if( left(md.functions[x].name,3) eq "set" and not listFindNoCase(cbox_reserved_functions,md.functions[x].name) ){ 
     543                                        if( left(md.functions[x].name,3) eq "set" AND NOT  
     544                                            listFindNoCase(cbox_reserved_functions,md.functions[x].name) ){ 
    367545                                                 
    368546                                                /* New MD Entry */ 
     
    372550                                                entry.type = "ioc"; 
    373551                                                 
    374                                                 /* Found Setter, append property Name */ 
    375                                                 ArrayAppend(arguments.dependencies, entry); 
     552                                                /* Add if not already in properties */ 
     553                                                if( not listFindNoCase(foundDependencies,entry.name) ){ 
     554                                                        /* Found Setter, append property Name */ 
     555                                                        listAppend(foundDependencies,entry.name); 
     556                                                        ArrayAppend(arguments.dependencies, entry); 
     557                                                } 
    376558                                         
    377559                                        }//end if setter found.