[overlord-commits] Overlord SVN: r243 - in sam/trunk/modules/core/src: main/java/org/jboss/sam/config and 10 other directories.

overlord-commits at lists.jboss.org overlord-commits at lists.jboss.org
Wed Aug 13 10:42:18 EDT 2008


Author: heiko.braun at jboss.com
Date: 2008-08-13 10:42:18 -0400 (Wed, 13 Aug 2008)
New Revision: 243

Added:
   sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntimeAware.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/
   sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceFactory.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceManager.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/internal/
   sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/internal/DBCPDataSourceFactory.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/DefaultRuntimeFactory.java
   sam/trunk/modules/core/src/test/resources/drools/sam-cfg.xml
Removed:
   sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/datasource/
Modified:
   sam/trunk/modules/core/src/main/java/org/jboss/sam/EventProcessor.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntime.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntimeFactory.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/config/Configurator.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/DefaultRuntimeImpl.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/drools/DroolsEventProcessorImpl.java
   sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/drools/SAMAdapter.java
   sam/trunk/modules/core/src/test/java/org/jboss/test/sam/InMemoryDatabase.java
   sam/trunk/modules/core/src/test/java/org/jboss/test/sam/SamTestSetup.java
   sam/trunk/modules/core/src/test/java/org/jboss/test/sam/adapter/invm/InVMAdapterTestCase.java
   sam/trunk/modules/core/src/test/java/org/jboss/test/sam/database/DatabaseTestCase.java
   sam/trunk/modules/core/src/test/java/org/jboss/test/sam/drools/ApiTestCase.java
   sam/trunk/modules/core/src/test/resources/database/sam-cfg.xml
   sam/trunk/modules/core/src/test/resources/database/test.drl
Log:
Force usage of SAMRuntimeFactory, otherwise the EventProcessor wouldn't be configured (or it needs to be done explicitly)

Modified: sam/trunk/modules/core/src/main/java/org/jboss/sam/EventProcessor.java
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/EventProcessor.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/EventProcessor.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -28,11 +28,11 @@
  * and provides a way to register {@link org.jboss.sam.ProcessingStatement}'s,
  * both bound (event forwarding) and unbound (event aggregation and filtering).
  * <p>
- * Instances are created through  {@link org.jboss.sam.EventProcessorFactory}
+ * Instances are created through {@link org.jboss.sam.EventProcessorFactory}
  * 
  * @author Heiko.Braun <heiko.braun at jboss.com>
  */
-public interface EventProcessor<T>
+public interface EventProcessor<T> extends SAMRuntimeAware
 {
    /**
     * Add input to the runtime

Modified: sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntime.java
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntime.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntime.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -21,30 +21,20 @@
  */
 package org.jboss.sam;
 
+import org.jboss.sam.datasource.DataSourceManager;
 import org.jboss.sam.config.Configurator;
-import org.jboss.sam.internal.datasource.DatasourceManager;
 
+
 /**
+ * Glues together SAM building blocks.
+ * 
  * @author Heiko.Braun <heiko.braun at jboss.com>
  */
-public abstract class SAMRuntime
+public interface SAMRuntime
 {
-   private Configurator configurator;
+   void applyConfiguration(Configurator cfg);
 
-   private EventProcessor eventProcessor;
-   private DatasourceManager datasourceManager;
+   DataSourceManager getDataSourceManager();
 
-   protected SAMRuntime(Configurator configurator)
-   {
-      this.configurator = configurator;
-
-      // create and configure a processor
-      this.eventProcessor = EventProcessorFactory.newInstance().createProcessor();
-      configurator.configure(this.eventProcessor);
-
-      // create and configure datasources
-      this.datasourceManager = new DatasourceManager();
-      configurator.configure(datasourceManager);
-
-   }
+   EventProcessor getEventProcessor();
 }

Added: sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntimeAware.java
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntimeAware.java	                        (rev 0)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntimeAware.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam;
+
+/**
+ * Marks SAM components that need to interact with the runtime itself. 
+ * 
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public interface SAMRuntimeAware
+{
+   void setRuntime(SAMRuntime runtime);
+}
+


Property changes on: sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntimeAware.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntimeFactory.java
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntimeFactory.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/SAMRuntimeFactory.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -22,26 +22,17 @@
 package org.jboss.sam;
 
 import org.jboss.sam.config.Configurator;
-import org.jboss.sam.internal.DefaultRuntimeImpl;
+import org.jboss.sam.internal.DefaultRuntimeFactory;
 
 /**
  * @author Heiko.Braun <heiko.braun at jboss.com>
  */
 public abstract class SAMRuntimeFactory
 {
-
    public abstract SAMRuntime createRuntime(Configurator configurator);
 
    public static SAMRuntimeFactory newInstance()
    {
       return new DefaultRuntimeFactory();
-   }
-
-   static class DefaultRuntimeFactory extends SAMRuntimeFactory
-   {
-      public SAMRuntime createRuntime(Configurator configurator)
-      {
-         return new DefaultRuntimeImpl(configurator);
-      }
-   }
+   }   
 }

