Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fd472a479c2eff89c3e6956374093ea15fb18a6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ecf.provider.irc.bot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.ecf.provider.irc.bot.handler.ICommandHandler;
import org.eclipse.ecf.provider.irc.internal.bot.BotEntry;
import org.eclipse.ecf.provider.irc.internal.bot.CommandEntry;
import org.eclipse.ecf.provider.irc.internal.bot.IBotEntry;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator implements BundleActivator {

	// The plug-in ID
	public static final String PLUGIN_ID = "org.eclipse.ecf.provider.irc.bot";

	// The shared instance
	private static Activator plugin;

	private ServiceTracker extensionRegistryTracker = null;

	private Map bots = new HashMap();
	private Map commands = new HashMap();

	/**
	 * The constructor
	 */
	public Activator() {
	}

	public IExtensionRegistry getExtensionRegistry() {
		return (IExtensionRegistry) extensionRegistryTracker.getService();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		plugin = this;
		this.extensionRegistryTracker = new ServiceTracker(context,
				IExtensionRegistry.class.getName(), null);
		this.extensionRegistryTracker.open();
		loadExtensions();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		if (extensionRegistryTracker != null) {
			extensionRegistryTracker.close();
			extensionRegistryTracker = null;
		}
		plugin = null;
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	public Map getBots() {
		return this.bots;
	}


	private void loadExtensions() throws CoreException {
		// load the command handlers
		IExtensionRegistry reg = getExtensionRegistry();
		if (reg != null) {
			IConfigurationElement[] elements = reg.getConfigurationElementsFor("org.eclipse.ecf.provider.irc.bot.commandHandler");
			for(int i = 0; i < elements.length; i++) {
				String id = elements[i].getAttribute("botId");
				String expression = elements[i].getAttribute("expression");
				ICommandHandler handler = (ICommandHandler) elements[i].createExecutableExtension("class");
				List c = (List) commands.get(id);
				if(c == null) {
					c = new ArrayList();
					c.add(new CommandEntry(expression, handler));
					commands.put(id, c);
				} else {
					c.add(new CommandEntry(expression, handler));
					commands.put(id, c);
				}
			}
	
			// load the bots
			elements = reg.getConfigurationElementsFor("org.eclipse.ecf.provider.irc.bot.ircbot");
			for(int i = 0; i < elements.length; i++) {
				String id = elements[i].getAttribute("id");
				String name = elements[i].getAttribute("name");
				String server = elements[i].getAttribute("server");
				String channel = elements[i].getAttribute("channel");
				List c = (List) commands.get(id);
				if(c == null)
					c = new ArrayList();
				IBotEntry bot = new BotEntry(id, name, server, channel, c);
				bots.put(id, bot);
			}
		}

	}

}

Back to the top