1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.component
;
15 import java
.lang
.reflect
.Array
;
16 import java
.util
.Vector
;
18 import org
.eclipse
.linuxtools
.tmf
.event
.TmfData
;
19 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfRequestHandler
;
20 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
23 * <b><u>TmfProvider</u></b>
25 * The TmfProvider<T> is a provider for a data of type <T>.
27 * This abstract class implements the housekeeking methods to register/
28 * deregister the event provider and to handle generically the event requests.
30 * The concrete class can either re-implement processRequest() entirely or
31 * just implement the hooks (initializeContext() and getNext()).
33 * TODO: Add support for providing multiple data types.
35 public abstract class TmfProvider
<T
extends TmfData
> extends TmfComponent
implements ITmfRequestHandler
<T
> {
37 private Class
<T
> fType
;
39 // ------------------------------------------------------------------------
40 // Constructors (enforce that a type be supplied)
41 // ------------------------------------------------------------------------
43 @SuppressWarnings("unused")
44 private TmfProvider() {
47 @SuppressWarnings("unused")
48 private TmfProvider(TmfProvider
<T
> other
) {
51 protected TmfProvider(Class
<T
> type
) {
57 public void register() {
59 TmfProviderManager
.register(fType
, this);
63 public void deregister() {
64 TmfProviderManager
.deregister(fType
, this);
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
72 // TODO: Request coalescing, filtering, result dispatching
74 public void processRequest(final TmfDataRequest
<T
> request
, boolean waitForCompletion
) {
77 processDataRequest(request
);
79 // Wait for completion if needed
80 if (waitForCompletion
) {
81 request
.waitForCompletion();
85 protected void processDataRequest(final TmfDataRequest
<T
> request
) {
87 // Process the request
88 Thread thread
= new Thread() {
93 // Extract the generic information
94 int blockSize
= request
.getBlockize();
95 int nbRequested
= request
.getNbRequested();
97 // Create the result buffer
98 Vector
<T
> result
= new Vector
<T
>();
101 // Initialize the execution
102 ITmfContext context
= setContext(request
);
104 // Get the ordered events
105 T data
= getNext(context
);
106 while (data
!= null && !request
.isCancelled() && nbRead
< nbRequested
&& !isCompleted(request
, data
))
109 if (++nbRead
% blockSize
== 0) {
110 pushData(request
, result
);
112 // To avoid an unnecessary read passed the last data requested
113 if (nbRead
< nbRequested
)
114 data
= getNext(context
);
116 pushData(request
, result
);
124 * Format the result data and forwards it to the requester.
125 * Note: after handling, the data is *removed*.
130 @SuppressWarnings("unchecked")
131 private void pushData(TmfDataRequest
<T
> request
, Vector
<T
> data
) {
132 synchronized(request
) {
133 if (!request
.isCompleted()) {
134 T
[] result
= (T
[]) Array
.newInstance(fType
, data
.size());
135 data
.toArray(result
);
136 request
.setData(result
);
137 request
.handleData();
138 data
.removeAllElements();
144 * Initialize the provider based on the request. The context is
145 * application specific and will be updated by getNext().
150 public abstract ITmfContext
setContext(TmfDataRequest
<T
> request
);
153 * Return the next piece of data based on the context supplied. The context
154 * would typically be updated for the subsequent read.
159 public abstract T
getNext(ITmfContext context
);
162 * Checks if the data meets the request completion criteria.
168 public abstract boolean isCompleted(TmfDataRequest
<T
> request
, T data
);
This page took 0.038798 seconds and 5 git commands to generate.