Modified: sam/trunk/modules/core/src/main/java/org/jboss/sam/config/Configurator.java
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/config/Configurator.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/config/Configurator.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -24,8 +24,8 @@
 import org.jboss.sam.EventProcessor;
 import org.jboss.sam.StreamInput;
 import org.jboss.sam.StreamOutput;
-import org.jboss.sam.internal.datasource.DatasourceManager;
-import org.jboss.sam.internal.datasource.DataSourceFactory;
+import org.jboss.sam.datasource.DataSourceManager;
+import org.jboss.sam.datasource.DataSourceFactory;
 
 import javax.sql.DataSource;
 
@@ -88,7 +88,7 @@
       }
    }
 
-   public void configure(DatasourceManager manager)
+   public void configure(DataSourceManager manager)
    {
       DataSourceFactory dsFactory = DataSourceFactory.newInstance();
       

Copied: sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceFactory.java (from rev 241, sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/datasource/DataSourceFactory.java)
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceFactory.java	                        (rev 0)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceFactory.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.datasource;
+
+import org.jboss.sam.config.DataSourceDef;
+import org.jboss.sam.datasource.internal.DBCPDataSourceFactory;
+
+import javax.sql.DataSource;
+
+/**
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public abstract class DataSourceFactory
+{   
+   public static DataSourceFactory newInstance()
+   {
+      return new DBCPDataSourceFactory();
+   }
+
+   public abstract DataSource createDataSource(DataSourceDef dsDef);
+
+}


Property changes on: sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceFactory.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Copied: sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceManager.java (from rev 241, sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/datasource/DatasourceManager.java)
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceManager.java	                        (rev 0)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceManager.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -0,0 +1,58 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.datasource;
+
+import javax.sql.DataSource;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class DataSourceManager
+{
+   private Map<String, DataSource> datasources = new ConcurrentHashMap<String, DataSource>();
+
+   public void addDataSource(String reference, DataSource ds)
+   {
+      if(datasources.containsKey(reference))
+         throw new IllegalArgumentException("DataSource '" + reference + "' does already exist!");
+      
+      this.datasources.put(reference, ds);
+   }
+
+   public DataSource getDataSource(String reference)
+   {
+      DataSource ds = datasources.get(reference);
+      if(null==ds)
+         throw new IllegalArgumentException("No DataSource for reference '" + reference + "'");
+
+      return ds;
+   }
+
+   public boolean hasDataSource(String reference)
+   {
+      return datasources.containsKey(reference);
+   }
+
+
+}


Property changes on: sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/DataSourceManager.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Copied: sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/internal/DBCPDataSourceFactory.java (from rev 241, sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/datasource/DBCPDataSourceFactory.java)
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/internal/DBCPDataSourceFactory.java	                        (rev 0)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/internal/DBCPDataSourceFactory.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -0,0 +1,90 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.datasource.internal;
+
+import org.jboss.sam.config.DataSourceDef;
+import org.jboss.sam.datasource.DataSourceFactory;
+import org.apache.commons.pool.ObjectPool;
+import org.apache.commons.pool.impl.GenericObjectPool;
+import org.apache.commons.dbcp.ConnectionFactory;
+import org.apache.commons.dbcp.DriverManagerConnectionFactory;
+import org.apache.commons.dbcp.PoolableConnectionFactory;
+import org.apache.commons.dbcp.PoolingDataSource;
+
+import javax.sql.DataSource;
+
+/**
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class DBCPDataSourceFactory extends DataSourceFactory
+{
+
+   public DBCPDataSourceFactory()
+   {
+   }
+
+   public DataSource createDataSource(DataSourceDef dsDef)
+   {
+      try {
+         Class.forName(dsDef.getDriver());
+      }
+      catch (ClassNotFoundException e)
+      {
+         throw new RuntimeException("Failed to load JDBC driver: " + dsDef.getDriver(), e);
+      }
+
+      //
+      // First, we'll need a ObjectPool that serves as the
+      // actual pool of connections.
+      //
+      // We'll use a GenericObjectPool instance, although
+      // any ObjectPool implementation will suffice.
+      //
+      ObjectPool connectionPool = new GenericObjectPool(null);
+
+      //
+      // Next, we'll create a ConnectionFactory that the
+      // pool will use to create Connections.
+      // We'll use the DriverManagerConnectionFactory,
+      // using the connect string passed in the command line
+      // arguments.
+      //
+      ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dsDef.getUrl(),null);
+
+      //
+      // Now we'll create the PoolableConnectionFactory, which wraps
+      // the "real" Connections created by the ConnectionFactory with
+      // the classes that implement the pooling functionality.
+      //
+      PoolableConnectionFactory poolableConnectionFactory =
+        new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
+
+      //
+      // Finally, we create the PoolingDriver itself,
+      // passing in the object pool we created.
+      //
+      PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
+
+      return dataSource;
+
+   }
+}


Property changes on: sam/trunk/modules/core/src/main/java/org/jboss/sam/datasource/internal/DBCPDataSourceFactory.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Added: sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/DefaultRuntimeFactory.java
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/DefaultRuntimeFactory.java	                        (rev 0)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/DefaultRuntimeFactory.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.internal;
+
+import org.jboss.sam.SAMRuntimeFactory;
+import org.jboss.sam.SAMRuntime;
+import org.jboss.sam.config.Configurator;
+
+/**
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class DefaultRuntimeFactory extends SAMRuntimeFactory
+{
+   public SAMRuntime createRuntime(Configurator configurator)
+   {
+      DefaultRuntimeImpl defaultRuntime = new DefaultRuntimeImpl();
+      defaultRuntime.applyConfiguration(configurator);
+      return defaultRuntime;
+   }
+}


Property changes on: sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/DefaultRuntimeFactory.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/DefaultRuntimeImpl.java
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/DefaultRuntimeImpl.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/DefaultRuntimeImpl.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -22,16 +22,45 @@
 package org.jboss.sam.internal;
 
 import org.jboss.sam.SAMRuntime;
+import org.jboss.sam.EventProcessor;
+import org.jboss.sam.EventProcessorFactory;
+import org.jboss.sam.SAMRuntimeAware;
+import org.jboss.sam.datasource.DataSourceManager;
 import org.jboss.sam.config.Configurator;
 
 /**
+ * Glues together SAM building blocks.
+ * 
  * @author Heiko.Braun <heiko.braun at jboss.com>
  */
-public class DefaultRuntimeImpl extends SAMRuntime
+public class DefaultRuntimeImpl implements SAMRuntime
 {
+   private EventProcessor eventProcessor;
+   private DataSourceManager dataSourceManager;
 
-   public DefaultRuntimeImpl(Configurator configurator)
+   DefaultRuntimeImpl()
    {
-      super(configurator);
+      // create and configure a processor
+      this.eventProcessor = EventProcessorFactory.newInstance().createProcessor();
+      ((SAMRuntimeAware)this.eventProcessor).setRuntime(this);
+
+      // create and configure datasources
+      this.dataSourceManager = new DataSourceManager();
    }
+
+   public void applyConfiguration(Configurator cfg)
+   {
+      cfg.configure(this.eventProcessor);
+      cfg.configure(this.dataSourceManager);      
+   }
+
+   public DataSourceManager getDataSourceManager()
+   {
+      return this.dataSourceManager;
+   }
+
+   public EventProcessor getEventProcessor()
+   {
+      return this.eventProcessor;
+   }
 }

Modified: sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/drools/DroolsEventProcessorImpl.java
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/drools/DroolsEventProcessorImpl.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/drools/DroolsEventProcessorImpl.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -31,6 +31,7 @@
 import org.drools.lang.descr.PackageDescr;
 import org.drools.spi.GlobalResolver;
 import org.jboss.sam.*;
+import org.jboss.sam.datasource.DataSourceManager;
 
 import java.io.*;
 import java.util.Iterator;
@@ -57,8 +58,8 @@
    private Map<String, Object> wmGlobals = new ConcurrentHashMap<String, Object>();
    private Map<String, StreamOutputAdapter> outputAdapters = new ConcurrentHashMap<String, StreamOutputAdapter>();
 
-   private final static String SAM_ADAPTER_NAME = "SAM";
-   
+   private SAMRuntime runtime;
+
    public DroolsEventProcessorImpl()
    {
       try
@@ -73,13 +74,21 @@
       }
    }
 
