inital plugin from webtools project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.core / src / net / sourceforge / phpdt / monitor / core / MonitorCore.java
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;
12
13 import java.io.BufferedReader;
14 import java.io.ByteArrayInputStream;
15 import java.io.InputStreamReader;
16 import java.util.List;
17
18 import net.sourceforge.phpdt.monitor.core.internal.MonitorManager;
19 import net.sourceforge.phpdt.monitor.core.internal.MonitorPlugin;
20 import net.sourceforge.phpdt.monitor.core.internal.Trace;
21 /**
22  * 
23  */
24 public class MonitorCore {
25         public static String TCPIP_PROTOCOL_ID = "TCPIP";
26         public static String HTTP_PROTOCOL_ID = "HTTP";
27
28         private static MonitorManager manager = MonitorManager.getInstance();
29         
30         private static final String lineSeparator = System.getProperty("line.separator");
31         
32         /**
33          * Return a list of all the existing monitors.
34          * 
35          * @return java.util.List
36          */
37         public static List getMonitors() {
38                 return manager.getMonitors();
39         }
40         
41         /**
42          * Create a new monitor.
43          * 
44          * @return working copy
45          */
46         public static IMonitorWorkingCopy createMonitor() {
47                 return manager.createMonitor();
48         }
49         
50         /**
51          * Start the given monitor.
52          * 
53          * @param monitor
54          * @throws Exception
55          */
56         public static void startMonitor(IMonitor monitor) throws Exception {
57                 manager.startMonitor(monitor);
58         }
59         
60         /**
61          * Stop the given monitor.
62          * 
63          * @param monitor
64          */
65         public static void stopMonitor(IMonitor monitor) {
66                 manager.stopMonitor(monitor);
67         }
68         
69         /**
70          * Return the protocol adapters.
71          * 
72          * @return array of protocol adapters
73          */
74         public static IProtocolAdapter[] getProtocolAdapters() {
75                 return MonitorPlugin.getInstance().getProtocolAdapters();
76         }
77         
78         /**
79          * Return the protocol adapter with the given id.
80          * 
81          * @return protocol adapter
82          */
83         public static IProtocolAdapter getProtocolAdapter(String id) {
84                 return MonitorPlugin.getInstance().getProtocolAdapter(id);
85         }
86         
87         /**
88          * Return the content filters.
89          * 
90          * @return array of content filters
91          */
92         public static IContentFilter[] getContentFilters() {
93                 return MonitorPlugin.getInstance().getContentFilters();
94         }
95         
96         /**
97          * Return the content filter with the given id.
98          * 
99          * @return content filter
100          */
101         public static IContentFilter getContentFilter(String id) {
102                 return MonitorPlugin.getInstance().getContentFilter(id);
103         }
104         
105         /**
106          * Add monitor listener.
107          * 
108          * @param listener
109          */
110         public static void addMonitorListener(IMonitorListener listener) {
111                 manager.addMonitorListener(listener);
112         }
113
114         /**
115          * Remove monitor listener.
116          * 
117          * @param listener
118          */
119         public static void removeMonitorListener(IMonitorListener listener) {
120                 manager.removeMonitorListener(listener);
121         }
122
123         /**
124          * Return all requests.
125          * 
126          * @return
127          */
128         public static List getRequests() {
129                 return manager.getRequests();
130         }
131         
132         /**
133          * Remove all requests.
134          */
135         public static void removeAllRequests() {
136                 manager.removeAllRequests();
137         }
138         
139         /**
140          * Add request listener.
141          * 
142          * @param listener
143          */
144         public static void addRequestListener(IRequestListener listener) {
145                 manager.addRequestListener(listener);
146         }
147
148         /**
149          * Remove request listener.
150          * 
151          * @param listener
152          */
153         public static void removeRequestListener(IRequestListener listener) {
154                 manager.removeRequestListener(listener);
155         }
156         
157         /**
158          * Parse the given bytes into String form.
159          * 
160          * @param b
161          * @return
162          */
163         public static String parse(byte[] b) {
164                 if (b == null)
165                         return "";
166
167                 ByteArrayInputStream bin = new ByteArrayInputStream(b);
168                 BufferedReader br = new BufferedReader(new InputStreamReader(bin));
169                 StringBuffer sb = new StringBuffer();
170                 try {
171                         String s = br.readLine();
172                         
173                         while (s != null) {
174                                 sb.append(s);
175                                 s = br.readLine();
176                                 if (s != null)
177                                         sb.append(lineSeparator);
178                         }
179                         sb.append(lineSeparator);
180                 } catch (Exception e) {
181                         Trace.trace(Trace.SEVERE, "Error parsing input", e);
182                 }
183                 
184                 return sb.toString();
185         }
186 }