Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Fixed ROOFLEX-39 and eliminated ConcurrentModificationWxception probl…
Browse files Browse the repository at this point in the history
…em when adding Flex directories for monitoring.
  • Loading branch information
Jeremy Grelle committed Mar 15, 2011
1 parent bcfa7b6 commit 417529b
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ public class FlexCommands implements CommandMarker {
@Reference
private MetadataService metadataService;

@CliAvailabilityIndicator( { "flex setup", "flex remoting scaffold", "flex remoting all" })
@CliAvailabilityIndicator( { "flex setup" })
public boolean isFlexAvailable() {
return this.operations.isFlexAvailable();
}

@CliAvailabilityIndicator( { "flex remoting scaffold", "flex remoting all" })
public boolean isFlexConfigured() {
return this.operations.isFlexConfigured();
}

@CliCommand(value = "flex setup", help = "Install Spring BlazeDS configuration artifacts into your project")
public void installFlex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ public interface FlexOperations {

void createScaffoldApp();

boolean isFlexConfigured();

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ public void installFlex() {
}

public boolean isFlexAvailable() {
return getPathResolver() != null;
return getPathResolver() != null && !isFlexConfigured();
}

public boolean isFlexConfigured() {
return this.metadataService.get(FlexProjectMetadata.getProjectIdentifier()) != null;
}

public void createScaffoldApp() {
Expand Down Expand Up @@ -252,6 +256,8 @@ private void createServicesConfig() {
} catch (IOException e) {
new IllegalStateException("Encountered an error during copying of resources for maven addon.", e);
}

fileManager.scan();
}

private void createFlexConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;
import org.springframework.flex.roo.addon.mojos.FlexPathResolver;
import org.springframework.roo.file.monitor.DirectoryMonitoringRequest;
import org.springframework.roo.file.monitor.MonitoringRequest;
import org.springframework.roo.file.monitor.NotifiableFileMonitorService;
import org.springframework.roo.file.monitor.event.FileDetails;
import org.springframework.roo.file.monitor.event.FileOperation;
import org.springframework.roo.file.undo.CreateDirectory;
import org.springframework.roo.file.undo.FilenameResolver;
Expand All @@ -36,20 +35,24 @@
import org.springframework.roo.metadata.MetadataIdentificationUtils;
import org.springframework.roo.metadata.MetadataNotificationListener;
import org.springframework.roo.metadata.MetadataService;
import org.springframework.roo.process.manager.internal.UndoableMonitoringRequest;
import org.springframework.roo.project.Path;
import org.springframework.roo.project.ProjectMetadata;
import org.springframework.roo.project.PathResolver;
import org.springframework.roo.project.UndoableMonitoringRequest;
import org.springframework.roo.support.util.Assert;

/**
* {@link MetadataNotificationListener} that monitors the filesystem for changes to Flex source files.
*
* @author Jeremy Grelle
*/
@Component
@Component(immediate = true)
@Service
public class FlexProjectListener implements MetadataNotificationListener {

// TODO - Is there a better way to achieve the monitoring of the necessary Flex directories?

@Reference
private FilenameResolver filenameResolver;

@Reference
private MetadataService metadataService;
Expand All @@ -63,9 +66,6 @@ public class FlexProjectListener implements MetadataNotificationListener {
@Reference
private NotifiableFileMonitorService fileMonitorService;

@Reference
private FlexPathResolver pathResolver;

private boolean pathsRegistered = false;

protected void activate(ComponentContext context) {
Expand All @@ -84,14 +84,15 @@ public void notify(String upstreamDependency, String downstreamDependency) {
Assert.isTrue(MetadataIdentificationUtils.isValid(upstreamDependency), "Upstream dependency is an invalid metadata identification string ('"
+ upstreamDependency + "')");

if (upstreamDependency.equals(ProjectMetadata.getProjectIdentifier())) {
if (upstreamDependency.equals(FlexProjectMetadata.getProjectIdentifier())) {
// Acquire the Project Metadata, if available
ProjectMetadata md = (ProjectMetadata) this.metadataService.get(upstreamDependency);
FlexProjectMetadata md = (FlexProjectMetadata) this.metadataService.get(upstreamDependency);
if (md == null) {
return;
}

FilenameResolver filenameResolver = new PathResolvingAwareFilenameResolver();
PathResolver pathResolver = md.getPathResolver();
Assert.notNull(pathResolver, "Path resolver could not be acquired from changed metadata '" + md + "'");

Set<FileOperation> notifyOn = new HashSet<FileOperation>();
notifyOn.add(FileOperation.MONITORING_START);
Expand All @@ -101,37 +102,35 @@ public void notify(String upstreamDependency, String downstreamDependency) {
notifyOn.add(FileOperation.UPDATED);
notifyOn.add(FileOperation.DELETED);

for (Path p : this.pathResolver.getFlexSourcePaths()) {
// Verify path exists and ensure it's monitored, except root (which we assume is already monitored via
// ProcessManager)
for (Path p : pathResolver.getPaths()) {
// Verify path exists and ensure it's monitored, except root (which we assume is already monitored via ProcessManager)
if (!Path.ROOT.equals(p)) {
String fileIdentifier = this.pathResolver.getRoot(p);
String fileIdentifier = pathResolver.getRoot(p);
File file = new File(fileIdentifier);
Assert.isTrue(!file.exists() || file.exists() && file.isDirectory(), "Path '" + fileIdentifier
+ "' must either not exist or be a directory");
Assert.isTrue(!file.exists() || (file.exists() && file.isDirectory()), "Path '" + fileIdentifier + "' must either not exist or be a directory");
if (!file.exists()) {
// Create directory, but no notifications as that will happen once we start monitoring it below
new CreateDirectory(this.undoManager, filenameResolver, file);
new CreateDirectory(undoManager, filenameResolver, file);
}
MonitoringRequest request = new DirectoryMonitoringRequest(file, true, notifyOn);
new UndoableMonitoringRequest(undoManager, fileMonitorService, request, md.isValid());
}
}

// Explicitly perform a scan now that we've added all the directories we wish to monitor
//this.fileMonitorService.scanAll();

// Avoid doing this operation again unless the validity changes
this.pathsRegistered = md.isValid();
pathsRegistered = md.isValid();

// Explicitly perform a scan now that we've added all the directories we wish to monitor
fileMonitorService.scanAll();
}
}

private final class PathResolvingAwareFilenameResolver implements FilenameResolver {
/*private final class PathResolvingAwareFilenameResolver implements FilenameResolver {
public String getMeaningfulName(File file) {
Assert.notNull(file, "File required");
return FlexProjectListener.this.pathResolver.getFriendlyName(FileDetails.getCanonicalPath(file));
}
}
}*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.springframework.flex.roo.addon;

import org.springframework.flex.roo.addon.mojos.FlexPathResolver;
import org.springframework.roo.metadata.AbstractMetadataItem;
import org.springframework.roo.metadata.MetadataIdentificationUtils;


public class FlexProjectMetadata extends AbstractMetadataItem {

private static final String FLEX_PROJECT_IDENTIFIER = MetadataIdentificationUtils.create(FlexProjectMetadata.class.getName(), "flex_project");

private FlexPathResolver pathResolver;

public FlexProjectMetadata(FlexPathResolver flexPathResolver) {
super(FLEX_PROJECT_IDENTIFIER);
this.pathResolver = flexPathResolver;
}

public FlexPathResolver getPathResolver() {
return pathResolver;
}

public static final String getProjectIdentifier() {
return FLEX_PROJECT_IDENTIFIER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.springframework.flex.roo.addon;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;
import org.springframework.flex.roo.addon.mojos.FlexPathResolver;
import org.springframework.roo.file.monitor.event.FileEvent;
import org.springframework.roo.file.monitor.event.FileEventListener;
import org.springframework.roo.file.monitor.event.FileOperation;
import org.springframework.roo.metadata.MetadataDependencyRegistry;
import org.springframework.roo.metadata.MetadataIdentificationUtils;
import org.springframework.roo.metadata.MetadataItem;
import org.springframework.roo.metadata.MetadataProvider;
import org.springframework.roo.metadata.MetadataService;
import org.springframework.roo.process.manager.FileManager;
import org.springframework.roo.project.Path;
import org.springframework.roo.project.PathResolver;
import org.springframework.roo.support.util.Assert;

@Component(immediate = true)
@Service
public class FlexProjectMetadataProvider implements MetadataProvider, FileEventListener {

private static final String PROVIDES_TYPE = MetadataIdentificationUtils.create(MetadataIdentificationUtils.getMetadataClass(FlexProjectMetadata.getProjectIdentifier()));

//private static final String FLEX_GROUP = "org.springframework.flex";

@Reference
private MetadataService metadataService;

@Reference
private MetadataDependencyRegistry metadataDependencyRegistry;

@Reference
private FileManager fileManager;

@Reference
private FlexPathResolver flexPathResolver;

@Reference
private PathResolver pathResolver;

private String servicesConfig;

protected void activate(ComponentContext context) {
this.servicesConfig = pathResolver.getIdentifier(Path.SRC_MAIN_WEBAPP, "WEB-INF/flex/services-config.xml");
}

public MetadataItem get(String metadataIdentificationString) {
Assert.isTrue(FlexProjectMetadata.getProjectIdentifier().equals(metadataIdentificationString), "Unexpected metadata request '" + metadataIdentificationString + "' for this provider");

if (!fileManager.exists(servicesConfig)) {
return null;
}

return new FlexProjectMetadata(flexPathResolver);
}

/*public void notify(String upstreamDependency, String downstreamDependency) {
Assert.isTrue(MetadataIdentificationUtils.isValid(upstreamDependency), "Upstream dependency is an invalid metadata identification string ('"
+ upstreamDependency + "')");
if (upstreamDependency.equals(ProjectMetadata.getProjectIdentifier())) {
//recalculate the FlexProjectMetadata and notify
metadataService.get(FlexProjectMetadata.getProjectIdentifier(), true);
metadataDependencyRegistry.notifyDownstream(FlexProjectMetadata.getProjectIdentifier());
}
}*/

public String getProvidesType() {
return PROVIDES_TYPE;
}

public void onFileEvent(FileEvent fileEvent) {
Assert.notNull(fileEvent, "File event required");

if (fileEvent.getFileDetails().getCanonicalPath().equals(servicesConfig)) {
// Something happened to the services-config

// Don't notify if we're shutting down
if (fileEvent.getOperation() == FileOperation.MONITORING_FINISH) {
return;
}

// Otherwise let everyone know something has happened of interest, plus evict any cached entries from the MetadataService
metadataService.get(FlexProjectMetadata.getProjectIdentifier(), true);
metadataDependencyRegistry.notifyDownstream(FlexProjectMetadata.getProjectIdentifier());
}
}
}

0 comments on commit 417529b

Please sign in to comment.