Skip to content
This repository was archived by the owner on Feb 21, 2019. It is now read-only.

Commit 6929e04

Browse files
committed
Separated the logic of file system sync from RepositoryAdapter
1 parent f06192c commit 6929e04

13 files changed

+58
-212
lines changed

org.eclipse.flux.core/src/org/eclipse/flux/core/Activator.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.eclipse.flux.client.config.SocketIOFluxConfig;
3838
import org.eclipse.flux.core.internal.CloudSyncMetadataListener;
3939
import org.eclipse.flux.core.util.ExceptionUtil;
40-
import org.eclipse.flux.watcher.core.Repository;
4140
import org.eclipse.flux.watcher.core.RepositoryModule;
4241
import org.eclipse.flux.watcher.fs.JDKProjectModule;
4342
import org.osgi.framework.BundleContext;
@@ -71,8 +70,6 @@ public class Activator extends Plugin {
7170
private IRepositoryListener repositoryListener;
7271
private IResourceChangeListener workspaceListener;
7372

74-
private Repository fluxRepository;
75-
7673
private final IChannelListener SERVICE_STARTER = new IChannelListener() {
7774
@Override
7875
public void connected(String userChannel) {
@@ -117,7 +114,6 @@ public void start(BundleContext context) throws Exception {
117114
final String userChannel = lazyStart ? MessageConstants.SUPER_USER : channel;
118115

119116
Injector injector = Guice.createInjector(new RepositoryModule(), new JDKProjectModule());
120-
fluxRepository = injector.getInstance(Repository.class);
121117
//Connecting to channel done asynchronously. To avoid blocking plugin state initialization.
122118
FluxClient.DEFAULT_INSTANCE.getExecutor().execute(new Runnable() {
123119
@Override
@@ -174,7 +170,7 @@ public void stop(BundleContext context) throws Exception {
174170
}
175171

176172
private void initCoreService(String userChannel) throws CoreException {
177-
repository = new RepositoryAdapter(messageConnector, fluxRepository, userChannel);
173+
repository = new RepositoryAdapter(messageConnector, userChannel);
178174
liveEditCoordinator = new LiveEditCoordinator(messageConnector);
179175

180176
IWorkspace workspace = ResourcesPlugin.getWorkspace();

org.eclipse.flux.core/src/org/eclipse/flux/core/RepositoryAdapter.java

Lines changed: 22 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -10,158 +10,67 @@
1010
*******************************************************************************/
1111
package org.eclipse.flux.core;
1212

13-
import java.util.ArrayList;
1413
import java.util.Collection;
1514
import java.util.HashSet;
1615
import java.util.Set;
1716
import java.util.concurrent.ConcurrentLinkedDeque;
1817

19-
import org.eclipse.core.resources.IMarker;
2018
import org.eclipse.core.resources.IMarkerDelta;
2119
import org.eclipse.core.resources.IProject;
2220
import org.eclipse.core.resources.IResource;
2321
import org.eclipse.core.resources.IResourceDelta;
24-
import org.eclipse.flux.core.handlers.MetadataRequestHandler;
25-
import org.eclipse.flux.core.handlers.ProjectRequestHandler;
26-
import org.eclipse.flux.core.handlers.ProjectResponseHandler;
27-
import org.eclipse.flux.core.handlers.ProjectsResponseHandler;
28-
import org.eclipse.flux.core.handlers.ResourceChangedHandler;
29-
import org.eclipse.flux.core.handlers.ResourceCreatedHandler;
30-
import org.eclipse.flux.core.handlers.ResourceDeletedHandler;
31-
import org.eclipse.flux.core.handlers.ResourceRequestHandler;
32-
import org.eclipse.flux.core.handlers.ResourceResponseHandler;
33-
import org.eclipse.flux.core.util.JSONUtils;
34-
import org.eclipse.flux.core.util.Utils;
35-
import org.eclipse.flux.client.IMessageHandler;
3622
import org.eclipse.flux.client.MessageConnector;
37-
import org.eclipse.flux.client.MessageConstants;
38-
import org.eclipse.flux.watcher.core.Repository;
23+
import org.eclipse.flux.core.sync.FluxSystemSync;
24+
import org.eclipse.flux.core.util.Utils;
3925
import org.eclipse.flux.watcher.core.RepositoryEvent;
40-
import org.eclipse.flux.watcher.core.RepositoryEventBus;
4126
import org.eclipse.flux.watcher.core.RepositoryListener;
42-
import org.eclipse.flux.watcher.core.Resource;
4327
import org.eclipse.flux.watcher.core.spi.Project;
44-
import org.json.JSONArray;
45-
import org.json.JSONObject;
4628

4729
/**
4830
* @author Martin Lippert
4931
*/
50-
public class RepositoryAdapter implements IRepositoryCallback{
51-
private static int GET_PROJECT_CALLBACK = "Repository - getProjectCallback".hashCode();
52-
private static int GET_RESOURCE_CALLBACK = "Repository - getResourceCallback".hashCode();
53-
54-
private String username;
32+
public class RepositoryAdapter implements RepositoryListener{
33+
private FluxSystemSync systemSync;
5534

5635
private Collection<IRepositoryListener> repositoryListeners;
5736

58-
private RepositoryEventBus repositoryEventBus;
59-
private Repository repository;
60-
private MessageConnector messageConnector;
61-
private Collection<IMessageHandler> messageHandlers;
62-
63-
public RepositoryAdapter(MessageConnector messageConnector, Repository repository, String user) {
64-
this.repository = repository;
65-
this.repositoryEventBus = repository.repositoryEventBus();
66-
this.messageConnector = messageConnector;
67-
this.username = user;
68-
this.messageHandlers = new ArrayList<>();
37+
public RepositoryAdapter(MessageConnector messageConnector, String user) {
38+
this.systemSync = new FluxSystemSync(messageConnector, user, this);
6939
this.repositoryListeners = new ConcurrentLinkedDeque<>();
70-
71-
addMessageHandler(new MetadataRequestHandler(this));
72-
addMessageHandler(new ProjectsResponseHandler(this));
73-
addMessageHandler(new ProjectRequestHandler(this));
74-
addMessageHandler(new ProjectResponseHandler(this, GET_PROJECT_CALLBACK));
75-
addMessageHandler(new ResourceRequestHandler(this));
76-
addMessageHandler(new ResourceResponseHandler(this, GET_RESOURCE_CALLBACK));
77-
addMessageHandler(new ResourceCreatedHandler(this, GET_RESOURCE_CALLBACK));
78-
addMessageHandler(new ResourceChangedHandler(this, GET_RESOURCE_CALLBACK));
79-
addMessageHandler(new ResourceDeletedHandler(this));
80-
81-
this.repositoryEventBus.addRepositoryListener(new RepositoryListener() {
82-
@Override
83-
public void onEvent(RepositoryEvent event) throws Exception {
84-
Project project = event.project();
85-
Resource resource = event.resource();
86-
switch (event.type()) {
87-
case PROJECT_RESOURCE_CREATED:
88-
JSONObject createdStoredMessage = new JSONObject();
89-
createdStoredMessage.put(MessageConstants.USERNAME, RepositoryAdapter.this.username);
90-
createdStoredMessage.put(MessageConstants.PROJECT_NAME, project.id());
91-
createdStoredMessage.put(MessageConstants.RESOURCE, resource.path());
92-
createdStoredMessage.put(MessageConstants.TIMESTAMP, resource.timestamp());
93-
createdStoredMessage.put(MessageConstants.HASH, resource.hash());
94-
createdStoredMessage.put(MessageConstants.TYPE, resource.type().name().toLowerCase());
95-
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_CREATED, createdStoredMessage);
96-
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_STORED, createdStoredMessage);
97-
break;
98-
case PROJECT_RESOURCE_MODIFIED:
99-
JSONObject modifiedStoredMessage = new JSONObject();
100-
modifiedStoredMessage.put(MessageConstants.USERNAME, RepositoryAdapter.this.username);
101-
modifiedStoredMessage.put(MessageConstants.PROJECT_NAME, project.id());
102-
modifiedStoredMessage.put(MessageConstants.RESOURCE, resource.path());
103-
modifiedStoredMessage.put(MessageConstants.TIMESTAMP, resource.timestamp());
104-
modifiedStoredMessage.put(MessageConstants.HASH, resource.hash());
105-
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_CHANGED, modifiedStoredMessage);
106-
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_STORED, modifiedStoredMessage);
107-
break;
108-
case PROJECT_RESOURCE_DELETED:
109-
JSONObject message = new JSONObject();
110-
message.put(MessageConstants.USERNAME, RepositoryAdapter.this.username);
111-
message.put(MessageConstants.PROJECT_NAME, project.id());
112-
message.put(MessageConstants.RESOURCE, resource.path());
113-
message.put(MessageConstants.TIMESTAMP, resource.timestamp());
114-
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_DELETED, message);
115-
break;
116-
default:
117-
break;
118-
}
119-
}
120-
});
12140
}
12241

12342
public String getUsername() {
124-
return this.username;
43+
return systemSync.getUsername();
12544
}
12645

12746
public ConnectedProject getProject(IProject project) {
128-
return new ConnectedProject(repository.getProject(project.getName()));
47+
return getProject(project.getName());
12948
}
13049

13150
public ConnectedProject getProject(String projectName) {
132-
return new ConnectedProject(repository.getProject(projectName));
51+
return new ConnectedProject(systemSync.getWatcherProject(projectName));
13352
}
13453

13554
public boolean isConnected(IProject project) {
13655
return isConnected(project.getName());
13756
}
13857

13958
public boolean isConnected(String projectName) {
140-
Project project = this.repository.getProject(projectName);
141-
return this.repository.getSynchronizedProjects().contains(project);
59+
return systemSync.isProjectConnected(projectName);
14260
}
14361

14462
public void addProject(IProject project) {
145-
this.repository.addProject(project.getName(), project.getLocationURI().getPath());
146-
notifyProjectConnected(project);
147-
sendProjectConnectedMessage(project.getName());
148-
syncConnectedProject(project.getName());
63+
this.systemSync.addProject(project.getName(), project.getLocationURI().getPath());
64+
notifyProjectConnected(project);
14965
}
15066

15167
public void removeProject(IProject project) {
152-
this.repository.removeProject(project.getName());
153-
notifyProjectDisonnected(project);
154-
JSONObject message = new JSONObject();
155-
try {
156-
message.put("username", this.username);
157-
message.put("project", project.getName());
158-
messageConnector.send("projectDisconnected", message);
159-
} catch (Exception e) {
160-
e.printStackTrace();
161-
}
68+
this.systemSync.removeProject(project.getName());
69+
notifyProjectDisonnected(project);
16270
}
71+
16372
public ConnectedProject[] getConnectedProjects() {
164-
Set<Project> projects = repository.getSynchronizedProjects();
73+
Set<Project> projects = systemSync.getSynchronizedProjects();
16574
Set<ConnectedProject> connectedProjects = new HashSet<>();
16675
for(Project project : projects){
16776
connectedProjects.add(new ConnectedProject(project));
@@ -173,27 +82,7 @@ public void metadataChanged(IResourceDelta delta) {
17382
IProject project = delta.getResource().getProject();
17483
IMarkerDelta[] markerDeltas = delta.getMarkerDeltas();
17584
if (project != null && isConnected(project) && markerDeltas != null && markerDeltas.length > 0) {
176-
sendMetadataUpdate(delta.getResource());
177-
}
178-
}
179-
180-
public void sendMetadataUpdate(IResource resource) {
181-
try {
182-
String project = resource.getProject().getName();
183-
String resourcePath = resource.getProjectRelativePath().toString();
184-
185-
JSONObject message = new JSONObject();
186-
message.put("username", this.username);
187-
message.put("project", project);
188-
message.put("resource", resourcePath);
189-
message.put("type", "marker");
190-
191-
IMarker[] markers = resource.findMarkers(null, true, IResource.DEPTH_INFINITE);
192-
JSONArray content = JSONUtils.toJSON(markers);
193-
message.put("metadata", content);
194-
messageConnector.send(IMessageHandler.METADATA_CHANGED, message);
195-
} catch (Exception e) {
196-
85+
systemSync.sendMetadataUpdate(delta.getResource());
19786
}
19887
}
19988

@@ -205,30 +94,6 @@ public void removeRepositoryListener(IRepositoryListener listener) {
20594
this.repositoryListeners.remove(listener);
20695
}
20796

208-
protected void syncConnectedProject(String projectName) {
209-
try {
210-
JSONObject message = new JSONObject();
211-
message.put("username", this.username);
212-
message.put("project", projectName);
213-
message.put("includeDeleted", true);
214-
message.put("callback_id", GET_PROJECT_CALLBACK);
215-
messageConnector.send("getProjectRequest", message);
216-
} catch (Exception e) {
217-
e.printStackTrace();
218-
}
219-
}
220-
221-
protected void sendProjectConnectedMessage(String projectName) {
222-
try {
223-
JSONObject message = new JSONObject();
224-
message.put("username", this.username);
225-
message.put("project", projectName);
226-
messageConnector.send("projectConnected", message);
227-
} catch (Exception e) {
228-
e.printStackTrace();
229-
}
230-
}
231-
23297
protected void notifyProjectConnected(IProject project) {
23398
for (IRepositoryListener listener : this.repositoryListeners) {
23499
listener.projectConnected(project);
@@ -248,36 +113,14 @@ protected void notifyResourceChanged(IResource resource) {
248113
}
249114

250115
public void dispose() {
251-
for(IMessageHandler messageHandler : messageHandlers){
252-
messageConnector.removeMessageHandler(messageHandler);
253-
}
254-
}
255-
256-
private void addMessageHandler(IMessageHandler messageHandler){
257-
this.messageConnector.addMessageHandler(messageHandler);
258-
this.messageHandlers.add(messageHandler);
116+
systemSync.dispose();
259117
}
260-
261-
@Override
262-
public void notifyResourceChanged(Resource resource, Project project) {
263-
notifyResourceChanged(Utils.getResourceByPath(project.id(), resource.path()));
264-
265-
}
266118

267119
@Override
268-
public void sendMessage(String messageType, JSONObject content) throws Exception {
269-
messageConnector.send(messageType, content);
120+
public void onEvent(RepositoryEvent event) throws Exception {
121+
String projectName = event.project().id();
122+
String path = event.resource().path();
123+
notifyResourceChanged(Utils.getResourceByPath(projectName, path));
270124
}
271125

272-
@Override
273-
public Project getWatcherProject(String projectName) {
274-
return repository.getProject(projectName);
275-
}
276-
277-
@Override
278-
public Set<Project> getSynchronizedProjects() {
279-
return repository.getSynchronizedProjects();
280-
}
281-
282-
283126
}

org.eclipse.flux.core/src/org/eclipse/flux/core/handlers/AbstractMsgHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import org.eclipse.flux.client.MessageConstants;
44
import org.eclipse.flux.client.MessageHandler;
5-
import org.eclipse.flux.core.IRepositoryCallback;
5+
import org.eclipse.flux.core.sync.ISystemSync;
66
import org.eclipse.flux.watcher.core.Resource;
77
import org.eclipse.flux.watcher.core.Resource.ResourceType;
88
import org.json.JSONObject;
99

1010
public abstract class AbstractMsgHandler extends MessageHandler {
11-
protected IRepositoryCallback repositoryCallback;
11+
protected ISystemSync repositoryCallback;
1212

13-
public AbstractMsgHandler(IRepositoryCallback repositoryCallback, String type) {
13+
public AbstractMsgHandler(ISystemSync repositoryCallback, String type) {
1414
super(type);
1515
this.repositoryCallback = repositoryCallback;
1616
}

org.eclipse.flux.core/src/org/eclipse/flux/core/handlers/MetadataRequestHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.eclipse.core.resources.IResource;
44
import org.eclipse.flux.client.MessageConstants;
5-
import org.eclipse.flux.core.IRepositoryCallback;
5+
import org.eclipse.flux.core.sync.ISystemSync;
66
import org.eclipse.flux.core.util.JSONUtils;
77
import org.eclipse.flux.core.util.Utils;
88
import org.eclipse.flux.watcher.core.Resource;
@@ -11,7 +11,7 @@
1111

1212
public class MetadataRequestHandler extends AbstractMsgHandler {
1313

14-
public MetadataRequestHandler(IRepositoryCallback repositoryCallback) {
14+
public MetadataRequestHandler(ISystemSync repositoryCallback) {
1515
super(repositoryCallback, GET_METADATA_REQUEST);
1616
}
1717

org.eclipse.flux.core/src/org/eclipse/flux/core/handlers/ProjectRequestHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package org.eclipse.flux.core.handlers;
22

33
import org.eclipse.flux.client.MessageConstants;
4-
import org.eclipse.flux.core.IRepositoryCallback;
4+
import org.eclipse.flux.core.sync.ISystemSync;
55
import org.eclipse.flux.watcher.core.Resource;
66
import org.eclipse.flux.watcher.core.spi.Project;
77
import org.json.JSONArray;
88
import org.json.JSONObject;
99

1010
public class ProjectRequestHandler extends AbstractMsgHandler {
1111

12-
public ProjectRequestHandler(IRepositoryCallback repositoryCallback) {
12+
public ProjectRequestHandler(ISystemSync repositoryCallback) {
1313
super(repositoryCallback, GET_PROJECT_REQUEST);
1414
}
1515

org.eclipse.flux.core/src/org/eclipse/flux/core/handlers/ProjectResponseHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.eclipse.flux.core.handlers;
22

33
import org.eclipse.flux.client.MessageConstants;
4-
import org.eclipse.flux.core.IRepositoryCallback;
4+
import org.eclipse.flux.core.sync.ISystemSync;
55
import org.eclipse.flux.watcher.core.Resource;
66
import org.eclipse.flux.watcher.core.spi.Project;
77
import org.json.JSONArray;
@@ -10,7 +10,7 @@
1010
public class ProjectResponseHandler extends AbstractMsgHandler {
1111
private int callbackID;
1212

13-
public ProjectResponseHandler(IRepositoryCallback repositoryCallback, int callbackID) {
13+
public ProjectResponseHandler(ISystemSync repositoryCallback, int callbackID) {
1414
super(repositoryCallback, GET_PROJECT_RESPONSE);
1515
this.callbackID = callbackID;
1616
}

0 commit comments

Comments
 (0)