001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-08-24 22:47:39 +0000 (Mon, 24 Aug 2015) $' 
007 * '$Revision: 33633 $'
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.srb;
031
032import java.util.StringTokenizer;
033
034import edu.sdsc.grid.io.MetaDataCondition;
035import edu.sdsc.grid.io.MetaDataRecordList;
036import edu.sdsc.grid.io.MetaDataTable;
037import edu.sdsc.grid.io.local.LocalFile;
038import edu.sdsc.grid.io.srb.SRBFile;
039import edu.sdsc.grid.io.srb.SRBFileSystem;
040import edu.sdsc.grid.io.srb.SRBMetaDataRecordList;
041import edu.sdsc.grid.io.srb.SRBMetaDataSet;
042import ptolemy.actor.NoTokenException;
043import ptolemy.actor.TypedAtomicActor;
044import ptolemy.actor.TypedIOPort;
045import ptolemy.data.ArrayToken;
046import ptolemy.data.ObjectToken;
047import ptolemy.data.StringToken;
048import ptolemy.data.Token;
049import ptolemy.data.type.ArrayType;
050import ptolemy.data.type.BaseType;
051import ptolemy.gui.GraphicalMessageHandler;
052import ptolemy.kernel.CompositeEntity;
053import ptolemy.kernel.util.Attribute;
054import ptolemy.kernel.util.IllegalActionException;
055import ptolemy.kernel.util.NameDuplicationException;
056
057//////////////////////////////////////////////////////////////////////////
058//// SRBAddMD
059/**
060 * <p>
061 * SRBAddMD is a Kepler Actor which adds user defined metadeta to an SRB dataset
062 * or collection. When ingesting metadata, only equal operator is used. The
063 * following actor expects as input a reference to the SRB file system. This
064 * reference connection is obtained via the SRBConnect Actor in Kepler. <i>See
065 * SRBConnect and its documentation.</i>
066 * </p>
067 * <p>
068 * The file reference system is created with a unique SRB user account and with
069 * this connection reference as input the SRBAddMD actor is able to gain access
070 * to the SRB file space. Once an alive SRB file connection system has been
071 * established the actor gets the remode SRB file/directory and the attribute
072 * value pair conditions defining the metadata. If the file/directory exists the
073 * conditions are added via jargon API methods to the SRB file or directory.
074 * </p>
075 * <p>
076 * <B>Actor Input:</B> Accepts a reference to the SRB files system, an SRB
077 * remote file/directory path and a list of attribute value pairs.
078 * </p>
079 * <p>
080 * Sample Attribute Value pairs : "a 5","b 10","c abc"
081 * </p>
082 * <p>
083 * The above is a string array of 3 conditions(a,b,c) and their values to be
084 * added to the SRB file/dir metadata.
085 * </p>
086 * <p>
087 * <B>Actor Output:</B> Outputs an exit status.The exit status gives a message
088 * of "success" or appropriate error to indicate the status of adding metadata
089 * process.
090 * 
091 * </p>
092 * <p>
093 * The following actor accesses SRB file reference system and SRB file space
094 * with the SRB Jargon API provided. The JARGON is a pure API for developing
095 * programs with a data grid interface and I/O for SRB file systems.
096 * </p>
097 * 
098 * <A href="http://www.sdsc.edu/srb"><I>Further information on SRB</I> </A>
099 * 
100 * @author Efrat Jaeger
101 * @version $Id: SRBAddMD.java 33633 2015-08-24 22:47:39Z crawl $
102 * @category.name srb
103 * @category.name put
104 */
105public class SRBAddMD extends TypedAtomicActor {
106
107        /**
108         * Construct a constant source with the given container and name. Create the
109         * <i>value</i> parameter, initialize its value to the default value of an
110         * IntToken with value 1.
111         * 
112         * @param container
113         *            The container.
114         * @param name
115         *            The name of this actor.
116         * @exception IllegalActionException
117         *                If the entity cannot be contained by the proposed
118         *                container.
119         * @exception NameDuplicationException
120         *                If the container already has an actor with this name.
121         */
122        public SRBAddMD(CompositeEntity container, String name)
123                        throws NameDuplicationException, IllegalActionException {
124                super(container, name);
125
126                SRBFileSystem = new TypedIOPort(this, "SRBFileSystem", true, false);
127                SRBFileSystem.setTypeEquals(BaseType.GENERAL);
128                new Attribute(SRBFileSystem, "_showName");
129
130                srbFilePath = new TypedIOPort(this, "srbFilePath", true, false);
131                srbFilePath.setTypeEquals(BaseType.STRING); // or should it be an array
132                                                                                                        // of strings.
133                new Attribute(srbFilePath, "_showName");
134
135                conditions = new TypedIOPort(this, "conditions", true, false);
136                conditions.setTypeEquals(new ArrayType(BaseType.STRING));
137                new Attribute(conditions, "_showName");
138
139                trigger = new TypedIOPort(this, "trigger", false, true);
140                trigger.setTypeEquals(BaseType.STRING);
141                new Attribute(trigger, "_showName");
142
143                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
144                                + "width=\"150\" height=\"40\" " + "style=\"fill:white\"/>\n"
145                                + "<text x=\"7\" y=\"30\""
146                                + "style=\"font-size:12; fill:black; font-family:SansSerif\">"
147                                + "SRB$</text>\n" + "<text x=\"41\" y=\"31\""
148                                + "style=\"font-size:16; fill:blue; font-family:SansSerif\">"
149                                + "Add MetaData</text>\n" + "</svg>\n");
150        }
151
152        /**
153         * pointer to the SRB file system.
154         */
155        public TypedIOPort SRBFileSystem;
156
157        /**
158         * Path to SRB file.
159         */
160        public TypedIOPort srbFilePath;
161
162        /**
163         * {"att val"}
164         */
165        public TypedIOPort conditions;
166
167        /**
168         * An input trigger.
169         */
170        public TypedIOPort trigger;
171
172        // /////////////////////////////////////////////////////////////////
173        // // public methods ////
174        /**
175         * Add the metadata conditions to the specified file/directory. The
176         * conditions are in the form of "Att Val".
177         * 
178         * @exception IllegalActionException
179         *                If it is thrown if the SRB file cannot be accessed or the
180         *                current directory cannot be broadcasted.
181         */
182        public void fire() throws IllegalActionException {
183
184                SRBFile srbFile;
185                LocalFile localFile;
186                String localFilePath;
187                String _exitCode = "";
188
189                try {
190                        // make sure there is an alive connection.
191                        try {
192                                srbFileSystem.getHost();
193                        } catch (Exception ex) { // connection was closed.
194                                srbFileSystem = null;
195                                ObjectToken SRBConOT = null;
196                                try { // try to get a new connection in case the previous one
197                                                // has terminated.
198                                        SRBConOT = (ObjectToken) SRBFileSystem.get(0);
199                                } catch (NoTokenException ntex) {
200                                }
201                                if (SRBConOT != null) {
202                                        srbFileSystem = (SRBFileSystem) SRBConOT.getValue();
203                                }
204                        }
205                        if (srbFileSystem == null) {
206                                throw new IllegalActionException(this,
207                                                "No SRB connection available in actor "
208                                                                + this.getName() + ".");
209                        }
210
211                        String srbFileStr = ((StringToken) srbFilePath.get(0))
212                                        .stringValue();
213                        srbFile = new SRBFile(srbFileSystem, srbFileStr);
214
215                        if (srbFile.exists()) {
216                                Token[] conds = ((ArrayToken) conditions.get(0)).arrayValue();
217                                int numConds = conds.length;
218                                String[][] definableMetaDataValues = new String[numConds][2];
219                                int operators[] = new int[numConds];
220
221                                for (int i = 0; i < numConds; i++) {
222                                        // ignoring [-dcur] for the mean time.
223                                        String condition = ((StringToken) conds[i]).stringValue();
224                                        StringTokenizer st = new StringTokenizer(condition);
225                                        int j = 0;
226                                        while (st.hasMoreTokens()) {
227                                                definableMetaDataValues[i][j++] = st.nextToken();
228                                        }
229                                        if (j < 2) // should be an attribute value pair.
230                                                GraphicalMessageHandler.error("incomplete condition '"
231                                                                + condition + "'.");
232                                        operators[i] = MetaDataCondition.EQUAL;
233                                }
234                                MetaDataTable metaDataTable = new MetaDataTable(operators,
235                                                definableMetaDataValues);
236
237                                MetaDataRecordList[] record = new MetaDataRecordList[1];
238
239                                if (srbFile.isDirectory()) {
240                                        record[0] = new SRBMetaDataRecordList(
241                                                        SRBMetaDataSet
242                                                                        .getField(SRBMetaDataSet.DEFINABLE_METADATA_FOR_DIRECTORIES),
243                                                        metaDataTable);
244
245                                } else {
246                                        record[0] = new SRBMetaDataRecordList(
247                                                        SRBMetaDataSet
248                                                                        .getField(SRBMetaDataSet.DEFINABLE_METADATA_FOR_FILES),
249                                                        metaDataTable);
250
251                                }
252
253                                srbFile.modifyMetaData(record[0]);
254
255                        } else
256                                GraphicalMessageHandler.error(srbFile.getAbsolutePath()
257                                                + " does not exist.");
258
259                        // FIXME: what should be returned here..
260                        if (_exitCode.equals("")) {
261                                _exitCode = "success";
262                        }
263                        trigger.broadcast(new StringToken(_exitCode));
264                } catch (Exception ex) {
265                        srbFileSystem = SRBUtil.closeConnection(srbFileSystem);
266                        ex.printStackTrace();
267                        throw new IllegalActionException(this, ex.getMessage()
268                                        + ". in actor " + this.getName());
269                }
270        }
271
272        /**
273         * Initialize the srb file system to null.
274         */
275        public void initialize() throws IllegalActionException {
276                super.initialize();
277                srbFileSystem = null;
278        }
279
280        /**
281         * Disconnect from SRB.
282         */
283        public void wrapup() {
284                srbFileSystem = SRBUtil.closeConnection(srbFileSystem);
285        }
286
287        private SRBFileSystem srbFileSystem = null;
288}