Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 739f84e21353c71e344648f1332065afaaec3026 (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
/*****************************************************************************
 * Copyright (c) 2016 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.emf.internal.resource.index;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.util.concurrent.Callable;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.papyrus.infra.emf.resource.index.WorkspaceModelIndex;

import com.google.common.util.concurrent.ListenableFuture;

/**
 * Internal implementation details of a {@link WorkspaceModelIndex}.
 */
public abstract class InternalModelIndex {

	private final QualifiedName indexKey;
	private final int maxIndexJobs;

	/** My manager. */
	private IndexManager manager;

	/** A class loader that knows the classes of the owner (bundle) context. */
	private ClassLoader ownerClassLoader;

	/**
	 * Initializes me.
	 */
	public InternalModelIndex(QualifiedName indexKey, int maxIndexJobs) {
		super();

		this.indexKey = indexKey;
		this.maxIndexJobs = maxIndexJobs;
	}

	/**
	 * Initializes me.
	 */
	public InternalModelIndex(QualifiedName indexKey) {
		this(indexKey, 0);
	}

	public final QualifiedName getIndexKey() {
		return indexKey;
	}

	public final int getMaxIndexJobs() {
		return maxIndexJobs;
	}

	protected final IContentType[] getContentTypes(IFile file) {
		return manager.getContentTypes(file);
	}

	/**
	 * Obtains an asynchronous future result that is scheduled to run after
	 * any pending indexing work has completed.
	 * 
	 * @param callable
	 *            the operation to schedule
	 * 
	 * @return the future result of the operation
	 */
	protected <V> ListenableFuture<V> afterIndex(final Callable<V> callable) {
		return manager.afterIndex(this, callable);
	}

	void setOwnerClassLoader(ClassLoader ownerClassLoader) {
		this.ownerClassLoader = ownerClassLoader;
	}

	protected final ObjectInputStream createObjectInput(InputStream underlying) throws IOException {
		return (ownerClassLoader == null)
				? new ObjectInputStream(underlying)
				: new ObjectInputStream(underlying) {
					@Override
					protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
						return Class.forName(desc.getName(), true, ownerClassLoader);
					}
				};
	}

	protected abstract void dispose();

	void start(IndexManager manager) {
		this.manager = manager;
		start();
	}

	protected abstract void start();

	protected abstract boolean match(IFile file);

	protected abstract void process(IFile file) throws CoreException;

	protected abstract void remove(IProject project, IFile file) throws CoreException;

	protected abstract void remove(IProject project) throws CoreException;
}

Back to the top