View Javadoc

1   package fr.in2p3.jsaga.adaptor.data.impl;
2   
3   import fr.in2p3.jsaga.adaptor.schema.data.emulator.*;
4   import org.ogf.saga.error.DoesNotExistException;
5   import org.ogf.saga.error.NoSuccessException;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  /* ***************************************************
11  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
12  * ***             http://cc.in2p3.fr/             ***
13  * ***************************************************
14  * File:   DataEmulatorConnectionAbstract
15  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
16  * Date:   26 juin 2007
17  * ***************************************************
18  * Description:                                      */
19  /**
20   *
21   */
22  public abstract class DataEmulatorConnectionAbstract {
23      protected DataEmulatorGrid m_grid;
24  
25      ////////////////////////////////////////// m_grid operations /////////////////////////////////////////
26  
27      protected DataEmulatorConnectionAbstract() throws NoSuccessException {
28          try {
29              m_grid = DataEmulatorGrid.getInstance();
30          } catch (Exception e) {
31              throw new NoSuccessException(e);
32          }
33      }
34  
35      public void commit() throws NoSuccessException {
36          try {
37              m_grid.commit();
38          } catch (Exception e) {
39              throw new NoSuccessException(e);
40          }
41      }
42  
43      ////////////////////////////////////////// abstract methods /////////////////////////////////////////
44  
45      protected abstract void destroy();
46      protected abstract ServerType getServerRoot();
47  
48      ////////////////////////////////////////// public methods /////////////////////////////////////////
49  
50      // add
51      public Directory addDirectory(String absolutePath) throws DoesNotExistException {
52          return addDirectory(getParentDirectory(absolutePath), getEntryName(absolutePath));
53      }
54      public Directory addDirectory(DirectoryType parent, String name) {
55          Directory dir = new Directory();
56          dir.setName(name);
57          parent.addDirectory(dir);
58          return dir;
59      }
60      public File addFile(String absolutePath) throws DoesNotExistException {
61          return addFile(getParentDirectory(absolutePath), getEntryName(absolutePath));
62      }
63      public File addFile(DirectoryType parent, String name) {
64          File file = new File();
65          file.setName(name);
66          parent.addFile(file);
67          return file;
68      }
69  
70      // remove
71      public void removeDirectory(String absolutePath) throws DoesNotExistException, NoSuccessException {
72          removeDirectory(getParentDirectory(absolutePath), getEntryName(absolutePath));
73      }
74      public void removeDirectory(DirectoryType parent, String name) throws DoesNotExistException, NoSuccessException {
75          Directory dir = getDirectory(parent, name);
76          if (dir.getDirectoryCount()>0 || dir.getFileCount()>0) {
77              throw new NoSuccessException("Directory is not empty: "+name);
78          }
79          parent.removeDirectory(dir);
80      }
81      public void removeFile(String absolutePath) throws DoesNotExistException {
82          removeFile(getParentDirectory(absolutePath), getEntryName(absolutePath));
83      }
84      public void removeFile(DirectoryType parent, String name) throws DoesNotExistException {
85          parent.removeFile(getFile(parent, name));
86      }
87  
88      // get
89      public DirectoryType getDirectory(String absolutePath) throws DoesNotExistException {
90          DirectoryType parent = getParentDirectory(absolutePath);
91          String name = getEntryName(absolutePath);
92          if (name != null) {
93              return getDirectory(parent, name);
94          } else {
95              return parent;
96          }
97      }
98      public Directory getDirectory(DirectoryType parent, String entryName) throws DoesNotExistException {
99          for (int i=0; i<parent.getDirectoryCount(); i++) {
100             if (parent.getDirectory(i).getName().equals(entryName)) {
101                 return parent.getDirectory(i);
102             }
103         }
104         throw new DoesNotExistException("Directory does not exist");
105     }
106     public File getFile(String absolutePath) throws DoesNotExistException {
107         return getFile(getParentDirectory(absolutePath), getEntryName(absolutePath));
108     }
109     public File getFile(DirectoryType parent, String entryName) throws DoesNotExistException {
110         for (int i=0; i<parent.getFileCount(); i++) {
111             if (parent.getFile(i).getName().equals(entryName)) {
112                 return parent.getFile(i);
113             }
114         }
115         throw new DoesNotExistException("File does not exist");
116     }
117     public EntryType getEntry(String absolutePath) throws DoesNotExistException {
118         DirectoryType parentDir = getParentDirectory(absolutePath);
119         String entryName = getEntryName(absolutePath);
120         return getEntry(parentDir, entryName);
121     }
122     public EntryType getEntry(DirectoryType parent, String entryName) throws DoesNotExistException {
123         if (entryName == null) {
124             return parent;
125         }
126         try {
127             return getFile(parent, entryName);
128         } catch(DoesNotExistException e) {
129             return getDirectory(parent, entryName);
130         }
131     }
132 
133     // list
134     public EntryType[] listEntries(String absolutePath) throws DoesNotExistException {
135         EntryType entry = this.getEntry(absolutePath);
136         if (entry instanceof DirectoryType) {
137             return listEntries((DirectoryType)entry);
138         } else {
139             return new EntryType[]{entry};
140         }
141     }
142     private EntryType[] listEntries(DirectoryType parent) {
143         List list = new ArrayList();
144         for (int i=0; i<parent.getDirectoryCount(); i++) {
145             list.add(parent.getDirectory(i));
146         }
147         for (int i=0; i<parent.getFileCount(); i++) {
148             list.add(parent.getFile(i));
149         }
150         return (EntryType[]) list.toArray(new EntryType[list.size()]);
151     }
152 
153     // list directories only
154     public DirectoryType[] listDirectories(String absolutePath) throws DoesNotExistException {
155         EntryType entry = this.getEntry(absolutePath);
156         if (entry instanceof DirectoryType) {
157             return listDirectories((DirectoryType)entry);
158         } else {
159             return new DirectoryType[]{};
160         }
161     }
162     private DirectoryType[] listDirectories(DirectoryType parent) {
163         List list = new ArrayList();
164         for (int i=0; i<parent.getDirectoryCount(); i++) {
165             list.add(parent.getDirectory(i));
166         }
167         return (DirectoryType[]) list.toArray(new DirectoryType[list.size()]);
168     }
169 
170     ////////////////////////////////////////// friend methods /////////////////////////////////////////
171 
172     public DirectoryType getParentDirectory(String absolutePath) throws DoesNotExistException {
173         String[] entryNames = toArray(absolutePath);
174         DirectoryType parent = this.getServerRoot();
175         for (int i=0; i<entryNames.length-1; i++) {
176             parent = getDirectory(parent, entryNames[i]);
177         }
178         return parent;
179     }
180 
181     public String getEntryName(String absolutePath) {
182         String[] entryNames = toArray(absolutePath);
183         if (entryNames.length > 0) {
184             return entryNames[entryNames.length-1];
185         } else {
186             return null;
187         }
188     }
189 
190     private static String[] toArray(String absolutePath) {
191         List list = new ArrayList();
192         String[] array = absolutePath.split("/");
193         for (int i=0; i<array.length; i++) {
194             if (!array[i].equals("")) {
195                 list.add(array[i]);
196             }
197         }
198         return (String[]) list.toArray(new String[list.size()]);
199     }
200 }