1 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 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 ATTR_ADDED = "persistence.added"; 43 44 static boolean isWrapped(Wrappee wrappee, FieldItem field) { 45 Object 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 66 public final Object normalizeOutput(Object value) throws Exception { 67 if (value instanceof OID) { 68 value = getAC().getObject((OID) value, null); 69 } 70 return value; 71 } 72 73 81 public final Object normalizeInput(Object value) throws Exception { 82 if (value != null) { 83 logger.debug( 84 "normalizeInput(" + value.getClass().getName() + ")"); 85 } 86 if (value instanceof Wrappee) { 87 Wrappee wrappee = (Wrappee) value; try { 89 Wrapping.invokeRoleMethod( 90 wrappee, 91 "makePersistent", 92 ExtArrays.emptyObjectArray); 93 } catch (Exception e) { 94 logger.error("makePersistent failed for "+wrappee, e); 95 } 96 97 99 if (getOID(wrappee) == null) { 100 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 126 public final boolean isPersistent(Wrappee wrappee) { 127 return getOID(wrappee) != null; 128 } 129 130 136 public final Storage getStorage(ClassItem cli) { 137 try { 138 return getAC().getStorage(cli); 139 } catch (Exception 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 { 155 public InvalidOidException(String msg) { 156 super(msg); 157 } 158 } 159 } 160 | Popular Tags |