You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2012/06/12 19:00:52 UTC
svn commit: r1349442 - in
/felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin:
SCRDescriptorGenerator.java om/Reference.java
Author: cziegeler
Date: Tue Jun 12 17:00:52 2012
New Revision: 1349442
URL: http://svn.apache.org/viewvc?rev=1349442&view=rev
Log:
New SCR Plugin (WiP)
Modified:
felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/om/Reference.java
Modified: felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java?rev=1349442&r1=1349441&r2=1349442&view=diff
==============================================================================
--- felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java (original)
+++ felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java Tue Jun 12 17:00:52 2012
@@ -21,7 +21,7 @@ package org.apache.felix.scrplugin;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.Iterator;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -84,6 +84,9 @@ public class SCRDescriptorGenerator {
/** The annotation scanner. */
private ClassScanner scanner;
+ /** The issue log. */
+ private IssueLog iLog;
+
/**
* Create an instance of this generator using the given {@link Log} instance
* of logging.
@@ -170,7 +173,7 @@ public class SCRDescriptorGenerator {
}
// create a log
- final IssueLog iLog = new IssueLog(this.options.isStrictMode());
+ this.iLog = new IssueLog(this.options.isStrictMode());
// create the annotation processor manager
final AnnotationProcessor aProcessor = new AnnotationProcessorManager(options.getAnnotationProcessors(),
@@ -246,7 +249,7 @@ public class SCRDescriptorGenerator {
if (!ref.isLookupStrategy() && ref.getField() != null
&& (ref.getCardinality() == ReferenceCardinality.OPTIONAL_UNARY || ref.getCardinality() == ReferenceCardinality.MANDATORY_UNARY)) {
- final String bindValue = ref.getBind();
+ final String bindValue = ref.getBind();
final String unbindValue = ref.getUnbind();
final String name = ref.getName();
final String type = ref.getInterfacename();
@@ -361,21 +364,9 @@ public class SCRDescriptorGenerator {
comp.setConfigurationPolicy(componentDesc.getConfigurationPolicy());
comp.setAbstract(componentDesc.isAbstract());
comp.setDs(componentDesc.isCreateDs());
- comp.setEnabled(componentDesc.getEnabled());
- comp.setImmediate(componentDesc.getImmediate());
comp.setFactory(componentDesc.getFactory());
comp.setSpecVersion(componentDesc.getSpecVersion());
- if (componentDesc.getActivate() != null) {
- comp.setActivate(componentDesc.getActivate().getName());
- }
- if (componentDesc.getDeactivate() != null) {
- comp.setDeactivate(componentDesc.getDeactivate().getName());
- }
- if (componentDesc.getModified() != null) {
- comp.setModified(componentDesc.getModified().getName());
- }
-
// Create metatype (if required)
final OCD ocd;
if ( !componentDesc.isAbstract() && componentDesc.isCreateMetatype() ) {
@@ -411,6 +402,9 @@ public class SCRDescriptorGenerator {
final Implementation impl = new Implementation(desc.getDescribedClass().getName());
comp.setImplementation(impl);
+ final Map<String, Reference> allReferences = new HashMap<String, Reference>();
+ final Map<String, Property> allProperties = new HashMap<String, Property>();
+
ClassDescription current = desc;
boolean inherit;
do {
@@ -418,18 +412,32 @@ public class SCRDescriptorGenerator {
inherit = (cd == null ? true : cd.isInherit());
if ( cd != null ) {
- if ( comp.isEnabled() != null ) {
+ // handle enabled and immediate
+ if ( comp.isEnabled() == null ) {
comp.setEnabled(cd.getEnabled());
}
- if ( comp.isImmediate() != null ) {
+ if ( comp.isImmediate() == null ) {
comp.setImmediate(cd.getImmediate());
}
+
+ // lifecycle methods
+ if ( comp.getActivate() == null && cd.getActivate() != null ) {
+ comp.setActivate(cd.getActivate().getName());
+ }
+ if ( comp.getDeactivate() == null && cd.getDeactivate() != null ) {
+ comp.setDeactivate(cd.getDeactivate().getName());
+ }
+ if ( comp.getModified() == null && cd.getModified() != null ) {
+ comp.setModified(cd.getModified().getName());
+ }
}
- this.processServices(desc, current, comp);
- this.processProperties(desc, current, comp, ocd);
- this.processReferences(desc, current, comp);
+ // services, properties, references
+ this.processServices(current, comp);
+ this.processProperties(current, comp, ocd, allProperties);
+ this.processReferences(current, comp, allReferences);
+ // go up in the class hierarchy
if ( !inherit || current.getDescribedClass().getSuperclass() == null ) {
current = null;
} else {
@@ -438,13 +446,15 @@ public class SCRDescriptorGenerator {
} while ( inherit && current != null);
// PID handling
- if ( componentDesc.isCreatePid() && !hasProperty(comp, org.osgi.framework.Constants.SERVICE_PID)) {
+ if ( componentDesc.isCreatePid() && !allProperties.containsKey(org.osgi.framework.Constants.SERVICE_PID)) {
final Property pid = new Property(null, "scr-generator");
- comp.addProperty( pid );
pid.setName( org.osgi.framework.Constants.SERVICE_PID );
pid.setValue( comp.getName() );
+
+ allProperties.put(org.osgi.framework.Constants.SERVICE_PID, pid);
+ comp.addProperty( pid );
}
- this.processGlobalProperties(desc, comp);
+ this.processGlobalProperties(desc, comp, allProperties);
return comp;
}
@@ -452,7 +462,7 @@ public class SCRDescriptorGenerator {
/**
* Process service directives
*/
- private void processServices(final ClassDescription desc, final ClassDescription current, final Component component)
+ private void processServices(final ClassDescription current, final Component component)
throws SCRDescriptorException, SCRDescriptorFailureException {
final ServiceDescription serviceDesc = current.getDescription(ServiceDescription.class);
@@ -467,36 +477,24 @@ public class SCRDescriptorGenerator {
service.setServiceFactory(true);
}
for(final String className : serviceDesc.getInterfaces()) {
- final Interface interf = new Interface(serviceDesc.getAnnotation(), desc.getSource());
+ final Interface interf = new Interface(serviceDesc.getAnnotation(), current.getSource());
interf.setInterfaceName(className);
service.addInterface(interf);
}
}
}
- /***
- * Check if the component has a property with the name
- */
- private boolean hasProperty(final Component component, final String name) {
- boolean found = false;
- final Iterator<Property> iter = component.getProperties().iterator();
- while ( !found && iter.hasNext() ) {
- final Property prop = iter.next();
- found = name.equals( prop.getName() );
- }
- return found;
- }
-
/**
* Process property directives
*/
- private void processProperties(final ClassDescription desc,
+ private void processProperties(
final ClassDescription current,
final Component component,
- final OCD ocd) throws SCRDescriptorException, SCRDescriptorFailureException {
+ final OCD ocd,
+ final Map<String, Property> allProperties)
+ throws SCRDescriptorException, SCRDescriptorFailureException {
for(final PropertyDescription pd : current.getDescriptions(PropertyDescription.class)) {
final Property prop = new Property(pd.getAnnotation(), current.getSource());
- component.addProperty(prop);
prop.setName(pd.getName());
prop.setType(pd.getType());
if ( pd.getValue() != null ) {
@@ -504,42 +502,73 @@ public class SCRDescriptorGenerator {
} else {
prop.setMultiValue(pd.getMultiValue());
}
+
+ if ( this.testProperty(current, allProperties, prop, current == component.getClassDescription())) {
+ component.addProperty(prop);
+ }
}
}
/**
- * Process all found properties for the component.
- * @param globalProperties Global properties are set on all components.
- * @param iLog The issue log.
- * @throws SCRDescriptorException
+ * Add global properties (if not already defined in the component)
*/
- private void processGlobalProperties(final ClassDescription desc, final Component component) {
+ private void processGlobalProperties(final ClassDescription desc,
+ final Component component,
+ final Map<String, Property> allProperties) {
// apply pre configured global properties
if ( this.options.getProperties() != null ) {
for(final Map.Entry<String, String> entry : this.options.getProperties().entrySet()) {
final String propName = entry.getKey();
final String value = entry.getValue();
// check if the service already provides this property
- if ( value != null && !hasProperty(component, propName) ) {
+ if ( value != null && !allProperties.containsKey(propName) ) {
final Property p = new Property(null, "scr-generator");
p.setName(propName);
p.setValue(value);
p.setType(PropertyType.String);
+
component.addProperty(p);
+ allProperties.put(propName, p);
}
}
}
}
/**
+ * Test a newly found property
+ */
+ private boolean testProperty(final ClassDescription current,
+ final Map<String, Property> allProperties,
+ final Property newProperty,
+ boolean isInspectedClass ) {
+ final String propName = newProperty.getName();
+
+ if ( !StringUtils.isEmpty(propName) ) {
+ if ( allProperties.containsKey(propName) ) {
+ // if the current class is the class we are currently inspecting, we
+ // have found a duplicate definition
+ if ( isInspectedClass ) {
+ iLog.addError("Duplicate definition for property " + propName + " in class "
+ + current.getDescribedClass().getName(), current.getSource() );
+ }
+ } else {
+ allProperties.put(propName, newProperty);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
* Process reference directives
*/
- private void processReferences(final ClassDescription desc, final ClassDescription current, final Component component)
+ private void processReferences(final ClassDescription current,
+ final Component component,
+ final Map<String, Reference> allReferences)
throws SCRDescriptorException, SCRDescriptorFailureException {
for(final ReferenceDescription rd : current.getDescriptions(ReferenceDescription.class)) {
final Reference ref = new Reference(rd.getAnnotation(), current.getSource());
- component.addReference(ref);
ref.setName(rd.getName());
ref.setInterfacename(rd.getInterfaceName());
ref.setCardinality(rd.getCardinality());
@@ -556,6 +585,38 @@ public class SCRDescriptorGenerator {
if ( rd.getUpdated() != null ) {
ref.setUpdated(rd.getUpdated().getName());
}
+
+ if ( this.testReference(current, allReferences, ref, component.getClassDescription() == current) ) {
+ component.addReference(ref);
+ }
+ }
+ }
+
+ /**
+ * Test a newly found reference
+ */
+ private boolean testReference(final ClassDescription current,
+ final Map<String, Reference> allReferences,
+ final Reference newReference,
+ boolean isInspectedClass ) {
+ String refName = newReference.getName();
+ if ( refName == null) {
+ refName = newReference.getInterfacename();
+ }
+
+ if ( refName != null ) {
+ if ( allReferences.containsKey( refName ) ) {
+ // if the current class is the class we are currently inspecting, we
+ // have found a duplicate definition
+ if ( isInspectedClass ) {
+ iLog.addError("Duplicate definition for reference " + refName + " in class "
+ + current.getDescribedClass().getName(), current.getSource() );
+ }
+ } else {
+ allReferences.put(refName, newReference);
+ return true;
+ }
}
+ return false;
}
}
Modified: felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/om/Reference.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/om/Reference.java?rev=1349442&r1=1349441&r2=1349442&view=diff
==============================================================================
--- felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/om/Reference.java (original)
+++ felix/sandbox/cziegeler/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/om/Reference.java Tue Jun 12 17:00:52 2012
@@ -165,6 +165,11 @@ public class Reference extends AbstractO
if (StringUtils.isEmpty(this.interfacename)) {
this.logError(context.getIssueLog(), "Missing interface name");
}
+ try {
+ context.getProject().getClassLoader().loadClass(this.interfacename);
+ } catch (final ClassNotFoundException e) {
+ this.logError(context.getIssueLog(), "Interface class can't be loaded: " + this.interfacename);
+ }
// validate cardinality
if (this.cardinality == null) {