Skip to main content
summaryrefslogtreecommitdiffstats
blob: 252373c80f3a22781cb8b8a9f474271bc006b669 (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
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
/*******************************************************************************
 * Copyright (c) 2004, 2012 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.tasks.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;

/**
 * @author Steffen Pingel
 * @author Benjamin Muskalla
 * @since 3.8
 */
public abstract class RepositoryClientManager<T, C extends Serializable> implements IRepositoryListener {

	/**
	 * Delegates to <code>repositoryConfigurationClass</code>'s class loader for accessing classes.
	 */
	private class OsgiAwareObjectInputStream extends ObjectInputStream {

		public OsgiAwareObjectInputStream(InputStream in) throws IOException {
			super(in);
		}

		@Override
		protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
			try {
				return Class.forName(desc.getName(), true, repositoryConfigurationClass.getClassLoader());
			} catch (Exception e) {
				return super.resolveClass(desc);
			}
		}

	}

	private final Map<String, T> clientByUrl = new HashMap<String, T>();

	private final Map<String, C> respoitoryConfigurationByUrl = new HashMap<String, C>();

	private final File cacheFile;

	private TaskRepositoryLocationFactory locationFactory;

	private final Class<C> repositoryConfigurationClass;

	public RepositoryClientManager(File cacheFile, Class<C> repositoryConfigurationClass) {
		Assert.isNotNull(cacheFile);
		this.cacheFile = cacheFile;
		this.repositoryConfigurationClass = repositoryConfigurationClass;
		readCache();
	}

	public synchronized T getClient(TaskRepository repository) {
		Assert.isNotNull(repository);
		T client = clientByUrl.get(repository.getRepositoryUrl());
		if (client == null) {
			C data = respoitoryConfigurationByUrl.get(repository.getRepositoryUrl());
			if (data == null) {
				data = createRepositoryConfiguration(repository);
				respoitoryConfigurationByUrl.put(repository.getRepositoryUrl(), data);
			}

			client = createClient(repository, data);
			clientByUrl.put(repository.getRepositoryUrl(), client);
		}
		return client;
	}

	protected C createRepositoryConfiguration(TaskRepository repository) {
		try {
			return repositoryConfigurationClass.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	protected abstract T createClient(TaskRepository taskRepository, C data);

	public void repositoriesRead() {
		// ignore
	}

	public synchronized void repositoryAdded(TaskRepository repository) {
		removeClient(repository);
		respoitoryConfigurationByUrl.remove(repository.getRepositoryUrl());
	}

	private void removeClient(TaskRepository repository) {
		clientByUrl.remove(repository.getRepositoryUrl());
	}

	public synchronized void repositoryRemoved(TaskRepository repository) {
		removeClient(repository);
		respoitoryConfigurationByUrl.remove(repository.getRepositoryUrl());
	}

	public synchronized void repositorySettingsChanged(TaskRepository repository) {
		removeClient(repository);
	}

	@SuppressWarnings("unchecked")
	public void readCache() {
		if (cacheFile == null || !cacheFile.exists()) {
			return;
		}

		ObjectInputStream in = null;
		try {
			in = new OsgiAwareObjectInputStream(new FileInputStream(cacheFile));
			int size = in.readInt();
			for (int i = 0; i < size; i++) {
				String url = (String) in.readObject();
				C data = (C) in.readObject();
				if (url != null && data != null) {
					respoitoryConfigurationByUrl.put(url, data);
				}
			}
		} catch (Throwable e) {
			handleError("The respository configuration cache could not be read", e); //$NON-NLS-1$
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// ignore
				}
			}
		}

	}

	protected void handleError(String message, Throwable e) {
		StatusHandler.log(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN, message, e));
	}

	public void writeCache() {
		if (cacheFile == null) {
			return;
		}

		ObjectOutputStream out = null;
		try {
			out = new ObjectOutputStream(new FileOutputStream(cacheFile));
			out.writeInt(respoitoryConfigurationByUrl.size());
			for (String url : respoitoryConfigurationByUrl.keySet()) {
				out.writeObject(url);
				out.writeObject(respoitoryConfigurationByUrl.get(url));
			}
		} catch (IOException e) {
			handleError("The respository configuration cache could not be written", e); //$NON-NLS-1$
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					// ignore
				}
			}
		}
	}

	public TaskRepositoryLocationFactory getLocationFactory() {
		return locationFactory;
	}

	public void setLocationFactory(TaskRepositoryLocationFactory locationFactory) {
		this.locationFactory = locationFactory;
	}

	public void repositoryUrlChanged(TaskRepository repository, String oldUrl) {
		// ignore
	}

}

Back to the top