001/** 002 * '$RCSfile$' 003 * '$Author: brooks $' 004 * '$Date: 2015-12-22 22:47:27 +0000 (Tue, 22 Dec 2015) $' 005 * '$Revision: 34375 $' 006 * 007 * For Details: 008 * http://www.kepler-project.org 009 * 010 * Copyright (c) 2010 The Regents of the 011 * University of California. All rights reserved. Permission is hereby granted, 012 * without written agreement and without license or royalty fees, to use, copy, 013 * modify, and distribute this software and its documentation for any purpose, 014 * provided that the above copyright notice and the following two paragraphs 015 * appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF 016 * CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, 017 * OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS 018 * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE 019 * POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY 020 * DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE 022 * SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 023 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 024 * ENHANCEMENTS, OR MODIFICATIONS. 025 */ 026package org.kepler.kar; 027 028import java.io.File; 029import java.io.IOException; 030import java.util.ArrayList; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.kepler.moml.NamedObjId; 035import org.kepler.objectmanager.cache.LocalRepositoryManager; 036import org.kepler.objectmanager.lsid.KeplerLSID; 037import org.kepler.util.FileUtil; 038 039import ptolemy.actor.gui.TableauFrame; 040import ptolemy.kernel.ComponentEntity; 041import ptolemy.kernel.util.NamedObj; 042import ptolemy.util.MessageHandler; 043/** 044 * This is a gui independent class to be used for saving a KAR file to disk and 045 * to remote repositories. 046 * 047 * @author Aaron Schultz 048 * 049 */ 050public class SaveKAR { 051 private static final Log log = LogFactory.getLog(SaveKAR.class); 052 private static final boolean isDebugging = log.isDebugEnabled(); 053 054 private File _karFile; 055 private ArrayList<ComponentEntity> _saveInitiatorObjs; 056 private KeplerLSID _specificLSID = null; 057 058 public SaveKAR() { 059 _saveInitiatorObjs = new ArrayList<ComponentEntity>(); 060 } 061 062 public File getFile() { 063 return _karFile; 064 } 065 066 public void addSaveInitiator(ComponentEntity ce) { 067 if (_saveInitiatorObjs.contains(ce)) { 068 if (isDebugging) 069 log.debug(ce.getName() 070 + " is already in the save initiator list"); 071 } else { 072 _saveInitiatorObjs.add(ce); 073 } 074 } 075 076 public ArrayList<ComponentEntity> getSaveInitiatorList() { 077 return _saveInitiatorObjs; 078 } 079 080 /** 081 * Set the LSID to be used for the KAR. If a specific LSID is not set then a 082 * new LSID will be generated. 083 * 084 * @param lsid 085 */ 086 public void specifyLSID(KeplerLSID lsid) { 087 _specificLSID = lsid; 088 } 089 090 public void setFile(File karFile) { 091 // Force the kar extension 092 if (!FileUtil.getExtension(karFile).equals(KARFile.EXTENSION)) { 093 karFile = new File(karFile.getAbsolutePath() + "." 094 + KARFile.EXTENSION); 095 } 096 _karFile = karFile; 097 } 098 099 /** 100 * Every NamedObj should have an LSID. 101 * 102 * @param no 103 */ 104 public KeplerLSID checkNamedObjLSID(NamedObj no) { 105 106 // Make sure the object has an LSID 107 // assign one if it doesn't 108 KeplerLSID lsid = NamedObjId.getIdFor(no); 109 110 if (isDebugging) 111 log.debug(lsid); 112 113 return lsid; 114 } 115 116 /** 117 * Check a namedObj's name. If null or just whitespace or 118 * startsWith "Unnamed" throw an exception. 119 * 120 * @param no 121 * @throws Exception 122 */ 123 public void checkNamedObjName(NamedObj no) throws Exception { 124 125 // Make sure the object has a name 126 if (no.getName() == null || no.getName().trim().equals("")) { 127 throw new Exception("No Name"); 128 } 129 130 if (no.getName().startsWith("Unnamed")) { 131 throw new Exception("Unnamed"); 132 } 133 134 } 135 136 /** 137 * Save this KAR file to disk. 138 */ 139 public KeplerLSID saveToDisk(TableauFrame tableauFrame, String overrideModDeps) { 140 if (isDebugging) 141 log.debug("saveToDisk()"); 142 143 KARBuilder karBuilder = new KARBuilder(); 144 karBuilder.setKarFile(getFile()); 145 try { 146 for (ComponentEntity initiator : _saveInitiatorObjs) { 147 karBuilder.addSaveInitiator(initiator); 148 } 149 karBuilder.setRegisterLSID(false); 150 karBuilder.setRevision(false); 151 if (_specificLSID != null) { 152 karBuilder.setKarLSID(_specificLSID); 153 } 154 karBuilder.generateKAR(tableauFrame, overrideModDeps); 155 156 // Update the File in case KARBuilder changes it for some reason 157 _karFile = karBuilder.getKarFile(); 158 159 // return the new lsid 160 return karBuilder.getKarLSID(); 161 162 } catch (Exception ex) { 163 log.error("Failed to create kar file: " + ex.getMessage()); 164 // If we do not catch exceptions here, then they 165 // disappear to stdout, which is bad if we launched 166 // where there is no stdout visible. 167 // See https://projects.ecoinformatics.org/ecoinfo/issues/6634 168 MessageHandler.error("Failed to create KAR file:", ex); 169 ex.printStackTrace(); 170 } 171 172 return null; 173 174 } 175 176 /** 177 * Save this KAR file to the cache only if it is being saved to disk in a 178 * local repository. 179 */ 180 public void saveToCache() { 181 if (isDebugging) 182 log.debug("saveToCache()"); 183 184 LocalRepositoryManager lrm = LocalRepositoryManager.getInstance(); 185 if (lrm.isInLocalRepository(getFile())) { 186 187 KARFile thekar; 188 try { 189 thekar = new KARFile(getFile()); 190 thekar.cacheKARContents(); 191 } catch (IOException e) { 192 e.printStackTrace(); 193 } catch (Exception e) { 194 log.error(e.getMessage()); 195 e.printStackTrace(); 196 } 197 } 198 } 199 200 201}