FELIX-3643 : Use BuildContext for scanning changed files
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1394025 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/Options.java b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/Options.java
index a57f858..ef1c893 100644
--- a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/Options.java
+++ b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/Options.java
@@ -205,4 +205,21 @@
public void setGenerateSeparateDescriptors(boolean generateSeparateDescriptors) {
this.generateSeparateDescriptors = generateSeparateDescriptors;
}
+
+ private static final String PARENT_NAME = "OSGI-INF";
+
+ /**
+ * Convenience method to get the meta type directory.
+ */
+ public File getMetaTypeDirectory() {
+ final File parentDir = new File(this.getOutputDirectory(), PARENT_NAME);
+ return new File(parentDir, "metatype");
+ }
+
+ /**
+ * Convenience method to get the component descriptor directory.
+ */
+ public File getComponentDescriptorDirectory() {
+ return new File(this.getOutputDirectory(), PARENT_NAME);
+ }
}
diff --git a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
index 63c0748..fe6ba6a 100644
--- a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
+++ b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
@@ -161,8 +161,8 @@
private static final String PROPERTIES = "properties";
public static List<ClassDescription> read(final InputStream file,
- final ClassLoader classLoader,
- final IssueLog iLog, final String location) throws SCRDescriptorException {
+ final ClassLoader classLoader,
+ final IssueLog iLog, final String location) throws SCRDescriptorException {
try {
final XmlHandler xmlHandler = new XmlHandler(classLoader, iLog, location);
IOUtils.parse(file, xmlHandler);
@@ -181,11 +181,11 @@
* @throws SAXException
*/
protected static void generateXML(final DescriptionContainer module,
- final List<ComponentContainer> components,
- final File descriptorFile,
- final Log logger) throws SAXException {
+ final List<ComponentContainer> components,
+ final File descriptorFile,
+ final Log logger) throws SAXException {
logger.info("Writing " + components.size() + " Service Component Descriptors to "
- + descriptorFile);
+ + descriptorFile);
final ContentHandler contentHandler = IOUtils.getSerializer(descriptorFile);
// detect namespace to use
final String namespace = module.getOptions().getSpecVersion().getNamespaceUrl();
@@ -216,10 +216,10 @@
* @throws SAXException
*/
protected static void generateXML(final String namespace,
- final DescriptionContainer module,
- final ComponentContainer container,
- final ContentHandler contentHandler)
- throws SAXException {
+ final DescriptionContainer module,
+ final ComponentContainer container,
+ final ContentHandler contentHandler)
+ throws SAXException {
final ComponentDescription component = container.getComponentDescription();
final AttributesImpl ai = new AttributesImpl();
@@ -276,9 +276,9 @@
IOUtils.addAttribute(ai, IMPLEMENTATION_ATTR_CLASS, component.getClassDescription().getDescribedClass().getName());
IOUtils.indent(contentHandler, 2);
contentHandler.startElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.IMPLEMENTATION,
- ComponentDescriptorIO.IMPLEMENTATION_QNAME, ai);
+ ComponentDescriptorIO.IMPLEMENTATION_QNAME, ai);
contentHandler.endElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.IMPLEMENTATION,
- ComponentDescriptorIO.IMPLEMENTATION_QNAME);
+ ComponentDescriptorIO.IMPLEMENTATION_QNAME);
IOUtils.newline(contentHandler);
}
@@ -290,9 +290,9 @@
* @throws SAXException
*/
protected static void generateServiceXML(
- final ServiceDescription service,
- final ContentHandler contentHandler)
- throws SAXException {
+ final ServiceDescription service,
+ final ContentHandler contentHandler)
+ throws SAXException {
final AttributesImpl ai = new AttributesImpl();
IOUtils.addAttribute(ai, SERVICE_ATTR_FACTORY, String.valueOf(service.isServiceFactory()));
IOUtils.indent(contentHandler, 2);
@@ -316,12 +316,12 @@
* @throws SAXException
*/
private static void generateServiceXML(final String interfaceName, final ContentHandler contentHandler)
- throws SAXException {
+ throws SAXException {
final AttributesImpl ai = new AttributesImpl();
IOUtils.addAttribute(ai, INTERFACE_ATTR_NAME, interfaceName);
IOUtils.indent(contentHandler, 3);
contentHandler.startElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.INTERFACE, ComponentDescriptorIO.INTERFACE_QNAME,
- ai);
+ ai);
contentHandler.endElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.INTERFACE, ComponentDescriptorIO.INTERFACE_QNAME);
IOUtils.newline(contentHandler);
}
@@ -365,10 +365,10 @@
* @throws SAXException
*/
protected static void generateReferenceXML(final ComponentDescription component,
- final DescriptionContainer module,
- final ReferenceDescription reference,
- final ContentHandler contentHandler)
- throws SAXException {
+ final DescriptionContainer module,
+ final ReferenceDescription reference,
+ final ContentHandler contentHandler)
+ throws SAXException {
final AttributesImpl ai = new AttributesImpl();
IOUtils.addAttribute(ai, ATTR_NAME, reference.getName());
IOUtils.addAttribute(ai, INTERFACE_ATTR_NAME, reference.getInterfaceName());
@@ -392,7 +392,7 @@
IOUtils.indent(contentHandler, 2);
contentHandler.startElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.REFERENCE, ComponentDescriptorIO.REFERENCE_QNAME,
- ai);
+ ai);
contentHandler.endElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.REFERENCE, ComponentDescriptorIO.REFERENCE_QNAME);
IOUtils.newline(contentHandler);
}
@@ -448,8 +448,9 @@
/**
* @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
+ @Override
public void startElement(String uri, final String localName, final String name, final Attributes attributes)
- throws SAXException {
+ throws SAXException {
// according to the spec, the elements should have the namespace,
// except when the root element is the "component" element
// So we check this for the first element, we receive.
@@ -654,6 +655,7 @@
/**
* @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/
+ @Override
public void endElement(String uri, String localName, String name) throws SAXException {
if (this.overrideNamespace != null && "".equals(uri)) {
uri = this.overrideNamespace;
@@ -690,6 +692,7 @@
/**
* @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
*/
+ @Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (this.pendingProperty != null) {
final String text = new String(ch, start, length);
@@ -708,7 +711,7 @@
* Generate descriptor file(s)
*/
public static List<String> generateDescriptorFiles(final DescriptionContainer module, final Options options, final Log logger)
- throws SCRDescriptorException, SCRDescriptorFailureException {
+ throws SCRDescriptorException, SCRDescriptorFailureException {
final List<ComponentContainer> components = new ArrayList<ComponentContainer>();
for(final ComponentContainer container : module.getComponents()) {
if (!container.getComponentDescription().isCreateDs()) {
@@ -720,7 +723,7 @@
}
// check descriptor file
- final File descriptorDir = new File(options.getOutputDirectory(), PARENT_NAME);
+ final File descriptorDir = options.getComponentDescriptorDirectory();
final File descriptorFile = StringUtils.isEmpty(options.getSCRName()) ? null : new File(descriptorDir, options.getSCRName());
// terminate if there is nothing else to write
diff --git a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java
index ccbf25a..8f21922 100644
--- a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java
+++ b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java
@@ -72,9 +72,9 @@
private static final String OPTION_ELEMENT_QNAME = OPTION_ELEMENT;
public static List<String> generateDescriptors(final DescriptionContainer module,
- final Options options,
- final Log logger)
- throws SCRDescriptorException {
+ final Options options,
+ final Log logger)
+ throws SCRDescriptorException {
int metatypeCount = 0;
for(final ComponentContainer component : module.getComponents()) {
if ( component.getMetatypeContainer() != null ) {
@@ -83,8 +83,8 @@
}
// write meta type info if there is a file name
if (!StringUtils.isEmpty(options.getMetaTypeName())) {
- final File parentDir = new File(options.getOutputDirectory(), "OSGI-INF");
- final File mtDir = new File(parentDir, "metatype");
+ final File mtDir = options.getMetaTypeDirectory();
+ final File parentDir = mtDir.getParentFile();
final File mtFile = new File(mtDir, options.getMetaTypeName());
@@ -128,7 +128,7 @@
* @throws SAXException
*/
private static void write(final DescriptionContainer metaData, final List<ComponentContainer> components, final File file)
- throws SCRDescriptorException {
+ throws SCRDescriptorException {
try {
final ContentHandler contentHandler = IOUtils.getSerializer(file);
@@ -159,7 +159,7 @@
}
private static void generateOCDXML(final MetatypeContainer ocd, final ContentHandler contentHandler)
- throws SAXException {
+ throws SAXException {
final AttributesImpl ai = new AttributesImpl();
IOUtils.addAttribute(ai, "id", ocd.getId());
IOUtils.addAttribute(ai, "name", ocd.getName());
@@ -182,7 +182,7 @@
}
private static void generateAttributeXML(final MetatypeAttributeDefinition ad, final ContentHandler contentHandler)
- throws SAXException {
+ throws SAXException {
final AttributesImpl ai = new AttributesImpl();
IOUtils.addAttribute(ai, "id", ad.getId());
IOUtils.addAttribute(ai, "type", ad.getType());
@@ -224,7 +224,7 @@
}
private static void generateDesignateXML(final MetatypeContainer designate, final ContentHandler contentHandler)
- throws SAXException {
+ throws SAXException {
final AttributesImpl ai = new AttributesImpl();
IOUtils.addAttribute(ai, "pid", designate.getId());
IOUtils.addAttribute(ai, "factoryPid", designate.getFactoryPid());
@@ -240,7 +240,7 @@
}
private static void generateObjectXML(final MetatypeContainer obj, final ContentHandler contentHandler)
- throws SAXException {
+ throws SAXException {
final AttributesImpl ai = new AttributesImpl();
IOUtils.addAttribute(ai, "ocdref", obj.getId());
IOUtils.indent(contentHandler, 2);
diff --git a/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java b/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java
index 4113a99..58d256c 100644
--- a/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java
+++ b/scrplugin/maven-scr-plugin/src/main/java/org/apache/felix/scrplugin/mojo/SCRDescriptorMojo.java
@@ -175,7 +175,7 @@
* @parameter
*/
private List<String> supportedProjectTypes = Arrays.asList( new String[]
- { "jar", "bundle" } );
+ { "jar", "bundle" } );
/**
* @component
@@ -188,7 +188,7 @@
// ignore unsupported project types, useful when bundleplugin is configured in parent pom
if ( !supportedProjectTypes.contains( projectType ) ) {
getLog().debug(
- "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
+ "Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
return;
}
@@ -197,13 +197,13 @@
// create project
final MavenProjectScanner scanner = new MavenProjectScanner(
- this.buildContext,
+ this.buildContext,
this.project, this.sourceIncludes, this.sourceExcludes, scrLog);
-
+
final Project project = new Project();
// create the class loader
project.setClassLoader(new URLClassLoader(getClassPath(), this
- .getClass().getClassLoader()));
+ .getClass().getClassLoader()));
project.setDependencies(scanner.getDependencies());
project.setSources(scanner.getSources());
project.setClassesDirectory(this.project.getBuild().getOutputDirectory());
@@ -234,16 +234,14 @@
final Result result = generator.execute();
this.setServiceComponentHeader(result.getScrFiles());
+
this.updateProjectResources();
-
- // TODO - should we skip performing these operations just one descriptor file is generated?
- this.cleanUpDeletedSources(scanner.getDeletedSources());
+ // don't try to delete per-class descriptors if only one descriptor is generated
+ if ( options.isGenerateSeparateDescriptors() )
+ this.cleanUpDeletedSources(scanner.getDeletedSources(), options);
this.refreshMessages(result.getProcessedSourceFiles());
-
- // TODO - refreshing the target files does not seem to be the right thing ; however, new files
- // are not refresh in Eclipse right now
- //this.updateBuildContext(result);
-
+ this.updateBuildContext(result);
+
} catch (final SCRDescriptorException sde) {
throw new MojoExecutionException(sde.getSourceLocation() + " : " + sde.getMessage(), sde);
} catch (final SCRDescriptorFailureException sdfe) {
@@ -252,48 +250,50 @@
}
}
- /**
- * @param scrFiles
- */
- private void refreshMessages(List<String> scrFiles) {
-
- for ( String scrFile : scrFiles )
- buildContext.removeMessages(new File(scrFile));
-
- }
-
- private void cleanUpDeletedSources(Collection<Source> deletedSources) {
-
- // TODO remove duplication of file name selection - MetaTypeIO, ComponentDescriptorIO
-
- final File parentDir = new File(this.outputDirectory, "OSGI-INF");
- final File mtDir = new File(parentDir, "metatype");
-
- for ( Source deletedSource : deletedSources ) {
-
- File metaTypeFile = new File(mtDir, deletedSource.getClassName() + ".xml");
- boolean deleted = metaTypeFile.delete();
- if ( deleted )
- buildContext.refresh(metaTypeFile);
-
- File componentDescriptorFile = new File(parentDir, deletedSource.getClassName()+".xml");
- deleted = componentDescriptorFile.delete();
- if ( deleted )
- buildContext.refresh(componentDescriptorFile);
- }
- }
+ /**
+ * @param scrFiles
+ */
+ private void refreshMessages(List<String> scrFiles) {
-
+ for ( String scrFile : scrFiles )
+ buildContext.removeMessages(new File(scrFile));
+ }
- private void updateBuildContext(final Result result) {
+ private void cleanUpDeletedSources(final Collection<Source> deletedSources, final Options options) {
+
+ final File componentDir = options.getComponentDescriptorDirectory();
+ final File mtDir = options.getMetaTypeDirectory();
+
+ for ( Source deletedSource : deletedSources ) {
+
+ File metaTypeFile = new File(mtDir, deletedSource.getClassName() + ".xml");
+ getLog().debug("Deleting " + metaTypeFile + " ");
+ boolean deleted = metaTypeFile.delete();
+ if ( deleted )
+ buildContext.refresh(metaTypeFile);
+
+ File componentDescriptorFile = new File(componentDir, deletedSource.getClassName() + ".xml");
+ getLog().debug("Deleting " + componentDescriptorFile);
+ deleted = componentDescriptorFile.delete();
+ if ( deleted )
+ buildContext.refresh(componentDescriptorFile);
+ }
+ }
+
+ private void updateBuildContext(final Result result) {
+
if ( result.getMetatypeFiles() != null ) {
for(final String name : result.getMetatypeFiles() ) {
- this.buildContext.refresh(new File(this.outputDirectory, name.replace('/', File.separatorChar)));
+ File metaTypeFile = new File(this.outputDirectory, name.replace('/', File.separatorChar));
+ getLog().debug("Refreshing " + metaTypeFile);
+ this.buildContext.refresh(metaTypeFile);
}
}
if ( result.getScrFiles() != null ) {
for(final String name : result.getScrFiles() ) {
- this.buildContext.refresh(new File(this.outputDirectory, name.replace('/', File.separatorChar)));
+ File scrFile = new File(this.outputDirectory, name.replace('/', File.separatorChar));
+ getLog().debug("Refreshing " + scrFile);
+ this.buildContext.refresh(scrFile);
}
}
}
@@ -305,7 +305,7 @@
try {
path.add(new File(this.project.getBuild().getOutputDirectory())
- .toURI().toURL());
+ .toURI().toURL());
} catch (final IOException ioe) {
throw new MojoFailureException(
"Unable to add target directory to classloader.");
@@ -401,7 +401,7 @@
boolean found = false;
@SuppressWarnings("unchecked")
final Iterator<Resource> rsrcIterator = this.project.getResources()
- .iterator();
+ .iterator();
while (!found && rsrcIterator.hasNext()) {
final Resource rsrc = rsrcIterator.next();
found = rsrc.getDirectory().equals(ourRsrcPath);