View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.client.callbacks;
30  
31  import uk.ac.rdg.resc.jstyx.client.StyxConnection;
32  import uk.ac.rdg.resc.jstyx.client.CStyxFile;
33  import uk.ac.rdg.resc.jstyx.client.MessageCallback;
34  
35  import uk.ac.rdg.resc.jstyx.messages.StyxMessage;
36  import uk.ac.rdg.resc.jstyx.messages.TwalkMessage;
37  import uk.ac.rdg.resc.jstyx.messages.RwalkMessage;
38  import uk.ac.rdg.resc.jstyx.messages.TcreateMessage;
39  import uk.ac.rdg.resc.jstyx.messages.RcreateMessage;
40  
41  /***
42   * Callback used when creating a file (see CStyxFile.createAsync())
43   *
44   * @author jdb
45   * $Revision: 350 $
46   * $Date: 2005-08-08 10:36:19 +0100 (Mon, 08 Aug 2005) $
47   * $Log$
48   * Revision 1.2  2005/08/08 09:36:19  jonblower
49   * Minor changes
50   *
51   * Revision 1.1  2005/08/05 13:46:40  jonblower
52   * Factored out all callback objects from CStyxFile into separate classes
53   *
54   */
55  
56  public class CreateCallback extends MessageCallback
57  {
58      private boolean isDirectory;
59      private int permissions;
60      private int mode;
61      private MessageCallback callback;
62      private long parentFid;
63      private CStyxFile file;
64      private StyxConnection conn;
65  
66      public CreateCallback(CStyxFile file, boolean isDirectory, int permissions,
67          int mode, MessageCallback callback)
68      {
69          this.file = file;
70          this.conn = this.file.getConnection();
71          this.isDirectory = isDirectory;
72          this.permissions = permissions;
73          this.mode = mode;
74          this.callback = callback;
75          this.parentFid = -1;
76      }
77  
78      public void nextStage()
79      {
80          if (this.file.hasFid())
81          {
82              // We already have a fid for this file
83              this.error("File already exists", null);
84          }
85          else if (this.parentFid < 0)
86          {
87              // We need to get a fid for the parent directory
88              TwalkMessage tWalkMsg = new TwalkMessage(this.conn.getRootFid(), 
89                  this.conn.getFreeFid(), this.file.getParentPath());
90              conn.sendAsync(tWalkMsg, this);
91          }
92          else
93          {
94              // We're ready to create the file
95              TcreateMessage tCreateMsg = new TcreateMessage(this.parentFid,
96                  this.file.getName(), this.permissions, this.isDirectory, this.mode);
97              conn.sendAsync(tCreateMsg, this);
98          }
99      }
100 
101     public void replyArrived(StyxMessage rMessage, StyxMessage tMessage)
102     {
103         if (rMessage instanceof RwalkMessage)
104         {
105             // Walk to the parent directory was at least partially successful
106             RwalkMessage rWalkMsg = (RwalkMessage)rMessage;
107             TwalkMessage tWalkMsg = (TwalkMessage)tMessage;
108             if (rWalkMsg.getNumSuccessfulWalks() == tWalkMsg.getNumPathElements())
109             {
110                 // We've got the fid of the parent directory
111                 this.parentFid = tWalkMsg.getNewFid();
112                 this.nextStage();
113             }
114             else
115             {
116                 String errMsg = "'" + 
117                 tWalkMsg.getPathElements()[rWalkMsg.getNumSuccessfulWalks()]
118                     + "' does not exist.";
119                 this.error(errMsg, tWalkMsg);
120             }
121         }
122         else
123         {
124             // This must be an RcreateMessage
125             RcreateMessage rCreateMsg = (RcreateMessage)rMessage;
126             TcreateMessage tCreateMsg = (TcreateMessage)tMessage;
127             this.file.setFid(tCreateMsg.getFid());
128             this.file.setMode(tCreateMsg.getMode());
129             this.file.setIoUnit((int)rCreateMsg.getIoUnit());
130             if (this.callback != null)
131             {
132                 callback.replyArrived(rMessage, tMessage);
133             }
134             else
135             {
136                 this.file.fireCreated();
137             }
138         }
139     }
140 
141     public void error(String message, StyxMessage tMessage)
142     {
143         if (tMessage instanceof TwalkMessage)
144         {
145             // return the fid to the pool
146             conn.returnFid(((TwalkMessage)tMessage).getNewFid());
147         }
148         if (this.callback != null)
149         {
150             this.callback.error(message, tMessage);
151         }
152         else
153         {
154             this.file.fireError(message);
155         }
156     }
157 }