1 /**********************************************************************
2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.monitor.core.internal;
13 import java.io.ByteArrayInputStream;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
20 import net.sourceforge.phpdt.monitor.core.*;
22 import org.eclipse.core.runtime.Preferences;
26 public class MonitorManager {
27 private static final int ADD = 0;
28 private static final int CHANGE = 1;
29 private static final int REMOVE = 2;
32 protected List monitors;
33 protected Map threads = new HashMap();
35 protected List monitorListeners = new ArrayList();
38 protected List requests = new ArrayList();
40 protected List requestListeners = new ArrayList();
42 private Preferences.IPropertyChangeListener pcl;
43 protected boolean ignorePreferenceChanges = false;
45 protected static MonitorManager instance;
47 public static MonitorManager getInstance() {
49 instance = new MonitorManager();
53 private MonitorManager() {
56 pcl = new Preferences.IPropertyChangeListener() {
57 public void propertyChange(Preferences.PropertyChangeEvent event) {
58 if (ignorePreferenceChanges)
60 String property = event.getProperty();
61 if (property.equals("monitors")) {
67 MonitorPlugin.getInstance().getPluginPreferences().addPropertyChangeListener(pcl);
70 protected void dispose() {
71 MonitorPlugin.getInstance().getPluginPreferences().removePropertyChangeListener(pcl);
74 public IMonitorWorkingCopy createMonitor() {
75 return new MonitorWorkingCopy();
78 public List getMonitors() {
79 return new ArrayList(monitors);
82 protected void addMonitor(IMonitor monitor) {
83 if (!monitors.contains(monitor))
84 monitors.add(monitor);
85 fireMonitorEvent(monitor, ADD);
89 protected boolean isRunning(IMonitor monitor) {
90 return (threads.get(monitor) != null);
93 public void startMonitor(IMonitor monitor) throws Exception {
94 if (!monitors.contains(monitor))
97 if (AcceptThread.isPortInUse(monitor.getLocalPort()))
98 throw new Exception(MonitorPlugin.getString("%errorPortInUse"));
100 AcceptThread thread = new AcceptThread(monitor);
101 thread.startServer();
102 threads.put(monitor, thread);
105 public void stopMonitor(IMonitor monitor) {
106 if (!monitors.contains(monitor))
109 AcceptThread thread = (AcceptThread) threads.get(monitor);
110 if (thread != null) {
112 threads.remove(monitor);
116 protected void removeMonitor(IMonitor monitor) {
117 if (monitor.isRunning())
118 stopMonitor(monitor);
119 monitors.remove(monitor);
120 fireMonitorEvent(monitor, REMOVE);
124 protected void monitorChanged(IMonitor monitor) {
125 fireMonitorEvent(monitor, CHANGE);
130 * Add monitor listener.
134 public void addMonitorListener(IMonitorListener listener) {
135 monitorListeners.add(listener);
139 * Remove monitor listener.
143 public void removeMonitorListener(IMonitorListener listener) {
144 monitorListeners.remove(listener);
148 * Fire a monitor event.
152 protected void fireMonitorEvent(IMonitor monitor, int type) {
153 Object[] obj = monitorListeners.toArray();
155 int size = obj.length;
156 for (int i = 0; i < size; i++) {
157 IMonitorListener listener = (IMonitorListener) obj[i];
159 listener.monitorAdded(monitor);
160 else if (type == CHANGE)
161 listener.monitorChanged(monitor);
162 else if (type == REMOVE)
163 listener.monitorRemoved(monitor);
168 * Returns a list of the current requests.
170 * @return java.util.List
172 public List getRequests() {
177 * Add a new request response pair.
179 * @param pair org.eclipse.tcpip.monitor.RequestResponse
181 public void addRequest(IRequest rr) {
182 if (requests.contains(rr))
186 fireRequestEvent(rr, ADD);
189 public void requestChanged(IRequest rr) {
190 fireRequestEvent(rr, CHANGE);
193 public void removeRequest(IRequest rr) {
194 if (!requests.contains(rr))
198 fireRequestEvent(rr, REMOVE);
201 public void removeAllRequests() {
202 int size = requests.size();
203 IRequest[] rrs = new IRequest[size];
204 requests.toArray(rrs);
206 for (int i = 0; i < size; i++) {
207 removeRequest(rrs[i]);
212 * Add request listener.
216 public void addRequestListener(IRequestListener listener) {
217 requestListeners.add(listener);
221 * Remove request listener.
225 public void removeRequestListener(IRequestListener listener) {
226 requestListeners.remove(listener);
230 * Fire a request event.
234 protected void fireRequestEvent(IRequest rr, int type) {
235 int size = requestListeners.size();
236 IRequestListener[] xrl = MonitorPlugin.getInstance().getRequestListeners();
237 int size2 = xrl.length;
239 IRequestListener[] rl = new IRequestListener[size + size2];
240 System.arraycopy(xrl, 0, rl, 0, size2);
241 for (int i = 0; i < size; i++)
242 rl[size2 + i] = (IRequestListener) requestListeners.get(i);
244 for (int i = 0; i < size + size2; i++) {
245 IRequestListener listener = rl[i];
247 listener.requestAdded(rr);
248 else if (type == CHANGE)
249 listener.requestChanged(rr);
250 else if (type == REMOVE)
251 listener.requestRemoved(rr);
255 protected void loadMonitors() {
256 Trace.trace(Trace.FINEST, "Loading monitors");
258 monitors = new ArrayList();
259 Preferences prefs = MonitorPlugin.getInstance().getPluginPreferences();
260 String xmlString = prefs.getString("monitors");
261 if (xmlString != null && xmlString.length() > 0) {
263 ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
264 IMemento memento = XMLMemento.loadMemento(in);
266 IMemento[] children = memento.getChildren("monitor");
267 if (children != null) {
268 int size = children.length;
269 for (int i = 0; i < size; i++) {
270 Monitor monitor = new Monitor();
271 monitor.load(children[i]);
272 monitors.add(monitor);
275 } catch (Exception e) {
276 Trace.trace(Trace.WARNING, "Could not load monitors: " + e.getMessage());
281 protected void saveMonitors() {
283 ignorePreferenceChanges = true;
284 XMLMemento memento = XMLMemento.createWriteRoot("monitors");
286 Iterator iterator = monitors.iterator();
287 while (iterator.hasNext()) {
288 Monitor monitor = (Monitor) iterator.next();
289 IMemento child = memento.createChild("monitor");
293 String xmlString = memento.saveToString();
294 Preferences prefs = MonitorPlugin.getInstance().getPluginPreferences();
295 prefs.setValue("monitors", xmlString);
296 MonitorPlugin.getInstance().savePluginPreferences();
297 } catch (Exception e) {
298 Trace.trace(Trace.SEVERE, "Could not save browsers", e);
300 ignorePreferenceChanges = false;