Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ad9460869c6aaea3f834b34636a196a8e20d1a2d (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
/*******************************************************************************
 * 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.tcf.filesystem.core.internal.operations;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IFileSystem;
import org.eclipse.tcf.services.IFileSystem.DoneStat;
import org.eclipse.tcf.services.IFileSystem.FileAttrs;
import org.eclipse.tcf.services.IFileSystem.FileSystemException;
import org.eclipse.tcf.te.tcf.core.Tcf;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.exceptions.TCFException;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.exceptions.TCFFileSystemException;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.nls.Messages;

/**
 * The base operation class for creating a file or a folder in the file system of Target
 * Explorer.
 */
public abstract class OpCreate extends Operation {
	// The folder in which a file/folder is going to be created.
	final protected FSTreeNode folder;
	// The node that is created after the operation.
	protected FSTreeNode node;
	// The name of the node to be created.
	final protected String name;

	/**
	 * Create an FSCreate instance with the specified folder and the name of the new node.
	 *
	 * @param folder The folder in which the new node is going to be created. Must not be <code>null</code>.
	 * @param name The new node's name. Must not be <code>null</code>.
	 */
	public OpCreate(FSTreeNode folder, String name) {
		Assert.isNotNull(folder);
		this.folder = folder;
		Assert.isNotNull(name);
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.Operation#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
		super.run(monitor);
		IChannel channel = null;
		try {
			channel = openChannel(folder.peerNode.getPeer());
			monitor.worked(1);
			IFileSystem service = getBlockingFileSystem(channel);
			if (service != null) {
				if (!folder.childrenQueried) {
					// If the children of folder is not queried, load it first.
					loadChildren(folder, service);
					monitor.worked(1);
				}
				monitor.worked(1);
				create(service);
				monitor.worked(1);
				addNode(service);
				monitor.worked(1);
				refresh(service);
				monitor.worked(1);
			}
			else {
				String message = NLS.bind(Messages.Operation_NoFileSystemError, folder.peerNode.getPeerId());
				throw new TCFFileSystemException(IStatus.ERROR, message);
			}
		}
		catch (TCFException e) {
			throw new InvocationTargetException(e, e.getMessage());
		}
		finally {
			if (channel != null) Tcf.getChannelManager().closeChannel(channel);
			monitor.done();
		}
	}

	/**
	 * Refresh new node's stat using the file system service.
	 *
	 * @param service The file system service.
	 * @throws TCFFileSystemException Thrown when refreshing the new node's stat.
	 */
	void refresh(final IFileSystem service) throws TCFFileSystemException {
		if (node != null) {
			final TCFFileSystemException[] errors = new TCFFileSystemException[1];
			String path = node.getLocation(true);
			service.stat(path, new DoneStat() {
				@Override
				public void doneStat(IToken token, FileSystemException error, FileAttrs attrs) {
					if (error == null) {
						if (node != null) node.setAttributes(attrs);
					}
					else {
						errors[0] = newTCFException(IStatus.WARNING, error);
					}
				}
			});
			if (errors[0] != null) {
				throw errors[0];
			}
		}
	}

	/**
	 * Add the new node to the folder and its RuntimeModel.
	 *
	 * @param service The file system service to be used.
	 * @throws TCFFileSystemException Thrown when adding.
	 */
	void addNode(final IFileSystem service) throws TCFFileSystemException {
		if (Protocol.isDispatchThread()) {
			node = newTreeNode();
			folder.addChild(node);
		}
		else {
			final TCFFileSystemException[] errors = new TCFFileSystemException[1];
			Protocol.invokeAndWait(new Runnable() {
				@Override
				public void run() {
					try {
						addNode(service);
					}
					catch (TCFFileSystemException e) {
						errors[0] = e;
					}
				}
			});
			if (errors[0] != null) throw errors[0];
		}
	}

	/**
	 * Create the new node, either a directory node or a file node.
	 *
	 * @return The new node.
	 */
	protected abstract FSTreeNode newTreeNode();

	/**
	 * Create the node in the target system.
	 *
	 * @param service The file system service used to create the new node.
	 * @throws TCFFileSystemException Thrown when creating the node.
	 */
	protected abstract void create(IFileSystem service) throws TCFFileSystemException;

	/**
	 * Get the node that is created by this operation.
	 *
	 * @return the node created.
	 */
	public FSTreeNode getNode() {
		return node;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation#getName()
	 */
	@Override
    public String getName() {
	    return NLS.bind(Messages.OpCreate_TaskName, name);
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation#getTotalWork()
	 */
	@Override
    public int getTotalWork() {
	    return folder.childrenQueried ? 5 : 6;
    }
}

Back to the top