| | 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> |
| 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 | |