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

Commit

Permalink
Separated the logic of file system sync from RepositoryAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
fjodorver committed Aug 23, 2016
1 parent f06192c commit 6929e04
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.eclipse.flux.client.config.SocketIOFluxConfig;
import org.eclipse.flux.core.internal.CloudSyncMetadataListener;
import org.eclipse.flux.core.util.ExceptionUtil;
import org.eclipse.flux.watcher.core.Repository;
import org.eclipse.flux.watcher.core.RepositoryModule;
import org.eclipse.flux.watcher.fs.JDKProjectModule;
import org.osgi.framework.BundleContext;
Expand Down Expand Up @@ -71,8 +70,6 @@ public class Activator extends Plugin {
private IRepositoryListener repositoryListener;
private IResourceChangeListener workspaceListener;

private Repository fluxRepository;

private final IChannelListener SERVICE_STARTER = new IChannelListener() {
@Override
public void connected(String userChannel) {
Expand Down Expand Up @@ -117,7 +114,6 @@ public void start(BundleContext context) throws Exception {
final String userChannel = lazyStart ? MessageConstants.SUPER_USER : channel;

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

private void initCoreService(String userChannel) throws CoreException {
repository = new RepositoryAdapter(messageConnector, fluxRepository, userChannel);
repository = new RepositoryAdapter(messageConnector, userChannel);
liveEditCoordinator = new LiveEditCoordinator(messageConnector);

IWorkspace workspace = ResourcesPlugin.getWorkspace();
Expand Down
201 changes: 22 additions & 179 deletions org.eclipse.flux.core/src/org/eclipse/flux/core/RepositoryAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,158 +10,67 @@
*******************************************************************************/
package org.eclipse.flux.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedDeque;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.flux.core.handlers.MetadataRequestHandler;
import org.eclipse.flux.core.handlers.ProjectRequestHandler;
import org.eclipse.flux.core.handlers.ProjectResponseHandler;
import org.eclipse.flux.core.handlers.ProjectsResponseHandler;
import org.eclipse.flux.core.handlers.ResourceChangedHandler;
import org.eclipse.flux.core.handlers.ResourceCreatedHandler;
import org.eclipse.flux.core.handlers.ResourceDeletedHandler;
import org.eclipse.flux.core.handlers.ResourceRequestHandler;
import org.eclipse.flux.core.handlers.ResourceResponseHandler;
import org.eclipse.flux.core.util.JSONUtils;
import org.eclipse.flux.core.util.Utils;
import org.eclipse.flux.client.IMessageHandler;
import org.eclipse.flux.client.MessageConnector;
import org.eclipse.flux.client.MessageConstants;
import org.eclipse.flux.watcher.core.Repository;
import org.eclipse.flux.core.sync.FluxSystemSync;
import org.eclipse.flux.core.util.Utils;
import org.eclipse.flux.watcher.core.RepositoryEvent;
import org.eclipse.flux.watcher.core.RepositoryEventBus;
import org.eclipse.flux.watcher.core.RepositoryListener;
import org.eclipse.flux.watcher.core.Resource;
import org.eclipse.flux.watcher.core.spi.Project;
import org.json.JSONArray;
import org.json.JSONObject;

/**
* @author Martin Lippert
*/
public class RepositoryAdapter implements IRepositoryCallback{
private static int GET_PROJECT_CALLBACK = "Repository - getProjectCallback".hashCode();
private static int GET_RESOURCE_CALLBACK = "Repository - getResourceCallback".hashCode();

private String username;
public class RepositoryAdapter implements RepositoryListener{
private FluxSystemSync systemSync;

private Collection<IRepositoryListener> repositoryListeners;

private RepositoryEventBus repositoryEventBus;
private Repository repository;
private MessageConnector messageConnector;
private Collection<IMessageHandler> messageHandlers;

public RepositoryAdapter(MessageConnector messageConnector, Repository repository, String user) {
this.repository = repository;
this.repositoryEventBus = repository.repositoryEventBus();
this.messageConnector = messageConnector;
this.username = user;
this.messageHandlers = new ArrayList<>();
public RepositoryAdapter(MessageConnector messageConnector, String user) {
this.systemSync = new FluxSystemSync(messageConnector, user, this);
this.repositoryListeners = new ConcurrentLinkedDeque<>();

addMessageHandler(new MetadataRequestHandler(this));
addMessageHandler(new ProjectsResponseHandler(this));
addMessageHandler(new ProjectRequestHandler(this));
addMessageHandler(new ProjectResponseHandler(this, GET_PROJECT_CALLBACK));
addMessageHandler(new ResourceRequestHandler(this));
addMessageHandler(new ResourceResponseHandler(this, GET_RESOURCE_CALLBACK));
addMessageHandler(new ResourceCreatedHandler(this, GET_RESOURCE_CALLBACK));
addMessageHandler(new ResourceChangedHandler(this, GET_RESOURCE_CALLBACK));
addMessageHandler(new ResourceDeletedHandler(this));

this.repositoryEventBus.addRepositoryListener(new RepositoryListener() {
@Override
public void onEvent(RepositoryEvent event) throws Exception {
Project project = event.project();
Resource resource = event.resource();
switch (event.type()) {
case PROJECT_RESOURCE_CREATED:
JSONObject createdStoredMessage = new JSONObject();
createdStoredMessage.put(MessageConstants.USERNAME, RepositoryAdapter.this.username);
createdStoredMessage.put(MessageConstants.PROJECT_NAME, project.id());
createdStoredMessage.put(MessageConstants.RESOURCE, resource.path());
createdStoredMessage.put(MessageConstants.TIMESTAMP, resource.timestamp());
createdStoredMessage.put(MessageConstants.HASH, resource.hash());
createdStoredMessage.put(MessageConstants.TYPE, resource.type().name().toLowerCase());
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_CREATED, createdStoredMessage);
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_STORED, createdStoredMessage);
break;
case PROJECT_RESOURCE_MODIFIED:
JSONObject modifiedStoredMessage = new JSONObject();
modifiedStoredMessage.put(MessageConstants.USERNAME, RepositoryAdapter.this.username);
modifiedStoredMessage.put(MessageConstants.PROJECT_NAME, project.id());
modifiedStoredMessage.put(MessageConstants.RESOURCE, resource.path());
modifiedStoredMessage.put(MessageConstants.TIMESTAMP, resource.timestamp());
modifiedStoredMessage.put(MessageConstants.HASH, resource.hash());
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_CHANGED, modifiedStoredMessage);
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_STORED, modifiedStoredMessage);
break;
case PROJECT_RESOURCE_DELETED:
JSONObject message = new JSONObject();
message.put(MessageConstants.USERNAME, RepositoryAdapter.this.username);
message.put(MessageConstants.PROJECT_NAME, project.id());
message.put(MessageConstants.RESOURCE, resource.path());
message.put(MessageConstants.TIMESTAMP, resource.timestamp());
RepositoryAdapter.this.messageConnector.send(IMessageHandler.RESOURCE_DELETED, message);
break;
default:
break;
}
}
});
}

public String getUsername() {
return this.username;
return systemSync.getUsername();
}

public ConnectedProject getProject(IProject project) {
return new ConnectedProject(repository.getProject(project.getName()));
return getProject(project.getName());
}

public ConnectedProject getProject(String projectName) {
return new ConnectedProject(repository.getProject(projectName));
return new ConnectedProject(systemSync.getWatcherProject(projectName));
}

public boolean isConnected(IProject project) {
return isConnected(project.getName());
}

public boolean isConnected(String projectName) {
Project project = this.repository.getProject(projectName);
return this.repository.getSynchronizedProjects().contains(project);
return systemSync.isProjectConnected(projectName);
}

public void addProject(IProject project) {
this.repository.addProject(project.getName(), project.getLocationURI().getPath());
notifyProjectConnected(project);
sendProjectConnectedMessage(project.getName());
syncConnectedProject(project.getName());
this.systemSync.addProject(project.getName(), project.getLocationURI().getPath());
notifyProjectConnected(project);
}

public void removeProject(IProject project) {
this.repository.removeProject(project.getName());
notifyProjectDisonnected(project);
JSONObject message = new JSONObject();
try {
message.put("username", this.username);
message.put("project", project.getName());
messageConnector.send("projectDisconnected", message);
} catch (Exception e) {
e.printStackTrace();
}
this.systemSync.removeProject(project.getName());
notifyProjectDisonnected(project);
}

public ConnectedProject[] getConnectedProjects() {
Set<Project> projects = repository.getSynchronizedProjects();
Set<Project> projects = systemSync.getSynchronizedProjects();
Set<ConnectedProject> connectedProjects = new HashSet<>();
for(Project project : projects){
connectedProjects.add(new ConnectedProject(project));
Expand All @@ -173,27 +82,7 @@ public void metadataChanged(IResourceDelta delta) {
IProject project = delta.getResource().getProject();
IMarkerDelta[] markerDeltas = delta.getMarkerDeltas();
if (project != null && isConnected(project) && markerDeltas != null && markerDeltas.length > 0) {
sendMetadataUpdate(delta.getResource());
}
}

public void sendMetadataUpdate(IResource resource) {
try {
String project = resource.getProject().getName();
String resourcePath = resource.getProjectRelativePath().toString();

JSONObject message = new JSONObject();
message.put("username", this.username);
message.put("project", project);
message.put("resource", resourcePath);
message.put("type", "marker");

IMarker[] markers = resource.findMarkers(null, true, IResource.DEPTH_INFINITE);
JSONArray content = JSONUtils.toJSON(markers);
message.put("metadata", content);
messageConnector.send(IMessageHandler.METADATA_CHANGED, message);
} catch (Exception e) {

systemSync.sendMetadataUpdate(delta.getResource());
}
}

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

protected void syncConnectedProject(String projectName) {
try {
JSONObject message = new JSONObject();
message.put("username", this.username);
message.put("project", projectName);
message.put("includeDeleted", true);
message.put("callback_id", GET_PROJECT_CALLBACK);
messageConnector.send("getProjectRequest", message);
} catch (Exception e) {
e.printStackTrace();
}
}

protected void sendProjectConnectedMessage(String projectName) {
try {
JSONObject message = new JSONObject();
message.put("username", this.username);
message.put("project", projectName);
messageConnector.send("projectConnected", message);
} catch (Exception e) {
e.printStackTrace();
}
}

protected void notifyProjectConnected(IProject project) {
for (IRepositoryListener listener : this.repositoryListeners) {
listener.projectConnected(project);
Expand All @@ -248,36 +113,14 @@ protected void notifyResourceChanged(IResource resource) {
}

public void dispose() {
for(IMessageHandler messageHandler : messageHandlers){
messageConnector.removeMessageHandler(messageHandler);
}
}

private void addMessageHandler(IMessageHandler messageHandler){
this.messageConnector.addMessageHandler(messageHandler);
this.messageHandlers.add(messageHandler);
systemSync.dispose();
}

@Override
public void notifyResourceChanged(Resource resource, Project project) {
notifyResourceChanged(Utils.getResourceByPath(project.id(), resource.path()));

}

@Override
public void sendMessage(String messageType, JSONObject content) throws Exception {
messageConnector.send(messageType, content);
public void onEvent(RepositoryEvent event) throws Exception {
String projectName = event.project().id();
String path = event.resource().path();
notifyResourceChanged(Utils.getResourceByPath(projectName, path));
}

@Override
public Project getWatcherProject(String projectName) {
return repository.getProject(projectName);
}

@Override
public Set<Project> getSynchronizedProjects() {
return repository.getSynchronizedProjects();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import org.eclipse.flux.client.MessageConstants;
import org.eclipse.flux.client.MessageHandler;
import org.eclipse.flux.core.IRepositoryCallback;
import org.eclipse.flux.core.sync.ISystemSync;
import org.eclipse.flux.watcher.core.Resource;
import org.eclipse.flux.watcher.core.Resource.ResourceType;
import org.json.JSONObject;

public abstract class AbstractMsgHandler extends MessageHandler {
protected IRepositoryCallback repositoryCallback;
protected ISystemSync repositoryCallback;

public AbstractMsgHandler(IRepositoryCallback repositoryCallback, String type) {
public AbstractMsgHandler(ISystemSync repositoryCallback, String type) {
super(type);
this.repositoryCallback = repositoryCallback;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.eclipse.core.resources.IResource;
import org.eclipse.flux.client.MessageConstants;
import org.eclipse.flux.core.IRepositoryCallback;
import org.eclipse.flux.core.sync.ISystemSync;
import org.eclipse.flux.core.util.JSONUtils;
import org.eclipse.flux.core.util.Utils;
import org.eclipse.flux.watcher.core.Resource;
Expand All @@ -11,7 +11,7 @@

public class MetadataRequestHandler extends AbstractMsgHandler {

public MetadataRequestHandler(IRepositoryCallback repositoryCallback) {
public MetadataRequestHandler(ISystemSync repositoryCallback) {
super(repositoryCallback, GET_METADATA_REQUEST);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.eclipse.flux.core.handlers;

import org.eclipse.flux.client.MessageConstants;
import org.eclipse.flux.core.IRepositoryCallback;
import org.eclipse.flux.core.sync.ISystemSync;
import org.eclipse.flux.watcher.core.Resource;
import org.eclipse.flux.watcher.core.spi.Project;
import org.json.JSONArray;
import org.json.JSONObject;

public class ProjectRequestHandler extends AbstractMsgHandler {

public ProjectRequestHandler(IRepositoryCallback repositoryCallback) {
public ProjectRequestHandler(ISystemSync repositoryCallback) {
super(repositoryCallback, GET_PROJECT_REQUEST);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.eclipse.flux.core.handlers;

import org.eclipse.flux.client.MessageConstants;
import org.eclipse.flux.core.IRepositoryCallback;
import org.eclipse.flux.core.sync.ISystemSync;
import org.eclipse.flux.watcher.core.Resource;
import org.eclipse.flux.watcher.core.spi.Project;
import org.json.JSONArray;
Expand All @@ -10,7 +10,7 @@
public class ProjectResponseHandler extends AbstractMsgHandler {
private int callbackID;

public ProjectResponseHandler(IRepositoryCallback repositoryCallback, int callbackID) {
public ProjectResponseHandler(ISystemSync repositoryCallback, int callbackID) {
super(repositoryCallback, GET_PROJECT_RESPONSE);
this.callbackID = callbackID;
}
Expand Down
Loading

0 comments on commit 6929e04

Please sign in to comment.