001/*
002 * Copyright (c) 2008-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-11-26 22:24:50 +0000 (Mon, 26 Nov 2012) $' 
007 * '$Revision: 31131 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029package org.kepler.kar;
030
031import java.io.File;
032import java.io.FileOutputStream;
033import java.io.FilenameFilter;
034import java.io.IOException;
035import java.io.InputStream;
036import java.util.Iterator;
037import java.util.LinkedHashMap;
038import java.util.Vector;
039import java.util.jar.Attributes;
040import java.util.jar.Attributes.Name;
041import java.util.jar.JarOutputStream;
042import java.util.zip.ZipEntry;
043
044import org.kepler.objectmanager.lsid.KeplerLSID;
045
046/**
047 * This class is a helper class for combining many kars together into one kar.
048 * To use this class, hard coded strings in the constructor must be changed
049 * before running.
050 * 
051 * @author Aaron Schultz
052 */
053public class KarCombiner {
054        boolean deb = true;
055
056        private File _karDir;
057
058        private Vector<KARFile> _karsIn;
059        private File _karOutFile;
060
061        private LinkedHashMap<KAREntry, InputStream> _karItems;
062
063        private KARManifest _manifest;
064
065        private KeplerLSID _karLsid;
066
067        /**
068         * 
069         */
070        public KarCombiner() {
071                
072                // The directory that contains the kar files to be combined
073                String karsInDirStr = "/Users/aaron/KeplerData/modules/actors/kar";
074                
075                // The file to write the new KAR file out to
076                String karOutFileStr = "/Users/aaron/CoreActors.kar";
077                
078                // The LSID for the new KAR file
079                String karLsid = "urn:lsid:kepler-project.org:corekar:1:1";
080
081                try {
082                        _karLsid = new KeplerLSID(karLsid);
083                } catch (Exception e) {
084                        e.printStackTrace();
085                }
086
087                _karDir = new File(karsInDirStr);
088
089                _karsIn = new Vector<KARFile>();
090
091                _karItems = new LinkedHashMap<KAREntry, InputStream>();
092                _manifest = new KARManifest();
093
094                _karOutFile = new File(karOutFileStr);
095        }
096
097        /**
098         *
099         */
100        private void writeKar() throws IOException {
101
102                JarOutputStream jos = new JarOutputStream(new FileOutputStream(
103                                _karOutFile), _manifest);
104                Iterator<KAREntry> li = _karItems.keySet().iterator();
105                while (li.hasNext()) {
106                        KAREntry entry = (KAREntry) li.next();
107                        if (deb)
108                                System.out.println("Writing " + entry.getName());
109                        try {
110                                jos.putNextEntry(entry);
111
112                                if (_karItems.get(entry) instanceof InputStream) {
113                                        // inputstream from a bin file
114                                        byte[] b = new byte[1024];
115                                        InputStream is = (InputStream) _karItems.get(entry);
116                                        int numread = is.read(b, 0, 1024);
117                                        while (numread != -1) {
118                                                jos.write(b, 0, numread);
119                                                numread = is.read(b, 0, 1024);
120                                        }
121                                        is.close();
122                                        // jos.flush();
123                                        jos.closeEntry();
124                                }
125                        } catch (IOException ioe) {
126                                System.out.println(" Tried to write Duplicate Entry to kar "
127                                                + entry.getName() + " " + entry.getLSID());
128                                ioe.printStackTrace();
129                        }
130                }
131                jos.flush();
132                jos.close();
133
134                System.out.println("done writing KAR file to "
135                                + _karOutFile.getAbsolutePath());
136        }
137
138        private void prepareManifest() {
139
140                // main attributes
141                _manifest
142                                .addMainAttribute(KARFile.LSID.toString(), _karLsid.toString());
143
144                // entry attributes
145                for (KAREntry ke : _karItems.keySet()) {
146                        if (deb)
147                                System.out.println(ke.getName());
148
149                        Attributes atts = ke.getAttributes();
150
151                        for (Object att : atts.keySet()) {
152                                if (deb)
153                                        System.out.println(att.toString());
154                                if (att instanceof Name) {
155
156                                        Name attrName = (Name) att;
157                                        String value = atts.getValue(attrName);
158
159                                        _manifest.addEntryAttribute(ke, attrName.toString(), value);
160
161                                }
162                        }
163
164                }
165
166        }
167
168        private void getKarItems() {
169
170                for (KARFile kf : _karsIn) {
171
172                        for (KAREntry ke : kf.karEntries()) {
173
174                                try {
175                                        InputStream is = kf.getInputStream((ZipEntry) ke);
176                                        _karItems.put(ke, is);
177                                } catch (IOException e) {
178                                        e.printStackTrace();
179                                }
180
181                        }
182
183                }
184
185        }
186
187        /**
188         * 
189         */
190        private void getKars() {
191                FilenameFilter ff = new FilenameFilter() {
192                        public boolean accept(File dir, String filename) {
193                                return filename.toLowerCase().endsWith(".kar");
194                        }
195                };
196                File[] karFiles = _karDir.listFiles(ff);
197                for (int i = 0; i < karFiles.length; i++) {
198                        File f = karFiles[i];
199                        try {
200                                KARFile kf = new KARFile(f);
201                                if (deb)
202                                        System.out.println(kf.getName());
203                                _karsIn.add(kf);
204                        } catch (IOException e) {
205                                e.printStackTrace();
206                        }
207                }
208        }
209
210        /**
211         * @param args
212         */
213        public static void main(String[] args) {
214
215                KarCombiner kc = new KarCombiner();
216                kc.getKars();
217                kc.getKarItems();
218                kc.prepareManifest();
219                try {
220                        kc.writeKar();
221                } catch (IOException e) {
222                        e.printStackTrace();
223                }
224
225        }
226
227}