1 package fr.in2p3.jsaga.impl.logicalfile;
2
3 import fr.in2p3.jsaga.adaptor.data.DataAdaptor;
4 import fr.in2p3.jsaga.adaptor.data.ParentDoesNotExist;
5 import fr.in2p3.jsaga.adaptor.data.read.DataReaderAdaptor;
6 import fr.in2p3.jsaga.adaptor.data.read.LogicalReader;
7 import fr.in2p3.jsaga.adaptor.data.write.LogicalWriter;
8 import fr.in2p3.jsaga.impl.logicalfile.copy.LogicalFileCopy;
9 import fr.in2p3.jsaga.impl.logicalfile.copy.LogicalFileCopyFrom;
10 import fr.in2p3.jsaga.impl.namespace.*;
11 import fr.in2p3.jsaga.impl.url.URLHelper;
12 import fr.in2p3.jsaga.impl.url.AbstractURLImpl;
13 import fr.in2p3.jsaga.sync.logicalfile.SyncLogicalFile;
14 import org.ogf.saga.SagaObject;
15 import org.ogf.saga.error.*;
16 import org.ogf.saga.namespace.*;
17 import org.ogf.saga.session.Session;
18 import org.ogf.saga.url.URL;
19 import org.ogf.saga.url.URLFactory;
20
21 import java.util.*;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public abstract class AbstractSyncLogicalFileImpl extends AbstractNSEntryImplWithMetaData implements SyncLogicalFile {
36
37 public AbstractSyncLogicalFileImpl(Session session, URL url, DataAdaptor adaptor, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
38 super(session, URLHelper.toFileURL(url), adaptor, new FlagsHelper(flags).keep(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS));
39 boolean initOK = false;
40 try {
41 this.init(flags);
42 initOK = true;
43 } finally {
44 if (!initOK) this.close();
45 }
46 }
47
48
49 public AbstractSyncLogicalFileImpl(AbstractNSDirectoryImpl dir, URL relativeUrl, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
50 super(dir, URLHelper.toFileURL(relativeUrl), new FlagsHelper(flags).keep(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS));
51 boolean initOK = false;
52 try {
53 this.init(flags);
54 initOK = true;
55 } finally {
56 if (!initOK) this.close();
57 }
58 }
59
60
61 public AbstractSyncLogicalFileImpl(AbstractNSEntryImpl entry, String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
62 super(entry, URLHelper.toFilePath(absolutePath), new FlagsHelper(flags).keep(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS));
63 boolean initOK = false;
64 try {
65 this.init(flags);
66 initOK = true;
67 } finally {
68 if (!initOK) this.close();
69 }
70 }
71
72 private void init(int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
73 if(Flags.CREATEPARENTS.isSet(flags)) flags=Flags.CREATE.or(flags);
74 if(Flags.CREATE.isSet(flags)) flags=Flags.WRITE.or(flags);
75 new FlagsHelper(flags).allowed(JSAGAFlags.BYPASSEXIST, Flags.ALLLOGICALFILEFLAGS);
76 if (Flags.CREATE.isSet(flags)) {
77 if (m_adaptor instanceof LogicalWriter) {
78 try {
79 this.tryToCreate(flags);
80 } catch (DoesNotExistException e) {
81 if (Flags.CREATEPARENTS.isSet(flags)) {
82
83 this._makeParentDirs();
84
85 this.tryToCreate(flags);
86 } else {
87 throw e;
88 }
89 }
90 } else {
91 throw new NotImplementedException("Not supported for this protocol: "+ m_url.getScheme());
92 }
93 } else if (!JSAGAFlags.BYPASSEXIST.isSet(flags) && !((AbstractURLImpl)m_url).hasCache() && m_adaptor instanceof DataReaderAdaptor) {
94 boolean exists = ((DataReaderAdaptor)m_adaptor).exists(
95 m_url.getPath(),
96 m_url.getQuery());
97 if (! exists) {
98 throw new DoesNotExistException("Logical file does not exist: "+ m_url);
99 }
100 }
101 }
102 private void tryToCreate(int flags) throws PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
103 try {
104 ((LogicalWriter)m_adaptor).create(
105 m_url.getPath(),
106 m_url.getQuery());
107 } catch (AlreadyExistsException e) {
108 if (Flags.EXCL.isSet(flags)) {
109 throw new AlreadyExistsException("Entry already exists: "+ m_url);
110 }
111 } catch (ParentDoesNotExist e) {
112 throw new DoesNotExistException("Failed to create parent directory", e.getCause());
113 }
114 }
115
116
117 public SagaObject clone() throws CloneNotSupportedException {
118 return super.clone();
119 }
120
121
122 public void copySync(URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, AlreadyExistsException, TimeoutException, NoSuccessException, IncorrectURLException {
123 new FlagsHelper(flags).allowed(JSAGAFlags.PRESERVETIMES, Flags.DEREFERENCE, Flags.CREATEPARENTS, Flags.OVERWRITE);
124 if (Flags.DEREFERENCE.isSet(flags)) {
125 AbstractSyncNSEntryImpl targetEntry = this._dereferenceEntry();
126 try {
127 targetEntry.copySync(target, flags - Flags.DEREFERENCE.getValue());
128 } finally {
129 targetEntry.close();
130 }
131 return;
132 }
133 URL effectiveTarget = this._getEffectiveURL(target);
134 if (JSAGAFlags.PRESERVETIMES.isSet(flags)) {
135
136 long sourceTimes = this.getMTime();
137 AbstractNSEntryImpl targetEntry = super._getTargetEntry_checkPreserveTimes(effectiveTarget);
138 try {
139
140 new LogicalFileCopy(m_session, this, m_adaptor).copy(effectiveTarget, flags);
141
142
143 targetEntry.setMTime(sourceTimes);
144 } finally {
145 targetEntry.close();
146 }
147 } else {
148
149 new LogicalFileCopy(m_session, this, m_adaptor).copy(effectiveTarget, flags);
150 }
151 }
152
153 public void copyFromSync(URL source, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException, IncorrectURLException {
154 new FlagsHelper(flags).allowed(JSAGAFlags.PRESERVETIMES, Flags.DEREFERENCE, Flags.OVERWRITE);
155 if (Flags.DEREFERENCE.isSet(flags)) {
156 AbstractSyncNSEntryImpl entry = this._dereferenceEntry();
157 try {
158 entry.copyFromSync(source, flags - Flags.DEREFERENCE.getValue());
159 } finally {
160 entry.close();
161 }
162 return;
163 }
164 URL effectiveSource = this._getEffectiveURL(source);
165 if (JSAGAFlags.PRESERVETIMES.isSet(flags)) {
166
167 long sourceTimes = super._getSourceTimes_checkPreserveTimes(effectiveSource);
168
169
170 new LogicalFileCopyFrom(m_session, this, m_adaptor).copyFrom(effectiveSource, flags);
171
172
173 this.setMTime(sourceTimes);
174 } else {
175
176 new LogicalFileCopyFrom(m_session, this, m_adaptor).copyFrom(effectiveSource, flags);
177 }
178 }
179
180
181
182 public NSDirectory openAbsoluteDir(String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
183 return new LogicalDirectoryImpl(this, absolutePath, flags);
184 }
185
186 public NSEntry openAbsolute(String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
187 if (URLHelper.isDirectory(absolutePath)) {
188 return new LogicalDirectoryImpl(this, absolutePath, flags);
189 } else {
190 return new LogicalFileImpl(this, absolutePath, flags);
191 }
192 }
193
194
195
196 public void addLocationSync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
197 if (m_adaptor instanceof LogicalWriter) {
198 ((LogicalWriter)m_adaptor).addLocation(
199 m_url.getPath(),
200 name,
201 m_url.getQuery());
202 } else {
203 throw new NotImplementedException("Not supported for this protocol: "+ m_url.getScheme(), this);
204 }
205 }
206
207 public void removeLocationSync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
208 if (m_adaptor instanceof LogicalWriter) {
209 ((LogicalWriter)m_adaptor).removeLocation(
210 m_url.getPath(),
211 name,
212 m_url.getQuery());
213 } else {
214 throw new NotImplementedException("Not supported for this protocol: "+ m_url.getScheme(), this);
215 }
216 }
217
218 public void updateLocationSync(URL nameOld, URL nameNew) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
219 if (m_adaptor instanceof LogicalWriter) {
220 ((LogicalWriter)m_adaptor).addLocation(
221 m_url.getPath(),
222 nameNew,
223 m_url.getQuery());
224 ((LogicalWriter)m_adaptor).removeLocation(
225 m_url.getPath(),
226 nameOld,
227 m_url.getQuery());
228 } else {
229 throw new NotImplementedException("Not supported for this protocol: "+ m_url.getScheme(), this);
230 }
231 }
232
233 public List<URL> listLocationsSync() throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
234 if (m_adaptor instanceof LogicalReader) {
235 try {
236 String[] array = ((LogicalReader)m_adaptor).listLocations(
237 m_url.getPath(),
238 m_url.getQuery());
239 List<URL> list = new ArrayList<URL>();
240 try {
241 for (String location : array) {
242 list.add(URLFactory.createURL(JSAGA_FACTORY, location));
243 }
244 } catch (BadParameterException e) {
245 throw new NoSuccessException(e);
246 }
247 return list;
248 } catch (BadParameterException badParameter) {
249 throw new IncorrectStateException("Logical entry is not a file: "+m_url, badParameter);
250 } catch (DoesNotExistException doesNotExist) {
251 throw new IncorrectStateException("Logical file does not exist: "+ m_url, doesNotExist);
252 }
253 } else {
254 throw new NotImplementedException("Not supported for this protocol: "+ m_url.getScheme(), this);
255 }
256 }
257
258 public void replicateSync(URL name, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
259 if (m_adaptor instanceof LogicalReader && m_adaptor instanceof LogicalWriter) {
260 this._replicate_step1(name, flags);
261 this._replicate_step2(name);
262 } else {
263 throw new NotImplementedException("Not supported for this protocol: "+ m_url.getScheme(), this);
264 }
265 }
266 public void replicateSync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
267 this.replicateSync(name, Flags.NONE.getValue());
268 }
269
270 private void _replicate_step1(URL physicalTarget, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
271 final String MESSAGE = "Failed to copy replica (state is still consistent)";
272 try {
273 List<URL> locations = this.listLocationsSync();
274 if (locations==null || locations.size()==0) {
275 throw new IncorrectStateException("Can not replicate a logical entry with empty location", this);
276 }
277 URL physicalSource = locations.get(0);
278 NSEntry physicalSourceEntry = NSFactory.createNSEntry(JSAGA_FACTORY, m_session, physicalSource, Flags.NONE.getValue());
279 try {
280 physicalSourceEntry.copy(physicalTarget, flags);
281 } finally {
282 physicalSourceEntry.close();
283 }
284 }
285 catch (NotImplementedException e) {throw new NotImplementedException(MESSAGE, e);}
286 catch (IncorrectURLException e) {throw new IncorrectURLException(MESSAGE, e);}
287 catch (AuthenticationFailedException e) {throw new AuthenticationFailedException(MESSAGE, e);}
288 catch (AuthorizationFailedException e) {throw new AuthorizationFailedException(MESSAGE, e);}
289 catch (PermissionDeniedException e) {throw new PermissionDeniedException(MESSAGE, e);}
290 catch (BadParameterException e) {throw new BadParameterException(MESSAGE, e);}
291 catch (IncorrectStateException e) {throw new IncorrectStateException(MESSAGE, e);}
292 catch (AlreadyExistsException e) {throw new AlreadyExistsException(MESSAGE, e);}
293 catch (TimeoutException e) {throw new TimeoutException(MESSAGE, e);}
294 catch (NoSuccessException e) {throw new NoSuccessException(MESSAGE, e);}
295 }
296 private void _replicate_step2(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, TimeoutException, NoSuccessException {
297 final String MESSAGE = "INCONSISTENT STATE: Replica has been copied but failed to register the new location";
298 try {
299 this.addLocationSync(name);
300 }
301 catch (NotImplementedException e) {throw new NotImplementedException(MESSAGE, e);}
302 catch (IncorrectURLException e) {throw new IncorrectURLException(MESSAGE, e);}
303 catch (AuthenticationFailedException e) {throw new AuthenticationFailedException(MESSAGE, e);}
304 catch (AuthorizationFailedException e) {throw new AuthorizationFailedException(MESSAGE, e);}
305 catch (PermissionDeniedException e) {throw new PermissionDeniedException(MESSAGE, e);}
306 catch (BadParameterException e) {throw new BadParameterException(MESSAGE, e);}
307 catch (IncorrectStateException e) {throw new IncorrectStateException(MESSAGE, e);}
308 catch (TimeoutException e) {throw new TimeoutException(MESSAGE, e);}
309 catch (NoSuccessException e) {throw new NoSuccessException(MESSAGE, e);}
310 }
311 }