/** * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ /******************************************************************************\ * * This product was developed by * * SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), * * a government company established under Brazilian law (5.615/70), * at Department of Development of Porto Alegre. * \******************************************************************************/ package serpro.mailarchiver.util.jdo; import javax.jdo.JDOHelper; import javax.jdo.ObjectState; import javax.jdo.listener.AttachLifecycleListener; import javax.jdo.listener.ClearLifecycleListener; import javax.jdo.listener.CreateLifecycleListener; import javax.jdo.listener.DeleteLifecycleListener; import javax.jdo.listener.DetachLifecycleListener; import javax.jdo.listener.DirtyLifecycleListener; import javax.jdo.listener.InstanceLifecycleEvent; import javax.jdo.listener.LoadLifecycleListener; import javax.jdo.listener.StoreLifecycleListener; import com.google.common.base.Supplier; import serpro.mailarchiver.domain.BaseObject; import serpro.mailarchiver.util.Logger; public class InstanceLifecycleListener implements AttachLifecycleListener, ClearLifecycleListener, CreateLifecycleListener, DeleteLifecycleListener, DetachLifecycleListener, DirtyLifecycleListener, LoadLifecycleListener, StoreLifecycleListener { private static final Logger log = Logger.getLocalLogger(); private void logLifecycleEvent(final String method, final InstanceLifecycleEvent ile, final Object o1, final Object o2) { log.debug("%s", new Supplier() { @Override public Object[] get() { StringBuilder sb = new StringBuilder(); sb.append(">>").append(method).append("<<\n\n"); Object persistentInstance; Object detachedInstance; if(ile != null) { persistentInstance = ile.getPersistentInstance(); detachedInstance = ile.getDetachedInstance(); sb.append("eventType: "); switch(ile.getEventType()) { case InstanceLifecycleEvent.CREATE: sb.append("CREATE"); break; case InstanceLifecycleEvent.LOAD: sb.append("LOAD"); break; case InstanceLifecycleEvent.STORE: sb.append("STORE"); break; case InstanceLifecycleEvent.CLEAR: sb.append("CLEAR"); break; case InstanceLifecycleEvent.DELETE: sb.append("DELETE"); break; case InstanceLifecycleEvent.DIRTY: sb.append("DIRTY"); break; case InstanceLifecycleEvent.DETACH: sb.append("DETACH"); break; case InstanceLifecycleEvent.ATTACH: sb.append("ATTACH"); break; } } else { persistentInstance = o1; detachedInstance = o2; } ObjectState objectState = JDOHelper.getObjectState(persistentInstance); sb.append("\nobjectState: ").append(objectState.toString()); boolean isDeleted = JDOHelper.isDeleted(persistentInstance); sb.append("\nisDeleted: ").append(isDeleted); boolean isDetached = JDOHelper.isDetached(persistentInstance); sb.append("\nisDetached: ").append(isDetached); boolean isDirty = JDOHelper.isDirty(persistentInstance); sb.append("\nisDirty: ").append(isDirty); boolean isNew = JDOHelper.isNew(persistentInstance); sb.append("\nisNew: ").append(isNew); boolean isPersistent = JDOHelper.isPersistent(persistentInstance); sb.append("\nisPersistent: ").append(isPersistent); boolean isTransactional = JDOHelper.isTransactional(persistentInstance); sb.append("\nisTransactional: ").append(isTransactional); if(!isDeleted) { sb.append("\n\npersistentInstance:\n").append(persistentInstance.toString()); } if(detachedInstance != null) { sb.append("\n\ndetachedInstance:\n").append(detachedInstance.toString()); } return new Object[] { sb.toString() }; } }); } // // AttachLifecycleListener // @Override public void preAttach(InstanceLifecycleEvent ile) { logLifecycleEvent("preAttach", ile, null, null); } @Override public void postAttach(InstanceLifecycleEvent ile) { logLifecycleEvent("postAttach", ile, null, null); } // // ClearLifecycleListener // @Override public void preClear(InstanceLifecycleEvent ile) { logLifecycleEvent("preClear", ile, null, null); } @Override public void postClear(InstanceLifecycleEvent ile) { logLifecycleEvent("postClear", ile, null, null); } // // CreateLifecycleListener // @Override public void postCreate(InstanceLifecycleEvent ile) { logLifecycleEvent("postCreate", ile, null, null); } // // DeleteLifecycleListener // @Override public void preDelete(InstanceLifecycleEvent ile) { logLifecycleEvent("preDelete", ile, null, null); } @Override public void postDelete(InstanceLifecycleEvent ile) { logLifecycleEvent("postDelete", ile, null, null); } // // DetachLifecycleListener // @Override public void preDetach(InstanceLifecycleEvent ile) { logLifecycleEvent("preDetach", ile, null, null); } @Override public void postDetach(InstanceLifecycleEvent ile) { logLifecycleEvent("postDetach", ile, null, null); } // // DirtyLifecycleListener // @Override public void preDirty(InstanceLifecycleEvent ile) { logLifecycleEvent("preDirty", ile, null, null); } @Override public void postDirty(InstanceLifecycleEvent ile) { logLifecycleEvent("postDirty", ile, null, null); } // // LoadLifecycleListener // @Override public void postLoad(InstanceLifecycleEvent ile) { logLifecycleEvent("postLoad", ile, null, null); } // // StoreLifecycleListener // @Override public void preStore(InstanceLifecycleEvent ile) { logLifecycleEvent("preStore", ile, null, null); } @Override public void postStore(InstanceLifecycleEvent ile) { logLifecycleEvent("postStore", ile, null, null); } // // InstanceCallbacks // public void jdoPreClear(BaseObject o1) { logLifecycleEvent("jdoPreClear", null, o1, null); } public void jdoPreDelete(BaseObject o1) { logLifecycleEvent("jdoPreDelete", null, o1, null); } public void jdoPostLoad(BaseObject o1) { logLifecycleEvent("jdoPostLoad", null, o1, null); } public void jdoPreStore(BaseObject o1) { logLifecycleEvent("jdoPreStore", null, o1, null); } // // AttachCallbacks // public void jdoPreAttach(BaseObject o1) { logLifecycleEvent("jdoPreAttach", null, o1, null); } public void jdoPostAttach(BaseObject o1, Object o2) { logLifecycleEvent("jdoPostAttach", null, o1, o2); } // // DetachCallbacks // public void jdoPreDetach(BaseObject o1) { logLifecycleEvent("jdoPreDetach", null, o1, null); } public void jdoPostDetach(BaseObject o1, Object o2) { logLifecycleEvent("jdoPostDetach", null, o1, o2); } }