Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 33659d8145be84da7b2b5d2b25fcd6f33a8bfe21 (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
/*******************************************************************************
 * Copyright (c) 2011, 2015 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.model;

import static org.eclipse.tcf.te.tcf.locator.model.ModelManager.getPeerModel;

import java.beans.PropertyChangeEvent;
import java.io.File;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.Assert;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IChannel.IChannelListener;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IFileSystem;
import org.eclipse.tcf.te.core.interfaces.IConnectable;
import org.eclipse.tcf.te.core.interfaces.IPropertyChangeProvider;
import org.eclipse.tcf.te.runtime.model.ContainerModelNode;
import org.eclipse.tcf.te.runtime.model.factory.Factory;
import org.eclipse.tcf.te.runtime.model.interfaces.factory.IFactory;
import org.eclipse.tcf.te.tcf.core.model.interfaces.services.IModelService;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IResultOperation;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.runtime.IFSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.runtime.IRuntimeModel;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.UserAccount;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.OpParsePath;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.CacheManager;
import org.eclipse.tcf.te.tcf.filesystem.core.nls.Messages;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;

/**
 * The file system model implementation.
 */
public final class RuntimeModel extends ContainerModelNode implements IRuntimeModel, IChannelListener {

	private final IPeerNode fPeerNode;
	private final FSTreeNode fRoot;
	private final UserAccount fUserAccount;
	private IChannel fChannel;
	private IFileSystem fFileSystem;

    /**
	 * Create a File System ModelManager.
	 */
	public RuntimeModel(IPeerNode peerNode, IChannel channel, IFileSystem fileSystem, UserAccount userAccount) {
		fPeerNode = peerNode;
		fChannel = channel;
		fFileSystem = fileSystem;
		fUserAccount = userAccount;
		fRoot = new FSTreeNode(this, Messages.FSTreeNodeContentProvider_rootNodeLabel);
		channel.addChannelListener(this);
	}

    @Override
    protected boolean checkThreadAccess() {
        return Protocol.isDispatchThread();
    }

    @Override
    public void onChannelOpened() {
    }

    @Override
    public void congestionLevel(int level) {
    }

    @Override
    public void onChannelClosed(Throwable error) {
    	ModelManager.disposeRuntimeModel(fPeerNode);
    }

    @Override
    public void dispose() {
        Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
        fFileSystem = null;
    }

    @Override
    public boolean isDisposed() {
        Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
        return fFileSystem == null;
    }


    @Override
    @SuppressWarnings("unchecked")
    public <V extends IModelService> V getService(Class<V> serviceInterface) {
        Assert.isNotNull(serviceInterface);
        return (V)getAdapter(serviceInterface);
    }

    @Override
    public Object getAdapter(Class adapter) {
        if (IPeerNode.class.isAssignableFrom(adapter) || IConnectable.class.isAssignableFrom(adapter)) {
            final AtomicReference<IPeerNode> peerNode = new AtomicReference<IPeerNode>();
            Protocol.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    peerNode.set(getPeerNode());
                }
            });
            return peerNode.get();
        }

        return super.getAdapter(adapter);
    }

    @Override
    public void setFactory(IFactory factory) {
    }

    @Override
    public IFactory getFactory() {
        return Factory.getInstance();
    }

    @Override
    public IPeerNode getPeerNode() {
        return fPeerNode;
    }

    /* (non-Javadoc)
     * @see org.eclipse.tcf.te.runtime.model.ModelNode#toString()
     */
    @Override
    public String toString() {
        if (isDisposed()) {
            return "*DISPOSED* : " + super.toString(); //$NON-NLS-1$
        }
        return super.toString();
    }

	@Override
	public FSTreeNode getRoot() {
		return fRoot;
	}

	public UserAccount getUserAccount() {
		return fUserAccount;
	}

	public void firePropertyChanged(PropertyChangeEvent propertyChangeEvent) {
		IPropertyChangeProvider provider = (IPropertyChangeProvider) fPeerNode.getAdapter(IPropertyChangeProvider.class);
		if (provider != null)
			provider.firePropertyChange(propertyChangeEvent);
	}

	@Override
	public IResultOperation<IFSTreeNode> operationRestoreFromPath(String path) {
		return new OpParsePath(fPeerNode, path);
	}

	public static IPeerNode getPeerFromPath(String path) {
		String cacheRoot = CacheManager.getCacheRoot().getAbsolutePath();
		if (!path.startsWith(cacheRoot))
			return null;

		path = path.substring(cacheRoot.length() + 1);
		int slash = path.indexOf(File.separator);
		if (slash == -1)
			return null;

		String peerId = path.substring(0, slash);
		peerId = peerId.replace(CacheManager.PATH_ESCAPE_CHAR, ':');

		for (IPeerNode peer : getPeerModel().getPeerNodes()) {
			if (peerId.equals(peer.getPeerId()))
				return peer;
		}
		return null;
	}

	@Override
	public IChannel getChannel() {
		return fChannel;
	}

	public IFileSystem getFileSystem() {
		return fFileSystem;
	}
}

Back to the top