View Javadoc

1   /*
2    * @(#) FileContext.java Oct 23, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.naming;
11  
12  import java.io.BufferedOutputStream;
13  import java.io.File;
14  import java.io.FileOutputStream;
15  import java.io.ObjectOutputStream;
16  import java.io.Serializable;
17  import java.util.HashMap;
18  import java.util.Iterator;
19  import java.util.Map;
20  
21  import javax.naming.Context;
22  import javax.naming.Name;
23  import javax.naming.NamingException;
24  
25  import nl.tudelft.simulation.logger.Logger;
26  
27  /***
28   * The FileContext as implementation of the Context interface.
29   * <p>
30   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
31   * University of Technology </a>, the Netherlands. <br>
32   * See for project information <a href="http://www.simulation.tudelft.nl">
33   * www.simulation.tudelft.nl </a> <br>
34   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
35   * License (GPL) </a>, no warranty <br>
36   * 
37   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
38   *         Jacobs </a>
39   * @version 1.3 2004-03-24
40   * @since 1.0
41   */
42  public class FileContext extends JVMContext implements Serializable
43  {
44  	/*** file links to the file */
45  	private File file = null;
46  
47  	/***
48  	 * constructs a new FileContext
49  	 * 
50  	 * @param file the file to write to
51  	 */
52  	public FileContext(final File file)
53  	{
54  		super();
55  		this.file = file;
56  	}
57  
58  	/***
59  	 * constructs a new FileContext
60  	 * 
61  	 * @param file the file to which to write
62  	 * @param parent the parent context
63  	 * @param atomicName the atomicName
64  	 */
65  	public FileContext(final File file, final Context parent,
66  			final String atomicName)
67  	{
68  		super(parent, atomicName);
69  		this.file = file;
70  	}
71  
72  	/***
73  	 * saves this object to file
74  	 * 
75  	 * @throws NamingException on ioException
76  	 */
77  	private synchronized void save() throws NamingException
78  	{
79  		try
80  		{
81  			FileContext clone = (FileContext) this.clone();
82  			clone.listeners.clear();
83  			ObjectOutputStream stream = new ObjectOutputStream(
84  					new BufferedOutputStream(new FileOutputStream(this.file)));
85  			stream.writeObject(this);
86  			stream.close();
87  		} catch (Exception exception)
88  		{
89  			Logger.warning(this, "save", exception);
90  			throw new NamingException(exception.getMessage());
91  		}
92  	}
93  
94  	/***
95  	 * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
96  	 */
97  	public void bind(final Name name, final Object value)
98  			throws NamingException
99  	{
100 		super.bind(name, value);
101 		this.save();
102 	}
103 
104 	/***
105 	 * @see javax.naming.Context#bind(java.lang.String, java.lang.Object)
106 	 */
107 	public void bind(final String name, final Object value)
108 			throws NamingException
109 	{
110 		super.bind(name, value);
111 		this.save();
112 	}
113 
114 	/***
115 	 * @see javax.naming.Context#createSubcontext(javax.naming.Name)
116 	 */
117 	public Context createSubcontext(final Name name) throws NamingException
118 	{
119 		Context result = super.createSubcontext(name);
120 		this.save();
121 		return result;
122 	}
123 
124 	/***
125 	 * @see javax.naming.Context#createSubcontext(java.lang.String)
126 	 */
127 	public Context createSubcontext(final String arg0) throws NamingException
128 	{
129 		Context result = super.createSubcontext(arg0);
130 		this.save();
131 		return result;
132 	}
133 
134 	/***
135 	 * @see javax.naming.Context#destroySubcontext(javax.naming.Name)
136 	 */
137 	public void destroySubcontext(final Name arg0) throws NamingException
138 	{
139 		super.destroySubcontext(arg0);
140 		this.save();
141 	}
142 
143 	/***
144 	 * @see javax.naming.Context#destroySubcontext(java.lang.String)
145 	 */
146 	public void destroySubcontext(final String arg0) throws NamingException
147 	{
148 		super.destroySubcontext(arg0);
149 		this.save();
150 	}
151 
152 	/***
153 	 * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
154 	 */
155 	public void rebind(final Name name, final Object value)
156 			throws NamingException
157 	{
158 		super.rebind(name, value);
159 		this.save();
160 	}
161 
162 	/***
163 	 * @see javax.naming.Context#rebind(java.lang.String, java.lang.Object)
164 	 */
165 	public void rebind(final String name, final Object value)
166 			throws NamingException
167 	{
168 		super.rebind(name, value);
169 		this.save();
170 	}
171 
172 	/***
173 	 * @see javax.naming.Context#removeFromEnvironment(java.lang.String)
174 	 */
175 	public Object removeFromEnvironment(final String arg0)
176 			throws NamingException
177 	{
178 		Object result = super.removeFromEnvironment(arg0);
179 		this.save();
180 		return result;
181 	}
182 
183 	/***
184 	 * @see javax.naming.Context#rename(javax.naming.Name, javax.naming.Name)
185 	 */
186 	public void rename(final Name nameOld, final Name nameNew)
187 			throws NamingException
188 	{
189 		super.rename(nameOld, nameNew);
190 		this.save();
191 	}
192 
193 	/***
194 	 * @see javax.naming.Context#rename(java.lang.String, java.lang.String)
195 	 */
196 	public void rename(final String nameOld, final String nameNew)
197 			throws NamingException
198 	{
199 		super.rename(nameOld, nameNew);
200 		this.save();
201 	}
202 
203 	/***
204 	 * @see javax.naming.Context#unbind(javax.naming.Name)
205 	 */
206 	public void unbind(final Name name) throws NamingException
207 	{
208 		super.unbind(name);
209 		this.save();
210 	}
211 
212 	/***
213 	 * @see javax.naming.Context#unbind(java.lang.String)
214 	 */
215 	public void unbind(final String name) throws NamingException
216 	{
217 		super.unbind(name);
218 		this.save();
219 	}
220 
221 	/***
222 	 * @see java.lang.Object#clone()
223 	 */
224 	public synchronized Object clone() throws CloneNotSupportedException
225 	{
226 		FileContext clone = new FileContext(this.file);
227 		Map elements = new HashMap(this.elements);
228 		for (Iterator i = elements.keySet().iterator(); i.hasNext();)
229 		{
230 			Object key = i.next();
231 			Object value = elements.get(key);
232 			if (value instanceof JVMContext)
233 			{
234 				JVMContext item = (JVMContext) value;
235 				value = item.clone();
236 			}
237 			elements.put(key, value);
238 		}
239 		clone.elements = elements;
240 		return clone;
241 	}
242 }