1 package fr.in2p3.jsaga.adaptor.data.impl;
2
3 import fr.in2p3.jsaga.Base;
4 import fr.in2p3.jsaga.adaptor.schema.data.catalog.*;
5 import fr.in2p3.jsaga.adaptor.schema.data.catalog.File;
6 import org.exolab.castor.util.LocalConfiguration;
7 import org.exolab.castor.xml.Marshaller;
8 import org.exolab.castor.xml.Unmarshaller;
9 import org.ogf.saga.error.DoesNotExistException;
10 import org.ogf.saga.error.NoSuccessException;
11
12 import java.io.*;
13 import java.util.*;
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class DataCatalogHandler {
28 private static final java.io.File FILE = new java.io.File(Base.JSAGA_VAR, "data-catalog.xml");
29 private static DataCatalogHandler _instance = null;
30 private DataCatalog m_catalogRoot;
31
32 public static synchronized DataCatalogHandler getInstance() throws NoSuccessException {
33 if (_instance == null) {
34 _instance = new DataCatalogHandler();
35 }
36 return _instance;
37 }
38 private DataCatalogHandler() throws NoSuccessException {
39 if (FILE.exists()) {
40 try {
41 m_catalogRoot = (DataCatalog) Unmarshaller.unmarshal(DataCatalog.class, new InputStreamReader(new FileInputStream(FILE)));
42 } catch (Exception e) {
43 throw new NoSuccessException(e);
44 }
45 } else {
46 m_catalogRoot = new DataCatalog();
47 m_catalogRoot.setName("/");
48 }
49 }
50
51
52
53
54 public void commit() throws NoSuccessException {
55 try {
56
57 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
58 Properties prop = LocalConfiguration.getInstance().getProperties();
59 prop.setProperty("org.exolab.castor.indent", "true");
60 Marshaller.marshal(m_catalogRoot, new OutputStreamWriter(buffer));
61
62 OutputStream out = new FileOutputStream(FILE);
63 out.write(buffer.toByteArray());
64 out.close();
65 } catch(Exception e) {
66 throw new NoSuccessException(e);
67 }
68 }
69
70
71
72
73 public Directory addDirectory(String absolutePath) throws DoesNotExistException {
74 return addDirectory(getParentDirectory(absolutePath), getEntryName(absolutePath));
75 }
76 public Directory addDirectory(DirectoryType parent, String name) {
77 Directory dir = new Directory();
78 dir.setName(name);
79 parent.addDirectory(dir);
80 return dir;
81 }
82 public File addFile(String absolutePath) throws DoesNotExistException {
83 return addFile(getParentDirectory(absolutePath), getEntryName(absolutePath));
84 }
85 public File addFile(DirectoryType parent, String name) {
86 File file = new File();
87 file.setName(name);
88 parent.addFile(file);
89 return file;
90 }
91
92
93 public void removeDirectory(String absolutePath) throws DoesNotExistException, NoSuccessException {
94 removeDirectory(getParentDirectory(absolutePath), getEntryName(absolutePath));
95 }
96 public void removeDirectory(DirectoryType parent, String name) throws DoesNotExistException, NoSuccessException {
97 Directory dir = getDirectory(parent, name);
98 if (dir.getDirectoryCount()>0 || dir.getFileCount()>0) {
99 throw new NoSuccessException("Directory is not empty: "+name);
100 }
101 parent.removeDirectory(dir);
102 }
103 public void removeFile(String absolutePath) throws DoesNotExistException {
104 removeFile(getParentDirectory(absolutePath), getEntryName(absolutePath));
105 }
106 public void removeFile(DirectoryType parent, String name) throws DoesNotExistException {
107 parent.removeFile(getFile(parent, name));
108 }
109
110
111 public DirectoryType getDirectory(String absolutePath) throws DoesNotExistException {
112 DirectoryType parent = getParentDirectory(absolutePath);
113 String name = getEntryName(absolutePath);
114 if (name != null) {
115 return getDirectory(parent, name);
116 } else {
117 return parent;
118 }
119 }
120 public Directory getDirectory(DirectoryType parent, String entryName) throws DoesNotExistException {
121 for (int i=0; i<parent.getDirectoryCount(); i++) {
122 if (parent.getDirectory(i).getName().equals(entryName)) {
123 return parent.getDirectory(i);
124 }
125 }
126 throw new DoesNotExistException("Directory does not exist: "+entryName);
127 }
128 public File getFile(String absolutePath) throws DoesNotExistException {
129 return getFile(getParentDirectory(absolutePath), getEntryName(absolutePath));
130 }
131 public File getFile(DirectoryType parent, String entryName) throws DoesNotExistException {
132 for (int i=0; i<parent.getFileCount(); i++) {
133 if (parent.getFile(i).getName().equals(entryName)) {
134 return parent.getFile(i);
135 }
136 }
137 throw new DoesNotExistException("File does not exist: "+entryName);
138 }
139 public EntryType getEntry(String absolutePath) throws DoesNotExistException {
140 DirectoryType parentDir = getParentDirectory(absolutePath);
141 String entryName = getEntryName(absolutePath);
142 return getEntry(parentDir, entryName);
143 }
144 public EntryType getEntry(DirectoryType parent, String entryName) throws DoesNotExistException {
145 if (entryName == null) {
146 return parent;
147 }
148 try {
149 return getFile(parent, entryName);
150 } catch(DoesNotExistException e) {
151 return getDirectory(parent, entryName);
152 }
153 }
154
155
156 public EntryType[] listEntries(String absolutePath) throws DoesNotExistException {
157 EntryType entry = this.getEntry(absolutePath);
158 if (entry instanceof DirectoryType) {
159 return listEntries((DirectoryType)entry);
160 } else {
161 return new EntryType[]{entry};
162 }
163 }
164 public EntryType[] listEntries(DirectoryType parent) {
165 List list = new ArrayList();
166 for (int i=0; i<parent.getDirectoryCount(); i++) {
167 list.add(parent.getDirectory(i));
168 }
169 for (int i=0; i<parent.getFileCount(); i++) {
170 list.add(parent.getFile(i));
171 }
172 return (EntryType[]) list.toArray(new EntryType[list.size()]);
173 }
174
175
176
177 public DirectoryType getParentDirectory(String absolutePath) throws DoesNotExistException {
178 String[] entryNames = toArray(absolutePath);
179 DirectoryType parent = m_catalogRoot;
180 for (int i=0; i<entryNames.length-1; i++) {
181 parent = getDirectory(parent, entryNames[i]);
182 }
183 return parent;
184 }
185
186 public String getEntryName(String absolutePath) {
187 String[] entryNames = toArray(absolutePath);
188 if (entryNames.length > 0) {
189 return entryNames[entryNames.length-1];
190 } else {
191 return null;
192 }
193 }
194
195 private static String[] toArray(String absolutePath) {
196 List list = new ArrayList();
197 String[] array = absolutePath.split("/");
198 for (int i=0; i<array.length; i++) {
199 if (!array[i].equals("")) {
200 list.add(array[i]);
201 }
202 }
203 return (String[]) list.toArray(new String[list.size()]);
204 }
205 }