001/**
002 *  '$Author: crawl $'
003 *  '$Date: 2011-09-28 22:46:45 +0000 (Wed, 28 Sep 2011) $'
004 *  '$Revision: 28666 $'
005 *
006 *  For Details:
007 *  http://www.kepler-project.org
008 *
009 *  Copyright (c) 2009-2010 The Regents of the
010 *  University of California. All rights reserved. Permission is hereby granted,
011 *  without written agreement and without license or royalty fees, to use, copy,
012 *  modify, and distribute this software and its documentation for any purpose,
013 *  provided that the above copyright notice and the following two paragraphs
014 *  appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF
015 *  CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
016 *  OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
017 *  DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
018 *  POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
019 *  DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
020 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
021 *  SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 *  ENHANCEMENTS, OR MODIFICATIONS.
024 */
025
026package org.kepler.objectmanager.cache;
027
028import java.io.Externalizable;
029import java.io.IOException;
030import java.io.ObjectInput;
031import java.io.ObjectOutput;
032import java.util.Vector;
033
034import org.apache.commons.logging.Log;
035import org.apache.commons.logging.LogFactory;
036import org.kepler.objectmanager.lsid.KeplerLSID;
037import org.kepler.sms.SMSServices;
038import org.kepler.sms.SemanticType;
039import org.kepler.util.WorkflowRun;
040
041import ptolemy.kernel.util.NamedObj;
042import ptolemy.moml.MoMLParser;
043
044/**
045 * Class that represents an object in the CacheManager. This class should be
046 * extended by each type of object that wants to control its own lifecycle
047 * events and serialization events.
048 */
049public class WorkflowRunCacheObject extends CacheObject implements Externalizable {
050
051        public static final Log log = LogFactory
052                        .getLog(WorkflowRunCacheObject.class);
053        private static final boolean isDebugging = log.isDebugEnabled();
054
055        private WorkflowRun workflowRun = null;
056
057        public WorkflowRunCacheObject() {
058                super();
059                workflowRun = null;
060        }
061
062        /**
063         * Create a WorkflowRunCacheObject from given run, put it into cache and
064         * return the WorkflowRunCacheObject. Returns null if unable.
065         * 
066         * @param run
067         * @return
068         * @throws Exception
069         */
070        public WorkflowRunCacheObject(String name, KeplerLSID lsid, WorkflowRun run)
071                        throws Exception {
072                super(name, lsid);
073                if (isDebugging) log.debug("WorkflowRunCacheObject("+name+","+lsid+","+run.getName()+")");
074                workflowRun = run;
075
076        }
077
078        /**
079         * create a WorkflowRun from workflowRunString
080         */
081        public Object getObject() {
082                return workflowRun;
083        }
084
085        /**
086         * call back for when this object is added to the cache
087         */
088        public void objectAdded() {
089                // System.out.println("WorkflowRunCache object " + lsid.toString() +
090                // " added");
091        }
092
093        /**
094         * call back for when this object is removed by the user
095         */
096        public void objectRemoved() {
097                // System.out.println("WorkflowRunCache object " + lsid.toString() +
098                // " removed");
099        }
100
101        /**
102         * call back for when this object is purged by CacheManager
103         */
104        public void objectPurged() {
105                // System.out.println("WorkflowRunCache object " + lsid.toString() +
106                // " purged");
107        }
108
109        // TODO, return all possible attribs
110        public String getAttribute(String attribute) {
111                String value = null;
112
113                if (attribute.equals(WorkflowRun.RunAttribute.DURATION.toString())) {
114                        Long runDuration = this.workflowRun.getDuration();
115                        if (runDuration != null) {
116                                value = runDuration.toString();
117                        } else {
118                                System.out
119                                                .println("WorkflowRunCacheObject getAttribute ERROR. runDuration == null");
120                        }
121                } else {
122                        value = super.getAttribute(attribute);
123                }
124                return value;
125        }
126
127        public void readExternal(ObjectInput in) throws IOException,
128                        ClassNotFoundException {
129                if (isDebugging) log.debug("readExternal("+")");
130                
131                
132                String moml = (String) in.readObject();
133                MoMLParser parser = new MoMLParser();
134                NamedObj no;
135                try {
136                        no = parser.parse(moml);
137                        workflowRun = (WorkflowRun) no;
138
139                        String name = workflowRun.getName();
140                        KeplerLSID lsid = workflowRun.getExecLSID();
141
142                        setName(name);
143                        setLSID(lsid);
144
145                        Vector<SemanticType> semTypes = SMSServices
146                                        .getActorSemanticTypes(workflowRun);
147                        Vector<String> semTypeStrings = new Vector<String>(semTypes.size());
148                        for (SemanticType st : semTypes) {
149                                String stString = st.toString();
150                                semTypeStrings.add(stString);
151                        }
152                        setSemanticTypes(semTypeStrings);
153                } catch (Exception e) {
154                        e.printStackTrace();
155                }
156        }
157
158        public void writeExternal(ObjectOutput out) throws IOException {
159                if (isDebugging) log.debug("writeExternal("+")");
160
161                String moml = workflowRun.exportMoML();
162                out.writeObject(moml);
163
164        }
165        
166        public void setName(String name) {
167                super.setName(name);
168                if (isDebugging) log.debug("setName("+name+")");
169        }
170
171}