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.RwalkMessage;
37 import uk.ac.rdg.resc.jstyx.messages.RstatMessage;
38 import uk.ac.rdg.resc.jstyx.messages.RopenMessage;
39 import uk.ac.rdg.resc.jstyx.messages.RcreateMessage;
40
41 /***
42 * Callback used by CStyxFile.openOrCreateAsync()
43 *
44 * @author jdb
45 * $Revision: 353 $
46 * $Date: 2005-08-10 19:33:48 +0100 (Wed, 10 Aug 2005) $
47 * $Log$
48 * Revision 1.2 2005/08/10 18:33:48 jonblower
49 * Bug fixes
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 OpenOrCreateCallback extends MessageCallback
57 {
58 private boolean isDirectory;
59 private int mode;
60 private MessageCallback callback;
61 private CStyxFile file;
62 private StyxConnection conn;
63
64 public OpenOrCreateCallback(CStyxFile file, boolean isDirectory, int mode,
65 MessageCallback callback)
66 {
67 this.file = file;
68 this.conn = this.file.getConnection();
69 this.isDirectory = isDirectory;
70 this.mode = mode;
71 this.callback = callback;
72 }
73
74 public void nextStage()
75 {
76 // We must first find out if this is a file or directory (will also
77 // test to see if it exists)
78 if (this.file.hasFid())
79 {
80 // See if we have a Qid, so that we can work out whether this is
81 // a file or directory
82 if (this.file.getQid() == null)
83 {
84 this.file.refreshAsync(this);
85 }
86 else
87 {
88 // Check to see if it is the right type of file (i.e. file or
89 // directory)
90 if (this.isDirectory == (this.file.getQid().getType() == 128))
91 {
92 // Now we can open the file
93 this.file.openAsync(this.mode, this);
94 }
95 else
96 {
97 String errMsg = this.file.getPath() + " already exists as a " +
98 ((this.file.getQid().getType() == 128) ? "directory" : "file");
99 this.error(errMsg, null);
100 }
101 }
102 }
103 else
104 {
105 // We don't have a fid for the file yet
106 this.file.walkFidAsync(this);
107 }
108 }
109
110 public void replyArrived(StyxMessage rMessage, StyxMessage tMessage)
111 {
112 if (rMessage instanceof RwalkMessage)
113 {
114 // We've just successfully walked to the file so we have a fid
115 // and it must exist. Move to the next stage (checking for existence
116 // of a qid)
117 this.nextStage();
118 }
119 else if (rMessage instanceof RstatMessage)
120 {
121 // We've just successfully got the stat of the file, so we have
122 // the qid. Move to the next stage (i.e. checking
123 // the file type)
124 this.nextStage();
125 }
126 else if (rMessage instanceof RopenMessage)
127 {
128 // We've just opened the file.
129 if (this.callback != null)
130 {
131 this.callback.replyArrived(rMessage, tMessage);
132 }
133 else
134 {
135 this.file.fireOpen();
136 }
137 }
138 else if (rMessage instanceof RcreateMessage)
139 {
140 // Must be an RcreateMessage
141 if (this.callback != null)
142 {
143 this.callback.replyArrived(rMessage, tMessage);
144 }
145 else
146 {
147 this.file.fireCreated();
148 }
149 }
150 }
151
152 public void error(String message, StyxMessage tMessage)
153 {
154 if (!this.file.hasFid())
155 {
156 // The file does not exist
157 int perm = this.isDirectory ? 0777 : 0666;
158 this.file.createAsync(this.isDirectory, perm, this.mode, this);
159 }
160 else if (this.callback != null)
161 {
162 this.callback.error(message, tMessage);
163 }
164 else
165 {
166 this.file.fireError(message);
167 }
168 }
169 }