001/*
002 *  The node controller for actor instances.
003 *  Copyright (c) 2010 The Regents of the University of California.
004 *  All rights reserved.
005 *  Permission is hereby granted, without written agreement and without
006 *  license or royalty fees, to use, copy, modify, and distribute this
007 *  software and its documentation for any purpose, provided that the above
008 *  copyright notice and the following two paragraphs appear in all copies
009 *  of this software.
010 *  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
011 *  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
012 *  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
013 *  THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
014 *  SUCH DAMAGE.
015 *  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016 *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
017 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
018 *  PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
019 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
020 *  ENHANCEMENTS, OR MODIFICATIONS.
021 *  PT_COPYRIGHT_VERSION_2
022 *  COPYRIGHTENDKEY
023 */
024package org.kepler.kar.handlers;
025
026import java.io.File;
027import java.io.FileOutputStream;
028import java.io.InputStream;
029import java.util.Hashtable;
030import java.util.Vector;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.kepler.kar.KAREntry;
035import org.kepler.kar.KAREntryHandler;
036import org.kepler.kar.KAREntryHandlerFactory;
037import org.kepler.kar.KARFile;
038import org.kepler.objectmanager.cache.CacheObject;
039import org.kepler.objectmanager.lsid.KeplerLSID;
040import org.kepler.util.DotKeplerManager;
041
042import ptolemy.actor.gui.TableauFrame;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.NameDuplicationException;
045import ptolemy.kernel.util.NamedObj;
046import util.ClassPathMunger;
047
048/**
049 * A KAREntryHandler extension that saves JAR files that are in KAR files to
050 * somewhere on disk.  
051 * 
052 * @author Aaron Schultz
053 * 
054 */
055public class JARKAREntryHandler implements KAREntryHandler {
056
057        private static final Log log = LogFactory.getLog(JARKAREntryHandler.class
058                        .getName());
059        private static final boolean isDebugging = log.isDebugEnabled();
060
061        private File _jarDirectory;
062
063        public JARKAREntryHandler() {
064                if (isDebugging) {
065                        log.debug("new JARKAREntryHandler()");
066                }
067        }
068
069        /**
070         * KAR version 1.0 type name.
071         * 
072         * @see org.kepler.kar.KAREntryHandler#getTypeName()
073         */
074        public String getTypeName() {
075                return "jar";
076        }
077
078        /**
079         * JARs are not supported in KAR version 2.0 and 2.1
080         * 
081         * @see org.kepler.kar.KAREntryHandler#handlesType(java.lang.String)
082         */
083        public boolean handlesType(String typeName) {
084                return false;
085        }
086
087        /*
088         * Initialize the directory that jars are copied to.
089         * 
090         * @see org.kepler.kar.KAREntryHandler#initialize()
091         */
092        public void initialize() {
093                if (isDebugging) {
094                        log.debug("initialize()");
095                }
096
097                String jardir = DotKeplerManager.getDotKeplerPath();
098                jardir += "kar" + File.separator;
099                jardir += "jar" + File.separator;
100                if (isDebugging)
101                        log.debug(jardir);
102
103                _jarDirectory = new File(jardir);
104                if (!_jarDirectory.exists()) {
105                        if (isDebugging)
106                                log.debug("jarDirectory does not exist, try creating it");
107                        try {
108                                _jarDirectory.mkdirs();
109                        } catch (Exception e) {
110                                log.error("Unable to create jar directory");
111                        }
112                }
113        }
114        
115        public boolean open(KARFile karFile, KAREntry entry, TableauFrame tableauFrame) throws Exception {
116                return false;
117        }
118
119        /*
120         * JAR files that are put in KAR files do not get cached in the CacheManager
121         * they are just copied to disk and added to the classpath.
122         * 
123         * @see org.kepler.kar.KAREntryHandler#open(org.kepler.kar.KARFile,
124         * java.util.jar.JarEntry)
125         */
126        public CacheObject cache(KARFile karFile, KAREntry entry) throws Exception {
127                if (isDebugging) {
128                        log
129                                        .debug("open(" + karFile.getName() + "," + entry.getName()
130                                                        + ")");
131                }
132
133                String outputFileName = _jarDirectory + File.separator
134                                + entry.getName();
135                if (isDebugging) {
136                        log.debug(outputFileName);
137                }
138                File outputFile = new File(outputFileName);
139                if (isDebugging) {
140                        log.debug(outputFile.toString());
141                        log.debug(outputFile.exists());
142                }
143                if (!outputFile.exists()) {
144                        FileOutputStream fos = new FileOutputStream(outputFile);
145                        InputStream in = karFile.getInputStream(entry);
146                        for (int c = in.read(); c != -1; c = in.read()) {
147                                fos.write(c);
148                        }
149                        in.close();
150                        fos.close();
151
152                        // add the jar to the classpath
153                        ClassPathMunger.addFile(outputFile);
154                        if (isDebugging) {
155                                log.debug(outputFile.toString());
156                        }
157                }
158                return null; // No CacheObject created for this KAREntry
159
160        }
161
162        /*
163         * (non-Javadoc)
164         * 
165         * @see
166         * org.kepler.kar.KAREntryHandler#save(org.kepler.objectmanager.lsid.KeplerLSID
167         * )
168         */
169        public Hashtable<KAREntry, InputStream> save(Vector<KeplerLSID> lsids, KeplerLSID karLsid,
170                        TableauFrame tableauFrame)
171                        throws Exception {
172                return null;
173        }
174
175        /**
176         * A factory that creates a KAREntryHandler object.
177         * 
178         *@author Aaron Schultz
179         */
180        public static class Factory extends KAREntryHandlerFactory {
181                /**
182                 * Create a factory with the given name and container.
183                 * 
184                 *@param container
185                 *            The container.
186                 *@param name
187                 *            The name of the entity.
188                 *@exception IllegalActionException
189                 *                If the container is incompatible with this attribute.
190                 *@exception NameDuplicationException
191                 *                If the name coincides with an attribute already in the
192                 *                container.
193                 */
194                public Factory(NamedObj container, String name)
195                                throws IllegalActionException, NameDuplicationException {
196                        super(container, name);
197                }
198
199                /**
200                 * Create a library pane that displays the given library of actors.
201                 * 
202                 * @return A new LibraryPaneTab that displays the library
203                 */
204                public KAREntryHandler createKAREntryHandler() {
205                        if (isDebugging)
206                                log.debug("createKAREntryHandler()");
207                        JARKAREntryHandler jkeh = new JARKAREntryHandler();
208                        return jkeh;
209                }
210        }
211
212}