KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > persistence > AbstractPersistenceWrapper


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

18
19 package org.objectweb.jac.aspects.persistence;
20
21 import org.apache.log4j.Logger;
22 import org.objectweb.jac.core.AspectComponent;
23 import org.objectweb.jac.core.Wrappee;
24 import org.objectweb.jac.core.Wrapper;
25 import org.objectweb.jac.core.Wrapping;
26 import org.objectweb.jac.core.rtti.ClassItem;
27 import org.objectweb.jac.core.rtti.FieldItem;
28 import org.objectweb.jac.util.ExtArrays;
29
30 /**
31  * This wrapper defines persistence extensions for objects that a
32  * defined persitent by a persistent aspect component. */

33
34 public abstract class AbstractPersistenceWrapper extends Wrapper {
35
36     static Logger logger = Logger.getLogger("persistence");
37
38     public AbstractPersistenceWrapper(AspectComponent ac) {
39         super(ac);
40     }
41
42     public static final String JavaDoc ATTR_ADDED = "persistence.added";
43
44     static boolean isWrapped(Wrappee wrappee, FieldItem field) {
45         Object JavaDoc value = field.get(wrappee);
46         if (value instanceof Wrappee
47             && Wrapping.isExtendedBy(
48                 (Wrappee) value,
49                 null,
50                 CollectionWrapper.class)) {
51             logger.debug(field + " is wrapped");
52             return true;
53         } else {
54             logger.debug(field + " is not wrapped");
55             return false;
56         }
57     }
58
59     /**
60      * Gets the object in memory object for a "storage" value.
61      * @param value the "storage" value
62      * @return if value is an OID, a reference to the object with that
63      * OID is returned, otherwise, value is returned.
64      * @see #normalizeInput(Object)
65      */

66     public final Object JavaDoc normalizeOutput(Object JavaDoc value) throws Exception JavaDoc {
67         if (value instanceof OID) {
68             value = getAC().getObject((OID) value, null);
69         }
70         return value;
71     }
72
73     /**
74      * Performs various stuff before storing a value in a storage. If
75      * the value is a Wrappee, it is made persistent.
76      * @param value the value to be stored
77      * @return if value is a wrappee, the OID of the object is
78      * returned, otherwise, value is returned.
79      * @see #normalizeInput(Object)
80      */

81     public final Object JavaDoc normalizeInput(Object JavaDoc value) throws Exception JavaDoc {
82         if (value != null) {
83             logger.debug(
84                 "normalizeInput(" + value.getClass().getName() + ")");
85         }
86         if (value instanceof Wrappee) {
87             Wrappee wrappee = (Wrappee) value; // added object
88
try {
89                 Wrapping.invokeRoleMethod(
90                     wrappee,
91                     "makePersistent",
92                     ExtArrays.emptyObjectArray);
93             } catch (Exception JavaDoc e) {
94                 logger.error("makePersistent failed for "+wrappee, e);
95             }
96
97             //PersistenceWrapper wrapper = getAC().makePersistent(wrappee);
98

99             if (getOID(wrappee) == null) {
100                 // this should never happen
101
throw new InvalidOidException("oid is NULL,");
102             }
103             value = getOID(wrappee);
104         } else {
105             if (value != null) {
106                 logger.debug(
107                     value.getClass().getName() + " is not a wrappee");
108             }
109         }
110         return value;
111     }
112
113     public final OID getOID(Wrappee wrappee) {
114         return getAC().getOID(wrappee);
115     }
116
117     final void setOID(Wrappee wrappee, OID oid) {
118         getAC().registerObject(oid, wrappee);
119     }
120
121     /**
122      * Tells if the current object is persistent (role method).
123      *
124      * @return true if persistent
125      */

126     public final boolean isPersistent(Wrappee wrappee) {
127         return getOID(wrappee) != null;
128     }
129
130     /**
131      * Returns the storage for a given class
132      *
133      * @param cli a class
134      * @return the storage of the class, or null.
135      */

136     public final Storage getStorage(ClassItem cli) {
137         try {
138             return getAC().getStorage(cli);
139         } catch (Exception JavaDoc e) {
140             logger.error("getStorage failed "+cli.getName(),e);
141             return null;
142         }
143     }
144
145     public final PersistenceAC getAC() {
146         return (PersistenceAC) getAspectComponent();
147     }
148
149     public final void checkOid(Wrappee wrappee) throws InvalidOidException {
150         if (getOID(wrappee) == null)
151             throw new InvalidOidException("oid is NULL");
152     }
153
154     public static class InvalidOidException extends RuntimeException JavaDoc {
155         public InvalidOidException(String JavaDoc msg) {
156             super(msg);
157         }
158     }
159 }
160
Popular Tags