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-2024 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/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
18   * project is distributed under a three-clause BSD-style license, which can be found at
19   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
20   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
21   * </p>
22   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
23   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   */
25  public class FileContext extends JvmContext
26  {
27      /** The default serial version UID for serializable classes. */
28      private static final long serialVersionUID = 20200101L;
29  
30      /** file links to the file. */
31      private File file;
32  
33      /**
34       * constructs a new FileContext.
35       * @param file File; the file to write to
36       * @param atomicName String; the name under which the root context will be registered
37       */
38      public FileContext(final File file, final String atomicName)
39      {
40          this(file, null, atomicName);
41      }
42  
43      /**
44       * constructs a new FileContext.
45       * @param file File; the file to which to write
46       * @param parent Context; the parent context
47       * @param atomicName String; the atomicName
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       * saves this object to file.
57       * @throws NamingException on ioException
58       * @throws RemoteException on network failure
59       */
60      private synchronized void save() throws NamingException, RemoteException
61      {
62          try
63          {
64              // TODO: check if non-serializability of the EventListeners is indeed needed (old version).
65              // FileContext clone = (FileContext) this.clone();
66              // clone.listeners.clear();
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
96      @Override
97      public void destroySubcontext(final String name) throws NamingException, RemoteException
98      {
99          super.destroySubcontext(name);
100         save();
101     }
102 
103     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
120     @Override
121     public void unbind(final String name) throws NamingException, RemoteException
122     {
123         super.unbind(name);
124         save();
125     }
126 
127 }