FELIX-1229 : Add support for new activate and deactivate signatures and custom method name
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@784142 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/Constants.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/Constants.java
index 9983e98..db2a641 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/Constants.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/Constants.java
@@ -62,6 +62,12 @@
public static final String COMPONENT_CONFIG_POLICY_REQUIRE = "require";
public static final String COMPONENT_CONFIG_POLICY_IGNORE = "ignore";
+ // The component activate method name (V1.1)
+ public static final String COMPONENT_ACTIVATE = "activate";
+
+ // The component activate method name (V1.1)
+ public static final String COMPONENT_DEACTIVATE = "deactivate";
+
public static final String PROPERTY = "scr.property";
public static final String PROPERTY_NAME = "name";
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java
index d82a01f..f07c646 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java
@@ -416,11 +416,20 @@
Constants.COMPONENT_IMMEDIATE, true)));
}
- // check for V1.1 attributes
+ // check for V1.1 attributes: configuration policy
if ( tag.getNamedParameter(Constants.COMPONENT_CONFIG_POLICY) != null ) {
component.setSpecVersion(Constants.VERSION_1_1);
component.setConfigurationPolicy(tag.getNamedParameter(Constants.COMPONENT_CONFIG_POLICY));
}
+ // check for V1.1 attributes: activate, deactivate
+ if ( tag.getNamedParameter(Constants.COMPONENT_ACTIVATE) != null ) {
+ component.setSpecVersion(Constants.VERSION_1_1);
+ component.setActivate(tag.getNamedParameter(Constants.COMPONENT_ACTIVATE));
+ }
+ if ( tag.getNamedParameter(Constants.COMPONENT_DEACTIVATE) != null ) {
+ component.setSpecVersion(Constants.VERSION_1_1);
+ component.setDeactivate(tag.getNamedParameter(Constants.COMPONENT_DEACTIVATE));
+ }
// whether metatype information is to generated for the component
final String metaType = tag.getNamedParameter(Constants.COMPONENT_METATYPE);
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/om/Component.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/om/Component.java
index 8b2049a..249d30b 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/om/Component.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/om/Component.java
@@ -66,6 +66,12 @@
/** Configuration policy. (V1.1) */
protected String configurationPolicy;
+ /** Activation method. (V1.1) */
+ protected String activate;
+
+ /** Deactivation method. (V1.1) */
+ protected String deactivate;
+
/** The spec version. */
protected int specVersion;
@@ -199,6 +205,33 @@
}
/**
+ * Get the name of the activate method (or null for default)
+ */
+ public String getActivate() {
+ return this.activate;
+ }
+
+ /**
+ * Set the name of the deactivate method (or null for default)
+ */
+ public void setDeactivate(final String value) {
+ this.deactivate = value;
+ }
+
+ /**
+ * Get the name of the deactivate method (or null for default)
+ */
+ public String getDeactivate() {
+ return this.deactivate;
+ }
+
+ /**
+ * Set the name of the activate method (or null for default)
+ */
+ public void setActivate(final String value) {
+ this.activate = value;
+ }
+ /**
* Validate the component description.
* If errors occur a message is added to the issues list,
* warnings can be added to the warnings list.
@@ -229,9 +262,12 @@
// no errors so far, let's continue
if ( iLog.getNumberOfErrors() == currentIssueCount ) {
+ final String activateName = this.activate == null ? "activate" : this.activate;
+ final String deactivateName = this.deactivate == null ? "deactivate" : this.deactivate;
+
// check activate and deactivate methods
- this.checkLifecycleMethod(javaClass, "activate", iLog);
- this.checkLifecycleMethod(javaClass, "deactivate", iLog);
+ this.checkLifecycleMethod(specVersion, javaClass, activateName, iLog);
+ this.checkLifecycleMethod(specVersion, javaClass, deactivateName, iLog);
// ensure public default constructor
boolean constructorFound = true;
@@ -299,15 +335,84 @@
}
}
+ private static final String TYPE_COMPONENT_CONTEXT = "org.osgi.service.component.ComponentContext";
+ private static final String TYPE_BUNDLE_CONTEXT = "org.osgi.framework.BundleContext";
+ private static final String TYPE_MAP = "java.util.Map";
/**
* Check for existence of lifecycle methods.
+ * @param specVersion The spec version
* @param javaClass The java class to inspect.
* @param methodName The method name.
* @param warnings The list of warnings used to add new warnings.
*/
- protected void checkLifecycleMethod(JavaClassDescription javaClass, String methodName, final IssueLog iLog)
+ protected void checkLifecycleMethod(final int specVersion,
+ final JavaClassDescription javaClass,
+ final String methodName,
+ final IssueLog iLog)
throws MojoExecutionException {
- final JavaMethod method = javaClass.getMethodBySignature(methodName, new String[] {"org.osgi.service.component.ComponentContext"});
+ // first candidate is (de)activate(ComponentContext)
+ JavaMethod method = javaClass.getMethodBySignature(methodName, new String[] {TYPE_COMPONENT_CONTEXT});
+ if ( method == null ) {
+ if ( specVersion == Constants.VERSION_1_1) {
+ // second candidate is (de)activate(BundleContext)
+ method = javaClass.getMethodBySignature(methodName, new String[] {TYPE_BUNDLE_CONTEXT});
+ if ( method == null ) {
+ // third candidate is (de)activate(Map)
+ method = javaClass.getMethodBySignature(methodName, new String[] {TYPE_MAP});
+
+ if ( method == null ) {
+ // fourth candidate is (de)activate with two or three arguments (type must be BundleContext, ComponentCtx and Map)
+ // as we have to iterate now and the fifth candidate is zero arguments
+ // we already store this option
+ JavaMethod zeroArgMethod = null;
+ JavaMethod found = null;
+ final JavaMethod[] methods = javaClass.getMethods();
+ int i = 0;
+ while ( i < methods.length && found == null ) {
+ if ( methodName.equals(methods[i].getName()) ) {
+
+ if ( methods[i].getParameters().length == 0 ) {
+ zeroArgMethod = methods[i];
+ } else if ( methods[i].getParameters().length == 2 || methods[i].getParameters().length == 3) {
+ boolean valid = true;
+ for(int m=0; m<methods[i].getParameters().length; m++) {
+ final String type = methods[i].getParameters()[m].getType();
+ if ( !type.equals(TYPE_BUNDLE_CONTEXT)
+ && !type.equals(TYPE_COMPONENT_CONTEXT)
+ && !type.equals(TYPE_MAP) ) {
+ valid = false;
+ }
+ }
+ if ( valid ) {
+ found = methods[i];
+ }
+ }
+ }
+ i++;
+ }
+ if ( found != null ) {
+ method = found;
+ } else {
+ method = zeroArgMethod;
+ }
+ }
+ }
+ }
+ // if no method is found, we check for any method with that name to print some warnings!
+ if ( method == null ) {
+ final JavaMethod[] methods = javaClass.getMethods();
+ for(int i=0; i<methods.length; i++) {
+ if ( methodName.equals(methods[i].getName()) ) {
+
+ if ( methods[i].getParameters().length != 1 ) {
+ iLog.addWarning(this.getMessage("Lifecycle method " + methods[i].getName() + " has wrong number of arguments"));
+ } else {
+ iLog.addWarning(this.getMessage("Lifecycle method " + methods[i].getName() + " has wrong argument " + methods[i].getParameters()[0].getType()));
+ }
+ }
+ }
+ }
+ }
if ( method != null ) {
// check protected
if (method.isPublic()) {
@@ -315,19 +420,6 @@
} else if (!method.isProtected()) {
iLog.addWarning(this.getMessage("Lifecycle method " + method.getName() + " has wrong qualifier, public or protected required"));
}
- } else {
- // if no method is found, we check for any method with that name
- final JavaMethod[] methods = javaClass.getMethods();
- for(int i=0; i<methods.length; i++) {
- if ( methodName.equals(methods[i].getName()) ) {
-
- if ( methods[i].getParameters().length != 1 ) {
- iLog.addWarning(this.getMessage("Lifecycle method " + methods[i].getName() + " has wrong number of arguments"));
- } else {
- iLog.addWarning(this.getMessage("Lifecycle method " + methods[i].getName() + " has wrong argument " + methods[i].getParameters()[0].getType()));
- }
- }
- }
}
}
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
index 3f61572..e7f93ba 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
@@ -63,18 +63,24 @@
/** The enabled attribute. */
private static final String COMPONENT_ATTR_ENABLED = "enabled";
- /** The policy attribute. */
+ /** Component: The policy attribute. */
private static final String COMPONENT_ATTR_POLICY = "configuration-policy";
- /** The factory attribute. */
+ /** Component: The factory attribute. */
private static final String COMPONENT_ATTR_FACTORY = "factory";
- /** The immediate attribute. */
+ /** Component: The immediate attribute. */
private static final String COMPONENT_ATTR_IMMEDIATE = "immediate";
- /** The name attribute. */
+ /** Component: The name attribute. */
private static final String COMPONENT_ATTR_NAME = "name";
+ /** Component: The immediate attribute. */
+ private static final String COMPONENT_ATTR_ACTIVATE = "activate";
+
+ /** Component: The immediate attribute. */
+ private static final String COMPONENT_ATTR_DEACTIVATE = "deactivate";
+
private static final String IMPLEMENTATION = "implementation";
private static final String IMPLEMENTATION_QNAME = IMPLEMENTATION;
@@ -182,6 +188,8 @@
// attributes new in 1.1
if ( NAMESPACE_URI_1_1.equals(namespace) ) {
IOUtils.addAttribute(ai, COMPONENT_ATTR_POLICY, component.getConfigurationPolicy());
+ IOUtils.addAttribute(ai, COMPONENT_ATTR_ACTIVATE, component.getActivate());
+ IOUtils.addAttribute(ai, COMPONENT_ATTR_DEACTIVATE, component.getDeactivate());
}
IOUtils.indent(contentHandler, 1);
@@ -405,6 +413,8 @@
// check for version 1.1 attributes
if ( components.getSpecVersion() == Constants.VERSION_1_1 ) {
this.currentComponent.setConfigurationPolicy(attributes.getValue(COMPONENT_ATTR_POLICY));
+ this.currentComponent.setActivate(attributes.getValue(COMPONENT_ATTR_ACTIVATE));
+ this.currentComponent.setDeactivate(attributes.getValue(COMPONENT_ATTR_DEACTIVATE));
}
} else if (localName.equals(IMPLEMENTATION)) {
// Set the implementation class name (mandatory)