Organized imports
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.core / src / net / sourceforge / phpdt / monitor / core / internal / MonitorPlugin.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.internal;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18
19 import net.sourceforge.phpdt.monitor.core.IContentFilter;
20 import net.sourceforge.phpdt.monitor.core.IProtocolAdapter;
21 import net.sourceforge.phpdt.monitor.core.IRequestListener;
22
23 import org.eclipse.core.runtime.IConfigurationElement;
24 import org.eclipse.core.runtime.IExtensionRegistry;
25 import org.eclipse.core.runtime.Platform;
26 import org.eclipse.core.runtime.Plugin;
27 /**
28  * The monitor core plugin.
29  */
30 public class MonitorPlugin extends Plugin {
31         public static final String PLUGIN_ID = "net.sourceforge.phpeclipse.monitor.core";
32
33         private static MonitorPlugin singleton;
34         
35         protected Map protocolAdapters;
36         protected Map contentFilters;
37         protected IRequestListener[] requestListeners;
38
39         /**
40          * MonitorPlugin constructor comment.
41          */
42         public MonitorPlugin() {
43                 super();
44                 singleton = this;
45                 loadProtocolAdapters();
46                 loadContentFilters();
47         }
48
49         /**
50          * Returns the singleton instance of this plugin.
51          *
52          * @return net.sourceforge.phpdt.monitor.core.MonitorPlugin
53          */
54         public static MonitorPlugin getInstance() {
55                 return singleton;
56         }
57
58         /**
59          * Returns the translated String found with the given key.
60          *
61          * @return java.lang.String
62          * @param key java.lang.String
63          */
64         public static String getString(String key) {
65                 try {
66                         return Platform.getResourceString(getInstance().getBundle(), key);
67                 } catch (Exception e) {
68                         return key;
69                 }
70         }
71         
72         public IProtocolAdapter getDefaultType() {
73                 return (ProtocolAdapter) protocolAdapters.get("HTTP");
74         }
75         
76         public IProtocolAdapter getProtocolAdapter(String id) {
77                 return (ProtocolAdapter) protocolAdapters.get(id);
78         }
79
80         public IProtocolAdapter[] getProtocolAdapters() {
81                 List list = new ArrayList();
82                 Iterator iterator = protocolAdapters.values().iterator();
83                 while (iterator.hasNext()) {
84                         list.add(iterator.next());
85                 }
86                 IProtocolAdapter[] types = new IProtocolAdapter[list.size()];
87                 list.toArray(types);
88                 return types;
89         }
90
91         public IContentFilter[] getContentFilters() {
92                 List list = new ArrayList();
93                 Iterator iterator = contentFilters.values().iterator();
94                 while (iterator.hasNext()) {
95                         list.add(iterator.next());
96                 }
97                 IContentFilter[] cf = new IContentFilter[list.size()];
98                 list.toArray(cf);
99                 return cf;
100         }
101         
102         public IContentFilter getContentFilter(String id) {
103                 return (IContentFilter) contentFilters.get(id);
104         }
105         
106         public IRequestListener[] getRequestListeners() {
107                 if (requestListeners == null)
108                         loadRequestListeners();
109                 return requestListeners;
110         }
111
112         public void loadProtocolAdapters() {
113                 Trace.trace(Trace.CONFIG, "Loading protocol adapters"); 
114                 IExtensionRegistry registry = Platform.getExtensionRegistry();
115                 IConfigurationElement[] cf = registry.getConfigurationElementsFor(MonitorPlugin.PLUGIN_ID, "protocolAdapters");
116
117                 int size = cf.length;
118                 protocolAdapters = new HashMap(size);
119                 for (int i = 0; i < size; i++) {
120                         String id = cf[i].getAttribute("id");
121                         Trace.trace(Trace.CONFIG, "Loading adapter: " + id);
122                         protocolAdapters.put(id, new ProtocolAdapter(cf[i]));
123                 }
124         }
125         
126         public void loadContentFilters() {
127                 Trace.trace(Trace.CONFIG, "Loading content filters"); 
128                 IExtensionRegistry registry = Platform.getExtensionRegistry();
129                 IConfigurationElement[] cf = registry.getConfigurationElementsFor(MonitorPlugin.PLUGIN_ID, "contentFilters");
130
131                 int size = cf.length;
132                 contentFilters = new HashMap(size);
133                 for (int i = 0; i < size; i++) {
134                         String id = cf[i].getAttribute("id");
135                         Trace.trace(Trace.CONFIG, "Loading filter: " + id);
136                         contentFilters.put(id, new ContentFilter(cf[i]));
137                 }
138         }
139         
140         public void loadRequestListeners() {
141                 Trace.trace(Trace.CONFIG, "Loading request listeners"); 
142                 IExtensionRegistry registry = Platform.getExtensionRegistry();
143                 IConfigurationElement[] cf = registry.getConfigurationElementsFor(MonitorPlugin.PLUGIN_ID, "requestListeners");
144
145                 int size = cf.length;
146                 List list = new ArrayList();
147                 for (int i = 0; i < size; i++) {
148                         String id = cf[i].getAttribute("id");
149                         Trace.trace(Trace.CONFIG, "Loading request listener: " + id);
150                         try {
151                                 IRequestListener rl = (IRequestListener) cf[i].createExecutableExtension("class");
152                                 list.add(rl);
153                         } catch (Exception e) {
154                                 Trace.trace(Trace.SEVERE, "Could not create request listener: " + id, e);
155                                 return;
156                         }
157                 }
158                 
159                 size = list.size();
160                 requestListeners = new IRequestListener[size];
161                 list.toArray(requestListeners);
162         }
163 }