From martijn at yall.nl Mon Feb 18 05:30:53 2008 From: martijn at yall.nl (Martijn Broos) Date: Mon, 18 Feb 2008 11:30:53 +0100 Subject: Question about Cougaar Binding a new componentService Message-ID: <47B95E5D.3090702@yall.nl> 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: 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. 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 -------------- next part -------------- A non-text attachment was scrubbed... Name: MASANSSociety.xml Type: text/xml Size: 3631 bytes Desc: not available Url : http://cougaar.org/pipermail/cougaar-developers/attachments/20080218/51675f3f/MASANSSociety.xml -------------- next part -------------- A non-text attachment was scrubbed... Name: SQLServiceTestPlugin.java Type: text/java Size: 1419 bytes Desc: not available Url : http://cougaar.org/pipermail/cougaar-developers/attachments/20080218/51675f3f/SQLServiceTestPlugin.bin -------------- next part -------------- A non-text attachment was scrubbed... Name: SqlService.java Type: text/java Size: 258 bytes Desc: not available Url : http://cougaar.org/pipermail/cougaar-developers/attachments/20080218/51675f3f/SqlService.bin -------------- next part -------------- A non-text attachment was scrubbed... Name: SqlServiceComponent.java Type: text/java Size: 1487 bytes Desc: not available Url : http://cougaar.org/pipermail/cougaar-developers/attachments/20080218/51675f3f/SqlServiceComponent.bin -------------- next part -------------- A non-text attachment was scrubbed... Name: SqlServiceImpl.java Type: text/java Size: 1536 bytes Desc: not available Url : http://cougaar.org/pipermail/cougaar-developers/attachments/20080218/51675f3f/SqlServiceImpl.bin -------------- next part -------------- A non-text attachment was scrubbed... Name: martijn.vcf Type: text/x-vcard Size: 271 bytes Desc: not available Url : http://cougaar.org/pipermail/cougaar-developers/attachments/20080218/51675f3f/martijn.vcf From twright at bbn.com Tue Feb 19 12:13:13 2008 From: twright at bbn.com (Todd Wright) Date: Tue, 19 Feb 2008 12:13:13 -0500 Subject: Question about Cougaar Binding a new componentService In-Reply-To: <47B95E5D.3090702@yall.nl> References: <47B95E5D.3090702@yall.nl> Message-ID: <47BB0E29.5070205@bbn.com> 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: > > > > 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. > > > > class="nl.decis.masans.util.SqlService.SQLServiceTestPlugin" /> > > > 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 > > > > > > > ------------------------------------------------------------------------ > > > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > name="MASANS-SOCIETY" > xsi:noNamespaceSchemaLocation="http://www.cougaar.org/2003/society.xsd"> > > > class='nl.decis.masans.util.SqlService.SqlServiceComponent' > priority='HIGH' > insertionpoint="Node.AgentManager.Agent.Component" > /> > class='org.cougaar.core.util.ComponentViewServlet'> > /components > > > > insertionpoint="Node.AgentManager.Agent.WPServer"> > > > class='org.cougaar.core.mobility.service.RootMobilityPlugin' > priority='COMPONENT' > insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'> > > > > > class="nl.decis.masans.cougaar.plugin.BootstrapPlugin" /> > class="nl.decis.masans.cougaar.plugin.SQLPlugin" /> > name='nl.decis.masans.cougaar.plugin.AgentLoaderPlugin()' > class='nl.decis.masans.cougaar.plugin.AgentLoaderPlugin' > priority='COMPONENT' > insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin' /> > class="nl.decis.masans.util.SqlService.SQLServiceTestPlugin" /> > > > > > > class="nl.decis.masans.cougaar.plugin.BijstuurderPlugin" /> > class="nl.decis.masans.cougaar.plugin.SQLPlugin" /> > name='org.cougaar.community.util.CommunityViewerServlet()' > class='org.cougaar.community.util.CommunityViewerServlet' > priority='COMPONENT' > insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'> > > name='nl.decis.masans.cougaar.servlet.BijstuurderServlet()' > class='nl.decis.masans.cougaar.servlet.BijstuurderServlet' > priority='COMPONENT' > insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'> > > name='nl.decis.masans.cougaar.plugin.HistoryServlet' > class='nl.decis.masans.cougaar.plugin.HistoryServlet' > priority='COMPONENT' > insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'> > > > > > class="nl.decis.masans.cougaar.plugin.ReisplannerPlugin" /> > class="nl.decis.masans.cougaar.plugin.SQLPlugin" /> > name='nl.decis.masans.cougaar.plugin.HistoryServlet' > class='nl.decis.masans.cougaar.plugin.HistoryServlet' > priority='COMPONENT' > insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin'> > > name='nl.decis.masans.cougaar.plugin.AgentLoaderPlugin()' > class='nl.decis.masans.cougaar.plugin.AgentLoaderPlugin' > priority='COMPONENT' > insertionpoint='Node.AgentManager.Agent.PluginManager.Plugin' /> > > > > > > > > ------------------------------------------------------------------------ > > 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 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 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