<?xml version="1.0" encoding="UTF-8"?>
<!--
  - Middle tier application context definition for the image database.
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
	<context:property-placeholder location="WEB-INF/jdbc.properties,WEB-INF/mail.properties"/>

	<!-- Local C3P0 DataSource that works in any environment -->
	<!-- See JPetStore for an example of using Apache Commons DBCP as alternative -->
	<!-- (Both DataSource implementations have a "close" method to be called on shutdown) -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>

	<!-- Transaction manager for a single JDBC DataSource -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

	<!-- Activates @Transactional for DefaultImageDatabase -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

	<!-- Default implementation of the ImageDatabase business interface -->
	<!-- (refers to a LobHandler for handling BLOBs and CLOBs -->
	<bean id="imageDatabase" class="org.springframework.samples.imagedb.DefaultImageDatabase">
		<property name="dataSource" ref="dataSource"/>
		<property name="lobHandler" ref="${imageDatabase.lobHandler}"/>
	</bean>

	<!-- MailSender implementation for JavaMail -->
	<!-- Used by ListImagesQuartzJob and ListImagesTimerTask; see "scheduling-*.xml" -->
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="${mail.host}"/>
	</bean>

	<!-- LobHandler for well-behaved JDBC drivers -->
	<!-- (simply delegating to corresponding PreparedStatement and ResultSet methods) -->
	<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"/>

	<!-- LobHandler for Oracle JDBC drivers -->
	<!-- (refers to the NativeJdbcExtractor above to get access to native OracleConnections) -->
	<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
		<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
	</bean>

	<!-- NativeJdbcExtractor for the C3P0 connection pool above -->
	<!-- (just needed for oracleLobHandler) -->
	<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor"
			lazy-init="true"/>

</beans>
