KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > mapper > lib > Object2JMIFileSerializer


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.mapper.lib;
19
20 import org.objectweb.util.monolog.api.BasicLevel;
21 import org.objectweb.util.monolog.api.Logger;
22
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29
30 /**
31  * This class contains primitive to serialize and deserialize an Object into a
32  * .jmi file.
33  *
34  * @author S.Chassande-Barrioz
35  */

36 public class Object2JMIFileSerializer {
37
38     public final static char[] ext = {'j', 'm', 'i'};
39
40     public static String JavaDoc serialize(String JavaDoc output,
41                                      String JavaDoc jdoFileName,
42                                      Object JavaDoc o,
43                                      Logger logger) throws IOException JavaDoc {
44         String JavaDoc fn = jdoFileName2FileName(jdoFileName);
45         File JavaDoc f = new File JavaDoc(output, fn);
46         logger.log(BasicLevel.DEBUG, "Store into the file: " + f.getAbsolutePath());
47         f.getParentFile().mkdirs();
48         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
49         oos.writeObject(o);
50         return f.getAbsolutePath();
51     }
52
53     public static Object JavaDoc deserialize(String JavaDoc jdoFileName, ClassLoader JavaDoc cl, Logger logger)
54             throws Exception JavaDoc {
55         String JavaDoc fn = jdoFileName2FileName(jdoFileName);
56         InputStream JavaDoc is = cl.getResourceAsStream(fn);
57         if (is == null) {
58             Exception JavaDoc e = new IOException JavaDoc("File " + fn
59                     + " is not availlable through the classloader (" + cl + ").");
60             logger.log(BasicLevel.DEBUG, e.getMessage(), e);
61             throw e;
62         }
63         logger.log(BasicLevel.DEBUG, "Load from the file: " + fn);
64         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
65         return ois.readObject();
66     }
67
68     public static String JavaDoc jdoFileName2FileName(String JavaDoc jdoFileName) {
69         char[] cs = jdoFileName.toCharArray();
70         int size = cs.length;
71         cs[size - 1] = ext[2];
72         cs[size - 2] = ext[1];
73         cs[size - 3] = ext[0];
74         for(int i=(size - 5); i>=0; i--) {
75             if (cs[i] == '.') {
76                 cs[i] = File.separatorChar;
77             }
78         }
79         return new String JavaDoc(cs);
80     }
81 }
82
Popular Tags