<commit_before><!---
 
  Copyright (c) 2002-2005	<NAME>,	<NAME>
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
		
			
 $Id: BeanDefinition.cfc,v 1.10 2005/10/06 16:18:28 rossd Exp $

--->

<cfcomponent name="BeanDefinition" 
			displayname="BeanDefinition" 
			hint="I model a single bean definition within a ColdSpring bean factory" 
			output="false">

	<cfset variables.instanceData = StructNew() />
	<!--- struct of all the constructor args for this bean definition: --->
	<cfset variables.instanceData.ConstructorArgs = StructNew() />
	<!--- struct of all the properties for this bean definition: --->	
	<cfset variables.instanceData.Properties = StructNew() />
	<!--- whether this bean is a singleton or a prototype: --->
	<cfset variables.instanceData.Singleton = true />
	<!--- whether this bean's instance has actually been constructed by the beanFactory:--->
	<cfset variables.instanceData.Constructed = false />
	<!--- whether this bean is a factory bean: --->	
	<cfset variables.instanceData.Factory = false />
	<!--- name of an init-method to call on this bean once all dependencies are set: --->
	<cfset variables.instanceData.initMethod = ''/>
	<!--- whether this bean should be constructed imeediately upon the beanFactory receiving its definition
			OR should the factory wait until someone asks for the bean: --->
	<cfset variables.instanceData.lazyInit = true />
	<!--- list of known dependent beans (by name): --->	
	<cfset variables.instanceData.Dependencies = '' />
	
	<cffunction name="init" returntype="coldspring.beans.BeanDefinition" output="false"
				hint="Constructor. Creates a new Bean Definition.">
		<cfargument name="beanFactory" type="coldspring.beans.BeanFactory" required="true" 
					hint="reference to the coldspring BeanFactory who is creating this bean definition."/>					
		<cfset setBeanFactory(arguments.beanFactory) />
		<cfreturn this/>	
	</cffunction>
	
	<cffunction name="getBeanID" access="public" output="false" returntype="string" 
				hint="I retrieve the BeanID from this instance's data">
		<cfreturn variables.instanceData.BeanID />
	</cffunction>

	<cffunction name="setBeanID" access="public" output="false" returntype="void"  
				hint="I set the BeanID in this instance's data">
		<cfargument name="BeanID" type="string" required="true"/>
		<cfset variables.instanceData.BeanID = arguments.BeanID />
	</cffunction>
	
	<cffunction name="getBeanClass" access="public" output="false" returntype="any"
				hint="I retrieve the BeanClass from this instance's data">
		<cfreturn variables.instanceData.BeanClass />
	</cffunction>

	<cffunction name="setBeanClass" access="public" output="false" returntype="void"  
				hint="I set the BeanClass in this instance's data">
		<cfargument name="BeanClass" type="string" required="true"/>
		<cfset variables.instanceData.BeanClass = arguments.BeanClass />
	</cffunction>
	
	<!--- bean constructor-arg getters/setters --->
	<cffunction name="getConstructorArgs" access="public" output="false" returntype="struct" 
				hint="I retrieve the ConstructorArgs from this instance's data">
		<cfreturn variables.instanceData.constructorArgs />
	</cffunction>

	<cffunction name="setConstructorArgs" access="public" output="false" returntype="void"  
				hint="I set the ConstructorArgs in this instance's data">
		<cfargument name="constructorArgs" type="struct" required="true"/>
		<cfset variables.instanceData.constructorArgs = arguments.constructorArgs />
	</cffunction>
	
	<cffunction name="addConstructorArg" access="public" output="false" returntype="void"  
				hint="I add a property to this bean definition">
		<cfargument name="constructorArg" type="coldspring.beans.BeanProperty" required="true"/>
		<cfset variables.instanceData.constructorArgs[arguments.constructorArg.getName()] = arguments.constructorArg />
	</cffunction>
	
	<cffunction name="getConstructorArg" access="public" output="false" returntype="coldspring.beans.BeanProperty">
		<cfargument name="constructorArgName" type="string" required="true"/>
		<cfif structKeyExists(variables.instanceData.constructorArgs,arguments.constructorArgName)>
			<cfreturn variables.instanceData.constructorArgs[arguments.constructorArgName] />
		<cfelse>
			<cfthrow type="coldspring.beanDefException" 
					 detail="constructor-arg requested does not exist for bean: #getBeanID()# "/>
		</cfif>
	</cffunction>
	
	<!--- bean property getters/setters --->
	<cffunction name="getProperties" access="public" output="false" returntype="struct" 
				hint="I retrieve the Properties from this instance's data">
		<cfreturn variables.instanceData.Properties />
	</cffunction>

	<cffunction name="setProperties" access="public" output="false" returntype="void"  
				hint="I set the Properties in this instance's data">
		<cfargument name="Properties" type="struct" required="true"/>
		<cfset variables.instanceData.Properties = arguments.Properties />
	</cffunction>
	
	<cffunction name="addProperty" access="public" output="false" returntype="void"  
				hint="I add a property to this bean definition">
		<cfargument name="property" type="coldspring.beans.BeanProperty" required="true"/>
		<cfset variables.instanceData.properties[arguments.property.getName()] = arguments.property />
	</cffunction>
	
	<cffunction name="getProperty" access="public" output="false" returntype="coldspring.beans.BeanProperty">
		<cfargument name="propertyName" type="string" required="true"/>
		<cfif structKeyExists(variables.instanceData.properties,arguments.propertyName)>
			<cfreturn variables.instanceData.properties[arguments.propertyName] />
		<cfelse>
			<cfthrow type="coldspring.beanDefException" 
					 detail="property requested does not exist for bean: #getBeanID()# "/>
		</cfif>
	</cffunction>
	
	<cffunction name="addDependency" access="public" output="false" returntype="void"  
				hint="I add a dependency to this bean definition">
		<cfargument name="refName" type="string" required="true" />
		<cfif not ListFindNoCase(variables.instanceData.Dependencies, arguments.refName)>
			<cfset variables.instanceData.Dependencies = ListAppend(variables.instanceData.Dependencies, arguments.refName) />
		</cfif>
	</cffunction>
	
	<cffunction name="getDependencies" access="public" output="false" returntype="string"
				hint="I retrieve the full list of dependencies for this bean definition. The BeanFactory will probably ask for this when I'm being constructed, so he can construct my dependencies first!">
		<cfargument name="dependencyList" type="string" required="true" />
		
		<!--- local vars --->
		<cfset var myDependencies = '' />
		<cfset var refName = '' />
		<cfset var md = '' />
		<cfset var functionIndex = '' />
		<cfset var argIndex = '' />
		<cfset var setterName = '' />
		<cfset var setterNameToCall = '' />				
		<cfset var setterType = '' />
		<cfset var temp_xml = '' />
		<cfset var beanInstance = 0/>
		<cfset var beanName = 0/>
		<cfset var autoArg = 0/>
		<cfset var prop = 0/>
		<cfset var argumentName = 0/>
		<cfset var tempProps = arraynew(1)/>		
				
		<!--- this is where the bean is actually created if it hasn't been --->
		<cfif not autoWireChecked()>
			<cfset  beanInstance = getBeanInstance() />
			<!--- look for autowirable collaborators --->
			<cfset md = getMetaData(beanInstance) />		
			<cfloop from="1" to="#arraylen(md.functions)#" index="functionIndex">
				<!--- look for init (constructor) --->
				<!--- todo: respect how we are told to autowire (byName|byType) --->
				<cfif md.functions[functionIndex].name eq "init" 
					  and arraylen(md.functions[functionIndex].parameters)>
					<!--- loop over args --->
					<cfloop from="1" to="#arraylen(md.functions[functionIndex].parameters)#" index="argIndex">
						<cfset autoArg = md.functions[functionIndex].parameters[argIndex]/>
						<!--- is this arg not explicitly defined?
								and the bean facotry knows it by name
								and if so, *that* bean's class matches this type of arg 
								then it's a dependency --->
						<cfif not structKeyExists(variables.instanceData.constructorArgs, autoArg.name)
							and getBeanFactory().containsBean(autoArg.name)
							and getBeanFactory().getBeanDefinition(autoArg.name).getBeanClass() eq autoArg.type>
							
							<!--- we are going to add the constructor arg as if it had been defined in the xml --->
							<cfset temp_xml = xmlnew()/>
							<cfset temp_xml.xmlRoot = XmlElemNew(temp_xml,"constructor-arg")/>
							<cfset temp_xml.xmlRoot.xmlAttributes['name'] = autoArg.name />
							<cfset temp_xml.xmlRoot.xmlChildren[1] = XmlElemNew(temp_xml,"ref")/>
							<cfset temp_xml.xmlRoot.xmlChildren[1].xmlAttributes['bean'] = autoArg.name />
								
							<cfset addConstructorArg(createObject("component","coldspring.beans.BeanProperty").init(
																							temp_xml.xmlRoot,this
																								)) />						
							
						</cfif>
						
						<!--- try set the argumentName on this constructor arg if it exists --->
						<cftry>
							<cfset prop = getConstructorArg(autoArg.name) />
							<cfset prop.setArgumentName(argumentName) />
							<cfcatch></cfcatch>
						</cftry>
						
					</cfloop>
				<cfelseif left(md.functions[functionIndex].name,3) eq "set" 
						and not structKeyExists(variables.instanceData.properties, mid(md.functions[functionIndex].name,4,len(md.functions[functionIndex].name)-3))
						and arraylen(md.functions[functionIndex].parameters) eq 1>
		
					<!--- look for setters (same as above for constructor-args) --->
					<!--- todo:
							respect how we are told to autowire (byName|byType) --->
							
					<cfset setterName = mid(md.functions[functionIndex].name,4,len(md.functions[functionIndex].name)-3)/>
					<cfset setterNameToCall = setterName/>
					<cfset setterType = md.functions[functionIndex].parameters[1].type/>	
					<cfset beanByType = getBeanFactory().findBeanNameByType(setterType)/>	
						
						<!--- this should be refactored
								basically if you register a bean with a name that matches the type we found here
								currently we are autowiring in that situation ---><commit_msg>fix for CSP-18 (constructor-args are being named wrong).<commit_after><!---
 
  Copyright (c) 2002-2005	<NAME>,	<NAME>
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
		
			
 $Id: BeanDefinition.cfc,v 1.11 2005/10/11 02:13:51 rossd Exp $

