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.Vector;
033
034import edu.sdsc.grid.io.MetaDataRecordList;
035import edu.sdsc.grid.io.local.LocalFile;
036import edu.sdsc.grid.io.srb.SRBFile;
037import edu.sdsc.grid.io.srb.SRBFileSystem;
038import edu.sdsc.grid.io.srb.SRBMetaDataSet;
039import ptolemy.actor.NoTokenException;
040import ptolemy.actor.TypedAtomicActor;
041import ptolemy.actor.TypedIOPort;
042import ptolemy.data.ArrayToken;
043import ptolemy.data.ObjectToken;
044import ptolemy.data.StringToken;
045import ptolemy.data.Token;
046import ptolemy.data.type.ArrayType;
047import ptolemy.data.type.BaseType;
048import ptolemy.kernel.CompositeEntity;
049import ptolemy.kernel.util.Attribute;
050import ptolemy.kernel.util.IllegalActionException;
051import ptolemy.kernel.util.NameDuplicationException;
052
053//////////////////////////////////////////////////////////////////////////
054//// SGetPhysicalLocation
055/**
056 * <p>
057 * Get the physical location of SRB files. Returns the physical location of a
058 * logical SRB path. This remote location cannot be accessed through windows.
059 * The following actor expects as input a reference to the SRB file system. This
060 * reference connection is obtained via the SRBConnect Actor in Kepler. <i>See
061 * SRBConnect and its documentation.</i>
062 * </p>
063 * <p>
064 * The file reference system is created with a unique SRB user account and with
065 * this connection reference as input the SRBPhysicalLocation actor is able to
066 * gain access to the SRB file space. Once an alive SRB file connection system
067 * has been established the actor gets the remode SRB file path and checks for
068 * it's existence. The SRB file is queried for its PATH_NAME (Physical Location)
069 * from its Metadata.
070 * </p>
071 * <p>
072 * <B>Actor Input:</B> Accepts a reference to the SRB files system, an SRB
073 * remote file name(s) as input. SRB remote file name is the logical file path.
074 * </p>
075 * <p>
076 * <B>SRB Logical Name Space</B> It is easy to think of SRB Collections as Unix
077 * directories (or Windows folders), but there is a fundamental difference. Each
078 * individual data object (file) in a collection can be stored on a different
079 * physical device. Unix directories and Windows folders use space from the
080 * physical device on which they reside, but SRB collections are part of a
081 * "logical name space" that exists in the MCAT and maps individual data objects
082 * (files) to physical files. The logical name space is the set of names of
083 * collections (directories) and data objects (files) maintained by the SRB.
084 * Users see and interact with the logical name space, and the physical location
085 * is handled by the SRB system and administrators. The SRB system adds this
086 * logical name space on top of the physcial name space, and derives much of its
087 * power and functionality from that.
088 * </p>
089 * <p>
090 * <B>Actor Output:</B> The actor returns the physical location of SRB path.Also
091 * returned is an exit code status indicating either success or failure/errors
092 * in getting file's physical location.
093 * </p>
094 * <p>
095 * <P>
096 * The following actor accesses SRB file reference system and SRB file space
097 * with the SRB Jargon API provided. The JARGON is a pure API for developing
098 * programs with a data grid interface and I/O for SRB file systems.
099 * </p>
100 * <A href="http://www.sdsc.edu/srb"><I>Further information on SRB</I> </A>h
101 * 
102 * @author Efrat Jaeger
103 * @version $Id: SGetPhysicalLocation.java 33633 2015-08-24 22:47:39Z crawl $
104 * @category.name srb
105 * @category.name put
106 */
107
108public class SGetPhysicalLocation extends TypedAtomicActor {
109
110        /**
111         * Construct a constant source with the given container and name. Create the
112         * <i>value</i> parameter, initialize its value to the default value of an
113         * IntToken with value 1.
114         * 
115         * @param container
116         *            The container.
117         * @param name
118         *            The name of this actor.
119         * @exception IllegalActionException
120         *                If the entity cannot be contained by the proposed
121         *                container.
122         * @exception NameDuplicationException
123         *                If the container already has an actor with this name.
124         */
125        public SGetPhysicalLocation(CompositeEntity container, String name)
126                        throws NameDuplicationException, IllegalActionException {
127                super(container, name);
128
129                SRBFileSystem = new TypedIOPort(this, "SRBFileSystem", true, false);
130                SRBFileSystem.setTypeEquals(BaseType.GENERAL);
131                new Attribute(SRBFileSystem, "_showName");
132
133                logicalPath = new TypedIOPort(this, "logicalPath", true, false);
134                logicalPath.setTypeEquals(new ArrayType(BaseType.STRING));
135                new Attribute(logicalPath, "_showName");
136
137                physicalPath = new TypedIOPort(this, "physicalPath", false, true);
138                physicalPath.setTypeEquals(new ArrayType(BaseType.STRING));
139                new Attribute(physicalPath, "_showName");
140
141                /*
142                 * exitCode = new TypedIOPort(this, "exitCode", false, true);
143                 * exitCode.setTypeEquals(BaseType.STRING); new Attribute(exitCode,
144                 * "_showName");
145                 */
146                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
147                                + "width=\"158\" height=\"30\" " + "style=\"fill:white\"/>\n"
148                                + "<text x=\"7\" y=\"24\" "
149                                + "style=\"font-size:12; fill:black; font-family:SansSerif\">"
150                                + "SRB$</text>\n" + "<text x=\"41\" y=\"25\" "
151                                + "style=\"font-size:14; fill:blue; font-family:SansSerif\">"
152                                + "Physical Location</text>\n" + "</svg>\n");
153        }
154
155        /**
156         * Connection reference
157         */
158        public TypedIOPort SRBFileSystem;
159
160        /**
161         * Logical path to SRB file
162         */
163        public TypedIOPort logicalPath;
164
165        /**
166         * Paths to the local location.
167         */
168        public TypedIOPort physicalPath;
169
170        // public TypedIOPort exitCode;
171
172        // /////////////////////////////////////////////////////////////////
173        // // public methods ////
174        /**
175         * Get the physical location of SRB logical file paths.
176         * 
177         * @exception IllegalActionException
178         *                If it is thrown if the SRB file cannot be accessed or the
179         *                current directory cannot be broadcasted.
180         */
181        public void fire() throws IllegalActionException {
182
183                SRBFile srbFile;
184                LocalFile localFile;
185                String localFilePath;
186                String _exitCode = "";
187
188                try {
189                        // make sure there is an alive connection.
190                        try {
191                                srbFileSystem.getHost();
192                        } catch (Exception ex) { // connection was closed.
193                                srbFileSystem = null;
194                                ObjectToken SRBConOT = null;
195                                try { // try to get a new connection in case the previous one
196                                                // has terminated.
197                                        SRBConOT = (ObjectToken) SRBFileSystem.get(0);
198                                } catch (NoTokenException ntex) {
199                                }
200                                if (SRBConOT != null) {
201                                        srbFileSystem = (SRBFileSystem) SRBConOT.getValue();
202                                }
203                        }
204                        if (srbFileSystem != null) {
205
206                                // Getting the list of file to query for logical path.
207                                ArrayToken logFilesTokenArr = null;
208                                try {
209                                        logFilesTokenArr = (ArrayToken) logicalPath.get(0);
210                                } catch (Exception ex) {
211                                        _debug("logFilesTokenArr port is null.");
212                                }
213                                if (logFilesTokenArr != null) {
214                                        Token[] logFilesToken = logFilesTokenArr.arrayValue();
215                                        Vector physicalLocVec = new Vector();
216                                        for (int i = 0; i < logFilesToken.length; i++) {
217
218                                                // srb file path.
219                                                String logFileStr = ((StringToken) logFilesToken[i])
220                                                                .stringValue();
221                                                _debug("<FILE_TO_PUT>" + logFileStr + "<FILE_TO_PUT>");
222
223                                                srbFile = new SRBFile(srbFileSystem, logFileStr);
224
225                                                if (srbFile.exists()) {
226                                                        MetaDataRecordList[] record = srbFile
227                                                                        .query(SRBMetaDataSet.PATH_NAME);
228
229                                                        // extract the physical location
230                                                        String physicalLocation = record[0]
231                                                                        .getStringValue(0);
232                                                        physicalLocVec
233                                                                        .add(new StringToken(physicalLocation));
234                                                } else
235                                                        _exitCode += srbFile.getAbsolutePath()
236                                                                        + "does not exist.\n";
237                                        }
238                                        if (physicalLocVec.size() > 0) {
239                                                Token[] physicalPathArr = new Token[physicalLocVec
240                                                                .size()];
241                                                physicalLocVec.toArray(physicalPathArr);
242                                                physicalPath.broadcast(new ArrayToken(physicalPathArr));
243                                        }
244                                        if (_exitCode.equals("")) {
245                                                _exitCode = "success";
246                                        }
247                                        // exitCode.broadcast(new StringToken(_exitCode));
248                                }
249                        } else
250                                throw new IllegalActionException(this,
251                                                "No SRB connection available in actor "
252                                                                + this.getName() + ".");
253                } catch (Exception ex) {
254                        srbFile = null;
255                        srbFileSystem = SRBUtil.closeConnection(srbFileSystem);
256                        throw new IllegalActionException(this, ex.getMessage()
257                                        + ". in actor " + this.getName() + ".");
258                }
259        }
260
261        /**
262         * Initialize the srb file system to null.
263         */
264        public void initialize() throws IllegalActionException {
265                super.initialize();
266                srbFileSystem = null;
267        }
268
269        /**
270         * Disconnect from SRB.
271         */
272        public void wrapup() {
273                srbFileSystem = SRBUtil.closeConnection(srbFileSystem);
274        }
275
276        private SRBFileSystem srbFileSystem = null;
277}