Skip to content

Commit d137566

Browse files
Neil DEVASNeil DEVAS
authored andcommitted
fix XML persistor
1 parent 7639819 commit d137566

7 files changed

Lines changed: 37 additions & 14 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ target
1313
*.log
1414
.classpath
1515
.project
16+
17+
# data persistence files
18+
Data

src/main/java/group25/Server/RMI/RMICarResourceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class RMICarResourceManager extends CarResourceManager {
2828
private static int s_customerServerPort = 2003;
2929

3030
public RMICarResourceManager(String name) {
31-
super(name, "carData1", "carData2", "carMasterRecord");
31+
super(name, RMIUtils.DATA_FILE_PATH+"/carData1.xml", RMIUtils.DATA_FILE_PATH+"/carData2.xml", RMIUtils.DATA_FILE_PATH+"/carMasterRecord.xml");
3232
}
3333

3434
public static void main(String args[]) {

src/main/java/group25/Server/RMI/RMICustomerResourceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class RMICustomerResourceManager extends CustomerResourceManager {
2727
private static int s_serverPort = 2003;
2828

2929
public RMICustomerResourceManager(String name) {
30-
super(name, "customerData1", "customerData2", "customerMasterRecord");
30+
super(name, RMIUtils.DATA_FILE_PATH+"/customerData1.xml", RMIUtils.DATA_FILE_PATH+"/customerData2.xml", RMIUtils.DATA_FILE_PATH+"/customerMasterRecord.xml");
3131
}
3232

3333
public static void main(String args[]) {

src/main/java/group25/Server/RMI/RMIFlightResourceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class RMIFlightResourceManager extends FlightResourceManager {
2020
private static int s_customerServerPort = 2003;
2121

2222
public RMIFlightResourceManager(String name) {
23-
super(name, "flightData1.xml", "flightData2.xml", "flightMasterRecord.xml");
23+
super(name, RMIUtils.DATA_FILE_PATH+"/flightData1.xml", RMIUtils.DATA_FILE_PATH+"/flightData2.xml", RMIUtils.DATA_FILE_PATH+"/flightMasterRecord.xml");
2424
}
2525

2626
public static void main(String args[]) {

src/main/java/group25/Server/RMI/RMIRoomResourceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class RMIRoomResourceManager extends RoomResourceManager {
2222
private static int s_customerServerPort = 2003;
2323

2424
public RMIRoomResourceManager(String name) {
25-
super(name, "roomData1", "roomData2", "roomMasterRecord");
25+
super(name, RMIUtils.DATA_FILE_PATH+"/roomData1.xml", RMIUtils.DATA_FILE_PATH+"/roomData2.xml", RMIUtils.DATA_FILE_PATH+"/roomMasterRecord.xml");
2626
}
2727

2828
public static void main(String args[]) {

src/main/java/group25/Server/RMI/RMIUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
public class RMIUtils {
1616

1717
private static final String RMI_OBJECT_NAME_PREFIX = "group25_";
18+
protected static final String DATA_FILE_PATH = (System.getProperty("user.home")) + "/comp512/DistributedSystemsProject/Data";
1819

1920
public static <T> T createRMIproxyObject(Remote objToStub, int port) {
2021
T retval = null;

src/main/java/group25/Utils/XMLPersistor.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import java.io.File;
66
import java.io.FileInputStream;
77
import java.io.FileOutputStream;
8+
import java.io.IOException;
89
import java.nio.charset.Charset;
910
import java.util.HashMap;
1011

1112
import static group25.Utils.AnsiColors.RED;
12-
13+
import group25.Server.Common.*;
1314
/**
1415
* Contains utils for creating XStream objects, writing java objects
1516
* to XML files and reading them from XML files.
@@ -27,36 +28,54 @@ public XMLPersistor(Class classToWrite) {
2728
xstream.allowTypes(new Class[] {
2829
HashMap.class,
2930
String.class,
30-
classToWrite
31+
classToWrite,
32+
Flight.class,
33+
Room.class,
34+
Car.class,
35+
Customer.class,
3136
});
3237
this.xstream = xstream;
3338
}
3439

35-
public boolean writeObject(Object obj, String filename) {
36-
String objAsXML = this.xstream.toXML(obj);
37-
40+
public boolean writeObject(Object obj, String filepath) {
41+
FileOutputStream fos = null;
42+
File dataFile = new File(filepath);
3843
try {
39-
FileOutputStream fos = new FileOutputStream(filename);
40-
fos.write("<?xml version=\"1.0\">\n".getBytes("UTF-8")); // XStream doesn't do this for you
41-
fos.write(objAsXML.getBytes("UTF-8"));
44+
dataFile.getParentFile().mkdir();
45+
dataFile.createNewFile();
46+
String objAsXML = this.xstream.toXML(obj);
47+
fos = new FileOutputStream(dataFile);
48+
fos.write("<?xml version=\"1.0\"?>\n".getBytes("UTF-8")); // XStream doesn't do this for you
49+
byte[] bytes = objAsXML.getBytes("UTF-8");
50+
fos.write(bytes);
51+
System.out.println("finished writing " + bytes);
4252
} catch (Exception e) {
4353
System.out.println(RED.colorString("XMLPersistor.writeObject() exception: ")+e.getMessage());
4454
System.exit(1);
55+
} finally {
56+
if (fos != null) {
57+
try {
58+
fos.close();
59+
} catch(IOException e) {
60+
e.printStackTrace();
61+
}
62+
}
4563
}
4664

4765
return true;
4866
}
4967

50-
public <T> T readObject(String filename) {
68+
public <T> T readObject(String filepath) {
5169
String str = null;
5270

5371
try {
54-
File file = new File(filename);
72+
File file = new File(filepath);
5573
FileInputStream fis = new FileInputStream(file);
5674
byte[] data = new byte[(int) file.length()];
5775

5876
fis.read(data);
5977
str = new String(data, "UTF-8");
78+
System.out.println(str);
6079
} catch (Exception e) {
6180
return null; // file not found
6281
}

0 commit comments

Comments
 (0)