1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 ******************************************************************************/
9 package org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.pattern
.stateprovider
;
11 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
13 import java
.io
.IOException
;
14 import java
.io
.ObjectInputStream
;
15 import java
.util
.concurrent
.CountDownLatch
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.AbstractSegmentStoreAnalysisModule
;
21 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.IAnalysisProgressListener
;
22 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
23 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
24 import org
.eclipse
.tracecompass
.segmentstore
.core
.SegmentStoreFactory
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
29 * Segment store module for pattern analysis defined in XML. This module will
30 * receive all the segments provided by an external source and will build a
33 * @author Jean-Christian Kouame
35 public class XmlPatternSegmentStoreModule
extends AbstractSegmentStoreAnalysisModule
implements ISegmentListener
{
38 * Fake segment indicated that the last segment have been received
40 public static final @NonNull EndSegment END_SEGMENT
= new EndSegment();
41 private final ISegmentStore
<@NonNull ISegment
> fSegments
= SegmentStoreFactory
.createSegmentStore();
42 private final CountDownLatch fFinished
= new CountDownLatch(1);
43 private final @NonNull XmlPatternAnalysis fParent
;
44 private boolean fSegmentStoreCompleted
;
52 public XmlPatternSegmentStoreModule(@NonNull XmlPatternAnalysis parent
) {
58 protected Object
@NonNull [] readObject(@NonNull ObjectInputStream ois
) throws ClassNotFoundException
, IOException
{
59 return checkNotNull((Object
[]) ois
.readObject());
63 protected boolean buildAnalysisSegments(@NonNull ISegmentStore
<@NonNull ISegment
> segments
, @NonNull IProgressMonitor monitor
) throws TmfAnalysisException
{
64 final @Nullable ITmfTrace trace
= getTrace();
66 /* This analysis was cancelled in the meantime */
67 segmentStoreReady(false);
71 if (waitForSegmentStoreCompletion()) {
72 segments
.addAll(getSegments());
73 segments
.close(false);
81 protected void canceling() {
83 segmentStoreReady(false);
87 protected @Nullable String
getDataFileName() {
88 return getId() + XmlPatternAnalysis
.SEGMENT_STORE_EXTENSION
;
92 * Broadcast the segment store to its listeners. Since this analysis is not
93 * directly register to the trace, the parent analysis is used as the source.
96 * The store to broadcast
99 protected void sendUpdate(final ISegmentStore
<@NonNull ISegment
> store
) {
100 for (IAnalysisProgressListener listener
: getListeners()) {
101 listener
.onComplete(fParent
, store
);
106 public void onNewSegment(@NonNull ISegment segment
) {
107 // We can accept segments until the first END_SEGMENT arrives. Nothing
108 // should be accept after it. This prevents to receive new segments if
109 // the analysis that generates the segments is rescheduled
110 if (!fSegmentStoreCompleted
) {
111 if (segment
== END_SEGMENT
) {
112 segmentStoreReady(true);
115 getSegments().add(segment
);
120 * Get the internal segment store of this module
122 * @return The segment store
124 private synchronized ISegmentStore
<@NonNull ISegment
> getSegments() {
129 * Wait until internal segment store of the module is fully filled. If all
130 * the segments have been received, the completion succeeded, otherwise it
133 * @return True if the completion succeeded, false otherwise
135 public boolean waitForSegmentStoreCompletion() {
138 } catch (InterruptedException e
) {
141 return fSegmentStoreCompleted
;
145 * Make the module available and set whether the segment store completion
146 * succeeded or not. If not, no segment store is available and
147 * {@link #waitForSegmentStoreCompletion()} should return false.
150 * True if the segment store completion succeeded, false
153 private void segmentStoreReady(boolean succeeded
) {
154 fSegmentStoreCompleted
= succeeded
;
155 fFinished
.countDown();
159 * Fake segment indicating the build is over, and the segment store is fully
162 public static class EndSegment
implements ISegment
{
164 * The serial version UID
166 private static final long serialVersionUID
= 7834984029618274707L;
169 public long getStart() {
170 return Long
.MIN_VALUE
;
174 public long getEnd() {
175 return Long
.MIN_VALUE
;