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 public class FileContext extends JvmContext
25 {
26
27 private static final long serialVersionUID = 20200101L;
28
29
30 private File file;
31
32
33
34
35
36
37 public FileContext(final File file, final String atomicName)
38 {
39 this(file, null, atomicName);
40 }
41
42
43
44
45
46
47
48 public FileContext(final File file, final ContextInterface parent, final String atomicName)
49 {
50 super(parent, atomicName);
51 this.file = file;
52 }
53
54
55
56
57
58
59 private synchronized void save() throws NamingException, RemoteException
60 {
61 try
62 {
63
64
65
66 ObjectOutputStream stream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(this.file)));
67 stream.writeObject(this);
68 stream.close();
69 }
70 catch (Exception exception)
71 {
72 CategoryLogger.always().warn(exception, "saving in FileContext failed");
73 throw new NamingException(exception.getMessage());
74 }
75 }
76
77 @Override
78 public void bind(final String name, final Object value) throws NamingException, RemoteException
79 {
80 super.bind(name, value);
81 save();
82 }
83
84 @Override
85 public ContextInterface createSubcontext(final String name) throws NamingException, RemoteException
86 {
87 ContextInterface result = super.createSubcontext(name);
88 save();
89 return result;
90 }
91
92 @Override
93 public void destroySubcontext(final String name) throws NamingException, RemoteException
94 {
95 super.destroySubcontext(name);
96 save();
97 }
98
99 @Override
100 public void rebind(final String name, final Object value) throws NamingException, RemoteException
101 {
102 super.rebind(name, value);
103 save();
104 }
105
106 @Override
107 public synchronized void rename(final String nameOld, final String nameNew) throws NamingException, RemoteException
108 {
109 super.rename(nameOld, nameNew);
110 save();
111 }
112
113 @Override
114 public void unbind(final String name) throws NamingException, RemoteException
115 {
116 super.unbind(name);
117 save();
118 }
119
120 }