Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 60fef30fcf24ccc2f0256402573b0cd2860e40d1 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.core.internal.plugin;

import java.util.HashMap;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.jpt.common.core.internal.InternalJptWorkspace;
import org.eclipse.jpt.common.core.internal.utility.JptPlugin;

public class JptCommonCorePlugin
	extends JptPlugin
{
	// NB: the plug-in must be synchronized whenever accessing any of this state
	private final HashMap<IWorkspace, InternalJptWorkspace> jptWorkspaces = new HashMap<IWorkspace, InternalJptWorkspace>();


	// ********** singleton **********

	private static volatile JptCommonCorePlugin INSTANCE;

	/**
	 * Return the singleton Dali common core plug-in.
	 */
	public static JptCommonCorePlugin instance() {
		return INSTANCE;
	}


	// ********** Dali plug-in **********

	public JptCommonCorePlugin() {
		super();
	}

	@Override
	protected void setInstance(JptPlugin plugin) {
		INSTANCE = (JptCommonCorePlugin) plugin;
	}

	@Override
	protected void stop_() throws Exception {
		try {
			for (InternalJptWorkspace jptWorkspace : this.jptWorkspaces.values()) {
				try {
					jptWorkspace.stop();
				} catch (Throwable ex) {
					this.logError(ex);  // keep going
				}
			}
			this.jptWorkspaces.clear();
		} finally {
			super.stop_();
		}
	}


	// ********** Dali workspaces **********

	/**
	 * Return the Dali workspace corresponding to the specified Eclipse workspace.
	 * <p>
	 * The preferred way to retrieve a Dali workspace is via the Eclipse
	 * adapter framework:
	 * <pre>
	 * JptWorkspace jptWorkspace = (JptWorkspace) ResourcesPlugin.getWorkspace().getAdapter(JptWorkspace.class)
	 * </pre>
	 * @see org.eclipse.jpt.common.core.internal.WorkspaceAdapterFactory#getJptWorkspace(IWorkspace)
	 */
	public synchronized InternalJptWorkspace getJptWorkspace(IWorkspace workspace) {
		InternalJptWorkspace jptWorkspace = this.jptWorkspaces.get(workspace);
		if ((jptWorkspace == null) && this.isActive()) {
			jptWorkspace = this.buildJptWorkspace(workspace);
			this.jptWorkspaces.put(workspace, jptWorkspace);
		}
		return jptWorkspace;
	}

	private InternalJptWorkspace buildJptWorkspace(IWorkspace workspace) {
		return new InternalJptWorkspace(workspace);
	}
}

Back to the top