| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012 Ericsson |
| 3 | * |
| 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 | * Contributors: |
| 10 | * Bernd Hufmann - Initial API and implementation |
| 11 | **********************************************************************/ |
| 12 | package org.eclipse.linuxtools.lttng.ui.views.control.model.impl; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.Iterator; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.linuxtools.lttng.ui.views.control.model.IDomainInfo; |
| 19 | import org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo; |
| 20 | import org.eclipse.linuxtools.lttng.ui.views.control.model.TraceSessionState; |
| 21 | |
| 22 | /** |
| 23 | * <b><u>SessionInfo</u></b> |
| 24 | * <p> |
| 25 | * Implementation of the trace session interface (ISessionInfo) to store session |
| 26 | * related data. |
| 27 | * </p> |
| 28 | */ |
| 29 | public class SessionInfo extends TraceInfo implements ISessionInfo { |
| 30 | |
| 31 | // ------------------------------------------------------------------------ |
| 32 | // Attributes |
| 33 | // ------------------------------------------------------------------------ |
| 34 | /** |
| 35 | * The trace session state. |
| 36 | */ |
| 37 | private TraceSessionState fState = TraceSessionState.INACTIVE; |
| 38 | /** |
| 39 | * The trace session path for storing traces. |
| 40 | */ |
| 41 | private String fSessionPath = ""; //$NON-NLS-1$ |
| 42 | /** |
| 43 | * The domains information of this session. |
| 44 | */ |
| 45 | private List<IDomainInfo> fDomains = new ArrayList<IDomainInfo>(); |
| 46 | |
| 47 | // ------------------------------------------------------------------------ |
| 48 | // Constructors |
| 49 | // ------------------------------------------------------------------------ |
| 50 | /** |
| 51 | * Constructor |
| 52 | * @param name - name of base event |
| 53 | */ |
| 54 | public SessionInfo(String name) { |
| 55 | super(name); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Copy constructor |
| 60 | * @param other - the instance to copy |
| 61 | */ |
| 62 | public SessionInfo(SessionInfo other) { |
| 63 | super(other); |
| 64 | fState = other.fState; |
| 65 | fSessionPath = other.fSessionPath; |
| 66 | |
| 67 | for (Iterator<IDomainInfo> iterator = other.fDomains.iterator(); iterator.hasNext();) { |
| 68 | IDomainInfo domain = iterator.next(); |
| 69 | if (domain instanceof DomainInfo) { |
| 70 | fDomains.add(new DomainInfo((DomainInfo)domain)); |
| 71 | } else { |
| 72 | fDomains.add(domain); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // ------------------------------------------------------------------------ |
| 78 | // Accessors |
| 79 | // ------------------------------------------------------------------------ |
| 80 | /* |
| 81 | * (non-Javadoc) |
| 82 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo#getSessionState() |
| 83 | */ |
| 84 | @Override |
| 85 | public TraceSessionState getSessionState() { |
| 86 | return fState; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * (non-Javadoc) |
| 91 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo#setSessionState(org.eclipse.linuxtools.lttng.ui.views.control.model.TraceSessionState) |
| 92 | */ |
| 93 | @Override |
| 94 | public void setSessionState(TraceSessionState state) { |
| 95 | fState = state; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * (non-Javadoc) |
| 100 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo#setSessionState(java.lang.String) |
| 101 | */ |
| 102 | @Override |
| 103 | public void setSessionState(String stateName) { |
| 104 | if (TraceSessionState.INACTIVE.getInName().equals(stateName)) { |
| 105 | fState = TraceSessionState.INACTIVE; |
| 106 | } else if (TraceSessionState.ACTIVE.getInName().equals(stateName)) { |
| 107 | fState = TraceSessionState.ACTIVE; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * (non-Javadoc) |
| 113 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo#getSessionPath() |
| 114 | */ |
| 115 | @Override |
| 116 | public String getSessionPath() { |
| 117 | return fSessionPath; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * (non-Javadoc) |
| 122 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo#setSessionPath(java.lang.String) |
| 123 | */ |
| 124 | @Override |
| 125 | public void setSessionPath(String path) { |
| 126 | fSessionPath = path; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * (non-Javadoc) |
| 131 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo#getDomains() |
| 132 | */ |
| 133 | @Override |
| 134 | public IDomainInfo[] getDomains() { |
| 135 | return fDomains.toArray(new IDomainInfo[fDomains.size()]); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * (non-Javadoc) |
| 140 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo#setDomains(java.util.List) |
| 141 | */ |
| 142 | @Override |
| 143 | public void setDomains(List<IDomainInfo> domains) { |
| 144 | for (Iterator<IDomainInfo> iterator = domains.iterator(); iterator.hasNext();) { |
| 145 | IDomainInfo domainInfo = (IDomainInfo) iterator.next(); |
| 146 | fDomains.add(domainInfo); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // ------------------------------------------------------------------------ |
| 151 | // Operations |
| 152 | // ------------------------------------------------------------------------ |
| 153 | /* |
| 154 | * (non-Javadoc) |
| 155 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.ISessionInfo#addDomain(org.eclipse.linuxtools.lttng.ui.views.control.model.IDomainInfo) |
| 156 | */ |
| 157 | @Override |
| 158 | public void addDomain(IDomainInfo domainInfo) { |
| 159 | fDomains.add(domainInfo); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * (non-Javadoc) |
| 164 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceInfo#formatString() |
| 165 | */ |
| 166 | @Override |
| 167 | @SuppressWarnings("nls") |
| 168 | public String formatString() { |
| 169 | StringBuffer output = new StringBuffer(); |
| 170 | // Tracing session mysession: [active] |
| 171 | output.append("Tracing session "); |
| 172 | output.append(getName()); |
| 173 | output.append(": ["); |
| 174 | output.append(getSessionState().getInName()); |
| 175 | output.append("]\n"); |
| 176 | |
| 177 | // Trace path: /home/user/lttng-traces/mysession-20120129-084256 |
| 178 | output.append(" Trace path: "); |
| 179 | output.append(getSessionPath()); |
| 180 | output.append("\n"); |
| 181 | |
| 182 | for (Iterator<IDomainInfo> iterator = fDomains.iterator(); iterator.hasNext();) { |
| 183 | IDomainInfo domain = (IDomainInfo) iterator.next(); |
| 184 | output.append(domain.formatString()); |
| 185 | } |
| 186 | return output.toString(); |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * (non-Javadoc) |
| 191 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceInfo#hashCode() |
| 192 | */ |
| 193 | @Override |
| 194 | public int hashCode() { |
| 195 | final int prime = 31; |
| 196 | int result = super.hashCode(); |
| 197 | result = prime * result + ((fDomains == null) ? 0 : fDomains.hashCode()); |
| 198 | result = prime * result + ((fSessionPath == null) ? 0 : fSessionPath.hashCode()); |
| 199 | result = prime * result + ((fState == null) ? 0 : (fState.ordinal() + 1)); |
| 200 | return result; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * (non-Javadoc) |
| 205 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceInfo#equals(java.lang.Object) |
| 206 | */ |
| 207 | @Override |
| 208 | public boolean equals(Object obj) { |
| 209 | if (this == obj) { |
| 210 | return true; |
| 211 | } |
| 212 | if (!super.equals(obj)) { |
| 213 | return false; |
| 214 | } |
| 215 | if (getClass() != obj.getClass()) { |
| 216 | return false; |
| 217 | } |
| 218 | SessionInfo other = (SessionInfo) obj; |
| 219 | if (fDomains == null) { |
| 220 | if (other.fDomains != null) { |
| 221 | return false; |
| 222 | } |
| 223 | } else if (!fDomains.equals(other.fDomains)) { |
| 224 | return false; |
| 225 | } |
| 226 | if (fSessionPath == null) { |
| 227 | if (other.fSessionPath != null) { |
| 228 | return false; |
| 229 | } |
| 230 | } else if (!fSessionPath.equals(other.fSessionPath)) { |
| 231 | return false; |
| 232 | } |
| 233 | if (fState != other.fState) { |
| 234 | return false; |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * (non-Javadoc) |
| 241 | * @see org.eclipse.linuxtools.lttng.ui.views.control.model.impl.TraceInfo#toString() |
| 242 | */ |
| 243 | @SuppressWarnings("nls") |
| 244 | @Override |
| 245 | public String toString() { |
| 246 | StringBuffer output = new StringBuffer(); |
| 247 | output.append("[SessionInfo("); |
| 248 | output.append(super.toString()); |
| 249 | output.append(",State="); |
| 250 | output.append(fState); |
| 251 | output.append(",Domains="); |
| 252 | for (Iterator<IDomainInfo> iterator = fDomains.iterator(); iterator.hasNext();) { |
| 253 | IDomainInfo domain = (IDomainInfo) iterator.next(); |
| 254 | output.append(domain.toString()); |
| 255 | } |
| 256 | output.append(")]"); |
| 257 | return output.toString(); |
| 258 | } |
| 259 | } |