1 package fr.in2p3.jsaga.adaptor.data;
2
3 import fr.in2p3.jsaga.Base;
4 import fr.in2p3.jsaga.adaptor.base.defaults.Default;
5 import fr.in2p3.jsaga.adaptor.base.usage.Usage;
6 import fr.in2p3.jsaga.adaptor.data.impl.DataCatalogHandler;
7 import fr.in2p3.jsaga.adaptor.data.link.LinkAdaptor;
8 import fr.in2p3.jsaga.adaptor.data.link.NotLink;
9 import fr.in2p3.jsaga.adaptor.data.read.FileAttributes;
10 import fr.in2p3.jsaga.adaptor.data.read.LogicalReaderMetaData;
11 import fr.in2p3.jsaga.adaptor.data.write.LogicalWriterMetaData;
12 import fr.in2p3.jsaga.adaptor.schema.data.catalog.*;
13 import fr.in2p3.jsaga.adaptor.security.SecurityCredential;
14 import org.ogf.saga.error.*;
15 import org.ogf.saga.url.URL;
16
17 import java.util.*;
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class PersonalCatalogDataAdaptor implements LogicalReaderMetaData, LogicalWriterMetaData, LinkAdaptor {
32 private DataCatalogHandler m_catalog;
33
34 public String getType() {
35 return "catalog";
36 }
37
38 public Usage getUsage() {
39 return null;
40 }
41
42 public Default[] getDefaults(Map attributes) throws IncorrectStateException {
43 return null;
44 }
45
46 public Class[] getSupportedSecurityCredentialClasses() {
47 return null;
48 }
49
50 public void setSecurityCredential(SecurityCredential credential) {
51
52 }
53
54 public int getDefaultPort() {
55 return NO_PORT;
56 }
57
58 public void connect(String userInfo, String host, int port, String basePath, Map attributes) throws AuthenticationFailedException, AuthorizationFailedException, IncorrectURLException, TimeoutException, NoSuccessException {
59 if (host != null) {
60 throw new IncorrectURLException("Protocol '"+this.getType()+"' does not support hostname in URL: "+host);
61 }
62 m_catalog = DataCatalogHandler.getInstance();
63 if(Base.DEBUG) m_catalog.commit();
64 }
65
66 public void disconnect() throws NoSuccessException {
67 m_catalog.commit();
68 }
69
70 public boolean exists(String absolutePath, String additionalArgs) throws PermissionDeniedException, TimeoutException, NoSuccessException {
71 try {
72 m_catalog.getEntry(absolutePath);
73 return true;
74 } catch (DoesNotExistException e) {
75 return false;
76 }
77 }
78
79 public void create(String logicalEntry, String additionalArgs) throws PermissionDeniedException, BadParameterException, AlreadyExistsException, ParentDoesNotExist, TimeoutException, NoSuccessException {
80 try {
81 m_catalog.getFile(logicalEntry);
82 throw new AlreadyExistsException("File already exists: "+logicalEntry);
83 } catch (DoesNotExistException e) {
84 try {
85 m_catalog.addFile(logicalEntry);
86 } catch (DoesNotExistException e2) {
87 throw new ParentDoesNotExist(e2);
88 }
89 if(Base.DEBUG) m_catalog.commit();
90 }
91 }
92
93 public void addLocation(String logicalEntry, URL replicaEntry, String additionalArgs) throws PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
94
95 File file;
96 try {
97 file = m_catalog.getFile(logicalEntry);
98 } catch(DoesNotExistException e) {
99 throw new IncorrectStateException(e);
100 }
101
102 if (! arrayContains(file.getReplica(), replicaEntry.toString())) {
103 file.addReplica(replicaEntry.toString());
104 }
105 if(Base.DEBUG) m_catalog.commit();
106 }
107
108 public void removeLocation(String logicalEntry, URL replicaEntry, String additionalArgs) throws PermissionDeniedException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
109
110 File file = m_catalog.getFile(logicalEntry);
111
112 if (! file.removeReplica(replicaEntry.toString())) {
113 throw new DoesNotExistException("Replica location not registered");
114 }
115 if(Base.DEBUG) m_catalog.commit();
116 }
117
118 public String[] listLocations(String logicalEntry, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
119 File file = m_catalog.getFile(logicalEntry);
120 return file.getReplica();
121 }
122
123 public void setMetaData(String logicalEntry, String name, String[] values, String additionalArgs) throws PermissionDeniedException, TimeoutException, NoSuccessException {
124 EntryType entry;
125 try {
126 entry = m_catalog.getEntry(logicalEntry);
127 } catch (DoesNotExistException e) {
128 throw new NoSuccessException(e);
129 }
130 Metadata md = findMetadata(entry, name);
131 if (md != null) {
132 md.setName(name);
133 md.setValue(values);
134 } else {
135 md = new Metadata();
136 md.setName(name);
137 md.setValue(values);
138 entry.addMetadata(md);
139 }
140 }
141
142 public void removeMetaData(String logicalEntry, String name, String additionalArgs) throws PermissionDeniedException, TimeoutException, NoSuccessException, DoesNotExistException {
143 EntryType entry;
144 try {
145 entry = m_catalog.getEntry(logicalEntry);
146 } catch (DoesNotExistException e) {
147 throw new NoSuccessException(e);
148 }
149 Metadata md = findMetadata(entry, name);
150 if (md != null) {
151 entry.removeMetadata(md);
152 } else {
153 throw new DoesNotExistException("Meta-data '"+name+"' does not exist for entry: "+logicalEntry);
154 }
155 }
156
157 public Map listMetaData(String logicalEntry, String additionalArgs) throws PermissionDeniedException, TimeoutException, NoSuccessException {
158 EntryType entry;
159 try {
160 entry = m_catalog.getEntry(logicalEntry);
161 } catch (DoesNotExistException e) {
162 throw new NoSuccessException(e);
163 }
164 Map map = new HashMap();
165 for (int i=0; i<entry.getMetadataCount(); i++) {
166 Metadata md = entry.getMetadata(i);
167 map.put(md.getName(), md.getValue());
168 }
169 return map;
170 }
171
172 public FileAttributes[] findAttributes(String logicalDir, Map keyValuePatterns, boolean recursive, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
173 List found = new ArrayList();
174 DirectoryType dir = m_catalog.getDirectory(logicalDir);
175 findAttributesRecursive(dir, keyValuePatterns, found, null, recursive);
176 return (FileAttributes[]) found.toArray(new FileAttributes[found.size()]);
177 }
178 private void findAttributesRecursive(DirectoryType dir, Map keyValuePatterns, List found, String relativePath, boolean recursive) {
179 for (int i=0; i<dir.getDirectoryCount(); i++) {
180 Directory childDir = dir.getDirectory(i);
181 String childRelativePath = (relativePath!=null ? relativePath+"/"+childDir.getName() : childDir.getName());
182 if (matchesAllPatterns(childDir, keyValuePatterns)) {
183 found.add(new CatalogFileAttributes(childDir, childRelativePath));
184 }
185 if (recursive) {
186 findAttributesRecursive(childDir, keyValuePatterns, found, childRelativePath, recursive);
187 }
188 }
189 for (int i=0; i<dir.getFileCount(); i++) {
190 File childFile = dir.getFile(i);
191 String childRelativePath = (relativePath!=null ? relativePath+"/"+childFile.getName() : childFile.getName());
192 if (matchesAllPatterns(childFile, keyValuePatterns)) {
193 found.add(new CatalogFileAttributes(childFile, childRelativePath));
194 }
195 }
196 }
197 private boolean matchesAllPatterns(EntryType entry, Map keyValuePatterns) {
198 boolean matchAllPatterns = (keyValuePatterns.size()>0);
199 for (Iterator it=keyValuePatterns.entrySet().iterator(); matchAllPatterns && it.hasNext(); ) {
200 Map.Entry e = (Map.Entry) it.next();
201 String key = (String) e.getKey();
202 String value = (String) e.getValue();
203 Metadata md = findMetadata(entry, key);
204 matchAllPatterns &= (md!=null && (value==null || toSet(md.getValue()).contains(value)));
205 }
206 return matchAllPatterns;
207 }
208 private Set toSet(String[] values) {
209 Set<String> set = new HashSet<String>();
210 for (int i=0; values!=null && i<values.length; i++) {
211 set.add(values[i]);
212 }
213 return set;
214 }
215
216 public FileAttributes[] listAttributes(String absolutePath, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
217 EntryType[] list = m_catalog.listEntries(absolutePath);
218 FileAttributes[] ret = new FileAttributes[list.length];
219 for (int i=0; i<list.length; i++) {
220 ret[i] = new CatalogFileAttributes(list[i]);
221 }
222 return ret;
223 }
224
225 public FileAttributes getAttributes(String absolutePath, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
226 EntryType entry = m_catalog.getEntry(absolutePath);
227 return new CatalogFileAttributes(entry);
228 }
229
230 public void makeDir(String parentAbsolutePath, String directoryName, String additionalArgs) throws PermissionDeniedException, BadParameterException, AlreadyExistsException, ParentDoesNotExist, TimeoutException, NoSuccessException {
231 DirectoryType parent;
232 try {
233 parent = m_catalog.getDirectory(parentAbsolutePath);
234 } catch (DoesNotExistException e) {
235 throw new ParentDoesNotExist("Parent directory does not exist");
236 }
237 try {
238 m_catalog.getEntry(parent, directoryName);
239 throw new AlreadyExistsException("Entry already exists");
240 } catch(DoesNotExistException e) {
241 m_catalog.addDirectory(parent, directoryName);
242 }
243 if(Base.DEBUG) m_catalog.commit();
244 }
245
246 public void removeDir(String parentAbsolutePath, String directoryName, String additionalArgs) throws PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
247 DirectoryType parent = m_catalog.getDirectory(parentAbsolutePath);
248 m_catalog.removeDirectory(parent, directoryName);
249 if(Base.DEBUG) m_catalog.commit();
250 }
251
252 public void removeFile(String parentAbsolutePath, String fileName, String additionalArgs) throws PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
253 DirectoryType parent = m_catalog.getDirectory(parentAbsolutePath);
254 m_catalog.removeFile(parent, fileName);
255 if(Base.DEBUG) m_catalog.commit();
256 }
257
258 public String readLink(String absolutePath) throws NotLink, PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
259 EntryType entry = m_catalog.getEntry(absolutePath);
260 if (entry instanceof FileType && entry.getLink()!=null) {
261 return entry.getLink();
262 } else {
263 throw new NotLink(absolutePath);
264 }
265 }
266
267 public void link(String sourceAbsolutePath, String linkAbsolutePath, boolean overwrite) throws PermissionDeniedException, DoesNotExistException, AlreadyExistsException, TimeoutException, NoSuccessException {
268 File link;
269 try {
270 link = m_catalog.getFile(linkAbsolutePath);
271 if (! overwrite) {
272 throw new AlreadyExistsException("Link already exists");
273 }
274 } catch(DoesNotExistException e) {
275 link = m_catalog.addFile(linkAbsolutePath);
276 }
277 link.setLink(sourceAbsolutePath);
278 }
279
280 private boolean arrayContains(String[] array, String value) {
281 for (int i=0; array!=null && i<array.length; i++) {
282 if (array[i].equals(value)) {
283 return true;
284 }
285 }
286 return false;
287 }
288
289 private Metadata findMetadata(EntryType entry, String name) {
290 for (int i=0; i<entry.getMetadataCount(); i++) {
291 Metadata md = entry.getMetadata(i);
292 if (md.getName().equals(name)) {
293 return md;
294 }
295 }
296 return null;
297 }
298 }