f8469476c68f392f05e8a1eb5cf0799638155ee8
[phpeclipse.git] /
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.core.internal;
12
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;
18 import java.util.Map;
19
20 import net.sourceforge.phpdt.monitor.core.*;
21
22 import org.eclipse.core.runtime.Preferences;
23 /**
24  * 
25  */
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;
30
31         // monitors
32         protected List monitors;
33         protected Map threads = new HashMap();
34         
35         protected List monitorListeners = new ArrayList();
36         
37         // requests
38         protected List requests = new ArrayList();
39         
40         protected List requestListeners = new ArrayList();
41         
42         private Preferences.IPropertyChangeListener pcl;
43         protected boolean ignorePreferenceChanges = false;
44         
45         protected static MonitorManager instance;
46         
47         public static MonitorManager getInstance() {
48                 if (instance == null)
49                         instance = new MonitorManager();
50                 return instance;
51         }
52         
53         private MonitorManager() {
54                 loadMonitors();
55                 
56                 pcl = new Preferences.IPropertyChangeListener() {
57                         public void propertyChange(Preferences.PropertyChangeEvent event) {
58                                 if (ignorePreferenceChanges)
59                                         return;
60                                 String property = event.getProperty();
61                                 if (property.equals("monitors")) {
62                                         loadMonitors();
63                                 }
64                         }
65                 };
66                 
67                 MonitorPlugin.getInstance().getPluginPreferences().addPropertyChangeListener(pcl);
68         }
69         
70         protected void dispose() {
71                 MonitorPlugin.getInstance().getPluginPreferences().removePropertyChangeListener(pcl);
72         }
73         
74         public IMonitorWorkingCopy createMonitor() {
75                 return new MonitorWorkingCopy();
76         }
77         
78         public List getMonitors() {
79                 return new ArrayList(monitors);
80         }
81
82         protected void addMonitor(IMonitor monitor) {
83                 if (!monitors.contains(monitor))
84                         monitors.add(monitor);
85                 fireMonitorEvent(monitor, ADD);
86                 saveMonitors();
87         }
88         
89         protected boolean isRunning(IMonitor monitor) {
90                 return (threads.get(monitor) != null);
91         }
92
93         public void startMonitor(IMonitor monitor) throws Exception {
94                 if (!monitors.contains(monitor))
95                         return;
96                 
97                 if (AcceptThread.isPortInUse(monitor.getLocalPort()))
98                         throw new Exception(MonitorPlugin.getString("%errorPortInUse"));
99                 
100                 AcceptThread thread = new AcceptThread(monitor);
101                 thread.startServer();
102                 threads.put(monitor, thread);
103         }
104         
105         public void stopMonitor(IMonitor monitor) {
106                 if (!monitors.contains(monitor))
107                         return;
108                 
109                 AcceptThread thread = (AcceptThread) threads.get(monitor);
110                 if (thread != null) {
111                         thread.stopServer();
112                         threads.remove(monitor);
113                 }
114         }
115         
116         protected void removeMonitor(IMonitor monitor) {
117                 if (monitor.isRunning())
118                         stopMonitor(monitor);
119                 monitors.remove(monitor);
120                 fireMonitorEvent(monitor, REMOVE);
121                 saveMonitors();
122         }
123         
124         protected void monitorChanged(IMonitor monitor) {
125                 fireMonitorEvent(monitor, CHANGE);
126                 saveMonitors();
127         }
128         
129         /**
130          * Add monitor listener.
131          * 
132          * @param listener
133          */
134         public void addMonitorListener(IMonitorListener listener) {
135                 monitorListeners.add(listener);
136         }
137
138         /**
139          * Remove monitor listener.
140          * 
141          * @param listener
142          */
143         public void removeMonitorListener(IMonitorListener listener) {
144                 monitorListeners.remove(listener);
145         }
146         
147         /**
148          * Fire a monitor event.
149          * @param rr
150          * @param type
151          */
152         protected void fireMonitorEvent(IMonitor monitor, int type) {
153                 Object[] obj = monitorListeners.toArray();
154                 
155                 int size = obj.length;
156                 for (int i = 0; i < size; i++) {
157                         IMonitorListener listener = (IMonitorListener) obj[i];
158                         if (type == ADD)
159                                 listener.monitorAdded(monitor);
160                         else if (type == CHANGE)
161                                 listener.monitorChanged(monitor);
162                         else if (type == REMOVE)
163                                 listener.monitorRemoved(monitor);
164                 }
165         }
166         
167         /**
168          * Returns a list of the current requests.
169          *
170          * @return java.util.List
171          */
172         public List getRequests() {
173                 return requests;
174         }
175         
176         /**
177          * Add a new request response pair.
178          *
179          * @param pair org.eclipse.tcpip.monitor.RequestResponse
180          */
181         public void addRequest(IRequest rr) {
182                 if (requests.contains(rr))
183                         return;
184
185                 requests.add(rr);
186                 fireRequestEvent(rr, ADD);
187         }
188         
189         public void requestChanged(IRequest rr) {
190                 fireRequestEvent(rr, CHANGE);
191         }
192         
193         public void removeRequest(IRequest rr) {
194                 if (!requests.contains(rr))
195                         return;
196
197                 requests.remove(rr);
198                 fireRequestEvent(rr, REMOVE);
199         }
200         
201         public void removeAllRequests() {
202                 int size = requests.size();
203                 IRequest[] rrs = new IRequest[size];
204                 requests.toArray(rrs);
205                 
206                 for (int i = 0; i < size; i++) {
207                         removeRequest(rrs[i]);
208                 }
209         }
210         
211         /**
212          * Add request listener.
213          * 
214          * @param listener
215          */
216         public void addRequestListener(IRequestListener listener) {
217                 requestListeners.add(listener);
218         }
219
220         /**
221          * Remove request listener.
222          * 
223          * @param listener
224          */
225         public void removeRequestListener(IRequestListener listener) {
226                 requestListeners.remove(listener);
227         }
228
229         /**
230          * Fire a request event.
231          * @param rr
232          * @param type
233          */
234         protected void fireRequestEvent(IRequest rr, int type) {
235                 int size = requestListeners.size();
236                 IRequestListener[] xrl = MonitorPlugin.getInstance().getRequestListeners();
237                 int size2 = xrl.length;
238                 
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);
243
244                 for (int i = 0; i < size + size2; i++) {
245                         IRequestListener listener = rl[i];
246                         if (type == ADD)
247                                 listener.requestAdded(rr);
248                         else if (type == CHANGE)
249                                 listener.requestChanged(rr);
250                         else if (type == REMOVE)
251                                 listener.requestRemoved(rr);
252                 }
253         }
254         
255         protected void loadMonitors() {
256                 Trace.trace(Trace.FINEST, "Loading monitors");
257                 
258                 monitors = new ArrayList();
259                 Preferences prefs = MonitorPlugin.getInstance().getPluginPreferences();
260                 String xmlString = prefs.getString("monitors");
261                 if (xmlString != null && xmlString.length() > 0) {
262                         try {
263                                 ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
264                                 IMemento memento = XMLMemento.loadMemento(in);
265                 
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);
273                                         }
274                                 }
275                         } catch (Exception e) {
276                                 Trace.trace(Trace.WARNING, "Could not load monitors: " + e.getMessage());
277                         }
278                 }
279         }
280         
281         protected void saveMonitors() {
282                 try {
283                         ignorePreferenceChanges = true;
284                         XMLMemento memento = XMLMemento.createWriteRoot("monitors");
285
286                         Iterator iterator = monitors.iterator();
287                         while (iterator.hasNext()) {
288                                 Monitor monitor = (Monitor) iterator.next();
289                                 IMemento child = memento.createChild("monitor");
290                                 monitor.save(child);
291                         }
292                         
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);
299                 }
300                 ignorePreferenceChanges = false;
301         }
302 }