Friday 23 November 2012

Reference external java beans with absolute path

I run a bunch of batch file exports from Salesforce calling Data Loader from the command line, with multiple process-conf.xml files defining various tasks. Originally I stored the SFDC credentials in each bean, then shifted the credentials to variables, still within the same xml file, using java.lang.String classes with constructor-arg properties:

<bean id="user" class="java.lang.String">
<constructor-arg value="username@org.com.au"/>
</bean>

But I also wanted to store multiple environment credentials (sandbox and production) so that I could perform each task on multiple instances. And as the number of actions increased, I quickly realised that I needed to store the credentials in an external location so that all the process-conf.xml files could access them. Plus, this makes it much easier to change credentials when security settings within the orgs force pw and security token changes.

I searched for ages for a quick solution to this problem but finally found what I needed hidden in the Spring Framework documentation (http://static.springsource.org/spring/docs/2.0.x/reference/beans.html).

1. Create an xml file containing all the global variables you require;
2. Store this anywhere in a top level folder, in my case I put it on my local C: drive;
3. Import the file as a resource at the top of the process-conf.xml file;

<beans>
        <import resource="file:C:\folder\NameofFile.xml"/>
...
</beans>

4. Call the external beans as if they were defined within the process-conf.xml file.

<entry>
<key> <value>sfdc.username</value></key>
<ref bean="prodUser" />
</entry>

No comments:

Post a Comment