01: /**
02: * Copyright (C) 2001-2004 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * 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: */package org.objectweb.speedo.mapper.lib;
18:
19: import org.objectweb.util.monolog.api.BasicLevel;
20: import org.objectweb.util.monolog.api.Logger;
21:
22: import java.io.File;
23: import java.io.FileOutputStream;
24: import java.io.IOException;
25: import java.io.InputStream;
26: import java.io.ObjectInputStream;
27: import java.io.ObjectOutputStream;
28:
29: /**
30: * This class contains primitive to serialize and deserialize an Object into a
31: * .jmi file.
32: *
33: * @author S.Chassande-Barrioz
34: */
35: public class Object2JMIFileSerializer {
36:
37: public final static char[] ext = { 'j', 'm', 'i' };
38:
39: public static String serialize(String output, String descFileName,
40: Object o, Logger logger) throws IOException {
41: String fn = descFileName2FileName(descFileName);
42: File f = new File(output, fn);
43: logger.log(BasicLevel.DEBUG, "Store into the file: "
44: + f.getAbsolutePath());
45: f.getParentFile().mkdirs();
46: ObjectOutputStream oos = new ObjectOutputStream(
47: new FileOutputStream(f));
48: oos.writeObject(o);
49: return f.getAbsolutePath();
50: }
51:
52: public static Object deserialize(String descFileName,
53: ClassLoader cl, Logger logger) throws Exception {
54: String fn = descFileName2FileName(descFileName);
55: InputStream is = cl.getResourceAsStream(fn);
56: if (is == null) {
57: Exception e = new IOException("File " + fn
58: + " is not availlable through the classloader ("
59: + 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 ois = new ObjectInputStream(is);
65: return ois.readObject();
66: }
67:
68: public static String descFileName2FileName(String descFileName) {
69: char[] cs = descFileName.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(cs);
80: }
81: }
|