--->

<cfcomponent name="BeanDefinition" 
			displayname="BeanDefinition" 
			hint="I model a single bean definition within a ColdSpring bean factory" 
			output="false">

	<cfset variables.instanceData = StructNew() />
	<!--- struct of all the constructor args for this bean definition: --->
	<cfset variables.instanceData.ConstructorArgs = StructNew() />
	<!--- struct of all the properties for this bean definition: --->	
	<cfset variables.instanceData.Properties = StructNew() />
	<!--- whether this bean is a singleton or a prototype: --->
	<cfset variables.instanceData.Singleton = true />
	<!--- whether this bean's instance has actually been constructed by the beanFactory:--->
	<cfset variables.instanceData.Constructed = false />
	<!--- whether this bean is a factory bean: --->	
	<cfset variables.instanceData.Factory = false />
	<!--- name of an init-method to call on this bean once all dependencies are set: --->
	<cfset variables.instanceData.initMethod = ''/>
	<!--- whether this bean should be constructed imeediately upon the beanFactory receiving its definition
			OR should the factory wait until someone asks for the bean: --->
	<cfset variables.instanceData.lazyInit = true />
	<!--- list of known dependent beans (by name): --->	
	<cfset variables.instanceData.Dependencies = '' />
	
	<cffunction name="init" returntype="coldspring.beans.BeanDefinition" output="false"
				hint="Constructor. Creates a new Bean Definition.">
		<cfargument name="beanFactory" type="coldspring.beans.BeanFactory" required="true" 
					hint="reference to the coldspring BeanFactory who is creating this bean definition."/>					
		<cfset setBeanFactory(arguments.beanFactory) />
		<cfreturn this/>	
	</cffunction>
	
	<cffunction name="getBeanID" access="public" output="false" returntype="string" 
				hint="I retrieve the BeanID from this instance's data">
		<cfreturn variables.instanceData.BeanID />
	</cffunction>

	<cffunction name="setBeanID" access="public" output="false" returntype="void"  
				hint="I set the BeanID in this instance's data">
		<cfargument name="BeanID" type="string" required="true"/>
		<cfset variables.instanceData.BeanID = arguments.BeanID />
	</cffunction>
	
	<cffunction name="getBeanClass" access="public" output="false" returntype="any"
				hint="I retrieve the BeanClass from this instance's data">
		<cfreturn variables.instanceData.BeanClass />
	</cffunction>

	<cffunction name="setBeanClass" access="public" output="false" returntype="void"  
				hint="I set the BeanClass in this instance's data">
		<cfargument name="BeanClass" type="string" required="true"/>
		<cfset variables.instanceData.BeanClass = arguments.BeanClass />
	</cffunction>
	
	<!--- bean constructor-arg getters/setters --->
	<cffunction name="getConstructorArgs" access="public" output="false" returntype="struct" 
				hint="I retrieve the ConstructorArgs from this instance's data">
		<cfreturn variables.instanceData.constructorArgs />
	</cffunction>

	<cffunction name="setConstructorArgs" access="public" output="false" returntype="void"  
				hint="I set the ConstructorArgs in this instance's data">
		<cfargument name="constructorArgs" type="struct" required="true"/>
		<cfset variables.instanceData.constructorArgs = arguments.constructorArgs />
	</cffunction>
	
	<cffunction name="addConstructorArg" access="public" output="false" returntype="void"  
				hint="I add a property to this bean definition">
		<cfargument name="constructorArg" type="coldspring.beans.BeanProperty" required="true"/>
		<cfset variables.instanceData.constructorArgs[arguments.constructorArg.getName()] = arguments.constructorArg />
	</cffunction>
	
	<cffunction name="getConstructorArg" access="public" output="false" returntype="coldspring.beans.BeanProperty">
		<cfargument name="constructorArgName" type="string" required="true"/>
		<cfif structKeyExists(variables.instanceData.constructorArgs,arguments.constructorArgName)>
			<cfreturn variables.instanceData.constructorArgs[arguments.constructorArgName] />
		<cfelse>
			<cfthrow type="coldspring.beanDefException" 
					 detail="constructor-arg requested does not exist for bean: #getBeanID()# "/>
		</cfif>
	</cffunction>
	
	<!--- bean property getters/setters --->
	<cffunction name="getProperties" access="public" output="false" returntype="struct" 
				hint="I retrieve the Properties from this instance's data">
		<cfreturn variables.instanceData.Properties />
	</cffunction>

	<cffunction name="setProperties" access="public" output="false" returntype="void"  
				hint="I set the Properties in this instance's data">
		<cfargument name="Properties" type="struct" required="true"/>
		<cfset variables.instanceData.Properties = arguments.Properties />
	</cffunction>
	
	<cffunction name="addProperty" access="public" output="false" returntype="void"  
				hint="I add a property to this bean definition">
		<cfargument name="property" type="coldspring.beans.BeanProperty" required="true"/>
		<cfset variables.instanceData.properties[arguments.property.getName()] = arguments.property />
	</cffunction>
	
	<cffunction name="getProperty" access="public" output="false" returntype="coldspring.beans.BeanProperty">
		<cfargument name="propertyName" type="string" required="true"/>
		<cfif structKeyExists(variables.instanceData.properties,arguments.propertyName)>
			<cfreturn variables.instanceData.properties[arguments.propertyName] />
		<cfelse>
			<cfthrow type="coldspring.beanDefException" 
					 detail="property requested does not exist for bean: #getBeanID()# "/>
		</cfif>
	</cffunction>
	
	<cffunction name="addDependency" access="public" output="false" returntype="void"  
				hint="I add a dependency to this bean definition">
		<cfargument name="refName" type="string" required="true" />
		<cfif not ListFindNoCase(variables.instanceData.Dependencies, arguments.refName)>
			<cfset variables.instanceData.Dependencies = ListAppend(variables.instanceData.Dependencies, arguments.refName) />
		</cfif>
	</cffunction>
	
	<cffunction name="getDependencies" access="public" output="false" returntype="string"
				hint="I retrieve the full list of dependencies for this bean definition. The BeanFactory will probably ask for this when I'm being constructed, so he can construct my dependencies first!">
		<cfargument name="dependencyList" type="string" required="true" />
		
		<!--- local vars --->
		<cfset var myDependencies = '' />
		<cfset var refName = '' />
		<cfset var md = '' />
		<cfset var functionIndex = '' />
		<cfset var argIndex = '' />
		<cfset var setterName = '' />
		<cfset var setterNameToCall = '' />				
		<cfset var setterType = '' />
		<cfset var temp_xml = '' />
		<cfset var beanInstance = 0/>
		<cfset var beanName = 0/>
		<cfset var autoArg = 0/>
		<cfset var prop = 0/>
		<cfset var argumentName = 0/>
		<cfset var tempProps = arraynew(1)/>		
				
		<!--- this is where the bean is actually created if it hasn't been --->
		<cfif not autoWireChecked()>
			<cfset  beanInstance = getBeanInstance() />
			<!--- look for autowirable collaborators --->
			<cfset md = getMetaData(beanInstance) />		
			<cfloop from="1" to="#arraylen(md.functions)#" index="functionIndex">
				<!--- look for init (constructor) --->
				<!--- todo: respect how we are told to autowire (byName|byType) --->
				<cfif md.functions[functionIndex].name eq "init" 
					  and arraylen(md.functions[functionIndex].parameters)>
					<!--- loop over args --->
					<cfloop from="1" to="#arraylen(md.functions[functionIndex].parameters)#" index="argIndex">
						<cfset autoArg = md.functions[functionIndex].parameters[argIndex]/>
						<!--- is this arg not explicitly defined?
								and the bean facotry knows it by name
								and if so, *that* bean's class matches this type of arg 
								then it's a dependency --->
						<cfif not structKeyExists(variables.instanceData.constructorArgs, autoArg.name)
							and getBeanFactory().containsBean(autoArg.name)
							and getBeanFactory().getBeanDefinition(autoArg.name).getBeanClass() eq autoArg.type>
							
							<!--- we are going to add the constructor arg as if it had been defined in the xml --->
							<cfset temp_xml = xmlnew()/>
							<cfset temp_xml.xmlRoot = XmlElemNew(temp_xml,"constructor-arg")/>
							<cfset temp_xml.xmlRoot.xmlAttributes['name'] = autoArg.name />
							<cfset temp_xml.xmlRoot.xmlChildren[1] = XmlElemNew(temp_xml,"ref")/>
							<cfset temp_xml.xmlRoot.xmlChildren[1].xmlAttributes['bean'] = autoArg.name />
								
							<cfset addConstructorArg(createObject("component","coldspring.beans.BeanProperty").init(
																							temp_xml.xmlRoot,this
																								)) />						
							
						</cfif>
						
						<!--- try set the argumentName on this constructor arg if it exists --->
						<cftry>
							<cfset prop = getConstructorArg(autoArg.name) />
							<cfset prop.setArgumentName(autoArg.name) />
							<cfcatch></cfcatch>
						</cftry>
						
					</cfloop>
				<cfelseif left(md.functions[functionIndex].name,3) eq "set" 
						and not structKeyExists(variables.instanceData.properties, mid(md.functions[functionIndex].name,4,len(md.functions[functionIndex].name)-3))
						and arraylen(md.functions[functionIndex].parameters) eq 1>
		
					<!--- look for setters (same as above for constructor-args) --->
					<!--- todo:
							respect how we are told to autowire (byName|byType) --->
							
					<cfset setterName = mid(md.functions[functionIndex].name,4,len(md.functions[functionIndex].name)-3)/>
					<cfset setterNameToCall = setterName/>
					<cfset setterType = md.functions[functionIndex].parameters[1].type/>	
					<cfset beanByType = getBeanFactory().findBeanNameByType(setterType)/>	
						
						<!--- this should be refactored
								basically if you register a bean with a name that matches the type we found here
								currently we are autowiring in that situation --->
{'fraction_non_alphanumeric': 0.12809508088478044, 'fraction_numerical': 0.00443333490543791, 'mean_word_length': 3.9844851904090266, 'pattern_counts': {'":': 0, '<': 287, '<?xml version=': 0, '>': 287, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 40}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2848929', 'n_tokens_mistral': 7132, 'n_tokens_neox': 6014, 'n_words': 1685}