001/* 002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat 018 */ 019package org.nuxeo.ecm.platform.audit.service; 020 021import java.io.Serializable; 022import java.util.Date; 023import java.util.List; 024import java.util.Map; 025 026import javax.persistence.EntityManager; 027 028import org.nuxeo.ecm.core.persistence.PersistenceProvider; 029import org.nuxeo.ecm.core.persistence.PersistenceProvider.RunCallback; 030import org.nuxeo.ecm.core.persistence.PersistenceProvider.RunVoid; 031import org.nuxeo.ecm.core.persistence.PersistenceProviderFactory; 032import org.nuxeo.ecm.platform.audit.api.ExtendedInfo; 033import org.nuxeo.ecm.platform.audit.api.FilterMapEntry; 034import org.nuxeo.ecm.platform.audit.api.LogEntry; 035import org.nuxeo.ecm.platform.audit.impl.ExtendedInfoImpl; 036import org.nuxeo.ecm.platform.audit.service.extension.AuditBackendDescriptor; 037import org.nuxeo.runtime.api.Framework; 038import org.nuxeo.runtime.model.DefaultComponent; 039import org.nuxeo.runtime.transaction.TransactionHelper; 040 041/** 042 * Contains the Hibernate based (legacy) implementation 043 * 044 * @author tiry 045 */ 046public class DefaultAuditBackend extends AbstractAuditBackend { 047 048 protected PersistenceProvider persistenceProvider; 049 050 public DefaultAuditBackend(NXAuditEventsService component, AuditBackendDescriptor config) { 051 super(component, config); 052 activatePersistenceProvider(); 053 } 054 055 @Override 056 public int getApplicationStartedOrder() { 057 return ((DefaultComponent) Framework.getRuntime() 058 .getComponent("org.nuxeo.ecm.core.persistence.PersistenceComponent")) 059 .getApplicationStartedOrder() 060 + 1; 061 } 062 063 @Override 064 public void onApplicationStarted() { 065 activatePersistenceProvider(); 066 } 067 068 @Override 069 public void onShutdown() { 070 try { 071 persistenceProvider.closePersistenceUnit(); 072 } finally { 073 persistenceProvider = null; 074 } 075 } 076 077 // public for testing purpose ! 078 public PersistenceProvider getOrCreatePersistenceProvider() { 079 if (persistenceProvider == null) { 080 activatePersistenceProvider(); 081 } 082 return persistenceProvider; 083 } 084 085 protected void activatePersistenceProvider() { 086 Thread thread = Thread.currentThread(); 087 ClassLoader last = thread.getContextClassLoader(); 088 try { 089 thread.setContextClassLoader(PersistenceProvider.class.getClassLoader()); 090 PersistenceProviderFactory persistenceProviderFactory = Framework.getLocalService( 091 PersistenceProviderFactory.class); 092 persistenceProvider = persistenceProviderFactory.newProvider("nxaudit-logs"); 093 persistenceProvider.openPersistenceUnit(); 094 } finally { 095 thread.setContextClassLoader(last); 096 } 097 } 098 099 @Override 100 public void addLogEntries(final List<LogEntry> entries) { 101 if (entries.isEmpty()) { 102 return; 103 } 104 if (!TransactionHelper.isTransactionActiveOrMarkedRollback()) { 105 TransactionHelper.startTransaction(); 106 try { 107 getOrCreatePersistenceProvider().run(true, new RunVoid() { 108 @Override 109 public void runWith(EntityManager em) { 110 addLogEntries(em, entries); 111 } 112 }); 113 } finally { 114 TransactionHelper.commitOrRollbackTransaction(); 115 } 116 return; 117 } 118 getOrCreatePersistenceProvider().run(true, new RunVoid() { 119 @Override 120 public void runWith(EntityManager em) { 121 addLogEntries(em, entries); 122 } 123 }); 124 } 125 126 protected void addLogEntries(EntityManager em, List<LogEntry> entries) { 127 LogEntryProvider.createProvider(em).addLogEntries(entries); 128 } 129 130 @Override 131 public List<LogEntry> getLogEntriesFor(final String uuid, final String repositoryId) { 132 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<LogEntry>>() { 133 @Override 134 public List<LogEntry> runWith(EntityManager em) { 135 return getLogEntriesFor(em, uuid, repositoryId); 136 } 137 }); 138 } 139 140 protected List<LogEntry> getLogEntriesFor(EntityManager em, String uuid, String repositoryId) { 141 return LogEntryProvider.createProvider(em).getLogEntriesFor(uuid, repositoryId); 142 } 143 144 @Override 145 public List<LogEntry> getLogEntriesFor(final String uuid) { 146 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<LogEntry>>() { 147 @Override 148 public List<LogEntry> runWith(EntityManager em) { 149 return getLogEntriesFor(em, uuid); 150 } 151 }); 152 } 153 154 protected List<LogEntry> getLogEntriesFor(EntityManager em, String uuid) { 155 return LogEntryProvider.createProvider(em).getLogEntriesFor(uuid); 156 } 157 158 @Override 159 public List<LogEntry> getLogEntriesFor(final String uuid, final Map<String, FilterMapEntry> filterMap, 160 final boolean doDefaultSort) { 161 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<LogEntry>>() { 162 @Override 163 public List<LogEntry> runWith(EntityManager em) { 164 return getLogEntriesFor(em, uuid, filterMap, doDefaultSort); 165 } 166 }); 167 } 168 169 protected List<LogEntry> getLogEntriesFor(EntityManager em, String uuid, Map<String, FilterMapEntry> filterMap, 170 boolean doDefaultSort) { 171 return LogEntryProvider.createProvider(em).getLogEntriesFor(uuid, filterMap, doDefaultSort); 172 } 173 174 @Override 175 public LogEntry getLogEntryByID(final long id) { 176 return getOrCreatePersistenceProvider().run(false, new RunCallback<LogEntry>() { 177 @Override 178 public LogEntry runWith(EntityManager em) { 179 return getLogEntryByID(em, id); 180 } 181 }); 182 } 183 184 protected LogEntry getLogEntryByID(EntityManager em, long id) { 185 return LogEntryProvider.createProvider(em).getLogEntryByID(id); 186 } 187 188 @Override 189 public List<LogEntry> nativeQueryLogs(final String whereClause, final int pageNb, final int pageSize) { 190 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<LogEntry>>() { 191 @Override 192 public List<LogEntry> runWith(EntityManager em) { 193 return nativeQueryLogs(em, whereClause, pageNb, pageSize); 194 } 195 }); 196 } 197 198 protected List<LogEntry> nativeQueryLogs(EntityManager em, String whereClause, int pageNb, int pageSize) { 199 return LogEntryProvider.createProvider(em).nativeQueryLogs(whereClause, pageNb, pageSize); 200 } 201 202 @Override 203 public List<?> nativeQuery(final String query, final int pageNb, final int pageSize) { 204 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<?>>() { 205 @Override 206 public List<?> runWith(EntityManager em) { 207 return nativeQuery(em, query, pageNb, pageSize); 208 } 209 }); 210 } 211 212 protected List<?> nativeQuery(EntityManager em, String query, int pageNb, int pageSize) { 213 return LogEntryProvider.createProvider(em).nativeQuery(query, pageNb, pageSize); 214 } 215 216 @Override 217 public List<?> nativeQuery(final String query, final Map<String, Object> params, final int pageNb, 218 final int pageSize) { 219 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<?>>() { 220 @Override 221 public List<?> runWith(EntityManager em) { 222 return nativeQuery(em, query, params, pageNb, pageSize); 223 } 224 }); 225 } 226 227 protected List<?> nativeQuery(EntityManager em, String query, Map<String, Object> params, int pageNb, 228 int pageSize) { 229 return LogEntryProvider.createProvider(em).nativeQuery(query, params, pageNb, pageSize); 230 } 231 232 @Override 233 public List<LogEntry> queryLogs(final String[] eventIds, final String dateRange) { 234 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<LogEntry>>() { 235 @Override 236 public List<LogEntry> runWith(EntityManager em) { 237 return queryLogs(em, eventIds, dateRange); 238 } 239 }); 240 } 241 242 protected List<LogEntry> queryLogs(EntityManager em, String[] eventIds, String dateRange) { 243 return LogEntryProvider.createProvider(em).queryLogs(eventIds, dateRange); 244 } 245 246 @Override 247 public List<LogEntry> queryLogsByPage(final String[] eventIds, final String dateRange, final String[] category, 248 final String path, final int pageNb, final int pageSize) { 249 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<LogEntry>>() { 250 @Override 251 public List<LogEntry> runWith(EntityManager em) { 252 return queryLogsByPage(em, eventIds, dateRange, category, path, pageNb, pageSize); 253 } 254 }); 255 } 256 257 protected List<LogEntry> queryLogsByPage(EntityManager em, String[] eventIds, String dateRange, String[] category, 258 String path, int pageNb, int pageSize) { 259 return LogEntryProvider.createProvider(em).queryLogsByPage(eventIds, dateRange, category, path, pageNb, 260 pageSize); 261 } 262 263 @Override 264 public List<LogEntry> queryLogsByPage(final String[] eventIds, final Date limit, final String[] category, 265 final String path, final int pageNb, final int pageSize) { 266 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<LogEntry>>() { 267 @Override 268 public List<LogEntry> runWith(EntityManager em) { 269 return queryLogsByPage(em, eventIds, limit, category, path, pageNb, pageSize); 270 } 271 }); 272 } 273 274 protected List<LogEntry> queryLogsByPage(EntityManager em, String[] eventIds, Date limit, String[] category, 275 String path, int pageNb, int pageSize) { 276 return LogEntryProvider.createProvider(em).queryLogsByPage(eventIds, limit, category, path, pageNb, pageSize); 277 } 278 279 @Override 280 public long syncLogCreationEntries(final String repoId, final String path, final Boolean recurs) { 281 return getOrCreatePersistenceProvider().run(true, new RunCallback<Long>() { 282 @Override 283 public Long runWith(EntityManager em) { 284 return syncLogCreationEntries(em, repoId, path, recurs); 285 } 286 }); 287 } 288 289 protected long syncLogCreationEntries(EntityManager em, String repoId, String path, Boolean recurs) { 290 LogEntryProvider provider = LogEntryProvider.createProvider(em); 291 return syncLogCreationEntries(provider, repoId, path, recurs); 292 } 293 294 @Override 295 public Long getEventsCount(final String eventId) { 296 return getOrCreatePersistenceProvider().run(false, new RunCallback<Long>() { 297 @Override 298 public Long runWith(EntityManager em) { 299 return getEventsCount(em, eventId); 300 } 301 302 }); 303 } 304 305 public Long getEventsCount(EntityManager em, String eventId) { 306 return LogEntryProvider.createProvider(em).countEventsById(eventId); 307 } 308 309 public List<String> getLoggedEventIds() { 310 return getOrCreatePersistenceProvider().run(false, new RunCallback<List<String>>() { 311 @Override 312 public List<String> runWith(EntityManager em) { 313 return getLoggedEventIds(em); 314 } 315 316 }); 317 } 318 319 protected List<String> getLoggedEventIds(EntityManager em) { 320 return LogEntryProvider.createProvider(em).findEventIds(); 321 } 322 323 // Compat APIs 324 325 protected List<LogEntry> queryLogsByPage(EntityManager em, String[] eventIds, String dateRange, String category, 326 String path, int pageNb, int pageSize) { 327 String[] categories = { category }; 328 return queryLogsByPage(em, eventIds, dateRange, categories, path, pageNb, pageSize); 329 } 330 331 protected List<LogEntry> queryLogsByPage(EntityManager em, String[] eventIds, Date limit, String category, 332 String path, int pageNb, int pageSize) { 333 String[] categories = { category }; 334 return queryLogsByPage(em, eventIds, limit, categories, path, pageNb, pageSize); 335 } 336 337 @Override 338 public ExtendedInfo newExtendedInfo(Serializable value) { 339 return ExtendedInfoImpl.createExtendedInfo(value); 340 } 341 342}