1 package org.astrogrid.mySpace.mySpaceServer;
2
3
4
5 import org.astrogrid.mySpace.mySpaceStatus.*;
6 import org.astrogrid.mySpace.mySpaceUtil.MySpaceUtils;
7
8
9
10
11
12 import java.io.File;
13
14
15
16
17
18 import org.apache.log4j.Logger;
19
20 import org.astrogrid.Configurator;
21 import org.astrogrid.mySpace.mySpaceUtil.*;
22
23 /***
24 * @WebService
25 *
26 * <p>
27 * The ServerManager class is invoked (usually as a Web service) to
28 * manipulate files on a MySpace server.
29 *
30 * @author C L Qin
31 * @author A C Davenhall
32 * @version Iteration 3.
33 */
34
35 public class ServerManager
36 { private static Logger logger = Logger.getLogger(ServerManager.class);
37 private static boolean DEBUG = true;
38 private static MySpaceStatus status = new MySpaceStatus();
39
40 private String response = "";
41 private String dataholderpath = " ";
42
43
44
45 /***
46 * Write the contents of an input string as a new file a MySpace
47 * server.
48 *
49 * @param contents The string containing the contents to be written
50 * to the new file.
51 *
52 * @param newDataHolderFileName The name of the file on the server
53 * corresponding to the DataHolder to be copied to (ie. the
54 * output file). The file name should include the full directory
55 * specification, as known to the server operating system.
56 */
57
58 public String upLoadString(String contents,
59 String newDataHolderFileName)
60 {
61
62
63
64
65 try
66 { File newDataHolderFile = new File(newDataHolderFileName);
67 boolean isOk = MySpaceUtils.writeToFile(newDataHolderFile,
68 contents);
69 if (isOk)
70 { response = MSC.SUCCESS + " File up-loaded.";
71 }
72 else
73 { status.addCode(MySpaceStatusCode.AGMSCE01040,
74 MySpaceStatusCode.ERROR,
75 MySpaceStatusCode.LOG, this.getClassName() );
76 response = MSC.FAULT + " " +
77 status.translateCode(MySpaceStatusCode.AGMSCE01040);
78 }
79 }
80 catch (Exception e)
81 { status.addCode(MySpaceStatusCode.AGMSCE01040,
82 MySpaceStatusCode.ERROR,
83 MySpaceStatusCode.LOG, this.getClassName() );
84 response = MSC.FAULT + " " +
85 status.translateCode(MySpaceStatusCode.AGMSCE01040);
86 }
87
88 return response;
89 }
90
91
92
93 /***
94 * Import a new dataHolder into a MySpace server. A remote file is
95 * imported into the MySpace server. This remote file is identified by
96 * a URI, which is passed as one of the input arguments. In Iteration
97 * 3 this URI must be a URL.
98 *
99 * @param importURI The URI of the remote file to be imported.
100 *
101 * @param newDataHolderFileName The name of the file on the server
102 * corresponding to the DataHolder to be copied to (ie. the
103 * output file). The file name should include the full directory
104 * specification, as known to the server operating system.
105 */
106
107 public String importDataHolder(String importURI,
108 String newDataHolderFileName)
109 {
110
111
112
113
114 try
115 {
116
117 setUp();
118
119
120 String fullPath=dataholderpath+newDataHolderFileName;
121 FileTransfer fetch = new FileTransfer(importURI, fullPath);
122 if (DEBUG) logger.debug("inside ServerManager.. importURI: "+importURI +" dataholderpath = "+dataholderpath+"newDataHolderFileName : "+newDataHolderFileName);
123 fetch.transfer();
124 if (fetch.getChosenUrl() != null)
125 { response = MSC.SUCCESS + " File imported.";
126 }
127 else
128 { status.addCode(MySpaceStatusCode.AGMSCE01040,
129 MySpaceStatusCode.ERROR,
130 MySpaceStatusCode.LOG, this.getClassName() );
131 response = MSC.FAULT + " " +
132 status.translateCode(MySpaceStatusCode.AGMSCE01040);
133 }
134 }
135 catch (Exception e)
136 { status.addCode(MySpaceStatusCode.AGMSCE01040,
137 MySpaceStatusCode.ERROR,
138 MySpaceStatusCode.LOG, this.getClassName() );
139 response = MSC.FAULT + " " +
140 status.translateCode(MySpaceStatusCode.AGMSCE01040);
141 }
142
143 return response;
144 }
145
146
147
148 /***
149 * Copy a DataHolder from one location on a MySpace server to another
150 * location on the same server.
151 *
152 * @param oldDataHolderFileName The name of the file on the server
153 * corresponding to the DataHolder to be copied from (ie. the
154 * input file). The file name should include the full directory
155 * specification, as known to the server operating system.
156 * @param newDataHolderFileName The name of the file on the server
157 * corresponding to the DataHolder to be copied to (ie. the
158 * output file). The file name should include the full directory
159 * specification, as known to the server operating system.
160 */
161
162 public String copyDataHolder(String oldDataHolderFileName,
163 String newDataHolderFileName)
164 { try
165 { File oldDataHolderFile = new File(oldDataHolderFileName);
166 File newDataHolderFile = new File(newDataHolderFileName);
167
168
169
170
171
172 long fileSize = oldDataHolderFile.length();
173
174 String command;
175 long sizeLimit;
176
177 try
178 { command = MSC.getProperty(MSC.copyCommand, MSC.CATLOG);
179 sizeLimit =
180 Long.parseLong(MSC.getProperty(MSC.sizeLimit, MSC.CATLOG));
181 }
182 catch (Exception e)
183 { command = "cp";
184 sizeLimit = 5000000;
185 }
186
187 if (fileSize>=sizeLimit)
188 { Runtime.getRuntime().exec(command);
189 }
190 else
191 { String contents = MySpaceUtils.readFromFile(oldDataHolderFile);
192
193 boolean isOk = MySpaceUtils.writeToFile(newDataHolderFile,
194 contents);
195 if (isOk)
196 { response = MSC.SUCCESS + " File copied.";
197 }
198 else
199 { status.addCode(MySpaceStatusCode.AGMSCE01043,
200 MySpaceStatusCode.ERROR,
201 MySpaceStatusCode.LOG, this.getClassName() );
202 response = MSC.FAULT + " " +
203 status.translateCode(MySpaceStatusCode.AGMSCE01043);
204 System.out.println("burble");
205 }
206 }
207 }
208 catch (Exception e)
209 { status.addCode(MySpaceStatusCode.AGMSCE01043,
210 MySpaceStatusCode.ERROR,
211 MySpaceStatusCode.LOG, this.getClassName() );
212 response = MSC.FAULT + " " +
213 status.translateCode(MySpaceStatusCode.AGMSCE01043);
214 System.out.println("yibble.");
215 }
216
217 return response;
218 }
219
220
221
222 /***
223 * Delete a DataHolder from a MySpace server.
224 *
225 * @param dataHolderFileName The name of the file on the server
226 * corresponding to the DataHolder to be deleted. The file name
227 * should include the full directory specification, as known to the
228 * server operating system.
229 */
230
231 public String deleteDataHolder(String dataHolderFileName)
232 {
233
234
235
236
237 try
238 { File dataHolderFile = new File(dataHolderFileName);
239 if (dataHolderFile != null || dataHolderFile.exists() )
240 { boolean isDeleted = dataHolderFile.delete();
241 if (isDeleted)
242 { response = MSC.SUCCESS + " File deleted.";
243 }
244 else
245 { status.addCode(MySpaceStatusCode.AGMSCE01046,
246 MySpaceStatusCode.ERROR,
247 MySpaceStatusCode.LOG, this.getClassName() );
248 response = MSC.FAULT + " " +
249 status.translateCode(MySpaceStatusCode.AGMSCE01046);
250 }
251 }
252 else
253 { status.addCode(MySpaceStatusCode.AGMSCE01047,
254 MySpaceStatusCode.ERROR,
255 MySpaceStatusCode.LOG, this.getClassName() );
256 response = MSC.FAULT + " " +
257 status.translateCode(MySpaceStatusCode.AGMSCE01047);
258 }
259 }
260 catch (Exception e)
261 { status.addCode(MySpaceStatusCode.AGMSCE01046,
262 MySpaceStatusCode.ERROR,
263 MySpaceStatusCode.LOG, this.getClassName() );
264 response = MSC.FAULT + " " +
265 status.translateCode(MySpaceStatusCode.AGMSCE01046);
266 }
267
268 return response;
269 }
270
271
272
273 /***
274 * Obtain the name of the current Java class.
275 */
276
277 protected String getClassName()
278 { Class currentClass = this.getClass();
279 String name = currentClass.getName();
280 int dotPos = name.lastIndexOf(".");
281 if (dotPos > -1)
282 { name = name.substring(dotPos+1, name.length() );
283 }
284
285 return name;
286 }
287 private void setUp()throws Exception{
288 MSC.getInstance().checkPropertiesLoaded();
289 dataholderpath = MSC.getProperty(MSC.dataHolderFolder, MSC.CATLOG);
290 }
291
292 protected String getComponentName() { return Configurator.getClassName( ServerManager.class) ; }
293 }