| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012 Ericsson |
| 3 | * Copyright (c) 2010, 2011 École Polytechnique de Montréal |
| 4 | * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com> |
| 5 | * |
| 6 | * All rights reserved. This program and the accompanying materials are |
| 7 | * made available under the terms of the Eclipse Public License v1.0 which |
| 8 | * accompanies this distribution, and is available at |
| 9 | * http://www.eclipse.org/legal/epl-v10.html |
| 10 | * |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.core.statesystem; |
| 14 | |
| 15 | import java.io.IOException; |
| 16 | |
| 17 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.StateHistorySystem; |
| 18 | |
| 19 | /** |
| 20 | * This is the high-level wrapper around the State History and its input and |
| 21 | * storage plugins. Just create the object using the constructor then .run() |
| 22 | * |
| 23 | * You can use one HistoryBuilder and it will instantiate everything underneath. |
| 24 | * If you need more fine-grained control you can still ignore this and |
| 25 | * instantiate everything manually. |
| 26 | * |
| 27 | * @author alexmont |
| 28 | * |
| 29 | */ |
| 30 | public class HistoryBuilder implements Runnable { |
| 31 | |
| 32 | private final IStateChangeInput sci; |
| 33 | private final StateHistorySystem shs; |
| 34 | private final IStateHistoryBackend hb; |
| 35 | |
| 36 | private final Thread sciThread; |
| 37 | |
| 38 | /** |
| 39 | * Instantiate a new HistoryBuilder helper. |
| 40 | * |
| 41 | * @param stateChangeInput |
| 42 | * The input plugin to use. This is required. |
| 43 | * @param backend |
| 44 | * The backend storage to use. Use "null" here if you want a |
| 45 | * state system with no history. |
| 46 | * @throws IOException |
| 47 | * Is thrown if anything went wrong (usually with the storage |
| 48 | * backend) |
| 49 | */ |
| 50 | public HistoryBuilder(IStateChangeInput stateChangeInput, |
| 51 | IStateHistoryBackend backend) throws IOException { |
| 52 | assert (stateChangeInput != null); |
| 53 | assert (backend != null); |
| 54 | |
| 55 | sci = stateChangeInput; |
| 56 | hb = backend; |
| 57 | shs = new StateHistorySystem(hb, true); |
| 58 | |
| 59 | sci.assignTargetStateSystem(shs); |
| 60 | sciThread = new Thread(sci, "Input Plugin"); //$NON-NLS-1$ |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Factory-style method to open an existing history, you only have to |
| 65 | * provide the already-instantiated IStateHistoryBackend object. |
| 66 | * |
| 67 | * @param hb |
| 68 | * The history-backend object |
| 69 | * @return A IStateSystemBuilder reference to the new state system. If you |
| 70 | * will only run queries on this history, you should *definitely* |
| 71 | * cast it to IStateSystemQuerier. |
| 72 | * @throws IOException |
| 73 | * If there was something wrong. |
| 74 | */ |
| 75 | public static IStateSystemBuilder openExistingHistory( |
| 76 | IStateHistoryBackend hb) throws IOException { |
| 77 | return new StateHistorySystem(hb, false); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void run() { |
| 82 | sciThread.start(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Since HistoryBuilder.run() simply starts the processing asynchronously, |
| 87 | * you should call .close() when you know the state history is completely |
| 88 | * built (or when the user closes the trace, whichever comes first). |
| 89 | */ |
| 90 | public void close() { |
| 91 | try { |
| 92 | sciThread.join(); |
| 93 | } catch (InterruptedException e) { |
| 94 | e.printStackTrace(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Return a read/write reference to the state system object that was |
| 100 | * created. |
| 101 | * |
| 102 | * @return Reference to the state system, with access to everything. |
| 103 | */ |
| 104 | public IStateSystemBuilder getStateSystemBuilder() { |
| 105 | return shs; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Return a read-only reference to the state system object that was created. |
| 110 | * |
| 111 | * @return Reference to the state system, but only with the query methods |
| 112 | * available. |
| 113 | */ |
| 114 | public IStateSystemQuerier getStateSystemQuerier() { |
| 115 | return shs; |
| 116 | } |
| 117 | |
| 118 | } |