View Javadoc
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   * The FileContext as a file-based implementation of the ContextInterface.
15   * <p>
16   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
18   * project is distributed under a three-clause BSD-style license, which can be found at
19   * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
20   * </p>
21   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
22   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
23   */
24  public class FileContext extends JvmContext
25  {
26      /** The default serial version UID for serializable classes. */
27      private static final long serialVersionUID = 20200101L;
28  
29      /** file links to the file. */
30      private File file;
31  
32      /**
33       * constructs a new FileContext.
34       * @param file File; the file to write to
35       * @param atomicName String; the name under which the root context will be registered
36       */
37      public FileContext(final File file, final String atomicName)
38      {
39          this(file, null, atomicName);
40      }
41  
42      /**
43       * constructs a new FileContext.
44       * @param file File; the file to which to write
45       * @param parent Context; the parent context
46       * @param atomicName String; the atomicName
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       * saves this object to file.
56       * @throws NamingException on ioException
57       * @throws RemoteException on network failure
58       */
59      private synchronized void save() throws NamingException, RemoteException
60      {
61          try
62          {
63              // TODO: check if non-serializability of the EventListeners is indeed needed (old version).
64              // FileContext clone = (FileContext) this.clone();
65              // clone.listeners.clear();
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 }