
org.artofsolving.jodconverter.office.ProcessPoolOfficeManager Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2010 Daniel Manzke
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// JODConverter - Java OpenDocument Converter
// Copyright 2009 Art of Solving Ltd
// Copyright 2004-2009 Mirko Nasato
//
// JODConverter is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// JODConverter is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General
// Public License along with JODConverter. If not, see
// .
//
package org.artofsolving.jodconverter.office;
import java.io.File;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import org.artofsolving.jodconverter.process.ProcessManager;
class ProcessPoolOfficeManager implements OfficeManager {
private final BlockingQueue pool;
private final PooledOfficeManager[] pooledManagers;
private final long taskQueueTimeout;
private volatile boolean running = false;
private final Logger logger = Logger.getLogger(ProcessPoolOfficeManager.class.getName());
public ProcessPoolOfficeManager(File officeHome, UnoUrl[] unoUrls, String[] runAsArgs, File templateProfileDir,
long taskQueueTimeout, long taskExecutionTimeout, int maxTasksPerProcess, ProcessManager processManager) {
this.taskQueueTimeout = taskQueueTimeout;
pool = new ArrayBlockingQueue(unoUrls.length);
pooledManagers = new PooledOfficeManager[unoUrls.length];
for (int i = 0; i < unoUrls.length; i++) {
PooledOfficeManagerSettings settings = new PooledOfficeManagerSettings(unoUrls[i]);
settings.setRunAsArgs(runAsArgs);
settings.setTemplateProfileDir(templateProfileDir);
settings.setOfficeHome(officeHome);
settings.setTaskExecutionTimeout(taskExecutionTimeout);
settings.setMaxTasksPerProcess(maxTasksPerProcess);
settings.setProcessManager(processManager);
pooledManagers[i] = new PooledOfficeManager(settings);
}
logger.info("ProcessManager implementation is " + processManager.getClass().getSimpleName());
}
public synchronized void start() throws OfficeException {
for (int i = 0; i < pooledManagers.length; i++) {
pooledManagers[i].start();
releaseManager(pooledManagers[i]);
}
running = true;
}
public void execute(OfficeTask task) throws IllegalStateException, OfficeException {
if (!running) {
throw new IllegalStateException("this OfficeManager is currently stopped");
}
PooledOfficeManager manager = null;
try {
manager = acquireManager();
if (manager == null) {
throw new OfficeException("no office manager available");
}
manager.execute(task);
} finally {
if (manager != null) {
releaseManager(manager);
}
}
}
public synchronized void stop() throws OfficeException {
running = false;
logger.info("stopping");
pool.clear();
for (int i = 0; i < pooledManagers.length; i++) {
pooledManagers[i].stop();
}
logger.info("stopped");
}
private PooledOfficeManager acquireManager() {
try {
return pool.poll(taskQueueTimeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException interruptedException) {
throw new OfficeException("interrupted", interruptedException);
}
}
private void releaseManager(PooledOfficeManager manager) {
try {
pool.put(manager);
} catch (InterruptedException interruptedException) {
throw new OfficeException("interrupted", interruptedException);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy