001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.importer.executor; 023 024import org.apache.commons.logging.Log; 025import org.nuxeo.ecm.core.api.NuxeoException; 026import org.nuxeo.ecm.platform.importer.base.ImporterRunner; 027import org.nuxeo.ecm.platform.importer.factories.DefaultDocumentModelFactory; 028import org.nuxeo.ecm.platform.importer.factories.ImporterDocumentModelFactory; 029import org.nuxeo.ecm.platform.importer.log.BasicLogger; 030import org.nuxeo.ecm.platform.importer.log.ImporterLogger; 031import org.nuxeo.ecm.platform.importer.threading.DefaultMultiThreadingPolicy; 032import org.nuxeo.ecm.platform.importer.threading.ImporterThreadingPolicy; 033 034/** 035 * base class for importers 036 * 037 * @author Thierry Delprat 038 */ 039public abstract class AbstractImporterExecutor { 040 041 protected abstract Log getJavaLogger(); 042 043 protected static ImporterLogger log; 044 045 protected static Thread executorMainThread; 046 047 protected static ImporterRunner lastRunner; 048 049 protected ImporterThreadingPolicy threadPolicy; 050 051 protected ImporterDocumentModelFactory factory; 052 053 protected int transactionTimeout = 0; 054 055 public ImporterLogger getLogger() { 056 if (log == null) { 057 log = new BasicLogger(getJavaLogger()); 058 } 059 return log; 060 } 061 062 public String getStatus() { 063 if (isRunning()) { 064 return "Running"; 065 } else { 066 return "Not Running"; 067 } 068 } 069 070 public boolean isRunning() { 071 if (executorMainThread == null) { 072 return false; 073 } else { 074 return executorMainThread.isAlive(); 075 } 076 } 077 078 public String kill() { 079 if (executorMainThread != null) { 080 if (lastRunner != null) { 081 lastRunner.stopImportProcrocess(); 082 } 083 executorMainThread.interrupt(); 084 return "Importer killed"; 085 } 086 return "Importer is not running"; 087 } 088 089 protected void startTask(ImporterRunner runner, boolean interactive) { 090 executorMainThread = new Thread(runner); 091 executorMainThread.setName("ImporterExecutorMainThread"); 092 if (interactive) { 093 executorMainThread.run(); 094 } else { 095 executorMainThread.start(); 096 } 097 } 098 099 protected String doRun(ImporterRunner runner, Boolean interactive) { 100 if (isRunning()) { 101 throw new NuxeoException("Task is already running"); 102 } 103 if (interactive == null) { 104 interactive = false; 105 } 106 lastRunner = runner; 107 startTask(runner, interactive); 108 109 if (interactive) { 110 return "Task compeleted"; 111 } else { 112 return "Started"; 113 } 114 } 115 116 public ImporterThreadingPolicy getThreadPolicy() { 117 if (threadPolicy == null) { 118 threadPolicy = new DefaultMultiThreadingPolicy(); 119 } 120 return threadPolicy; 121 } 122 123 public void setThreadPolicy(ImporterThreadingPolicy threadPolicy) { 124 this.threadPolicy = threadPolicy; 125 } 126 127 public ImporterDocumentModelFactory getFactory() { 128 if (factory == null) { 129 factory = new DefaultDocumentModelFactory(); 130 } 131 return factory; 132 } 133 134 public void setFactory(ImporterDocumentModelFactory factory) { 135 this.factory = factory; 136 } 137 138 /** 139 * @since 5.9.4 140 */ 141 public int getTransactionTimeout() { 142 return transactionTimeout; 143 } 144 145 /** 146 * @since 5.9.4 147 */ 148 public void setTransactionTimeout(int transactionTimeout) { 149 this.transactionTimeout = transactionTimeout; 150 } 151 152 /*** 153 * since 5.5 this method is invoked when using the <code>DefaultImporterService</code> and passing the executor to 154 * the importDocuments method 155 * 156 * @param runner 157 * @param interactive 158 * @return 159 * @throws Exception 160 */ 161 public String run(ImporterRunner runner, Boolean interactive) { 162 return doRun(runner, interactive); 163 } 164}