| 1 | package de.uka.ipd.sdq.sensorframework; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Collections; |
| 6 | |
| 7 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
| 8 | |
| 9 | /** |
| 10 | * Provides access to the singleton dataset of the SensorFramework. Used to display |
| 11 | * all datasources in the SimuBench Perspective. |
| 12 | * |
| 13 | * @author Steffen Becker |
| 14 | * @author groenda |
| 15 | */ |
| 16 | public class SensorFrameworkDataset { |
| 17 | private static SensorFrameworkDataset singleton = new SensorFrameworkDataset();//why not final? |
| 18 | private ArrayList<IDAOFactory> datasources = new ArrayList<IDAOFactory>(); |
| 19 | private long nextID = 1; |
| 20 | |
| 21 | private SensorFrameworkDataset() { |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @return the one central instance containing all datasources. |
| 26 | */ |
| 27 | public static SensorFrameworkDataset singleton() { |
| 28 | return singleton; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Returns all data sources in the dataset. |
| 33 | * |
| 34 | * @return datasources. |
| 35 | */ |
| 36 | public synchronized Collection<IDAOFactory> getDataSources() { |
| 37 | return Collections.unmodifiableCollection(datasources); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Retrieve a datasource by its dynamic numeric identifier. |
| 42 | * |
| 43 | * @param id |
| 44 | * id of the datasource. |
| 45 | * @return datasource. |
| 46 | */ |
| 47 | public synchronized IDAOFactory getDataSourceByID(long id) { |
| 48 | for (IDAOFactory f : datasources) |
| 49 | if (f.getID() == id) |
| 50 | return f; |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Add a provided datasource. |
| 56 | * |
| 57 | * @param dataSource |
| 58 | * the datasource. |
| 59 | */ |
| 60 | public synchronized void addDataSource(IDAOFactory dataSource) { |
| 61 | for (IDAOFactory f : datasources) { |
| 62 | if (f.getID() == dataSource.getID()) |
| 63 | throw new RuntimeException( |
| 64 | "Attemped to add Datasource (of a type inherting from IDAOFactory) " + |
| 65 | "with an ID already existing in the Sensorframework Dataset."); |
| 66 | if (f.getID() == nextID |
| 67 | && dataSource.getID() == IDAOFactory.ID_NOT_SET){//why is it an "&&"? |
| 68 | throw new RuntimeException( |
| 69 | "Conflict of IDs occurred when attemping to add a new datasource to the Sensorframework Dataset."); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | datasources.add(dataSource); |
| 74 | if (dataSource.getID() == IDAOFactory.ID_NOT_SET) { |
| 75 | dataSource.setID(nextID); |
| 76 | nextID += 1; |
| 77 | } else { |
| 78 | if (dataSource.getID() >= nextID) { |
| 79 | // reset nextID completely |
| 80 | this.nextID = Long.MIN_VALUE; |
| 81 | for (IDAOFactory f : datasources) { |
| 82 | if (f.getID() >= nextID) |
| 83 | this.nextID = f.getID() + 1; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Removes a datasource from the current Dataset. |
| 91 | * |
| 92 | * @param factory |
| 93 | * the datasource itself to remove. |
| 94 | */ |
| 95 | public synchronized void removeDataSource(IDAOFactory factory) { |
| 96 | factory.finalizeAndClose(); |
| 97 | datasources.remove(factory);//this method returns a boolean - the calling method should do so, too |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Removes all data sources from the current data set. |
| 102 | */ |
| 103 | public synchronized void removeAllDataSources() { |
| 104 | while (datasources.size() > 0) { |
| 105 | removeDataSource(datasources.get(0)); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Reloads all known datasources. Throws runtime exeception if some |
| 111 | * datasources are faulty. |
| 112 | */ |
| 113 | public synchronized void reload() { |
| 114 | String errorMessage = ""; |
| 115 | boolean failed = false; |
| 116 | for (IDAOFactory f : datasources) { |
| 117 | try { |
| 118 | f.reload(); |
| 119 | } catch (Exception e) { |
| 120 | failed = true; |
| 121 | errorMessage += ">" + e.getMessage() + "< "; |
| 122 | this.removeDataSource(f); |
| 123 | } |
| 124 | } |
| 125 | if (failed) { |
| 126 | throw new RuntimeException( |
| 127 | "Some Datasources failed to reload. Please see Error Log for details. Details: " |
| 128 | + errorMessage); |
| 129 | } |
| 130 | } |
| 131 | } |