+   public void setRuntime(SAMRuntime runtime)
+   {
+      this.runtime = runtime;
+   }
+
    public Object resolveGlobal(String key)
    {
+      assert runtime!=null : "Runtime not set!";
+      
       Object global = null;
       
-      if(SAM_ADAPTER_NAME.equals(key))
-      {
-         global = new SAMAdapter(this.session, this.outputAdapters);
+      if("SAM".equals(key))
+      {         
+         DataSourceManager dsMgr = this.runtime.getDataSourceManager();         
+         global = new SAMAdapter(this.session, this.outputAdapters, dsMgr);
       }
       else
       {
@@ -109,10 +118,10 @@
    }
 
    public void onEvent(Object args)
-   {
+   {      
       session.insert(args);      
 
-      // TODO: shouldn't be necessary
+      // TODO: shouldn't be necessary, revisit!
       session.fireAllRules();      
    }
 

Modified: sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/drools/SAMAdapter.java
===================================================================
--- sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/drools/SAMAdapter.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/main/java/org/jboss/sam/internal/drools/SAMAdapter.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -23,15 +23,21 @@
 
 import org.drools.StatefulSession;
 import org.drools.WorkingMemoryEntryPoint;
+import org.jboss.sam.datasource.DataSourceManager;
 
+import javax.sql.DataSource;
 import java.util.Map;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.sql.SQLException;
 
 /**
- * Will be injected into WorkingMemory to give access drools entities that
- * are relevant to the SAM processing model. This class basically acts as a workaround for
- * features that we would like to see:
+ * Will be injected into WorkingMemory to give drools access to SAM extensions.<br>
+ * This class basically acts as a workaround for features that we would like to see:
  *
  * <ul>
  * <li>Hidden behind a custom SAM DSL
@@ -44,11 +50,13 @@
 {
    private StatefulSession session;
    private Map<String, StreamOutputAdapter> outputAdapters;
+   private DataSourceManager datasourceManager;
 
-   public SAMAdapter(StatefulSession session, Map<String, StreamOutputAdapter> outputAdapters)
+   public SAMAdapter(StatefulSession session, Map<String, StreamOutputAdapter> outputAdapters, DataSourceManager dsMgr)
    {
       this.session = session;
       this.outputAdapters = outputAdapters;
+      this.datasourceManager = dsMgr;
    }
 
    public WorkingMemoryEntryPoint getStream(String name)
@@ -68,12 +76,44 @@
       return adapter;
    }
 
-   public List executeSQL(String dbName, String sql)
+   public List query(String dbName, String sql)
    {
-      System.out.println("SQL: " + sql);
+      DataSource dataSource = datasourceManager.getDataSource(dbName);
       ArrayList results = new ArrayList();
-      results.add("Hello");
-      results.add("World");
+      
+      Connection conn = null;
+      Statement stmt = null;
+      ResultSet rset = null;
+
+      try
+      {
+         conn = dataSource.getConnection();
+         stmt = conn.createStatement();
+         rset = stmt.executeQuery(sql);
+
+         int numcols = rset.getMetaData().getColumnCount();
+
+         while(rset.next())
+         {
+            Map row = new HashMap();
+            for(int i=1;i<=numcols;i++)
+            {
+               row.put(rset.getMetaData().getColumnName(i), rset.getObject(i));
+            }
+            results.add(row);
+         }
+      }
+      catch(SQLException e)
+      {
+         e.printStackTrace();
+      }
+      finally
+      {
+         try { rset.close(); } catch(Exception e) { }
+         try { stmt.close(); } catch(Exception e) { }
+         try { conn.close(); } catch(Exception e) { }
+      }
+      
       return results;
    }   
 }

Modified: sam/trunk/modules/core/src/test/java/org/jboss/test/sam/InMemoryDatabase.java
===================================================================
--- sam/trunk/modules/core/src/test/java/org/jboss/test/sam/InMemoryDatabase.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/test/java/org/jboss/test/sam/InMemoryDatabase.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -21,6 +21,7 @@
  */
 package org.jboss.test.sam;
 
+import javax.sql.DataSource;
 import java.sql.*;
 
 /**
@@ -43,6 +44,18 @@
    private static final String USER = "sa";
    private static final String PASSWORD = "";
 
+
+   public InMemoryDatabase(DataSource dataSource)
+   {
+      try
+      {
+         conn = dataSource.getConnection();
+      } catch (SQLException e)
+      {
+         throw new RuntimeException("Failed to setup HSQL database", e);
+      }
+   }
+
    public InMemoryDatabase()
    {
       try
@@ -97,7 +110,7 @@
       }
 
       st.close();
-   }    // void update()
+   }    
 
    public void shutdown() throws SQLException {
 

Modified: sam/trunk/modules/core/src/test/java/org/jboss/test/sam/SamTestSetup.java
===================================================================
--- sam/trunk/modules/core/src/test/java/org/jboss/test/sam/SamTestSetup.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/test/java/org/jboss/test/sam/SamTestSetup.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -22,14 +22,13 @@
 package org.jboss.test.sam;
 
 import junit.framework.TestCase;
-import org.jboss.sam.EventProcessor;
-import org.jboss.sam.EventProcessorFactory;
-import org.jboss.sam.StreamInput;
+import org.jboss.sam.*;
 import org.jboss.sam.config.ConfigFactory;
 import org.jboss.sam.config.Configurator;
 import org.jboss.sam.internal.stream.InVMDispatcher;
 import org.jboss.sam.internal.stream.InVMStreamInput;
 
+import javax.sql.DataSource;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -42,6 +41,7 @@
  */
 public abstract class SamTestSetup extends TestCase
 {
+   protected SAMRuntime runtime;
    protected EventProcessor eventProcessor;
    protected StreamInput streamInput;
    protected BufferedStreamOutput bufferedOutput;
@@ -57,19 +57,18 @@
     * @param name the runtime name
     * @param configFile name of the esper-config file relative to test execution
     */
-   public void createProcessorWithDefaultIO(String name, String configFile)
+   public void createProcessor(String name, String configFile)
    {
+      assert configFile!=null : "No configFile!";
+      
       // Main runtime
-      eventProcessor = EventProcessorFactory.newInstance().createProcessor();
+      URL config = loadTestConfiguration(configFile);
+      Configurator configurator = new Configurator( ConfigFactory.newInstance().unmarshall(config) );
 
-      URL config = configFile!=null ? loadTestConfiguration(configFile) : null;
-      if(config!=null)
-      {
-         Configurator configurator = new Configurator( ConfigFactory.newInstance().unmarshall(config) );
-         configurator.configure(eventProcessor);
-      }
+      runtime = SAMRuntimeFactory.newInstance().createRuntime(configurator);
+      eventProcessor = runtime.getEventProcessor();
 
-        // Stream input and output
+      // Stream input and output
       streamInput = new InVMStreamInput("InVM").start();
       dispatcher = new InVMDispatcher(streamInput.getStreamName());
 
@@ -89,6 +88,13 @@
       return inMemoryDB;
    }
 
+   protected InMemoryDatabase createDatabase(DataSource dataSource)
+   {
+      InMemoryDatabase inMemoryDB = new InMemoryDatabase(dataSource);
+      System.out.println("Database has been setup: " + inMemoryDB);
+      return inMemoryDB;
+   }
+
    protected void generateInVMEvent(Object obj)
    {
       dispatcher.dispatch(obj);

Modified: sam/trunk/modules/core/src/test/java/org/jboss/test/sam/adapter/invm/InVMAdapterTestCase.java
===================================================================
--- sam/trunk/modules/core/src/test/java/org/jboss/test/sam/adapter/invm/InVMAdapterTestCase.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/test/java/org/jboss/test/sam/adapter/invm/InVMAdapterTestCase.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -21,7 +21,6 @@
  */
 package org.jboss.test.sam.adapter.invm;
 
-import org.jboss.sam.ProcessingStatement;
 import org.jboss.test.sam.SamTestSetup;
 
 /**
@@ -31,7 +30,7 @@
 {
    public void setUp()
    {
-      createProcessorWithDefaultIO("InVMAdapterTestCase", "adapter/invm/sam-cfg.xml");
+      createProcessor("InVMAdapterTestCase", "adapter/invm/sam-cfg.xml");
    }
    
    public void testAdapter() throws Exception

Modified: sam/trunk/modules/core/src/test/java/org/jboss/test/sam/database/DatabaseTestCase.java
===================================================================
--- sam/trunk/modules/core/src/test/java/org/jboss/test/sam/database/DatabaseTestCase.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/test/java/org/jboss/test/sam/database/DatabaseTestCase.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -37,11 +37,13 @@
    public void setUp() throws Exception
    {
       // Runtime
-      createProcessorWithDefaultIO("DataBaseTestCase", "database/sam-cfg.xml");
+      createProcessor("DataBaseTestCase", "database/sam-cfg.xml");
 
       // InMemory Database
-      db = createDatabase();
-
+      db = createDatabase(
+        runtime.getDataSourceManager().getDataSource("hsqlDB")
+      );
+      
       // test schema
       db.update("CREATE TABLE quotes (" +
         "  symbol varchar(10) NOT NULL," +
@@ -89,7 +91,7 @@
       for(EventMessage event : eventMessages)
       {
          System.out.println("!! " + event.getUnderlying() );
-      } 
+      }
    }
 
    public void testEsperPullAPI() throws Exception

Modified: sam/trunk/modules/core/src/test/java/org/jboss/test/sam/drools/ApiTestCase.java
===================================================================
--- sam/trunk/modules/core/src/test/java/org/jboss/test/sam/drools/ApiTestCase.java	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/test/java/org/jboss/test/sam/drools/ApiTestCase.java	2008-08-13 14:42:18 UTC (rev 243)
@@ -34,7 +34,7 @@
    protected void setUp() throws Exception
    {
       // Creates default adapter inputs and outputs
-      createProcessorWithDefaultIO("ApiTestCase", null);
+      createProcessor("ApiTestCase", "drools/sam-cfg.xml");
    }
 
    public void testBoundStatement()

Modified: sam/trunk/modules/core/src/test/resources/database/sam-cfg.xml
===================================================================
--- sam/trunk/modules/core/src/test/resources/database/sam-cfg.xml	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/test/resources/database/sam-cfg.xml	2008-08-13 14:42:18 UTC (rev 243)
@@ -1,8 +1,23 @@
 <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
 <ns2:processingNode name="DatabaseTestCase"
-                    domain="http://org.jboss.sam/test/database" 
+                    domain="http://org.jboss.sam/test/database"
                     xmlns:ns2="http://org.jboss.sam/08/2008/">
 
+   <properties/>
+
+   <database-references>
+      <database-reference name="hsqlDB">
+         <datasource url="jdbc:hsqldb:mem:SamTestSetup" name="hsqlDS" driver="org.hsqldb.jdbcDriver">
+            <connection-arg value="sa" name="user"/>
+            <connection-arg value="" name="password"/>
+         </datasource>
+      </database-reference>
+   </database-references>
+
+   <inputs/>
+
+   <outputs/>
+
    <statements/>
-   
+
 </ns2:processingNode>
\ No newline at end of file

Modified: sam/trunk/modules/core/src/test/resources/database/test.drl
===================================================================
--- sam/trunk/modules/core/src/test/resources/database/test.drl	2008-08-13 14:40:01 UTC (rev 242)
+++ sam/trunk/modules/core/src/test/resources/database/test.drl	2008-08-13 14:42:18 UTC (rev 243)
@@ -1,6 +1,6 @@
 package org.jboss.test.sam.database;
 
-import java.util.ArrayList;
+import java.util.*;
 
 global org.jboss.sam.internal.drools.SAMAdapter SAM;
 
@@ -8,7 +8,7 @@
 when
    $query: Query();
    $sqlStream: ArrayList() from collect (
-      String() from SAM.executeSQL("hsqlDB", "SELECT * FROM StockQuotes WHERE symbol='"+$query.criteria+"'")
+      Map() from SAM.query("hsqlDB", "SELECT * FROM quotes WHERE symbol='"+$query.criteria+"'")
       );
 then
    SAM.getListener("InVM").update($sqlStream);

Added: sam/trunk/modules/core/src/test/resources/drools/sam-cfg.xml
===================================================================
--- sam/trunk/modules/core/src/test/resources/drools/sam-cfg.xml	                        (rev 0)
+++ sam/trunk/modules/core/src/test/resources/drools/sam-cfg.xml	2008-08-13 14:42:18 UTC (rev 243)
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
+<ns2:processingNode name="DatabaseTestCase"
+                    domain="http://org.jboss.sam/test/database"
+                    xmlns:ns2="http://org.jboss.sam/08/2008/">
+
+   <statements/>
+
+</ns2:processingNode>
\ No newline at end of file


Property changes on: sam/trunk/modules/core/src/test/resources/drools/sam-cfg.xml
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF




More information about the overlord-commits mailing list