| 607 | | <cfset autowireCfc = createAutowireDynamicMethodsComponent(autowireBeanNames) /> |
| 608 | | |
| 609 | | <!--- Loop over all the methods ---> |
| 610 | | <cfloop from="1" to="#ArrayLen(autowireBeanNames)#" index="i"> |
| 611 | | |
| 612 | | <cfset beanName = Trim(autowireBeanNames[i]) /> |
| 613 | | |
| 614 | | <!--- Inject the _methodInject() so we can get the methods into the variables scope |
| 615 | | in addition to the this scope of the component ---> |
| 616 | | <cfset arguments.targetObj["_methodInject"] = autowireCfc["_methodInject"] /> |
| 617 | | |
| 618 | | <!--- Only dynamically inject the setter if there isn't a concrete getter ---> |
| 619 | | <cfif NOT StructKeyExists(arguments.targetObj, "get" & beanName)> |
| 620 | | <cfset arguments.targetObj._methodInject("get" & beanName, autowireCfc["get" & beanName]) /> |
| 621 | | </cfif> |
| 622 | | |
| 623 | | <!--- Only dynamically inject the setter if there isn't a concrete setter ---> |
| 624 | | <cfif NOT StructKeyExists(arguments.targetObj, "set" & beanName)> |
| 625 | | <cfset arguments.targetObj._methodInject("set" & beanName, autowireCfc["set" & beanName]) /> |
| 626 | | </cfif> |
| 627 | | |
| 628 | | <!--- Inject appropriate bean if the factory has a bean by that name ---> |
| 629 | | <cfif getAssert().isTrue(beanFactory.containsBean(beanName) |
| 630 | | , "Cannot find bean named '#beanName#' to autowire by method injection in a '#ListLast(targetObjMetadata.extends.name, '.')#' of type '#targetObjMetadata.name#' in module '#getAppManager().getModuleName()#'." |
| 631 | | , "Check that there is a bean named '#beanName#' defined in your ColdSpring bean factory.")> |
| 632 | | <cfinvoke component="#arguments.targetObj#" method="set#beanName#"> |
| 633 | | <cfinvokeargument name="#beanName#" value="#beanFactory.getBean(beanName)#" /> |
| 634 | | </cfinvoke> |
| 635 | | </cfif> |
| 636 | | |
| 637 | | <!--- Delete the _methodInject() from the target ---> |
| 638 | | <cfset StructDelete(arguments.targetObj, "_methodInject") /> |
| 639 | | </cfloop> |
| | 601 | <cfset autowireCfc = CreateObject("component", "MachII.properties.ColdSpringProperty_InjectionMethods").init(autowireBeanNames) /> |
| | 602 | |
| | 603 | <!--- Build all the targets ---> |
| | 604 | <cftry> |
| | 605 | <cfloop from="1" to="#ArrayLen(autowireBeanNames)#" index="i"> |
| | 606 | <cfset beanName = autowireBeanNames[i] /> |
| | 607 | |
| | 608 | <!--- Add appropriate bean if the factory has a bean by that name ---> |
| | 609 | <cfset targets[beanName] = beanFactory.getBean(beanName) /> |
| | 610 | </cfloop> |
| | 611 | <!--- Faster to fast fail and handle a missing bean exception than to check if the bean exists in the factor ---> |
| | 612 | <cfcatch type="any"> |
| | 613 | <cfthrow type="MachII.properties.ColdSpringProperty.NoBean" |
| | 614 | message="Cannot find bean named '#beanName#' to autowire by method injection in a '#ListLast(targetObjMetadata.extends.name, '.')#' of type '#targetObjMetadata.name#' in module '#getAppManager().getModuleName()#'." |
| | 615 | detail="Check that there is a bean named '#beanName#' defined in your ColdSpring bean factory." /> |
| | 616 | </cfcatch> |
| | 617 | </cftry> |
| | 618 | |
| | 619 | <!--- Inject the _methodInject() so we can get the methods into the variables scope |
| | 620 | in addition to the this scope of the component ---> |
| | 621 | <cfset arguments.targetObj["_injectMethods"] = autowireCfc["_injectMethods"] /> |
| | 622 | |
| | 623 | <!--- Now inject everything into the target ---> |
| | 624 | <cfset arguments.targetObj._injectMethods(autowireCfc, targets) /> |
| | 625 | |
| | 626 | <!--- Delete the _methodInject() from the target ---> |
| | 627 | <cfset StructDelete(arguments.targetObj, "_injectMethods") /> |
| 781 | | <cffunction name="createAutowireDynamicMethodsComponent"access="private" returntype="any" output="false" |
| 782 | | hint="Create a component with the neccessary methods to dynamically inject into targets."> |
| 783 | | <cfargument name="autowireBeanNames" type="array" required="true" /> |
| 784 | | |
| 785 | | <cfset var beanName = "" /> |
| 786 | | <cfset var cfcData = CreateObject("java", "java.lang.StringBuffer") /> |
| 787 | | <cfset var cfcName = "" /> |
| 788 | | <cfset var cfcDirectory = getCfcGenerationLocation() /> |
| 789 | | <cfset var autowireCfc = "" /> |
| 790 | | <cfset var i = "" /> |
| 791 | | |
| 792 | | <!--- Add the opening cfcomponent tag and _methodInject method ---> |
| 793 | | <!--- Used string concatenation otherwise CFEclipse marks this as bad code ---> |
| 794 | | <cfset cfcData.append('<cfcomponent><cffunction name="_methodInject" access="public" returntype="void" output="false"><cfargument name="methodName" type="string" required="true" /><cfargument name="method" type="any" required="true" /><cfset this[arguments.methodName] = arguments.method /><cfset variables[arguments.methodName] = arguments.method /></' & 'cffunction>') /> |
| 795 | | |
| 796 | | <!--- Create the getter/setter methods for each beanName ---> |
| 797 | | <cfloop from="1" to="#ArrayLen(arguments.autowireBeanNames)#" index="i"> |
| 798 | | <!--- Clean any spaces from the bean name ---> |
| 799 | | <cfset beanName = Trim(arguments.autowireBeanNames[i]) /> |
| 800 | | |
| 801 | | <!--- Used string concatenation otherwise CFEclipse marks this as bad code ---> |
| 802 | | <cfset cfcData.append('<cffunction name="set' & beanName & '" access="public" returntype="void" output="false"><cfargument name="' & beanName & '" type="any" required="true" /><cfset variables.' & beanName & ' = arguments.' & beanName & ' /></' & 'cffunction><cffunction name="get' & beanName & '" access="public" returntype="any" output="false"><cfreturn variables.' & beanName & ' /></' & 'cffunction>') /> |
| 803 | | </cfloop> |
| 804 | | |
| 805 | | <!--- Add the closing cfcomponent tag ---> |
| 806 | | <cfset cfcData.append('</cfcomponent>') /> |
| 807 | | |
| 808 | | <!--- Create a name for the CFC using Hash() since that is faster than creating a UUID ---> |
| 809 | | <cfset cfcName = Hash(getTickCount() & RandRange(0, 10000) & RandRange(0, 10000)) /> |
| 810 | | |
| 811 | | <!--- Write the cfc data to a temp file ---> |
| 812 | | <cftry> |
| 813 | | <cffile action="write" |
| 814 | | output="#cfcData.toString()#" |
| 815 | | file="#cfcDirectory#/#cfcName#.cfc" /> |
| 816 | | <cfcatch type="any"> |
| 817 | | <cfthrow type="MachII.properties.ColdspringProperty.CFCWritePermissions" |
| 818 | | message="Cannot write temporary CFC for autowiring to '#cfcDirectory#'. Does your CFML engine have write permissions to this directory?" |
| 819 | | detail="#getAppManager().getUtils().buildMessageFromCfCatch(cfcatch)#" /> |
| 820 | | </cfcatch> |
| 821 | | </cftry> |
| 822 | | |
| 823 | | <!--- Instantiate the component ---> |
| 824 | | <cftry> |
| 825 | | <cfset autowireCfc = CreateObject("component", getDotPathToCfcGenerationLocation() & cfcName) /> |
| 826 | | <cfcatch type="any"> |
| 827 | | <cfif StructKeyExists(cfcatch, "missingFileName")> |
| 828 | | <cfthrow type="MachII.properties.ColdspringProperty.CannotFindCFC" |
| 829 | | message="Cannot find a temporary CFC at '#getDotPathToCfcGenerationLocation() & cfcName#'." |
| 830 | | detail="Please check that the dot path location '#getDotPathToCfcGenerationLocation() & cfcName#' and cfcGenerationLocation '#cfcDirectory#' point to the same directory." /> |
| 831 | | <cfelse> |
| 832 | | <cfrethrow /> |
| 833 | | </cfif> |
| 834 | | </cfcatch> |
| 835 | | </cftry> |
| 836 | | |
| 837 | | <!--- Delete the temp cfc ---> |
| 838 | | <cffile action="delete" |
| 839 | | file="#cfcDirectory#/#cfcName#.cfc" /> |
| 840 | | |
| 841 | | <cfreturn autowireCfc /> |
| 842 | | </cffunction> |
| 843 | | |
| 886 | | <cffunction name="setCfcGenerationLocation" access="private" returntype="void" output="false"> |
| 887 | | <cfargument name="cfcGenerationLocation" type="string" required="true" /> |
| 888 | | <cfset variables.instance.cfcGenerationLocation = arguments.cfcGenerationLocation /> |
| 889 | | </cffunction> |
| 890 | | <cffunction name="getCfcGenerationLocation" access="public" returntype="string" output="false"> |
| 891 | | <cfreturn variables.instance.cfcGenerationLocation /> |
| 892 | | </cffunction> |
| 893 | | |
| 894 | | <cffunction name="setDotPathToCfcGenerationLocation" access="private" returntype="void" output="false"> |
| 895 | | <cfargument name="dotPathToCfcGenerationLocation" type="string" required="true" /> |
| 896 | | |
| 897 | | <!--- Add a trailing dot of the path exists ---> |
| 898 | | <cfif Len(dotPathToCfcGenerationLocation)> |
| 899 | | <cfset arguments.dotPathToCfcGenerationLocation = arguments.dotPathToCfcGenerationLocation & "." /> |
| 900 | | </cfif> |
| 901 | | |
| 902 | | <cfset variables.instance.dotPathToCfcGenerationLocation = arguments.dotPathToCfcGenerationLocation /> |
| 903 | | </cffunction> |
| 904 | | <cffunction name="getDotPathToCfcGenerationLocation" access="public" returntype="string" output="false"> |
| 905 | | <cfreturn variables.instance.dotPathToCfcGenerationLocation /> |
| 906 | | </cffunction> |
| 907 | | |