001/*
002 * Copyright (c) 2002-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.io.IOException;
033
034import edu.sdsc.grid.io.srb.SRBAccount;
035import edu.sdsc.grid.io.srb.SRBFileSystem;
036import ptolemy.actor.lib.Source;
037import ptolemy.data.ObjectToken;
038import ptolemy.data.type.BaseType;
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.util.Attribute;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.kernel.util.NameDuplicationException;
043import ptolemy.kernel.util.StringAttribute;
044
045//////////////////////////////////////////////////////////////////////////
046//// SRBConnect
047/**
048 * <p>
049 * The SRBConnection actor provides users with valid accounts an ability to
050 * connect to the SDSC Storage Resoure Broker from within the Kepler
051 * computational environment. SRB actors enable access to a Data Grid Management
052 * System (DGMS) that provides a hierarchical logical namespace to manage the
053 * organization of data (usually files).
054 * </p>
055 * <p>
056 * This actor connects to the SRB and returns a reference to the SRB file
057 * system. This reference can be further shared by other SRB Actors which
058 * perform various SCommands like functionality from within the Kepler Worfklows
059 * system. The connection reference can be propagated to all actors accessing
060 * the SRB workspace. This actor will create a different connection object to
061 * each connected channel to allow paralel operations.
062 * </p>
063 * <p>
064 * To access the Distributed Logical File System and other SRB features each
065 * user is assigned an SRB account which includes the following information:
066 * <ul>
067 * <li>srbHost</li>
068 * <li>srbPort</li>
069 * <li>srbUserName</li>
070 * <li>srbPasswd</li>
071 * <li>srbHomeCollection</li>
072 * <li>srbDomainName</li>
073 * <li>srbDefaultResource</li>
074 * </ul>
075 * </p>
076 * <p>
077 * <B>Required User input: </B>The SRBConnection actor asks the user to specify
078 * the above connection parameters and in return creates an SRB Account and
079 * outputs the created SRB file system. The user needs to specify the following
080 * connection parameters: srbHost, srbPort, srbUserName, srbPasswd,
081 * srbHomeCollection, srbMdasDomainHome and srbDefaultResource by double-
082 * clicking the actor and entering the above information in the Edit Parameters
083 * Dialog Box.
084 * </p>
085 * <p>
086 * <B>Actor Output:</B> The SRB connection reference system.
087 * </p>
088 * <p>
089 * The following actor creates SRB Account and SRB file reference system with
090 * the SRB Jargon API provided. The JARGON is a pure API for developing programs
091 * with a data grid interface and I/O for SRB file systems.
092 * </p>
093 * <A href="http://www.sdsc.edu/srb"><I>Further information on SRB</I> </A>
094 * 
095 * @author Bing Zhu and Efrat Jaeger
096 * @version $Id: SRBConnect.java 33633 2015-08-24 22:47:39Z crawl $
097 * @since Ptolemy II 3.0.2
098 */
099public class SRBConnect extends Source {
100
101        /**
102         * Construct an actor with the given container and name.
103         * 
104         * @param container
105         *            The container.
106         * @param name
107         *            The name of this actor.
108         * @exception IllegalActionException
109         *                If the actor cannot be contained by the proposed
110         *                container.
111         * @exception NameDuplicationException
112         *                If the container already has an actor with this name.
113         */
114        public SRBConnect(CompositeEntity container, String name)
115                        throws NameDuplicationException, IllegalActionException {
116
117                super(container, name);
118
119                srbHost = new StringAttribute(this, "srbHost");
120                srbPort = new StringAttribute(this, "srbPort");
121                srbUserName = new StringAttribute(this, "srbUserName");
122                srbPasswd = new StringAttribute(this, "srbPasswd");
123                srbHomeCollection = new StringAttribute(this, "srbHomeCollection");
124                srbMdasDomainHome = new StringAttribute(this, "srbMdasDomainHome");
125                srbDefaultResource = new StringAttribute(this, "srbDefaultResource");
126
127                // Set the type constraint.
128                output.setName("SRBFileSystem");
129                output.setTypeEquals(BaseType.GENERAL);
130                output.setMultiport(true);
131                new Attribute(output, "_showName");
132
133                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
134                                + "width=\"128\" height=\"30\" " + "style=\"fill:white\"/>\n"
135                                + "<text x=\"7\" y=\"24\" "
136                                + "style=\"font-size:12; fill:black; font-family:SansSerif\">"
137                                + "SRB$</text>\n" + "<text x=\"41\" y=\"25\" "
138                                + "style=\"font-size:16; fill:blue; font-family:SansSerif\">"
139                                + "CONNECT</text>\n" + "</svg>\n");
140
141        }
142
143        // /////////////////////////////////////////////////////////////////
144        // // ports and parameters ////
145
146        /**
147         * srbHost : represents available srb server hosts
148         * 
149         */
150        public StringAttribute srbHost;
151
152        /**
153         * the port number required to connect to the srb server
154         * 
155         */
156        public StringAttribute srbPort;
157
158        /**
159         * SRB Users are uniquely identified by their usernames combined with their
160         * domains. SRBadmin has the authority to create domains.
161         * 
162         */
163        public StringAttribute srbUserName;
164
165        /**
166         * SRB User's password
167         * 
168         */
169        public StringAttribute srbPasswd;
170
171        /**
172         * Each SRB-registered user is started with a 'home' collection. They are
173         * given read, write and create-sub collection and grant permitsin that
174         * collection.
175         * 
176         */
177        public StringAttribute srbHomeCollection;
178
179        /**
180         * A domainHome is used to identify a site or project
181         * 
182         */
183        public StringAttribute srbMdasDomainHome;
184
185        /**
186         * A SRB resource is a system that is capable of storing data objects and is
187         * accessible to the SRB
188         * 
189         */
190        public StringAttribute srbDefaultResource;
191
192        // /////////////////////////////////////////////////////////////////
193        // // public methods ////
194
195        /**
196         * Connects to SRB and returns a connection reference.
197         */
198        public void fire() throws IllegalActionException {
199                if (_first) {
200                        for (int i = 0; i < output.getWidth(); i++)
201                                output.send(i, new ObjectToken(srbConnections[i]));
202                        _first = false;
203                } else {
204                        for (int i = 0; i < output.getWidth(); i++) {
205                                // making sure that each connection is still alive
206                                // if not create a new instance and send it.
207                                try {
208                                        srbConnections[i].getHost();
209                                } catch (Exception ex) {
210                                        try { // the connection was terminated - reconnect.
211                                                srbConnections[i] = new SRBFileSystem(srbAccount);
212                                                output.send(i, new ObjectToken(srbConnections[i]));
213                                        } catch (IOException ioex) {
214                                                // if cannot reconnect to srb throw the failure reason.
215                                                throw new IllegalActionException(this,
216                                                                "SRB connection closed on channel " + i
217                                                                                + " due to " + ex.getMessage()
218                                                                                + ".\n Could not reconnect to SRB: "
219                                                                                + ioex.getMessage());
220                                        }
221                                }
222                        }
223                }
224        }
225
226        /**
227         * Connect to SRB account.
228         */
229        public void initialize() throws IllegalActionException {
230                super.initialize();
231
232                _first = true;
233                String _srbHost = srbHost.getExpression();
234                String _srbPort = srbPort.getExpression();
235                String _srbUserName = srbUserName.getExpression();
236                String _srbPasswd = srbPasswd.getExpression();
237                String _srbHomeCollection = srbHomeCollection.getExpression();
238                String _srbMdasDomainHome = srbMdasDomainHome.getExpression();
239                String _srbDefaultResource = srbDefaultResource.getExpression();
240
241                // reset existing connections.
242                if (srbConnections != null) {
243                        for (int i = 0; i < srbConnections.length; i++) {
244                                srbConnections[i] = null;
245                        }
246                }
247
248                try {
249                        srbAccount = new SRBAccount(_srbHost, Integer.parseInt(_srbPort),
250                                        _srbUserName, _srbPasswd, _srbHomeCollection,
251                                        _srbMdasDomainHome, _srbDefaultResource);
252
253                        int outWidth = output.getWidth();
254                        srbConnections = new SRBFileSystem[outWidth];
255                        for (int i = 0; i < outWidth; i++) {
256                                try {
257                                        srbConnections[i] = new SRBFileSystem(srbAccount);
258                                } catch (Exception ex) {
259                                        ex.printStackTrace();
260                                        for (int j = 0; j <= i; j++) {
261                                                srbConnections[j] = SRBUtil
262                                                                .closeConnection(srbConnections[j]);
263                                        }
264                                        throw new IllegalActionException(this,
265                                                        "Could not create an SRB connection for channel "
266                                                                        + i + ": " + ex.getMessage());
267                                }
268                        }
269                } catch (Exception ex) {
270                        ex.printStackTrace();
271                        throw new IllegalActionException(this, ex.getMessage());
272                }
273        }
274
275        /**
276         * Disconnect from SRB.
277         */
278        public void wrapup() {
279                _first = true;
280                System.out.println(this.getName() + ":");
281                for (int i = 0; i < srbConnections.length; i++) {
282                        // System.out.println(srbConnections[i].toString());
283                        srbConnections[i] = SRBUtil.closeConnection(srbConnections[i]);
284                }
285                srbAccount = null;
286        }
287
288        // /////////////////////////////////////////////////////////////////
289        // // private members ////
290
291        /**
292         * SRB account
293         */
294        private SRBAccount srbAccount = null;
295
296        /**
297         * SRB connections array.
298         */
299        private SRBFileSystem[] srbConnections = null;
300
301        /**
302         * Indicates first iteration - send all connections
303         */
304        private boolean _first = true;
305
306}