001/* 002 * (C) Copyright 2006-2012 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 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.core.event.impl; 023 024import java.io.Serializable; 025import java.util.ArrayList; 026import java.util.HashMap; 027import java.util.List; 028import java.util.Map; 029import java.util.Map.Entry; 030 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.DocumentRef; 033import org.nuxeo.ecm.core.event.Event; 034import org.nuxeo.ecm.core.event.EventBundle; 035import org.nuxeo.ecm.core.event.EventContext; 036 037/** 038 * Light Event implementation. Used to reduce memory footprint of {@link Event} stacked in {@link EventBundle}. 039 * 040 * @author Thierry Delprat 041 */ 042public class ShallowEvent extends EventImpl { 043 044 private static final long serialVersionUID = 1L; 045 046 public static ShallowEvent create(Event event) { 047 EventContext ctx = event.getContext(); 048 List<Object> newArgs = new ArrayList<Object>(); 049 for (Object arg : ctx.getArguments()) { 050 Object newArg = arg; 051 if (arg instanceof DocumentModel) { 052 DocumentModel oldDoc = (DocumentModel) arg; 053 DocumentRef ref = oldDoc.getRef(); 054 if (ref != null) { 055 newArg = new ShallowDocumentModel(oldDoc); 056 } else { 057 newArg = null; 058 } 059 } 060 // XXX treat here other cases !!!! 061 newArgs.add(newArg); 062 } 063 064 EventContext newCtx = null; 065 if (ctx instanceof DocumentEventContext) { 066 newCtx = new DocumentEventContext(null, ctx.getPrincipal(), (DocumentModel) newArgs.get(0), 067 (DocumentRef) newArgs.get(1)); 068 } else { 069 newCtx = new EventContextImpl(null, ctx.getPrincipal()); 070 ((EventContextImpl) newCtx).setArgs(newArgs.toArray()); 071 } 072 073 newCtx.setRepositoryName(ctx.getRepositoryName()); 074 Map<String, Serializable> newProps = new HashMap<String, Serializable>(); 075 for (Entry<String, Serializable> prop : ctx.getProperties().entrySet()) { 076 Serializable propValue = prop.getValue(); 077 if (propValue instanceof DocumentModel) { 078 DocumentModel oldDoc = (DocumentModel) propValue; 079 propValue = new ShallowDocumentModel(oldDoc); 080 } 081 // XXX treat here other cases !!!! 082 newProps.put(prop.getKey(), propValue); 083 } 084 newCtx.setProperties(newProps); 085 return new ShallowEvent(event.getName(), newCtx, event.getFlags(), event.getTime()); 086 } 087 088 public ShallowEvent(String name, EventContext ctx, int flags, long creationTime) { 089 super(name, ctx, flags, creationTime); 090 } 091 092}