| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.bpm.api; |
| |
| import java.util.Set; |
| import java.util.TreeMap; |
| import java.util.TreeSet; |
| |
| import org.eclipse.osbp.utils.constants.ExtendedDate; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class BlipBPMProcessProtocol extends TreeMap<Long, String> implements IBlipBPMProcessProtocol { |
| |
| private static final long serialVersionUID = -5351403298895312061L; |
| private final static Logger log = LoggerFactory.getLogger(BlipBPMProcessProtocol.class); |
| private Set<Long> processIds = new TreeSet<>(); |
| |
| protected BlipBPMProcessProtocol(long processId) { |
| this.processIds.add(processId); |
| } |
| |
| @Override |
| public void addToProtocol(long actualProcessId, String line) { |
| addToProtocol(actualProcessId, line, null); |
| } |
| |
| /** TODO */ |
| @Override |
| public void addToProtocol(long actualProcessId, String line, IBlipBPMProcessProtocol toBeMerged) { |
| if ((toBeMerged != null) && !this.equals(toBeMerged)) { |
| // if (!processIds.contains(toBeMerged.getProcessIds())) { |
| // log.warn("Merging protocols for different process ids "+this.processId+" and "+toBeMerged.getProcessId()); |
| // } |
| processIds.addAll(toBeMerged.getProcessIds()); |
| for (long millis : toBeMerged.keySet()) { |
| String newLine = toBeMerged.get(millis); |
| if (!containsKey(millis) || newLine.equals(get(millis))) { |
| insertToProtocol(millis, newLine); |
| } |
| } |
| } |
| if ((line != null) && !line.isEmpty()) { |
| insertToProtocol(System.currentTimeMillis(), "#"+actualProcessId+": "+line); |
| } |
| } |
| |
| static final int MAX_RETRIES = 50; |
| |
| @Override |
| public String processIdsAsString() { |
| String result = ""; |
| for (long id : processIds) { |
| if (!result.isEmpty()) { |
| result += "|"; |
| } |
| result += id; |
| } |
| return result; |
| } |
| |
| private void insertToProtocol(final long initialMillis, String line) { |
| long millis = initialMillis; |
| int retry = MAX_RETRIES+1; |
| while ((retry > 0) && containsKey(millis)) { |
| retry--; |
| millis++; |
| } |
| if (retry > 0) { |
| put(millis, line); |
| } |
| else { |
| throw new IllegalArgumentException("Could not insert protocol line for "+processIdsAsString()+" between "+ExtendedDate.timeStampToString(initialMillis)+" and "+ExtendedDate.timeStampToString(millis)); |
| } |
| } |
| |
| @Override |
| public boolean containsProcessId(long searchProcessId) { |
| return processIds.contains(searchProcessId); |
| } |
| |
| @Override |
| public Set<Long> getProcessIds() { |
| return processIds; |
| } |
| |
| @Override |
| public void updateProcessId(long processId) { |
| this.processIds.add(processId); |
| } |
| |
| /** TODO */ |
| @Override |
| public String toString() { |
| StringBuffer output = new StringBuffer(); |
| output.append("Protocol - Process ID(s):"+processIdsAsString()+" - "+size()+" lines"); |
| int line = 0; |
| Set<Long> keys = keySet(); |
| for (long millis : keys) { |
| line++; |
| output.append(System.lineSeparator()+"- #"+line+" - "+ExtendedDate.timeStampToString(millis)+" - "+get(millis)); |
| } |
| return output.toString(); |
| } |
| } |