Question about Cougaar Binding a new componentService

Todd Wright twright at bbn.com
Tue Feb 19 12:13:13 EST 2008


Your SqlServiceComponent should advertise the service in the node-level "root" 
ServiceBroker.  See:
   http://www.cougaar.org/pipermail/cougaar-developers/2004/001111.html

Todd

Martijn Broos wrote:
> Hi,
> 
> I am writing an service that should run on Node level which is actually
> working as an SQL server. It should be a service which each node
> provides to their agents and setup an connection to the database and
> executes a query sending back the resultset to the requestor.
> 
> Attached is the code which I have.
> Now when I attach the Service to the node, it is registered at the
> insertionpoint:
> 
>         <node name="SysteemNode">
>             <component
> 
> class='nl.decis.masans.util.SqlService.SqlServiceComponent'
>                     priority='HIGH'
> 
> insertionpoint="Node.AgentManager.Agent.Component"
>             />
>           ETC.....
> 
> This means now that the service is running at node level in the conde
> agetn.component. I assume that everything below, (agents, agentsplugins
> etc, are now able to get there by the getservicebroker() function)
> but when I create a agent with a plugin (see attached society.xml
> snippet) then it will not find the right service.
>             <agent name="BootstrapAgent">
> 
>                 <component
> 
> class="nl.decis.masans.util.SqlService.SQLServiceTestPlugin" />
>             </agent>
> 
> Can you help me out or explain what I am doing wrong?
> I have tested it attaching the service to the agent and that is working,
> but I only want one service per node instead of a service per agent.
> BTW this service is derived from the UIDService which I found out is
> attached to each agent.
> 
> kind regards,
> 
> Martijn Broos,
> Y'all
> 
> 
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> <?xml version='1.0'?>
> <society
>   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>   name="MASANS-SOCIETY" 
>   xsi:noNamespaceSchemaLocation="http://www.cougaar.org/2003/society.xsd">
> 	<host name="localhost">
> 		<node name="SysteemNode">
> 			<component
> 					class='nl.decis.masans.util.SqlService.SqlServiceComponent'
> 					priority='HIGH'
> 					insertionpoint="Node.AgentManager.Agent.Component"				
> 			/>
> 			<component 
>   				class='org.cougaar.core.util.ComponentViewServlet'> 
>   				<argument>/components</argument> 
> 			</component> 
> 					
> 			<!--				White page server			-->			
> 			<component class="org.cougaar.core.wp.server.Server"				
> 						insertionpoint="Node.AgentManager.Agent.WPServer">			
> 			</component>			
> 			<!--				Mobility plugin 			-->			
> 			<component	name='org.cougaar.core.mobility.service.RootMobilityPlugin()'				
> 						class='org.cougaar.core.mobility.service.RootMobilityPlugin'				
> 						priority='COMPONENT'				
> 						insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'>			
> 			</component>
> 			<!--  BOOTSTRAP AGENT -->
> 			<!--  This agent may not be renamed. In the BootstrapPlugin this name is used 
> 			      to leave the  Machinist Community after startup!
> 			-->
> 			<agent name="BootstrapAgent">
> 				<component
> 					class="nl.decis.masans.cougaar.plugin.BootstrapPlugin" />
> 				<component
> 					class="nl.decis.masans.cougaar.plugin.SQLPlugin" />
> 				<component
> 					name='nl.decis.masans.cougaar.plugin.AgentLoaderPlugin()'
> 					class='nl.decis.masans.cougaar.plugin.AgentLoaderPlugin'
> 					priority='COMPONENT'
> 					insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin' />
> 				<component
> 					class="nl.decis.masans.util.SqlService.SQLServiceTestPlugin" />	
> 			</agent>
> 
> 
> 			<!--  BIJSTUURDER AGENT -->
> 			<agent name="Bijstuurder_1">
> 				<component
> 					class="nl.decis.masans.cougaar.plugin.BijstuurderPlugin" />
> 				<component
> 					class="nl.decis.masans.cougaar.plugin.SQLPlugin" />
>  			    <component
> 		          	name='org.cougaar.community.util.CommunityViewerServlet()'
>   		          	class='org.cougaar.community.util.CommunityViewerServlet'
>   		          	priority='COMPONENT'
>   		          	insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'>
>  		        </component>
> 			    <component
> 		          	name='nl.decis.masans.cougaar.servlet.BijstuurderServlet()'
>   		          	class='nl.decis.masans.cougaar.servlet.BijstuurderServlet'
>   		          	priority='COMPONENT'
>   		          	insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'>
>  		        </component>
> 		        <component
>  		         	name='nl.decis.masans.cougaar.plugin.HistoryServlet'
> 	 	         	class='nl.decis.masans.cougaar.plugin.HistoryServlet'
>  		         	priority='COMPONENT'
>  		         	insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'>
> 				</component>
> 			</agent>
> 			<!--  Reisplanner agent -->
> 			<agent name="Reisplanner_1">
> 				<component
> 					class="nl.decis.masans.cougaar.plugin.ReisplannerPlugin" />
> 				<component
> 					class="nl.decis.masans.cougaar.plugin.SQLPlugin" />
>  			    <component
>  		         	name='nl.decis.masans.cougaar.plugin.HistoryServlet'
> 	 	         	class='nl.decis.masans.cougaar.plugin.HistoryServlet'
>  		         	priority='COMPONENT'
>  		         	insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'>
> 				</component>
> 				<component
> 					name='nl.decis.masans.cougaar.plugin.AgentLoaderPlugin()'
> 					class='nl.decis.masans.cougaar.plugin.AgentLoaderPlugin'
> 					priority='COMPONENT'
> 					insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin' />
> 				</agent>
> 
> 		</node>
> 	</host>
> </society>
> 
> 
> ------------------------------------------------------------------------
> 
> package nl.decis.masans.util.SqlService;
> 
> 
> import nl.decis.masans.util.PropertyManager;
> 
> import org.cougaar.core.blackboard.BlackboardClient;
> import org.cougaar.core.component.BindingSite;
> import org.cougaar.core.plugin.ComponentPlugin;
> import org.cougaar.core.service.LoggingService;
> import org.apache.log4j.PropertyConfigurator;
> import java.sql.*;
> 
> public class SQLServiceTestPlugin extends ComponentPlugin implements BlackboardClient{
> 
> 	private LoggingService log;
> 	private SqlService sql;
> 	private PropertyConfigurator propertyConfigurator = new PropertyConfigurator();
> 
> 	/** This method is called when the agent is created. */
> 	public void load() {
> 		super.load();
> 		// Cougaar logging
> 		log = (LoggingService) getServiceBroker().getService(this,
> 				LoggingService.class, null);
> 		sql = (SqlService) getServiceBroker().getService(this, SqlService.class, null);
> 		if (sql == null)
> 		{
> 			log.fatal("SQL object is not instantiated appropriate, so do not call it because it is null");
> 		}
> 				
> 	}
> 	
> //	public void start()
> //	{
> //		ResultSet test = sql.executeQuery("SELECT count( * ) FROM `personeelslid`", null);
> //		try
> //		{		
> //			while(test.next())
> //			{
> //				log.fatal("Number of records in personeelslid:" + test.getObject(0).toString());
> //			}
> //		} 
> //		catch (SQLException e) 
> //		{
> //			e.printStackTrace();
> //		}
> //	}
> 
> 	protected void execute() {		
> 	}
> 
> 	protected void setupSubscriptions() {
> 	}
> 	
> 
> }
> 
> 
> 
> ------------------------------------------------------------------------
> 
> package nl.decis.masans.util.SqlService;
> 
> 
> import java.sql.ResultSet;
> import java.util.Vector;
> import org.cougaar.core.component.Service;
> 
> public interface SqlService extends Service {
> 	ResultSet executeQuery(String query, Vector<Object> parameters);
> 		
> }
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> package nl.decis.masans.util.SqlService;
> 
> import org.cougaar.core.component.Component;
> import org.cougaar.core.component.ServiceBroker;
> import org.cougaar.core.component.ServiceProvider;
> import org.cougaar.core.mts.MessageAddress;
> import org.cougaar.core.service.AgentIdentificationService;
> import org.cougaar.util.GenericStateModelAdapter;
> 
> public final class SqlServiceComponent extends GenericStateModelAdapter implements Component 
> {
> 	  private ServiceBroker sb;
> 	  private SqlService sqlS;
> 	  private SqlServiceProvider sqlSP;
> 	  
> 	  public void setServiceBroker(ServiceBroker sb) 
> 	  {
> 	    this.sb = sb;
> 	  }
> 	  
> 	  public void load() {
> 		  	  super.load();
> 			  this.sqlS = new SqlServiceImpl();
> 			  this.sqlSP = new SqlServiceProvider();
> 			  boolean state = sb.addService(SqlService.class, sqlSP);
> 		  }
> 
> 	  public void unload() {
> 		    // revoke our service
> 		    if (sqlSP != null) {
> 		      sb.revokeService(SqlService.class, sqlSP);
> 		      sqlSP = null;
> 		    }
> 		    super.unload();
> 		  }
> 		
> 	  private class SqlServiceProvider implements ServiceProvider {
> 			public Object getService(ServiceBroker sb, Object requestor, Class serviceClass) 
> 		    {
> 		      if (SqlService.class.isAssignableFrom(serviceClass)) 
> 		      {
> 		        return sqlS;
> 		      } 
> 		      else 
> 		      {
> 		        return null;
> 		      }
> 		    }
> 
> 			public void releaseService(
> 		        ServiceBroker sb, Object requestor, 
> 		        Class serviceClass, Object service)  {
> 		    }
> 
> 		  }	  
> }
> 
> 
> 
> ------------------------------------------------------------------------
> 
> package nl.decis.masans.util.SqlService;
> 
> import java.sql.Connection;
> import java.sql.PreparedStatement;
> import java.sql.ResultSet;
> import java.sql.SQLException;
> import java.util.Vector;
> 
> import org.apache.log4j.PropertyConfigurator;
> import org.cougaar.core.mts.MessageAddress;
> 
> import nl.decis.masans.database.ConnectionManager;
> import nl.decis.masans.util.PropertyManager;
> 
> public class SqlServiceImpl implements SqlService {
> 	
> 	public SqlServiceImpl()
> 	{
> 		PropertyManager.getInstance().load(
> 				System.getProperty("nl.decis.masans.database.propertyfile"));
> 
> 		// Configure logging
> 		PropertyConfigurator.configure(System
> 				.getProperty("nl.decis.masans.database.propertyfile"));
> 
> 	}
> 	
> 	public ResultSet executeQuery(String query, Vector<Object> parameters) {
> 		PreparedStatement stmt = null;
> 		ResultSet rs = null;
> 		Connection con = null;
> 		try {
> 
>             ConnectionManager conMan = new ConnectionManager();
>             conMan.openConnection();
>             con = conMan.getConnection();
> 			// Execute the query
> 			stmt = con.prepareStatement(query);
> 			if (parameters != null) {
> 				for (int i = 0; i < parameters.size(); i++) {
> 					stmt.setObject(i+1, parameters.get(i));
> 				}
> 			}
> 			//System.err.println("SOLVER: query: " + stmt.toString());
> 			rs = stmt.executeQuery();
> 		} catch (SQLException e) {
> 			e.printStackTrace();
> 		} finally {
>             try { stmt.close(); } catch(Exception e) {e.printStackTrace(); }
>             try { con.close(); } catch(Exception e) {e.printStackTrace(); }				
> 		}
> 		return rs;
> 	}
> 
> }
> 
> 
> _______________________________________________
> Cougaar-developers mailing list
> Cougaar-developers at cougaar.org
> http://cougaar.org/mailman/listinfo/cougaar-developers



More information about the Cougaar-developers mailing list