You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2011/08/12 15:26:15 UTC
svn commit: r1157095 -
/incubator/lcf/trunk/framework/jetty-runner/src/main/java/org/apache/manifoldcf/jettyrunner/ManifoldCFJettyRunner.java
Author: kwright
Date: Fri Aug 12 13:26:15 2011
New Revision: 1157095
URL: http://svn.apache.org/viewvc?rev=1157095&view=rev
Log:
Reorganize the jetty-runner class to break out the various bits of functionality into static methods. Part of CONNECTORS-61.
Modified:
incubator/lcf/trunk/framework/jetty-runner/src/main/java/org/apache/manifoldcf/jettyrunner/ManifoldCFJettyRunner.java
Modified: incubator/lcf/trunk/framework/jetty-runner/src/main/java/org/apache/manifoldcf/jettyrunner/ManifoldCFJettyRunner.java
URL: http://svn.apache.org/viewvc/incubator/lcf/trunk/framework/jetty-runner/src/main/java/org/apache/manifoldcf/jettyrunner/ManifoldCFJettyRunner.java?rev=1157095&r1=1157094&r2=1157095&view=diff
==============================================================================
--- incubator/lcf/trunk/framework/jetty-runner/src/main/java/org/apache/manifoldcf/jettyrunner/ManifoldCFJettyRunner.java (original)
+++ incubator/lcf/trunk/framework/jetty-runner/src/main/java/org/apache/manifoldcf/jettyrunner/ManifoldCFJettyRunner.java Fri Aug 12 13:26:15 2011
@@ -138,123 +138,204 @@ public class ManifoldCFJettyRunner
return conns[0].getLocalPort();
}
- /**
- * A main class that starts jetty+mcf
- */
- public static void main( String[] args )
+ /** Clear the agents shutdown signal */
+ public static void clearShutdownSignal(IThreadContext tc)
+ throws ManifoldCFException
{
- if (args.length != 4 && args.length != 1 && args.length != 0)
- {
- System.err.println("Usage: ManifoldCFJettyRunner [<port> [<crawler-war-path> <authority-service-war-path> <api-war-path>]]");
- System.exit(1);
- }
+ // Clear the agents shutdown signal.
+ ILockManager lockManager = LockManagerFactory.make(tc);
+ lockManager.clearGlobalFlag(agentShutdownSignal);
+ }
- int jettyPort = 8345;
- if (args.length > 0)
+ /** Create the database and the schema */
+ public static void createDatabaseAndSchema(IThreadContext tc, String superuserName, String superuserPassword)
+ throws ManifoldCFException
+ {
+ ManifoldCF.createSystemDatabase(tc,superuserName,superuserPassword);
+ ManifoldCF.installTables(tc);
+ }
+
+ /** Register the agents */
+ public static void registerAgents(IThreadContext tc)
+ throws ManifoldCFException
+ {
+ // Register
+ IAgentManager agentMgr = AgentManagerFactory.make(tc);
+ agentMgr.registerAgent("org.apache.manifoldcf.crawler.system.CrawlerAgent");
+ }
+
+ /** Read connectors configuration file.
+ */
+ public static Connectors readConnectorDeclarations(File connectorConfigFile)
+ throws ManifoldCFException
+ {
+ Connectors c = null;
+ if (connectorConfigFile != null)
{
try
{
- jettyPort = Integer.parseInt(args[0]);
+ // Open the file, read it, and attempt to do the connector registrations
+ InputStream is = new FileInputStream(connectorConfigFile);
+ try
+ {
+ c = new Connectors(is);
+ }
+ finally
+ {
+ is.close();
+ }
}
- catch (NumberFormatException e)
+ catch (FileNotFoundException e)
{
- e.printStackTrace(System.err);
- System.exit(1);
+ throw new ManifoldCFException("Couldn't find connector configuration file: "+e.getMessage(),e);
+ }
+ catch (IOException e)
+ {
+ throw new ManifoldCFException("Error reading connector configuration file: "+e.getMessage(),e);
}
}
-
- String crawlerWarPath = "war/mcf-crawler-ui.war";
- String authorityserviceWarPath = "war/mcf-authority-service.war";
- String apiWarPath = "war/mcf-api-service.war";
- if (args.length == 4)
- {
- crawlerWarPath = args[1];
- authorityserviceWarPath = args[2];
- apiWarPath = args[3];
- }
-
- // Ready to begin in earnest...
- if (System.getProperty(ManifoldCF.lcfConfigFileProperty) == null)
- System.setProperty(ManifoldCF.lcfConfigFileProperty,"./properties.xml");
- try
- {
- ManifoldCF.initializeEnvironment();
- IThreadContext tc = ThreadContextFactory.make();
-
- // Clear the agents shutdown signal.
- ILockManager lockManager = LockManagerFactory.make(tc);
- lockManager.clearGlobalFlag(agentShutdownSignal);
-
- // Grab a database handle, so we can use transactions later.
- IDBInterface database = DBInterfaceFactory.make(tc,
- ManifoldCF.getMasterDatabaseName(),
- ManifoldCF.getMasterDatabaseUsername(),
- ManifoldCF.getMasterDatabasePassword());
+ return c;
+ }
- // Get the specified superuser name and password, in case this isn't Derby we're using
- String superuserName = ManifoldCF.getProperty(databaseSuperuserName);
- if (superuserName == null)
- superuserName = "";
- String superuserPassword = ManifoldCF.getProperty(databaseSuperuserPassword);
- if (superuserPassword == null)
- superuserPassword = "";
-
- // Do the basic initialization of the database and its schema
- ManifoldCF.createSystemDatabase(tc,superuserName,superuserPassword);
- ManifoldCF.installTables(tc);
- IAgentManager agentMgr = AgentManagerFactory.make(tc);
- agentMgr.registerAgent("org.apache.manifoldcf.crawler.system.CrawlerAgent");
+ /** Unregister all connectors.
+ */
+ public static void unregisterAllConnectors(IThreadContext tc)
+ throws ManifoldCFException
+ {
+ // Grab a database handle, so we can use transactions later.
+ IDBInterface database = DBInterfaceFactory.make(tc,
+ ManifoldCF.getMasterDatabaseName(),
+ ManifoldCF.getMasterDatabaseUsername(),
+ ManifoldCF.getMasterDatabasePassword());
- // Read connectors configuration file (to figure out what we need to register)
- Connectors c = null;
- File connectorConfigFile = ManifoldCF.getFileProperty(connectorsConfigurationFile);
- if (connectorConfigFile != null)
+ // Output connectors...
+ {
+ IOutputConnectorManager mgr = OutputConnectorManagerFactory.make(tc);
+ IOutputConnectionManager connManager = OutputConnectionManagerFactory.make(tc);
+ IResultSet classNames = mgr.getConnectors();
+ int i = 0;
+ while (i < classNames.getRowCount())
{
+ IResultRow row = classNames.getRow(i++);
+ String className = (String)row.getValue("classname");
+ // Deregistration should be done in a transaction
+ database.beginTransaction();
try
{
- // Open the file, read it, and attempt to do the connector registrations
- InputStream is = new FileInputStream(connectorConfigFile);
- try
- {
- c = new Connectors(is);
- }
- finally
- {
- is.close();
- }
+ // Find the connection names that come with this class
+ String[] connectionNames = connManager.findConnectionsForConnector(className);
+ // For all connection names, notify all agents of the deregistration
+ AgentManagerFactory.noteOutputConnectorDeregistration(tc,connectionNames);
+ // Now that all jobs have been placed into an appropriate state, actually do the deregistration itself.
+ mgr.unregisterConnector(className);
}
- catch (FileNotFoundException e)
+ catch (ManifoldCFException e)
{
- throw new ManifoldCFException("Couldn't find connector configuration file: "+e.getMessage(),e);
+ database.signalRollback();
+ throw e;
}
- catch (IOException e)
+ catch (Error e)
{
- throw new ManifoldCFException("Error reading connector configuration file: "+e.getMessage(),e);
+ database.signalRollback();
+ throw e;
+ }
+ finally
+ {
+ database.endTransaction();
}
}
+ System.err.println("Successfully unregistered all output connectors");
+ }
- // Unregister all connectors.
+ // Authority connectors...
+ {
+ IAuthorityConnectorManager mgr = AuthorityConnectorManagerFactory.make(tc);
+ IResultSet classNames = mgr.getConnectors();
+ int i = 0;
+ while (i < classNames.getRowCount())
+ {
+ IResultRow row = classNames.getRow(i++);
+ mgr.unregisterConnector((String)row.getValue("classname"));
+ }
+ System.err.println("Successfully unregistered all authority connectors");
+ }
- // Output connectors...
+ // Repository connectors...
+ {
+ IConnectorManager mgr = ConnectorManagerFactory.make(tc);
+ IJobManager jobManager = JobManagerFactory.make(tc);
+ IRepositoryConnectionManager connManager = RepositoryConnectionManagerFactory.make(tc);
+ IResultSet classNames = mgr.getConnectors();
+ int i = 0;
+ while (i < classNames.getRowCount())
{
- IOutputConnectorManager mgr = OutputConnectorManagerFactory.make(tc);
- IOutputConnectionManager connManager = OutputConnectionManagerFactory.make(tc);
- IResultSet classNames = mgr.getConnectors();
- int i = 0;
- while (i < classNames.getRowCount())
+ IResultRow row = classNames.getRow(i++);
+ String className = (String)row.getValue("classname");
+ // Deregistration should be done in a transaction
+ database.beginTransaction();
+ try
{
- IResultRow row = classNames.getRow(i++);
- String className = (String)row.getValue("classname");
- // Deregistration should be done in a transaction
+ // Find the connection names that come with this class
+ String[] connectionNames = connManager.findConnectionsForConnector(className);
+ // For each connection name, modify the jobs to note that the connector is no longer installed
+ jobManager.noteConnectorDeregistration(connectionNames);
+ // Now that all jobs have been placed into an appropriate state, actually do the deregistration itself.
+ mgr.unregisterConnector(className);
+ }
+ catch (ManifoldCFException e)
+ {
+ database.signalRollback();
+ throw e;
+ }
+ catch (Error e)
+ {
+ database.signalRollback();
+ throw e;
+ }
+ finally
+ {
+ database.endTransaction();
+ }
+ }
+ System.err.println("Successfully unregistered all repository connectors");
+ }
+ }
+
+ /** Register all connectors as specified by a Connectors structure, usually read from the connectors.xml file.
+ */
+ public static void registerConnectors(IThreadContext tc, Connectors c)
+ throws ManifoldCFException
+ {
+ if (c != null)
+ {
+ // Grab a database handle, so we can use transactions later.
+ IDBInterface database = DBInterfaceFactory.make(tc,
+ ManifoldCF.getMasterDatabaseName(),
+ ManifoldCF.getMasterDatabaseUsername(),
+ ManifoldCF.getMasterDatabasePassword());
+
+ // Other code will go here to discover and register various connectors that exist in the classpath
+ int i = 0;
+ while (i < c.getChildCount())
+ {
+ ConfigurationNode cn = c.findChild(i++);
+ if (cn.getType().equals(NODE_OUTPUTCONNECTOR))
+ {
+ String name = cn.getAttributeValue(ATTRIBUTE_NAME);
+ String className = cn.getAttributeValue(ATTRIBUTE_CLASS);
+ IOutputConnectorManager mgr = OutputConnectorManagerFactory.make(tc);
+ IOutputConnectionManager connManager = OutputConnectionManagerFactory.make(tc);
+ // Registration should be done in a transaction
database.beginTransaction();
try
{
+ // First, register connector
+ mgr.registerConnector(name,className);
+ // Then, signal to all jobs that might depend on this connector that they can switch state
// Find the connection names that come with this class
String[] connectionNames = connManager.findConnectionsForConnector(className);
- // For all connection names, notify all agents of the deregistration
- AgentManagerFactory.noteOutputConnectorDeregistration(tc,connectionNames);
- // Now that all jobs have been placed into an appropriate state, actually do the deregistration itself.
- mgr.unregisterConnector(className);
+ // For all connection names, notify all agents of the registration
+ AgentManagerFactory.noteOutputConnectorRegistration(tc,connectionNames);
}
catch (ManifoldCFException e)
{
@@ -270,44 +351,34 @@ public class ManifoldCFJettyRunner
{
database.endTransaction();
}
+ System.err.println("Successfully registered output connector '"+className+"'");
}
- System.err.println("Successfully unregistered all output connectors");
- }
-
- // Authority connectors...
- {
- IAuthorityConnectorManager mgr = AuthorityConnectorManagerFactory.make(tc);
- IResultSet classNames = mgr.getConnectors();
- int i = 0;
- while (i < classNames.getRowCount())
+ else if (cn.getType().equals(NODE_AUTHORITYCONNECTOR))
{
- IResultRow row = classNames.getRow(i++);
- mgr.unregisterConnector((String)row.getValue("classname"));
+ String name = cn.getAttributeValue(ATTRIBUTE_NAME);
+ String className = cn.getAttributeValue(ATTRIBUTE_CLASS);
+ IAuthorityConnectorManager mgr = AuthorityConnectorManagerFactory.make(tc);
+ mgr.registerConnector(name,className);
+ System.err.println("Successfully registered authority connector '"+className+"'");
}
- System.err.println("Successfully unregistered all authority connectors");
- }
-
- // Repository connectors...
- {
- IConnectorManager mgr = ConnectorManagerFactory.make(tc);
- IJobManager jobManager = JobManagerFactory.make(tc);
- IRepositoryConnectionManager connManager = RepositoryConnectionManagerFactory.make(tc);
- IResultSet classNames = mgr.getConnectors();
- int i = 0;
- while (i < classNames.getRowCount())
+ else if (cn.getType().equals(NODE_REPOSITORYCONNECTOR))
{
- IResultRow row = classNames.getRow(i++);
- String className = (String)row.getValue("classname");
+ String name = cn.getAttributeValue(ATTRIBUTE_NAME);
+ String className = cn.getAttributeValue(ATTRIBUTE_CLASS);
+ IConnectorManager mgr = ConnectorManagerFactory.make(tc);
+ IJobManager jobManager = JobManagerFactory.make(tc);
+ IRepositoryConnectionManager connManager = RepositoryConnectionManagerFactory.make(tc);
// Deregistration should be done in a transaction
database.beginTransaction();
try
{
+ // First, register connector
+ mgr.registerConnector(name,className);
+ // Then, signal to all jobs that might depend on this connector that they can switch state
// Find the connection names that come with this class
String[] connectionNames = connManager.findConnectionsForConnector(className);
- // For each connection name, modify the jobs to note that the connector is no longer installed
- jobManager.noteConnectorDeregistration(connectionNames);
- // Now that all jobs have been placed into an appropriate state, actually do the deregistration itself.
- mgr.unregisterConnector(className);
+ // For each connection name, modify the jobs to note that the connector is now installed
+ jobManager.noteConnectorRegistration(connectionNames);
}
catch (ManifoldCFException e)
{
@@ -323,99 +394,108 @@ public class ManifoldCFJettyRunner
{
database.endTransaction();
}
+ System.err.println("Successfully registered repository connector '"+className+"'");
}
- System.err.println("Successfully unregistered all repository connectors");
+ else
+ throw new ManifoldCFException("Unrecognized connectors node type '"+cn.getType()+"'");
}
-
- if (c != null)
+ }
+ }
+
+ /** Run the agents process. This method will not return unless the agents process is shut down.
+ */
+ public static void runAgents(IThreadContext tc)
+ throws ManifoldCFException
+ {
+ ILockManager lockManager = LockManagerFactory.make(tc);
+
+ while (true)
+ {
+ // Any shutdown signal yet?
+ if (lockManager.checkGlobalFlag(agentShutdownSignal))
+ break;
+
+ // Start whatever agents need to be started
+ ManifoldCF.startAgents(tc);
+
+ try
+ {
+ ManifoldCF.sleep(5000);
+ }
+ catch (InterruptedException e)
{
+ break;
+ }
+ }
+ }
- // Other code will go here to discover and register various connectors that exist in the classpath
- int i = 0;
- while (i < c.getChildCount())
- {
- ConfigurationNode cn = c.findChild(i++);
- if (cn.getType().equals(NODE_OUTPUTCONNECTOR))
- {
- String name = cn.getAttributeValue(ATTRIBUTE_NAME);
- String className = cn.getAttributeValue(ATTRIBUTE_CLASS);
- IOutputConnectorManager mgr = OutputConnectorManagerFactory.make(tc);
- IOutputConnectionManager connManager = OutputConnectionManagerFactory.make(tc);
- // Registration should be done in a transaction
- database.beginTransaction();
- try
- {
- // First, register connector
- mgr.registerConnector(name,className);
- // Then, signal to all jobs that might depend on this connector that they can switch state
- // Find the connection names that come with this class
- String[] connectionNames = connManager.findConnectionsForConnector(className);
- // For all connection names, notify all agents of the registration
- AgentManagerFactory.noteOutputConnectorRegistration(tc,connectionNames);
- }
- catch (ManifoldCFException e)
- {
- database.signalRollback();
- throw e;
- }
- catch (Error e)
- {
- database.signalRollback();
- throw e;
- }
- finally
- {
- database.endTransaction();
- }
- System.err.println("Successfully registered output connector '"+className+"'");
- }
- else if (cn.getType().equals(NODE_AUTHORITYCONNECTOR))
- {
- String name = cn.getAttributeValue(ATTRIBUTE_NAME);
- String className = cn.getAttributeValue(ATTRIBUTE_CLASS);
- IAuthorityConnectorManager mgr = AuthorityConnectorManagerFactory.make(tc);
- mgr.registerConnector(name,className);
- System.err.println("Successfully registered authority connector '"+className+"'");
- }
- else if (cn.getType().equals(NODE_REPOSITORYCONNECTOR))
- {
- String name = cn.getAttributeValue(ATTRIBUTE_NAME);
- String className = cn.getAttributeValue(ATTRIBUTE_CLASS);
- IConnectorManager mgr = ConnectorManagerFactory.make(tc);
- IJobManager jobManager = JobManagerFactory.make(tc);
- IRepositoryConnectionManager connManager = RepositoryConnectionManagerFactory.make(tc);
- // Deregistration should be done in a transaction
- database.beginTransaction();
- try
- {
- // First, register connector
- mgr.registerConnector(name,className);
- // Then, signal to all jobs that might depend on this connector that they can switch state
- // Find the connection names that come with this class
- String[] connectionNames = connManager.findConnectionsForConnector(className);
- // For each connection name, modify the jobs to note that the connector is now installed
- jobManager.noteConnectorRegistration(connectionNames);
- }
- catch (ManifoldCFException e)
- {
- database.signalRollback();
- throw e;
- }
- catch (Error e)
- {
- database.signalRollback();
- throw e;
- }
- finally
- {
- database.endTransaction();
- }
- System.err.println("Successfully registered repository connector '"+className+"'");
- }
- else
- throw new ManifoldCFException("Unrecognized connectors node type '"+cn.getType()+"'");
- }
+ /**
+ * A main class that starts jetty+mcf
+ */
+ public static void main( String[] args )
+ {
+ if (args.length != 4 && args.length != 1 && args.length != 0)
+ {
+ System.err.println("Usage: ManifoldCFJettyRunner [<port> [<crawler-war-path> <authority-service-war-path> <api-war-path>]]");
+ System.exit(1);
+ }
+
+ int jettyPort = 8345;
+ if (args.length > 0)
+ {
+ try
+ {
+ jettyPort = Integer.parseInt(args[0]);
}
+ catch (NumberFormatException e)
+ {
+ e.printStackTrace(System.err);
+ System.exit(1);
+ }
+ }
+
+ String crawlerWarPath = "war/mcf-crawler-ui.war";
+ String authorityserviceWarPath = "war/mcf-authority-service.war";
+ String apiWarPath = "war/mcf-api-service.war";
+ if (args.length == 4)
+ {
+ crawlerWarPath = args[1];
+ authorityserviceWarPath = args[2];
+ apiWarPath = args[3];
+ }
+
+ // Ready to begin in earnest...
+ if (System.getProperty(ManifoldCF.lcfConfigFileProperty) == null)
+ System.setProperty(ManifoldCF.lcfConfigFileProperty,"./properties.xml");
+ try
+ {
+ ManifoldCF.initializeEnvironment();
+ IThreadContext tc = ThreadContextFactory.make();
+
+ // Clear the shutdown signal
+ clearShutdownSignal(tc);
+
+ // Get the specified superuser name and password, in case this isn't Derby we're using
+ String superuserName = ManifoldCF.getProperty(databaseSuperuserName);
+ if (superuserName == null)
+ superuserName = "";
+ String superuserPassword = ManifoldCF.getProperty(databaseSuperuserPassword);
+ if (superuserPassword == null)
+ superuserPassword = "";
+
+ // Do the basic initialization of the database and its schema
+ createDatabaseAndSchema(tc,superuserName,superuserPassword);
+ registerAgents(tc);
+
+ // Read connectors configuration file (to figure out what we need to register)
+ File connectorConfigFile = ManifoldCF.getFileProperty(connectorsConfigurationFile);
+ Connectors c = readConnectorDeclarations(connectorConfigFile);
+
+ // Unregister all connectors.
+ unregisterAllConnectors(tc);
+
+ // Register connections specified by connectors.xml
+ registerConnectors(tc,c);
System.err.println("Starting jetty...");
@@ -427,24 +507,7 @@ public class ManifoldCFJettyRunner
System.err.println("Jetty started.");
System.err.println("Starting crawler...");
- while (true)
- {
- // Any shutdown signal yet?
- if (lockManager.checkGlobalFlag(agentShutdownSignal))
- break;
-
- // Start whatever agents need to be started
- ManifoldCF.startAgents(tc);
-
- try
- {
- ManifoldCF.sleep(5000);
- }
- catch (InterruptedException e)
- {
- break;
- }
- }
+ runAgents(tc);
System.err.println("Shutting down crawler...");
}
catch (ManifoldCFException e)