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