001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
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 */
029
030package org.kepler.objectmanager.repository;
031
032import java.io.File;
033import java.io.FileReader;
034import java.io.FileWriter;
035import java.io.IOException;
036import java.util.Iterator;
037import java.util.StringTokenizer;
038import java.util.Vector;
039
040import org.ecoinformatics.ecogrid.client.IdentifierServiceClient;
041
042/**
043 * This class bootstraps an ecogrid repository. It uploads all kar files in the
044 * cache to the repository and includes methods to keep the kepler cache and the
045 * repository in sync.
046 * 
047 * @author Chad Berkley
048 */
049public class EcogridRepositoryBootstrap {
050        /**
051         * Constructor
052         */
053        public EcogridRepositoryBootstrap() {
054        }
055
056        /**
057         * upload all kar files in the cache to the repository. this can be called
058         * from main()
059         */
060        public void bootstrap(String sessionid) throws Exception {
061
062                /* whoa
063                File placeHolderFile = new File("placeholder.txt");
064                if (!placeHolderFile.exists()) {
065                        placeHolderFile.createNewFile();
066                }
067
068                // keep track of where we are so if something dies, we can startup where
069                // we left off.
070                Vector placeVector = readPlaceHolderFile(placeHolderFile);
071                Repository repository = getRepository();
072                IdentifierServiceClient lsidClient = getLSIDClient();
073
074                CacheManager manager = CacheManager.getInstance();
075                // get an iterator over all of the cache objects
076                Iterator cacheObjects = manager.getCacheObjectIterator();
077                // find all of the kar objects, create a metadata file and upload
078                // both to the repository
079                while (cacheObjects.hasNext()) {
080                        CacheObject co = (CacheObject) cacheObjects.next();
081                        if (co instanceof KARCacheObject) {
082                                // create the metadata file, then upload
083                                KARCacheObject kco = (KARCacheObject) co;
084                                KeplerLSID KARLsid = kco.getLSID();
085                                if (placeVectorContains(KARLsid.toString(), placeVector)) {
086                                        continue; // skip this kar file
087                                }
088                                KeplerLSID[] actorLSIDs = kco.getActors();
089                                for (int i = 0; i < actorLSIDs.length; i++) { // upload each
090                                                                                                                                // actor for
091                                                                                                                                // this kar file
092                                        ActorCacheObject aco = (ActorCacheObject) manager
093                                                        .getObject(actorLSIDs[i]);
094                                        if (aco == null)
095                                                continue;
096                                        ActorMetadata am = aco.getMetadata();
097                                        // add the karId attributes to the metadata before uploading
098                                        StringAttribute karId = new StringAttribute();
099                                        karId.setName("karId"); // put the id of the kar file into
100                                                                                        // the AM
101                                        karId.setExpression(KARLsid.toString());
102                                        am.addAttribute(karId);
103                                        UploadToRepository.addDocumentation(am);
104                                        // put the actor metadata
105
106                                        try {
107                                                System.out.println("uploading actor " + am.getName());
108                                                System.out.println("lsid: " + actorLSIDs[i]);
109            
110                                                repository.put(am.toString(), actorLSIDs[i], sessionid);
111                                                // create and upload an access file to make the actor
112                                                // public
113                                                KeplerLSID accessLSID = LSIDGenerator.getInstance().getNewLSID();
114                                                System.out
115                                                                .println("uploading access doc " + accessLSID);
116                                                repository
117                                                                .put(UploadToRepository
118                                                                                .buildAccessDocument(actorLSIDs[i]),
119                                                                                accessLSID, sessionid);
120                                        } catch (Exception e) {
121                                                try {
122                                                        File errorLog = new File("ERROR.LOG");
123                                                        FileOutputStream fos = new FileOutputStream(
124                                                                        errorLog, true);
125                                                        String errormsg = "";
126                                                        errormsg += "Classname: " + am.getClassName()
127                                                                        + "\n";
128                                                        errormsg += e.getMessage();
129                                                        errormsg += "=================================\n=================================\n";
130                                                        byte[] b = errormsg.getBytes();
131                                                        fos.write(b, 0, b.length);
132                                                        fos.flush();
133                                                        fos.close();
134                                                } catch (Exception ee) {
135                                                        System.out
136                                                                        .println("could not write to the error log: "
137                                                                                        + ee.getMessage());
138                                                }
139
140                                        }
141                                }
142
143                                // upload the kar file and access document
144                                try {
145                                        File karFile = (File) kco.getObject();
146                                        System.out.println("uploading kar file " + kco.getLSID());
147                                        repository.put(karFile, kco.getLSID(), sessionid);
148                                        KeplerLSID accessLSID = LSIDGenerator.getInstance().getNewLSID();
149                                        System.out.println("upload access doc " + accessLSID);
150                                        repository.put(UploadToRepository.buildAccessDocument(kco
151                                                        .getLSID()), accessLSID, sessionid);
152                                } catch (Exception e) {
153                                        // accession number in use, ignore.
154                                }
155
156                                // if something dies, this lets us start up where we left off
157                                // instead
158                                // of redoing the whole process
159                                writePlaceHolderFile(KARLsid.toString(), placeHolderFile);
160                        }
161                        System.out.println();
162                }*/
163
164        }
165
166        /**
167         * main method to be called from ant.
168         */
169        public static void main(String[] args) {
170                try {
171                        EcogridRepositoryBootstrap erb = new EcogridRepositoryBootstrap();
172      if(args.length != 1)
173      {
174        System.out.println("Error, must pass an authenticated sessionid.");
175      }
176      String sessionid = args[0];
177                        erb.bootstrap(sessionid);
178                } catch (Exception e) {
179                        System.out.println("Error: " + e.getMessage());
180                        e.printStackTrace();
181                }
182        }
183
184        /**
185         * get the lsid client library and return it
186         */
187        private static IdentifierServiceClient getLSIDClient() throws Exception {
188                // search the repository for everything
189                Repository repository = getRepository();
190                String LSIDServer = repository.getLSIDServerURL();
191                IdentifierServiceClient lsidClient = new IdentifierServiceClient(
192                                LSIDServer);
193                return lsidClient;
194        }
195
196        /**
197         * get the ecogrid repository
198         */
199        private static Repository getRepository() throws Exception {
200                RepositoryManager rmanager = RepositoryManager.getInstance();
201                Iterator repoList = rmanager.repositoryList();
202                // HACKALERT: get the first repository
203                Repository repository = (Repository) repoList.next();
204                if (!(repository instanceof EcogridRepository)) {
205                        throw new Exception("Right now, we can only sync ecogrid "
206                                        + "repositories.  Sorry.");
207                }
208                return repository;
209        }
210
211        /**
212         * reads the place holder file into a vector
213         */
214        private Vector readPlaceHolderFile(File f) throws IOException {
215                FileReader fr = new FileReader(f);
216                StringBuffer sb = new StringBuffer();
217                char[] c = new char[1024];
218                int numread = fr.read(c, 0, 1024);
219                while (numread != -1) {
220                        String s = new String(c, 0, numread);
221                        sb.append(s);
222                        numread = fr.read(c, 0, 1024);
223                }
224
225                StringTokenizer st = new StringTokenizer(sb.toString(), "\n");
226                Vector v = new Vector();
227                while (st.hasMoreElements()) {
228                        v.addElement(st.nextElement());
229                }
230                return v;
231        }
232
233        /**
234         * writes an lsid to the place holder file
235         */
236        private void writePlaceHolderFile(String lsid, File f) throws IOException {
237                FileWriter fw = new FileWriter(f, true);
238                fw.write(lsid + "\n", 0, lsid.length() + 1);
239                fw.flush();
240                fw.close();
241        }
242
243        /**
244         * returns true if the vector v contains s
245         */
246        private boolean placeVectorContains(String s, Vector v) {
247                for (int i = 0; i < v.size(); i++) {
248                        String vs = (String) v.elementAt(i);
249                        if (vs.trim().equals(s.trim())) {
250                                return true;
251                        }
252                }
253                return false;
254        }
255}