src/de/uka/ipd/idaho/goldenGateServer/imp/slave/GoldenGateImpSlave.java
/**	the main method to run GoldenGATE Imagine as a batch application
	 */
the main method to run GoldenGATE Imagine as a batch application
the main method to run GoldenGATE Imagine as a batch application
public static void main(String[] args) throws Exception {
String basePathStr = ".";
String cacheRootPath = null;
String docRootPath = null;
String logRootPath = null;
String ggiConfigHost = null;
String ggiConfigName = null;
String imtNameString = null;
boolean requireDocStyle = true;
boolean forkSystemOut = false;
int maxCpuCores = -1;
boolean useSingleCpuCore = false;
//	parse remaining args
for (int a = 0; a < args.length; a++) {
	if (args[a] == null)
		continue;
	if (args[a].startsWith(CACHE_PATH_PARAMETER + "="))
		cacheRootPath = args[a].substring((CACHE_PATH_PARAMETER + "=").length());
	else if (args[a].startsWith(DATA_PATH_PARAMETER + "="))
		docRootPath = args[a].substring((DATA_PATH_PARAMETER + "=").length());
	else if (args[a].startsWith(LOG_PATH_PARAMETER + "="))
		logRootPath = args[a].substring((LOG_PATH_PARAMETER + "=").length());
	else if (args[a].startsWith(CONFIG_HOST_PARAMETER + "="))
		ggiConfigHost = args[a].substring((CONFIG_HOST_PARAMETER + "=").length());
	else if (args[a].startsWith(CONFIG_NAME_PARAMETER + "="))
		ggiConfigName = args[a].substring((CONFIG_NAME_PARAMETER + "=").length());
	else if (args[a].startsWith(TOOL_SEQUENCE_PARAMETER + "="))
		imtNameString = args[a].substring((TOOL_SEQUENCE_PARAMETER + "=").length());
	else if (WAIVE_DOCUMENT_SYTLE_PARAMETER.equals(args[a]))
		requireDocStyle = false;
	else if (VERBOSE_PARAMETER.equals(args[a]))
		forkSystemOut = true;
	else if (USE_SINGLE_CORE_PARAMETER.equals(args[a]))
		useSingleCpuCore = true;
	else if (args[a].startsWith(MAX_CORES_PARAMETER + "=")) try {
		maxCpuCores = Integer.parseInt(args[a].substring((MAX_CORES_PARAMETER + "=").length()).trim());
	} catch (RuntimeException re) {}
}
		Properties argsMap = SlaveRuntimeUtils.parseArguments(args);
		ImpMasterProcessInterface mpi = new ImpMasterProcessInterface();
		String imtNameString = argsMap.getProperty(TOOL_SEQUENCE_PARAMETER);
		if (imtNameString == null) {
			mpi.sendError("No Image Markup Tools configured to run, check parameter " + TOOL_SEQUENCE_PARAMETER);
			System.exit(0);
		}
		String[] imtNames = imtNameString.split("\\+");
		SlaveRuntimeUtils.setUpLogFiles(argsMap, "ImpSlaveBatch");
		mpi.start();
		File basePath = new File(".");
		String ggiConfigHost = argsMap.getProperty(CONFIG_HOST_PARAMETER);
		String ggiConfigName = argsMap.getProperty(CONFIG_NAME_PARAMETER);
		GoldenGateConfiguration ggiConfig = ConfigurationUtils.getConfiguration(ggiConfigName, null, ggiConfigHost, basePath);
		if (ggiConfig == null) {
			mpi.sendError("Configuration '" + ggiConfigName + "' not found, check parameter " + CONFIG_NAME_PARAMETER);
	System.exit(0);
			return;
		}
		String cacheRootPath = argsMap.getProperty(CACHE_PATH_PARAMETER);
		if (cacheRootPath != null) {
			if (!cacheRootPath.endsWith("/"))
				cacheRootPath += "/";
			Settings set = ggiConfig.getSettings();
			set.setSetting("cacheRootFolder", cacheRootPath);
			set.setSetting("pageImageFolder", (cacheRootPath + "PageImages"));
			set.setSetting("supplementFolder", (cacheRootPath + "Supplements"));
		}
		GoldenGateImagine goldenGateImagine = GoldenGateImagine.openGoldenGATE(ggiConfig, basePath, false);
		mpi.sendResult("GoldenGATE Imagine core created, configuration is " + ggiConfigName);
		if (LIST_TOOLS_SEQUENCE_NAME.equals(imtNameString)) {
			ImageMarkupToolProvider[] imtps = goldenGateImagine.getImageMarkupToolProviders();
			for (int p = 0; p < imtps.length; p++) {
				String imtpName = imtps[p].getMainMenuTitle();
				if (imtpName == null)
					imtpName = imtps[p].getPluginName();
				if (imtpName == null)
					continue;
				TreeSet pImtNames = new TreeSet();
				String[] pemImtNames = imtps[p].getEditMenuItemNames();
				if(pemImtNames != null)
					pImtNames.addAll(Arrays.asList(pemImtNames));
				String[] ptmImtNames = imtps[p].getToolsMenuItemNames();
				if(ptmImtNames != null)
					pImtNames.addAll(Arrays.asList(ptmImtNames));
				if(pImtNames.isEmpty())
					continue;
				mpi.sendOutput("MTP:" + imtpName + " (" + pImtNames.size() + "):");
				for (Iterator tnit = pImtNames.iterator(); tnit.hasNext();)
					mpi.sendOutput("MT:" + tnit.next());
			}
	System.exit(0);
			return;
		}
		ImageMarkupTool[] imts = new ImageMarkupTool[imtNames.length];
		for (int t = 0; t < imtNames.length; t++) {
			imts[t] = goldenGateImagine.getImageMarkupToolForName(imtNames[t]);
			if (imts[t] == null) {
				mpi.sendError("Image Markup Tool '" + imtNames[t] + "' not found, check parameter " + TOOL_SEQUENCE_PARAMETER);
		System.exit(0);
				return;
			}
			else mpi.sendResult("Image Markup Tool '" + imtNames[t] + "' loaded");
		}
		SlaveRuntimeUtils.setUpMaxCores(argsMap);
		String docRootPath = argsMap.getProperty(DATA_PATH_PARAMETER);
		File docFolder = new File(docRootPath);
		SlaveImDocumentData docData = new SlaveImDocumentData(docFolder, mpi);
		ProgressMonitor pm = mpi.createProgressMonitor();
		try {
			ImDocument doc = ImDocumentIO.loadDocument(docData, pm);
			goldenGateImagine.notifyDocumentOpened(doc, docFolder, pm);
			String docStyleMode = argsMap.getProperty(DOCUMENT_SYTLE_MODE_PARAMETER, "R");
			if ("I".equals(docStyleMode)) {
				GoldenGatePlugin[] dsps = goldenGateImagine.getImplementingPlugins(DocumentStyle.Provider.class);
				for (int p = 0; p < dsps.length; p++)
					DocumentStyle.removeProvider((DocumentStyle.Provider) dsps[p]);
			}
			else {
				DocumentStyle docStyle = DocumentStyle.getStyleFor(doc);
				if (docStyle != null)
					mpi.sendOutput(" - assigned document style '" + ((String) doc.getAttribute(DocumentStyle.DOCUMENT_STYLE_NAME_ATTRIBUTE)) + "'");
				else if ("U".equals(docStyleMode))
					mpi.sendOutput(" - could not assigned document style");
				else {
					mpi.sendError("Unable to assign document style");
					return;
				}
			}
			for (int t = 0; t < imts.length; t++) {
				mpi.sendOutput("PR:" + imts[t].getLabel());
				imts[t].process(doc, null, null, pm);
			}
			goldenGateImagine.notifyDocumentSaving(doc, docFolder, pm);
			ImDocumentIO.storeDocument(doc, docData, pm);
			goldenGateImagine.notifyDocumentSaved(doc, docFolder, pm);
			goldenGateImagine.notifyDocumentClosed(doc.docId);
			doc.dispose();
		}
		catch (Throwable t) {
			mpi.sendError("Error processing document: " + t.getMessage());
			mpi.sendError(t);
		}
//	shut down whatever threads are left
System.exit(0);
	}
