On Aug 24, 2012, at 1:53 PM, exabrial wrote:

> @TomEEModuleConfiguration("my-application-name")
> public class ConfigModule {
> 
>        @Inject
>        private ServerContext context;
> 
>        @ConfigurationId(env = "Development")
>        public void getDevConfig(ModuleConfig config) {
> 
>                DataSourceConfig dataSourceConfig =
> 
> createResourceConfig(MySqlDataSource.class).setUserName("bob").setPassword("thebuilder").setHostName("localhost")

Did some experimenting with this.  As I mentioned we have a (mostly) strongly 
typed API we use internally:

 - http://openejb.apache.org/dev/configuration-and-assembly.html

The only part of that that isn't strongly typed are of course the individual 
name=value pairs, but other than that the API is pretty good.  Everything (xml, 
properties, you name it) boils down to the configuration and assembly API 
eventually.  Nothing sneaks by that hasn't gone through it at some point.

Anyway, I did some experimenting with hacking up strongly typed versions of the 
various things you can configure in a tomee.xml file.

 - 
http://svn.apache.org/repos/asf/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/typed/DataSourceBuilder.java

I generated these based off the possible configuration options for each.  The 
format modeled after the Amazon AWS API for EC2 which has both "get" and "set" 
methods as well as builder methods.  They prefix all builder methods with 
"with", which I kind of like as it makes it easy to find all the builder 
methods in an IDE -- just type ".with" then hit the tab completion.

Anyway, here's a sample usage:

 - 
http://svn.apache.org/repos/asf/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/typed/util/ServerContextTest.java

    public void test() throws Exception {
        final ServerContext serverContext = new ServerContext();


        serverContext.createTransactionManager(new TransactionManagerBuilder()
                .withDefaultTransactionTimeout(3, TimeUnit.MINUTES)
                .withBufferSizeKb(1024)
                .withMaxBuffers(10));

        serverContext.createSecurityService(new SecurityServiceBuilder()
                .withDefaultUser("unknown"));

        serverContext.createContainer(new StatelessContainerBuilder()
                .withStrictPooling(true)
                .withMaxSize(11)
                .withMinSize(5)
                .withReplaceAged(true)
                .withMaxAge(1, TimeUnit.DAYS)
                .withIdleTimeout(30, TimeUnit.MINUTES)
                .withSweepInterval(3, TimeUnit.MINUTES)
        );

        serverContext.createResource(new DataSourceBuilder()
                .id("FooDataSource")
                .withJtaManaged(true)
                .withJdbcDriver("org.hsqldb.jdbcDriver")
                .withJdbcUrl(new URI("jdbc:hsqldb:mem:hsqldb"))
                .withAccessToUnderlyingConnectionAllowed(false)
                .withMaxActive(10)
                .withMaxIdle(5)
                .withMinEvictableIdleTime(15, TimeUnit.MINUTES)
                .withTimeBetweenEvictionRuns(5, TimeUnit.MINUTES)
        );
    }

Here is what an xml version of the above code might look like (I generated the 
builders with JAXB annotations on them so they could easily be built from xml):

    <ServerContext>
      <TransactionManager
          id="TransactionManager"
          defaultTransactionTimeoutSeconds="180"
          txRecovery="false"
          bufferSizeKb="1024"
          checksumEnabled="true"
          adler32Checksum="true"
          flushSleepTimeMilliseconds="50"
          logFileDir="txlog"
          logFileExt="log"
          logFileName="howl"
          maxBlocksPerFile="-1"
          maxBuffers="10"
          maxLogFiles="2"
          minBuffers="4"
          threadsWaitingForceThreshold="-1"
          />
      <SecurityService
          id="SecurityService"
          defaultUser="unknown"/>
      <StatelessContainer
          id="StatelessContainer"
          accessTimeout="30 SECONDS"
          maxSize="11"
          minSize="5"
          strictPooling="true"
          maxAge="1 DAYS"
          replaceAged="true"
          replaceFlushed="false"
          maxAgeOffset="-1"
          idleTimeout="30 MINUTES"
          garbageCollection="false"
          sweepInterval="3 MINUTES"
          callbackThreads="5"
          closeTimeout="5 MINUTES"
          />
      <DataSource
          id="FooDataSource"
          jtaManaged="true"
          jdbcDriver="org.hsqldb.jdbcDriver"
          jdbcUrl="jdbc:hsqldb:mem:hsqldb"
          userName="sa"
          passwordCipher="PlainText"
          defaultAutoCommit="true"
          initialSize="0"
          maxActive="10"
          maxIdle="5"
          minIdle="0"
          maxWait="-1"
          testOnBorrow="true"
          testOnReturn="false"
          testWhileIdle="false"
          timeBetweenEvictionRunsMillis="300000"
          numTestsPerEvictionRun="3"
          minEvictableIdleTimeMillis="900000"
          poolPreparedStatements="false"
          maxOpenPreparedStatements="0"
          accessToUnderlyingConnectionAllowed="false"
          ignoreDefaultValues="false"
          />
    </ServerContext>

I don't consider any of this perfect, just sort of experimenting with the 
feedback.


-David

Reply via email to