1 23 24 29 30 package com.sun.jdo.api.persistence.support; 31 32 import java.util.ResourceBundle ; 33 import java.util.Collection ; 34 35 import com.sun.jdo.spi.persistence.utility.I18NHelper; 36 37 41 42 45 public class JDOHelper 46 { 47 59 static public PersistenceManager getPersistenceManager(Object obj) 60 { 61 if (obj instanceof PersistenceCapable) 62 return ((PersistenceCapable)obj).jdoGetPersistenceManager(); 63 return null; 64 } 65 66 82 static public void makeDirty(Object obj, String fieldName) 83 { 84 if (obj instanceof PersistenceCapable) 85 ((PersistenceCapable)obj).jdoMakeDirty(fieldName); 86 } 87 88 113 static public Object getObjectId(Object obj) 114 { 115 if (obj instanceof PersistenceCapable) 116 return ((PersistenceCapable)obj).jdoGetObjectId(); 117 return null; 118 } 119 120 134 static public boolean isDirty(Object obj) 135 { 136 if (obj instanceof PersistenceCapable) 137 return ((PersistenceCapable)obj).jdoIsDirty(); 138 return false; 139 } 140 141 158 static public boolean isTransactional(Object obj) 159 { 160 if (obj instanceof PersistenceCapable) 161 return ((PersistenceCapable)obj).jdoIsTransactional(); 162 return false; 163 } 164 165 178 static public boolean isPersistent(Object obj) 179 { 180 if (obj instanceof PersistenceCapable) 181 return ((PersistenceCapable)obj).jdoIsPersistent(); 182 return false; 183 } 184 185 199 static public boolean isNew(Object obj) 200 { 201 if (obj instanceof PersistenceCapable) 202 return ((PersistenceCapable)obj).jdoIsNew(); 203 return false; 204 } 205 206 220 static public boolean isDeleted(Object obj) 221 { 222 if (obj instanceof PersistenceCapable) 223 return ((PersistenceCapable)obj).jdoIsDeleted(); 224 return false; 225 } 226 227 230 private final static ResourceBundle messages = I18NHelper.loadBundle( 231 "com.sun.jdo.spi.persistence.support.sqlstore.impl.Bundle", JDOHelper.class.getClassLoader()); 233 234 237 static final String null_instance = "null"; 239 245 static public String printObject(Object o) { 246 if (o==null) 247 return null_instance; 248 else if (isDeleted(o)) 249 return I18NHelper.getMessage(messages, "jdohelper.deleted_instance", o.getClass().getName()); 251 else 252 return o.toString(); 253 } 254 255 264 static private ClassLoader getObjectClassLoader(Object obj) { 265 Class clazz = obj.getClass(); 266 267 if (obj instanceof Collection ) { 268 return getCollectionClassLoader((Collection )obj); 269 } else if (clazz.isArray()) { 270 return getArrayClassLoader((Object [])obj); 271 } else { 272 return clazz.getClassLoader(); 273 } 274 } 275 276 284 static private ClassLoader getCollectionClassLoader (Collection col) { 285 Object [] arr = col.toArray(); 286 return getArrayClassLoader(arr); 287 } 288 289 298 static private ClassLoader getArrayClassLoader (Object [] arr) { 299 ClassLoader cl = null; 300 301 for (int i = 0; i < arr.length; i++) { 302 cl = getObjectClassLoader(arr[i]); 303 if (cl != null) { 304 break; 305 } 306 } 307 return cl; 308 } 309 } 310 | Popular Tags |