1 /***
2 * BaseMemento.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2003 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.state;
27
28 import java.util.prefs.Preferences;
29
30 /***
31 * This class provides convenience methods for actual Memento implementations.
32 *
33 * @author $Author: dbregeon $
34 * @version $Revision: 1.1 $
35 */
36 public abstract class BaseMemento implements Memento {
37 public static final String STORED_OBJECT_TYPE_PREFERENCE = "storedObjectType";
38 public static final String SUB_MEMENTO_CONTENTS_PREFERENCE = "subMementoContents";
39 public static final String SUB_MEMENTO_TYPE_PREFERENCE = "subMementoType";
40
41 protected Object buildObject(String type) throws MementoException {
42 try {
43 return getClass().getClassLoader().loadClass(type).newInstance();
44 } catch (ClassNotFoundException e) {
45 throw new MementoException("Could not find class " + type,e);
46 } catch (IllegalAccessException e) {
47 throw new MementoException("Could not access class " + type,e);
48 } catch (InstantiationException e) {
49 throw new MementoException("Could not instantiate class " + type,e);
50 }
51 }
52
53 /***
54 *
55 * @param node
56 * @param memento
57 * @param objectType
58 * @throws MementoException
59 */
60 protected void storeSubMemento(Preferences node, Memento memento, Class objectType) throws MementoException {
61 node.put(STORED_OBJECT_TYPE_PREFERENCE,objectType.getName());
62 node.put(SUB_MEMENTO_TYPE_PREFERENCE,memento.getClass().getName());
63 memento.store(node.node(SUB_MEMENTO_CONTENTS_PREFERENCE));
64 }
65 /***
66 *
67 * @param node
68 * @return
69 * @throws MementoException
70 */
71 protected Memento loadSubMemento(Preferences node) throws MementoException {
72 String mementoType = node.get(SUB_MEMENTO_TYPE_PREFERENCE,null);
73 if (mementoType == null) {
74 throw new MementoException("No subMementoType found in this node.");
75 }
76 Memento result = (Memento) buildObject(mementoType);
77 result.load(node.node(SUB_MEMENTO_CONTENTS_PREFERENCE));
78 return result;
79 }
80 /***
81 *
82 * @param node
83 * @return
84 * @throws MementoException
85 */
86 protected Class getObjectType(Preferences node) throws MementoException {
87 String objectType = node.get(STORED_OBJECT_TYPE_PREFERENCE,null);
88 if (objectType == null) {
89 throw new MementoException("No storedObjectType found in this node.");
90 }
91 try {
92 return getClass().getClassLoader().loadClass(objectType);
93 } catch (ClassNotFoundException e) {
94 throw new MementoException("Could not find class " + objectType);
95 }
96 }
97
98 }