001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.platform.importer.executor; 021 022import org.apache.commons.logging.Log; 023import org.nuxeo.ecm.core.api.NuxeoException; 024import org.nuxeo.ecm.platform.importer.base.ImporterRunner; 025import org.nuxeo.ecm.platform.importer.factories.DefaultDocumentModelFactory; 026import org.nuxeo.ecm.platform.importer.factories.ImporterDocumentModelFactory; 027import org.nuxeo.ecm.platform.importer.log.BasicLogger; 028import org.nuxeo.ecm.platform.importer.log.ImporterLogger; 029import org.nuxeo.ecm.platform.importer.threading.DefaultMultiThreadingPolicy; 030import org.nuxeo.ecm.platform.importer.threading.ImporterThreadingPolicy; 031 032/** 033 * base class for importers 034 * 035 * @author Thierry Delprat 036 */ 037public abstract class AbstractImporterExecutor { 038 039 protected abstract Log getJavaLogger(); 040 041 protected static ImporterLogger log; 042 043 protected static Thread executorMainThread; 044 045 protected static ImporterRunner lastRunner; 046 047 protected ImporterThreadingPolicy threadPolicy; 048 049 protected ImporterDocumentModelFactory factory; 050 051 protected int transactionTimeout = 0; 052 053 public ImporterLogger getLogger() { 054 if (log == null) { 055 log = new BasicLogger(getJavaLogger()); 056 } 057 return log; 058 } 059 060 public String getStatus() { 061 if (isRunning()) { 062 return "Running"; 063 } else { 064 return "Not Running"; 065 } 066 } 067 068 public boolean isRunning() { 069 if (executorMainThread == null) { 070 return false; 071 } else { 072 return executorMainThread.isAlive(); 073 } 074 } 075 076 public String kill() { 077 if (executorMainThread != null) { 078 if (lastRunner != null) { 079 lastRunner.stopImportProcrocess(); 080 } 081 executorMainThread.interrupt(); 082 return "Importer killed"; 083 } 084 return "Importer is not running"; 085 } 086 087 protected void startTask(ImporterRunner runner, boolean interactive) { 088 executorMainThread = new Thread(runner); 089 executorMainThread.setName("ImporterExecutorMainThread"); 090 if (interactive) { 091 executorMainThread.run(); 092 } else { 093 executorMainThread.start(); 094 } 095 } 096 097 protected String doRun(ImporterRunner runner, Boolean interactive) { 098 if (isRunning()) { 099 throw new NuxeoException("Task is already running"); 100 } 101 if (interactive == null) { 102 interactive = false; 103 } 104 lastRunner = runner; 105 startTask(runner, interactive); 106 107 if (interactive) { 108 return "Task compeleted"; 109 } else { 110 return "Started"; 111 } 112 } 113 114 public ImporterThreadingPolicy getThreadPolicy() { 115 if (threadPolicy == null) { 116 threadPolicy = new DefaultMultiThreadingPolicy(); 117 } 118 return threadPolicy; 119 } 120 121 public void setThreadPolicy(ImporterThreadingPolicy threadPolicy) { 122 this.threadPolicy = threadPolicy; 123 } 124 125 public ImporterDocumentModelFactory getFactory() { 126 if (factory == null) { 127 factory = new DefaultDocumentModelFactory(); 128 } 129 return factory; 130 } 131 132 public void setFactory(ImporterDocumentModelFactory factory) { 133 this.factory = factory; 134 } 135 136 /** 137 * @since 5.9.4 138 */ 139 public int getTransactionTimeout() { 140 return transactionTimeout; 141 } 142 143 /** 144 * @since 5.9.4 145 */ 146 public void setTransactionTimeout(int transactionTimeout) { 147 this.transactionTimeout = transactionTimeout; 148 } 149 150 /*** 151 * since 5.5 this method is invoked when using the <code>DefaultImporterService</code> and passing the executor to 152 * the importDocuments method 153 * 154 * @param runner 155 * @param interactive 156 * @return 157 * @throws Exception 158 */ 159 public String run(ImporterRunner runner, Boolean interactive) { 160 return doRun(runner, interactive); 161 } 162}