aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/ui/Activator.java
blob: 0d1ad266ca080eed1262e532eef670760ea9e843 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*****************************************************************************
 * Copyright (c) 2009 Ken Gilmer
 * 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:
 *     Ken Gilmer - initial API and implementation
 *******************************************************************************/
package org.openembedded.bc.ui;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.openembedded.bc.bitbake.BBSession;
import org.openembedded.bc.bitbake.ProjectInfoHelper;
import org.openembedded.bc.bitbake.ShellSession;
import org.openembedded.bc.ui.model.ProjectInfo;
import org.osgi.framework.BundleContext;


public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "org.openembedded.bc.ui";
	public static final String IMAGE_VARIABLE = "IMAGE_VARIABLE";
	public static final String IMAGE_FUNCTION = "IMAGE_FUNCTION";

	// The shared instance
	private static Activator plugin;
	private static Map shellMap;
	private static Map projInfoMap;
	private static Hashtable bbSessionMap;

	/**
	 * Get or create a BitBake session passing in ProjectInfo
	 * 
	 * @param pinfo
	 * @return
	 * @throws IOException
	 */
	public static BBSession getBBSession(String projectRoot, Writer out) throws IOException {
		if (bbSessionMap == null) {
			bbSessionMap = new Hashtable();
		}

		BBSession bbs = (BBSession) bbSessionMap.get(projectRoot);

		if (bbs == null) {
			bbs = new BBSession(getShellSession(projectRoot, out), projectRoot);
			bbSessionMap.put(projectRoot, bbs);
		}

		return bbs;
	}

	public static void clearBBSession(String projectRoot) {
		bbSessionMap.remove(projectRoot);
	}

	/**
	 * Get or create a BitBake session passing in ProjectInfo
	 * 
	 * @param pinfo
	 * @return
	 * @throws IOException
	 */
	public static BBSession getBBSession(String projectRoot) throws IOException {
		if (bbSessionMap == null) {
			bbSessionMap = new Hashtable();
		}

		BBSession bbs = (BBSession) bbSessionMap.get(projectRoot);

		if (bbs == null) {
			bbs = new BBSession(getShellSession(projectRoot, null), projectRoot);
			bbSessionMap.put(projectRoot, bbs);
		}

		return bbs;
	}

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

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	public static ProjectInfo getProjInfo(String location) throws CoreException, InvocationTargetException, InterruptedException {
		if (projInfoMap == null) {
			projInfoMap = new Hashtable();
		}

		ProjectInfo pi = (ProjectInfo) projInfoMap.get(location);

		if (pi == null) {
			pi = new ProjectInfo();
			pi.setLocation(location);
			try {
				pi.setInitScriptPath(ProjectInfoHelper.getInitScriptPath(location));
			} catch (IOException e) {
				throw new InvocationTargetException(e);
			}
		}

		return pi;
	}

	/**
	 * @param absolutePath
	 * @return a cached shell session for a given project root.
	 * @throws IOException
	 */
	private static ShellSession getShellSession(String absolutePath, Writer out) throws IOException {
		if (shellMap == null) {
			shellMap = new Hashtable();
		}

		try {
			ShellSession ss = (ShellSession) shellMap.get(absolutePath);

			if (ss == null) {
				ss = new ShellSession(ShellSession.SHELL_TYPE_BASH, new File(absolutePath), ProjectInfoHelper.getInitScriptPath(absolutePath), out);
			}

			return ss;
		} catch (IOException e) {
			Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Invalid bitbake commander project at " + absolutePath));
			return null;
		}
	}

	public static void putProjInfo(String location, ProjectInfo pinfo) {
		if (projInfoMap == null) {
			projInfoMap = new Hashtable();
		}

		projInfoMap.put(location, pinfo);
	}

	/**
	 * The constructor
	 */
	public Activator() {
		this.getImageRegistry().put(IMAGE_VARIABLE, Activator.getImageDescriptor("icons/variable.gif"));
		this.getImageRegistry().put(IMAGE_FUNCTION, Activator.getImageDescriptor("icons/function.gif"));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Reset a configuration
	 * 
	 * @param path
	 */
	public static void resetBBSession(String path) {
		shellMap.remove(path);
		bbSessionMap.remove(path);
	}
}