1 package nl.tudelft.simulation.naming.context;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.ObjectOutputStream;
7 import java.rmi.RemoteException;
8
9 import javax.naming.NamingException;
10
11 import org.djutils.logger.CategoryLogger;
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class FileContext extends JvmContext
26 {
27
28 private static final long serialVersionUID = 20200101L;
29
30
31 private File file;
32
33
34
35
36
37
38 public FileContext(final File file, final String atomicName)
39 {
40 this(file, null, atomicName);
41 }
42
43
44
45
46
47
48
49 public FileContext(final File file, final ContextInterface parent, final String atomicName)
50 {
51 super(parent, atomicName);
52 this.file = file;
53 }
54
55
56
57
58
59
60 private synchronized void save() throws NamingException, RemoteException
61 {
62 try
63 {
64
65
66
67 ObjectOutputStream stream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(this.file)));
68 stream.writeObject(this);
69 stream.close();
70 }
71 catch (Exception exception)
72 {
73 CategoryLogger.always().warn(exception, "saving in FileContext failed");
74 throw new NamingException(exception.getMessage());
75 }
76 }
77
78
79 @Override
80 public void bind(final String name, final Object value) throws NamingException, RemoteException
81 {
82 super.bind(name, value);
83 save();
84 }
85
86
87 @Override
88 public ContextInterface createSubcontext(final String name) throws NamingException, RemoteException
89 {
90 ContextInterface result = super.createSubcontext(name);
91 save();
92 return result;
93 }
94
95
96 @Override
97 public void destroySubcontext(final String name) throws NamingException, RemoteException
98 {
99 super.destroySubcontext(name);
100 save();
101 }
102
103
104 @Override
105 public void rebind(final String name, final Object value) throws NamingException, RemoteException
106 {
107 super.rebind(name, value);
108 save();
109 }
110
111
112 @Override
113 public synchronized void rename(final String nameOld, final String nameNew) throws NamingException, RemoteException
114 {
115 super.rename(nameOld, nameNew);
116 save();
117 }
118
119
120 @Override
121 public void unbind(final String name) throws NamingException, RemoteException
122 {
123 super.unbind(name);
124 save();
125 }
126
127 }