| /******************************************************************************* |
| * Copyright (C) 2007 The University of Manchester |
| * |
| * Modifications to the initial code base are copyright of their |
| * respective authors, or their employers as appropriate. |
| * |
| * This program 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 2.1 of |
| * the License, or (at your option) any later version. |
| * |
| * This program 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 this program; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| ******************************************************************************/ |
| package net.sf.taverna.t2.workbench.file.impl; |
| |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileNotFoundException; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.net.URISyntaxException; |
| import java.net.URL; |
| import java.net.URLConnection; |
| import java.util.Arrays; |
| import java.util.Date; |
| import java.util.List; |
| |
| import net.sf.taverna.t2.workbench.file.AbstractDataflowPersistenceHandler; |
| import net.sf.taverna.t2.workbench.file.DataflowInfo; |
| import net.sf.taverna.t2.workbench.file.DataflowPersistenceHandler; |
| import net.sf.taverna.t2.workbench.file.FileType; |
| import net.sf.taverna.t2.workbench.file.exceptions.OpenException; |
| |
| import org.apache.log4j.Logger; |
| |
| import uk.org.taverna.scufl2.api.container.WorkflowBundle; |
| import uk.org.taverna.scufl2.api.io.ReaderException; |
| import uk.org.taverna.scufl2.api.io.WorkflowBundleIO; |
| |
| public class WorkflowBundleOpener extends AbstractDataflowPersistenceHandler |
| implements DataflowPersistenceHandler { |
| private static final WorkflowBundleFileType WF_BUNDLE_FILE_TYPE = new WorkflowBundleFileType(); |
| private static Logger logger = Logger.getLogger(WorkflowBundleOpener.class); |
| private WorkflowBundleIO workflowBundleIO; |
| |
| @SuppressWarnings("resource") |
| @Override |
| public DataflowInfo openDataflow(FileType fileType, Object source) |
| throws OpenException { |
| if (!getOpenFileTypes().contains(fileType)) |
| throw new OpenException("Unsupported file type " + fileType); |
| InputStream inputStream; |
| Date lastModified = null; |
| Object canonicalSource = source; |
| if (source instanceof InputStream) { |
| inputStream = (InputStream) source; |
| } else if (source instanceof File) { |
| try { |
| inputStream = new FileInputStream((File) source); |
| } catch (FileNotFoundException e) { |
| throw new OpenException("Could not open file " + source + ":\n" |
| + e.getLocalizedMessage(), e); |
| } |
| } else if (source instanceof URL) { |
| URL url = ((URL) source); |
| try { |
| URLConnection connection = url.openConnection(); |
| connection.setRequestProperty("Accept", "application/zip"); |
| inputStream = connection.getInputStream(); |
| if (connection.getLastModified() != 0) |
| lastModified = new Date(connection.getLastModified()); |
| } catch (IOException e) { |
| throw new OpenException("Could not open connection to URL " |
| + source + ":\n" + e.getLocalizedMessage(), e); |
| } |
| try { |
| if (url.getProtocol().equalsIgnoreCase("file")) |
| canonicalSource = new File(url.toURI()); |
| } catch (URISyntaxException e) { |
| logger.warn("Invalid file URI created from " + url); |
| } |
| } else |
| throw new OpenException("Unsupported source type " |
| + source.getClass()); |
| |
| final WorkflowBundle workflowBundle; |
| try { |
| workflowBundle = openDataflowStream(inputStream); |
| } finally { |
| // We created the stream, we'll close it |
| try { |
| if (!(source instanceof InputStream)) |
| inputStream.close(); |
| } catch (IOException ex) { |
| logger.warn("Could not close inputstream " + inputStream, ex); |
| } |
| } |
| if (canonicalSource instanceof File) |
| return new FileDataflowInfo(WF_BUNDLE_FILE_TYPE, |
| (File) canonicalSource, workflowBundle); |
| return new DataflowInfo(WF_BUNDLE_FILE_TYPE, canonicalSource, |
| workflowBundle, lastModified); |
| } |
| |
| protected WorkflowBundle openDataflowStream(InputStream inputStream) |
| throws OpenException { |
| WorkflowBundle workflowBundle; |
| try { |
| workflowBundle = workflowBundleIO.readBundle(inputStream, null); |
| } catch (ReaderException e) { |
| throw new OpenException("Could not read the workflow", e); |
| } catch (IOException e) { |
| throw new OpenException("Could not open the workflow for parsing", |
| e); |
| } catch (Exception e) { |
| throw new OpenException("Error while opening workflow", e); |
| } |
| |
| return workflowBundle; |
| } |
| |
| @Override |
| public List<FileType> getOpenFileTypes() { |
| return Arrays.<FileType> asList(WF_BUNDLE_FILE_TYPE); |
| } |
| |
| @Override |
| public List<Class<?>> getOpenSourceTypes() { |
| return Arrays.<Class<?>> asList(InputStream.class, URL.class, |
| File.class); |
| } |
| |
| public void setWorkflowBundleIO(WorkflowBundleIO workflowBundleIO) { |
| this.workflowBundleIO = workflowBundleIO; |
| } |
| } |