Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eba38a6272b2196bd2fe27d8456db10d74d9e581 (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
/*******************************************************************************
 * Copyright (c) 2011, 2014 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.persistence.services;

import java.io.IOException;
import java.net.URI;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.tcf.te.runtime.persistence.PersistenceManager;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistableURIProvider;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
import org.eclipse.tcf.te.runtime.services.AbstractService;

/**
 * Persistence service implementation.
 */
public class URIPersistenceService extends AbstractService implements IURIPersistenceService {

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService#write(java.lang.Object, java.net.URI)
	 */
	@Override
	public void write(Object context, URI uri) throws IOException {
		Assert.isNotNull(context);

		uri = uri != null ? uri : getURI(context);

		// Determine the persistence delegate
		IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(context, uri);
		// If the persistence delegate could not be determined, throw an IOException
		if (delegate == null) {
			throw new IOException("The persistence delegate for context '" + context.getClass().getName() + "' cannot be determined."); //$NON-NLS-1$ //$NON-NLS-2$
		}

		// Pass on to the delegate for writing
		delegate.write(context, uri);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService#read(java.lang.Object, java.net.URI)
	 */
	@Override
	public Object read(Object context, URI uri) throws IOException {
		Assert.isNotNull(context);

		uri = uri != null ? uri : getURI(context);

		// Determine the persistence delegate
		IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(context, uri);
		// If the persistence delegate could not be determined, throw an IOException
		if (delegate == null) {
			throw new IOException("The persistence delegate for context '" + context.getClass().getName() + "' cannot be determined."); //$NON-NLS-1$ //$NON-NLS-2$
		}

		// Pass on to the delegate for reading
		return delegate.read(context, uri);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService#delete(java.lang.Object, java.net.URI)
	 */
	@Override
	public boolean delete(Object context, URI uri) throws IOException {
		Assert.isNotNull(context);

		uri = uri != null ? uri : getURI(context);

		// Determine the persistence delegate
		IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(context, uri);
		// If the persistence delegate could not be determined, throw an IOException
		if (delegate == null) {
			throw new IOException("The persistence delegate for context '" + context.getClass().getName() + "' cannot be determined."); //$NON-NLS-1$ //$NON-NLS-2$
		}

		return delegate.delete(context, uri);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService#getURI(java.lang.Object)
	 */
	@Override
	public URI getURI(Object context) throws IOException {
		Assert.isNotNull(context);

		// Determine the persistable element for the given data object
		IPersistableURIProvider persistableURIProvider = context instanceof IPersistableURIProvider ? (IPersistableURIProvider)context : null;
		// If the element isn't a persistable by itself, try to adapt the element
		if (persistableURIProvider == null) {
			persistableURIProvider = context instanceof IAdaptable ? (IPersistableURIProvider) ((IAdaptable)context).getAdapter(IPersistableURIProvider.class) : null;
		}
		if (persistableURIProvider == null) {
			persistableURIProvider = (IPersistableURIProvider) Platform.getAdapterManager().getAdapter(context, IPersistableURIProvider.class);
		}

		// If the persistable could be still not determined, throw an IOException
		if (persistableURIProvider == null) {
			throw new IOException("'context' must be adaptable to IPersistableURIProvider."); //$NON-NLS-1$
		}

		URI uri = persistableURIProvider.getURI(context);

		if (uri == null) {
			throw new IOException("The URI cannot be determined for context '" + context.getClass().getName() + "'."); //$NON-NLS-1$ //$NON-NLS-2$
		}

		// Determine the URI
		return uri;
	}
}

Back to the top