KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > persist > FilePersister


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.persist;
10
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.NotSerializableException JavaDoc;
17 import java.io.ObjectInputStream JavaDoc;
18 import java.io.ObjectOutputStream JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import javax.management.InstanceNotFoundException JavaDoc;
21 import javax.management.MBeanException JavaDoc;
22 import javax.management.RuntimeOperationsException JavaDoc;
23
24 import mx4j.loading.ClassLoaderObjectInputStream;
25
26 /**
27  * A persister object that stores to files.
28  *
29  * @version $Revision: 1.10 $
30  */

31 public class FilePersister extends Persister
32 {
33    private File JavaDoc m_store;
34
35    /**
36     * Creates a new FilePersister.
37     *
38     * @param location the directory where the file will be written (must already exist);
39     * if null the name is used as a location
40     * @param name the file name used to store information
41     */

42    public FilePersister(String JavaDoc location, String JavaDoc name) throws MBeanException JavaDoc
43    {
44       if (name == null)
45       {
46          throw new MBeanException JavaDoc(new IllegalArgumentException JavaDoc("Persist name cannot be null"));
47       }
48
49       if (location != null)
50       {
51          File JavaDoc dir = new File JavaDoc(location);
52          if (!dir.exists())
53          {
54             throw new MBeanException JavaDoc(new FileNotFoundException JavaDoc(location));
55          }
56          m_store = new File JavaDoc(dir, name);
57       }
58       else
59       {
60          m_store = new File JavaDoc(name);
61       }
62    }
63
64    /**
65     * Returns the path where the information is stored.
66     */

67    public String JavaDoc getFileName()
68    {
69       return m_store.getAbsolutePath();
70    }
71
72    public Object JavaDoc load() throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc, InstanceNotFoundException JavaDoc
73    {
74       FileInputStream JavaDoc fin = null;
75       ObjectInputStream JavaDoc clois = null;
76       Object JavaDoc result = null;
77
78       synchronized (this)
79       {
80          try
81          {
82             // Create the inputStream
83
fin = new FileInputStream JavaDoc(m_store);
84
85             // Use the ClassLoaderObjectInputStream
86
clois = new ClassLoaderObjectInputStream(fin, Thread.currentThread().getContextClassLoader());
87             try
88             {
89                // Try load the object using the ContextClassLoader
90
result = clois.readObject();
91             }
92             catch (ClassNotFoundException JavaDoc ex)
93             {
94                // Close previous streams, FileInputStream does not support reset(),
95
// so I have to create a new one
96
try
97                {
98                   clois.close();
99                }
100                catch (IOException JavaDoc ignored)
101                {
102                }
103
104                throw new MBeanException JavaDoc(ex);
105 /*
106                     // Try using the DefaultLoaderRepository
107                     fin = new FileInputStream(m_store);
108                     clois = new ClassLoaderObjectInputStream(fin);
109                     try
110                     {
111                         result = clois.readObject();
112                     }
113                     catch(ClassNotFoundException e)
114                     {
115                         throw new MBeanException(e);
116                     }
117 */

118             }
119          }
120          catch (IOException JavaDoc ex)
121          {
122             throw new MBeanException JavaDoc(ex);
123          }
124          finally
125          {
126             try
127             {
128                if (clois != null) clois.close();
129             }
130             catch (IOException JavaDoc ignored)
131             {
132             }
133          }
134       }
135       return result;
136    }
137
138    public void store(Object JavaDoc data) throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc, InstanceNotFoundException JavaDoc
139    {
140       if (data == null) throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Cannot store a null object."));
141       if (!(data instanceof Serializable JavaDoc)) throw new MBeanException JavaDoc(new NotSerializableException JavaDoc(data.getClass().getName() + " must implement java.io.Serializable"));
142
143       FileOutputStream JavaDoc fos = null;
144       ObjectOutputStream JavaDoc oos = null;
145       synchronized (this)
146       {
147          try
148          {
149             fos = new FileOutputStream JavaDoc(m_store);
150             oos = new ObjectOutputStream JavaDoc(fos);
151          }
152          catch (IOException JavaDoc ex)
153          {
154             throw new MBeanException JavaDoc(ex);
155          }
156          try
157          {
158             //write out the data
159
oos.writeObject(data);
160             // flush the stream
161
oos.flush();
162          }
163          catch (IOException JavaDoc ex)
164          {
165             throw new MBeanException JavaDoc(ex);
166          }
167          finally
168          {
169             try
170             {
171                oos.close();
172             }
173             catch (IOException JavaDoc ex)
174             {/* unable to close the stream nothing to do*/
175             }
176          }
177       }
178    }
179 }
180
Popular Tags