public static void main ( String [ ] args ) throws Exception { Properties argsMap = SlaveRuntimeUtils . parseArguments ( args ) ; ImpMasterProcessInterface mpi = new ImpMasterProcessInterface ( ) ; String imtNameString = argsMap . getProperty ( TOOL_SEQUENCE_PARAMETER ) ; if ( imtNameString == null ) { mpi . sendError ( "No Image Markup Tools configured to run, check parameter " + TOOL_SEQUENCE_PARAMETER ) ; System . exit ( 0 ) ; } String [ ] imtNames = imtNameString . split ( "\\+" ) ; SlaveRuntimeUtils . setUpLogFiles ( argsMap , "ImpSlaveBatch" ) ; mpi . start ( ) ; File basePath = new File ( "." ) ; String ggiConfigHost = argsMap . getProperty ( CONFIG_HOST_PARAMETER ) ; String ggiConfigName = argsMap . getProperty ( CONFIG_NAME_PARAMETER ) ; GoldenGateConfiguration ggiConfig = ConfigurationUtils . getConfiguration ( ggiConfigName , null , ggiConfigHost , basePath ) ; if ( ggiConfig == null ) { mpi . sendError ( "Configuration '" + ggiConfigName + "' not found, check parameter " + CONFIG_NAME_PARAMETER ) ; return ; } String cacheRootPath = argsMap . getProperty ( CACHE_PATH_PARAMETER ) ; if ( cacheRootPath != null ) { if ( ! cacheRootPath . endsWith ( "/" ) ) cacheRootPath += "/" ; Settings set = ggiConfig . getSettings ( ) ; set . setSetting ( "cacheRootFolder" , cacheRootPath ) ; set . setSetting ( "pageImageFolder" , ( cacheRootPath + "PageImages" ) ) ; set . setSetting ( "supplementFolder" , ( cacheRootPath + "Supplements" ) ) ; } GoldenGateImagine goldenGateImagine = GoldenGateImagine . openGoldenGATE ( ggiConfig , basePath , false ) ; mpi . sendResult ( "GoldenGATE Imagine core created, configuration is " + ggiConfigName ) ; if ( LIST_TOOLS_SEQUENCE_NAME . equals ( imtNameString ) ) { ImageMarkupToolProvider [ ] imtps = goldenGateImagine . getImageMarkupToolProviders ( ) ; for ( int p = 0 ; p < imtps . length ; p ++ ) { String imtpName = imtps [ p ] . getMainMenuTitle ( ) ; if ( imtpName == null ) imtpName = imtps [ p ] . getPluginName ( ) ; if ( imtpName == null ) continue ; TreeSet pImtNames = new TreeSet ( ) ; String [ ] pemImtNames = imtps [ p ] . getEditMenuItemNames ( ) ; if ( pemImtNames != null ) pImtNames . addAll ( Arrays . asList ( pemImtNames ) ) ; String [ ] ptmImtNames = imtps [ p ] . getToolsMenuItemNames ( ) ; if ( ptmImtNames != null ) pImtNames . addAll ( Arrays . asList ( ptmImtNames ) ) ; if ( pImtNames . isEmpty ( ) ) continue ; mpi . sendOutput ( "MTP:" + imtpName + " (" + pImtNames . size ( ) + "):" ) ; for ( Iterator tnit = pImtNames . iterator ( ) ; tnit . hasNext ( ) ; ) mpi . sendOutput ( "MT:" + tnit . next ( ) ) ; } return ; } ImageMarkupTool [ ] imts = new ImageMarkupTool [ imtNames . length ] ; for ( int t = 0 ; t < imtNames . length ; t ++ ) { imts [ t ] = goldenGateImagine . getImageMarkupToolForName ( imtNames [ t ] ) ; if ( imts [ t ] == null ) { mpi . sendError ( "Image Markup Tool '" + imtNames [ t ] + "' not found, check parameter " + TOOL_SEQUENCE_PARAMETER ) ; return ; } else mpi . sendResult ( "Image Markup Tool '" + imtNames [ t ] + "' loaded" ) ; } SlaveRuntimeUtils . setUpMaxCores ( argsMap ) ; String docRootPath = argsMap . getProperty ( DATA_PATH_PARAMETER ) ; File docFolder = new File ( docRootPath ) ; SlaveImDocumentData docData = new SlaveImDocumentData ( docFolder , mpi ) ; ProgressMonitor pm = mpi . createProgressMonitor ( ) ; try { ImDocument doc = ImDocumentIO . loadDocument ( docData , pm ) ; goldenGateImagine . notifyDocumentOpened ( doc , docFolder , pm ) ; String docStyleMode = argsMap . getProperty ( DOCUMENT_SYTLE_MODE_PARAMETER , "R" ) ; if ( "I" . equals ( docStyleMode ) ) { GoldenGatePlugin [ ] dsps = goldenGateImagine . getImplementingPlugins ( DocumentStyle . Provider . class ) ; for ( int p = 0 ; p < dsps . length ; p ++ ) DocumentStyle . removeProvider ( ( DocumentStyle . Provider ) dsps [ p ] ) ; } else { DocumentStyle docStyle = DocumentStyle . getStyleFor ( doc ) ; if ( docStyle != null ) mpi . sendOutput ( " - assigned document style '" + ( ( String ) doc . getAttribute ( DocumentStyle . DOCUMENT_STYLE_NAME_ATTRIBUTE ) ) + "'" ) ; else if ( "U" . equals ( docStyleMode ) ) mpi . sendOutput ( " - could not assigned document style" ) ; else { mpi . sendError ( "Unable to assign document style" ) ; return ; } } for ( int t = 0 ; t < imts . length ; t ++ ) { mpi . sendOutput ( "PR:" + imts [ t ] . getLabel ( ) ) ; imts [ t ] . process ( doc , null , null , pm ) ; } goldenGateImagine . notifyDocumentSaving ( doc , docFolder , pm ) ; ImDocumentIO . storeDocument ( doc , docData , pm ) ; goldenGateImagine . notifyDocumentSaved ( doc , docFolder , pm ) ; goldenGateImagine . notifyDocumentClosed ( doc . docId ) ; doc . dispose ( ) ; } catch ( Throwable t ) { mpi . sendError ( "Error processing document: " + t . getMessage ( ) ) ; mpi . sendError ( t ) ; } }
the main method to run GoldenGATE Imagine as a batch application
the main method to run GoldenGATE Imagine as a batch application
//		 //		//	adjust basic parameters //		String basePathStr = "."; //		String cacheRootPath = null; //		String docRootPath = null; //		String logRootPath = null; //		String ggiConfigHost = null; //		String ggiConfigName = null; //		String imtNameString = null; //		boolean requireDocStyle = true; //		boolean forkSystemOut = false; //		int maxCpuCores = -1; //		boolean useSingleCpuCore = false; //		 //		//	parse remaining args //		for (int a = 0; a < args.length; a++) { //			if (args[a] == null) //				continue; //			if (args[a].startsWith(CACHE_PATH_PARAMETER + "=")) //				cacheRootPath = args[a].substring((CACHE_PATH_PARAMETER + "=").length()); //			else if (args[a].startsWith(DATA_PATH_PARAMETER + "=")) //				docRootPath = args[a].substring((DATA_PATH_PARAMETER + "=").length()); //			else if (args[a].startsWith(LOG_PATH_PARAMETER + "=")) //				logRootPath = args[a].substring((LOG_PATH_PARAMETER + "=").length()); //			else if (args[a].startsWith(CONFIG_HOST_PARAMETER + "=")) //				ggiConfigHost = args[a].substring((CONFIG_HOST_PARAMETER + "=").length()); //			else if (args[a].startsWith(CONFIG_NAME_PARAMETER + "=")) //				ggiConfigName = args[a].substring((CONFIG_NAME_PARAMETER + "=").length()); //			else if (args[a].startsWith(TOOL_SEQUENCE_PARAMETER + "=")) //				imtNameString = args[a].substring((TOOL_SEQUENCE_PARAMETER + "=").length()); //			else if (WAIVE_DOCUMENT_SYTLE_PARAMETER.equals(args[a])) //				requireDocStyle = false; //			else if (VERBOSE_PARAMETER.equals(args[a])) //				forkSystemOut = true; //			else if (USE_SINGLE_CORE_PARAMETER.equals(args[a])) //				useSingleCpuCore = true; //			else if (args[a].startsWith(MAX_CORES_PARAMETER + "=")) try { //				maxCpuCores = Integer.parseInt(args[a].substring((MAX_CORES_PARAMETER + "=").length()).trim()); //			} catch (RuntimeException re) {} //		} //	adjust basic parameters //	set up communication with master (before logging tampers with output streams) //	get list of image markup tools to run //	set up logging (if we have a folder) //	start receiving control commands from master process //	remember program base path //	get GoldenGATE Imagine configuration //	check if configuration found //			System.exit(0); //	if cache path set, add settings for page image and supplement cache //	instantiate GoldenGATE Imagine //	list markup tools //			System.exit(0); //	get individual image markup tools //				System.exit(0); //	impose parallel processing limitations //	load document from folder //	create progress monitor reporting back to master //	process document //	test if document style detected //	remove at least any plug-in based providers if ignoring document style templates //	get and check document style template otherwise //	process document //	store updates //	catch and log whatever might go wrong //		 //		//	shut down whatever threads are left //		System.exit(0);