« Return to Thread: DataSource configuration for Tomcat 5x, Java API

DataSource configuration for Tomcat 5x, Java API

by Tomas-35 :: Rate this Message:

Reply to Author | View in Thread

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hello,

First of all, congratulations to the development team for the 1.0 release.

I'm writing an integration test for a webapp with Tomcat5x.
The war contains a /META-INF/context.xml file with a Resource element that is a datasource.
I'm trying to replace this datasource definition with a datasource that points to a test database, for the integration test.

I have a TomcatFixture class that attempts to setup all the configuration for the test:

    private InstalledLocalContainer initializeTomcat() throws Exception {
        // Initialize the deploy artifact
        URL warURL = this.getClass().getResource("/myapp.war");
        File warFile = new File(warURL.toURI());
        Deployable war = new TomcatWAR(warFile.getAbsolutePath());

        // Initialize the configuration
        AbstractLocalConfiguration configuration = new Tomcat5xStandaloneLocalConfiguration("target/tomcat5x"); // (1)
        configuration.addDeployable(war);
        configuration.setProperty(ServletPropertySet.PORT, CARGO_PORT);
        DataSource dsConfig = new DataSource();
        dsConfig.setId("jdbc/myAppDb");
        dsConfig.setJndiLocation("jdbc/myAppDb");
        dsConfig.setConnectionType(ConfigurationEntryType.JDBC_DRIVER);
        dsConfig.setTransactionSupport(TransactionSupport.NO_TRANSACTION);
        dsConfig.setDriverClass("com.mysql.jdbc.Driver");
        dsConfig.setUrl("jdbc:mysql://sqlhost/dbname");
        dsConfig.setUsername("johndoe");
        dsConfig.setPassword("secret");
        Properties props = new Properties();
        props.setProperty("username", "johndoe"); // (2)
        dsConfig.setConnectionProperties(props);
        configuration.addDataSource(dsConfig);

        // Install Tomcat if necessary
        Installer installer = new ZipURLInstaller(new URL(TOMCAT_URL));
        installer.install();

        // Initialize the server (don't start it yet)
        InstalledLocalContainer container = new Tomcat5xInstalledLocalContainer(configuration);
        container.setHome(installer.getHome());
        container.setLogger(new FileLogger("tomcat5x.log", false));

        // Add the JDBC driver to the server's classpath
        URL jdbcDriverURL = this.getClass().getResource("/mysql-connector-java-5.1.6.jar");
        container.addExtraClasspath(FileUtils.toFile(jdbcDriverURL).getAbsolutePath());
        return container;
    }

Now, my comments/questions:
* I had to declare an AbstractLocalConfiguration instead of a LocalConfiguration (1), since the addDataSource method is in the abstract class but not in the interface. Is this ok? Shouldn't the interface contain that method?
* After some trial and error, I noticed that the context.xml file that Cargo is generating with the Resource element has a 'user' attribute instead of a 'username' attribute that is expected by Tomcat5x. I had to work around this passing an extra property (2). Is this a bug in Cargo? I found no documentation for this. (The code responsible for this is AbstractTomcatConfigurationBuilder, line 96).
* As I mentioned, I had to do some trial and error to get this configuration running. I think Cargo is a great tool, but it would be great if you guys could add 'end-to-end' configuration examples like this to your wiki, both for the Java API, Maven, etc.

Cheers,
Tomás

 « Return to Thread: DataSource configuration for Tomcat 5x, Java API