initial commit of the DependencyManager Annotation project which contains DM annotation API, and corresponding Bnd/Maven plugins used to parse annotations at compilation time.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@898075 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/annotation/LICENSE b/dependencymanager/annotation/LICENSE
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/dependencymanager/annotation/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/dependencymanager/annotation/NOTICE b/dependencymanager/annotation/NOTICE
new file mode 100644
index 0000000..cfc3633
--- /dev/null
+++ b/dependencymanager/annotation/NOTICE
@@ -0,0 +1,26 @@
+Apache Felix Dependency Manager
+Copyright 2009 The Apache Software Foundation
+
+
+I. Included Software
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+Licensed under the Apache License 2.0.
+
+This product includes software developed at
+The OSGi Alliance (http://www.osgi.org/).
+Copyright (c) OSGi Alliance (2000, 2007).
+Licensed under the Apache License 2.0.
+
+
+II. Used Software
+
+This product uses software developed at
+The OSGi Alliance (http://www.osgi.org/).
+Copyright (c) OSGi Alliance (2000, 2007).
+Licensed under the Apache License 2.0.
+
+
+III. License Summary
+- Apache License 2.0
diff --git a/dependencymanager/annotation/pom.xml b/dependencymanager/annotation/pom.xml
new file mode 100644
index 0000000..79c43c7
--- /dev/null
+++ b/dependencymanager/annotation/pom.xml
@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="UTF-8"?>
+ <!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to you under the Apache License, Version
+ 2.0 (the "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+ applicable law or agreed to in writing, software distributed under
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
+ OR CONDITIONS OF ANY KIND, either express or implied. See the
+ License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>felix-parent</artifactId>
+ <version>1.2.0</version>
+ <relativePath>../../pom/pom.xml</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.dependencymanager.annotation</artifactId>
+ <version>3.0.0-SNAPSHOT</version>
+ <name>Apache Felix Dependency Manager Annotation</name>
+ <packaging>maven-plugin</packaging>
+
+ <repositories>
+ <repository>
+ <id>maven2-repository.biz.aQute</id>
+ <name>aQute SARL Maven 2 Repository</name>
+ <url>http://www.aqute.biz/repo</url>
+ </repository>
+ </repositories>
+
+ <description>DM annotation API, and corresponding scanners</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>biz.aQute</groupId>
+ <artifactId>bnd</artifactId>
+ <version>0.0.389</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.0.7</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-artifact</artifactId>
+ <version>2.0.7</version>
+ <scope>compile</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <!-- JDK 1.5 needed for annotation support -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Composition.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Composition.java
new file mode 100644
index 0000000..3abbead
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Composition.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotates a method returning the list of objects which are part of the Service implementation.
+ * Each instances will be injected with Service dependencies.
+ */
+@Retention(RetentionPolicy.CLASS)
+@Target(ElementType.METHOD)
+public @interface Composition
+{
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/ConfigurationDependency.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/ConfigurationDependency.java
new file mode 100644
index 0000000..f0d19d4
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/ConfigurationDependency.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotates a method for injecting a Configuration Dependency.
+ */
+@Retention(RetentionPolicy.CLASS)
+@Target(ElementType.METHOD)
+public @interface ConfigurationDependency
+{
+ /**
+ * Returns the pid for a given service (by default, the pid is the service class name).
+ * @return the pid for a given service (default = Service class name)
+ */
+ String pid() default "";
+
+ /**
+ * Returns true if the configuration properties must be published along with the service.
+ * Any additional service properties specified directly are merged with these.
+ * @return true if configuration must be published along with the service, false if not.
+ */
+ boolean propagate() default false;
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Destroy.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Destroy.java
new file mode 100644
index 0000000..c3ac138
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Destroy.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotates a method which is invoked when the Service is destroyed.
+ * If you don't supply this annotation, then no "destroy" method will be invoked.
+ */
+@Retention(RetentionPolicy.CLASS)
+@Target(ElementType.METHOD)
+public @interface Destroy
+{
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Init.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Init.java
new file mode 100644
index 0000000..f4e3054
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Init.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotates a method which will be invoked when the Service is initializing.
+ * If you don't supply this annotation, then no "init" method will be invoked.
+ */
+@Retention(RetentionPolicy.CLASS)
+@Target(ElementType.METHOD)
+public @interface Init
+{
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Service.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Service.java
new file mode 100644
index 0000000..bd37cbc
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Service.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotates an OSGi Service class.
+ */
+@Retention(RetentionPolicy.CLASS)
+@Target(ElementType.TYPE)
+public @interface Service
+{
+ /**
+ * Returns the list of provided interface. By default, the directly implemented interfaces are provided.
+ * @return The list of provided interface.
+ */
+ Class<?>[] provide() default Object.class;
+
+ /**
+ * Returns the list of provided service properties. Each string must be separated by "=" char.
+ * @return The list of provided service properties.
+ */
+ String[] properties() default "";
+
+ /**
+ * Returns the Class of the class which acts as a factory for this Service. The default method
+ * factory name is "create". If you need to invoke another method, then you can use the
+ * <code>factoryMethod</code> attribute.
+ * @return the factory Class name.
+ */
+ Class<?> factory() default Object.class;
+
+ /**
+ * Returns the method name of the factory class which will create our Service instance.
+ * @return the factory method name.
+ */
+ String factoryMethod() default "";
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/ServiceDependency.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/ServiceDependency.java
new file mode 100644
index 0000000..c36c18b
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/ServiceDependency.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotates a method or a field for injecting a Service Dependency. When applied on a class
+ * field, optional unavailable dependencies are injected with a NullObject.
+ */
+@Retention(RetentionPolicy.CLASS)
+@Target({ElementType.METHOD, ElementType.FIELD})
+public @interface ServiceDependency
+{
+ /**
+ * Returns the Service dependency type (by default, the type is method parameter type).
+ * @return the Service dependency type.
+ */
+ Class<?> service() default Object.class;
+
+ /**
+ * Returns the Service dependency OSGi filter.
+ * @return The Service dependency filter.
+ */
+ String filter() default "";
+
+ /**
+ * Returns the class for the default implementation, if the dependency is not available.
+ * @return The default class used when the dependency is not available.
+ */
+ Class<?> defaultImpl() default Object.class;
+
+ /**
+ * Returns whether the Service dependency is required or not.
+ * @return true if the dependency is required, false if not.
+ */
+ boolean required() default true;
+
+ /**
+ * Returns the callback method to be invoked when the service is available. This attribute is only meaningful when
+ * the annotation is applied on a class field.
+ */
+ String added() default "";
+
+ /**
+ * Returns the callback method to be invoked when the service properties have changed.
+ */
+ String changed() default "";
+
+ /**
+ * Returns the callback method to invoke when the service is lost.
+ */
+ String removed() default "";
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Start.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Start.java
new file mode 100644
index 0000000..4106c0b
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Start.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotates a method which will be invoked when the Service is started.
+ * If you don't supply this annotation, then no "start" method will be invoked.
+ */
+@Retention(RetentionPolicy.CLASS)
+@Target(ElementType.METHOD)
+public @interface Start
+{
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Stop.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Stop.java
new file mode 100644
index 0000000..3d5ec17
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/api/Stop.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotates a method which is invoked when the Service is stopped.
+ * If you don't supply this annotation, then no "stop" method will be invoked.
+ */
+@Retention(RetentionPolicy.CLASS)
+@Target(ElementType.METHOD)
+public @interface Stop
+{
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java
new file mode 100644
index 0000000..f673461
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java
@@ -0,0 +1,453 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.plugin.bnd;
+
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.felix.dm.annotation.api.Composition;
+import org.apache.felix.dm.annotation.api.ConfigurationDependency;
+import org.apache.felix.dm.annotation.api.Destroy;
+import org.apache.felix.dm.annotation.api.Init;
+import org.apache.felix.dm.annotation.api.Service;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.annotation.api.Start;
+import org.apache.felix.dm.annotation.api.Stop;
+
+import aQute.lib.osgi.Annotation;
+import aQute.lib.osgi.ClassDataCollector;
+import aQute.lib.osgi.Verifier;
+import aQute.libg.reporter.Reporter;
+
+/**
+ * This is the scanner which does all the annotation parsing on a given class.
+ * Once parsed, the corresponding component descriptors can be built using the "writeTo" method.
+ */
+public class AnnotationCollector extends ClassDataCollector
+{
+ private final static String A_INIT = "L" + Init.class.getName().replace('.', '/') + ";";
+ private final static String A_START = "L" + Start.class.getName().replace('.', '/') + ";";
+ private final static String A_STOP = "L" + Stop.class.getName().replace('.', '/') + ";";
+ private final static String A_DESTROY = "L" + Destroy.class.getName().replace('.', '/') + ";";
+ private final static String A_COMPOSITION = "L" + Composition.class.getName().replace('.', '/')
+ + ";";
+ private final static String A_SERVICE = "L" + Service.class.getName().replace('.', '/') + ";";
+ private final static String A_SERVICE_DEP = "L"
+ + ServiceDependency.class.getName().replace('.', '/') + ";";
+ private final static String A_CONFIGURATION_DEPENDENCY = "L"
+ + ConfigurationDependency.class.getName().replace('.', '/') + ";";
+
+ private Reporter m_reporter;
+ private String m_className;
+ private String[] m_interfaces;
+ private boolean m_isField;
+ private String m_field;
+ private String m_method;
+ private String m_descriptor;
+ private Set<String> m_methods = new HashSet<String>();
+ private List<Info> m_infos = new ArrayList<Info>();
+
+ // Pattern used to parse the class parameter from the bind methods ("bind(Type)" or "bind(Map, Type)")
+ private final static Pattern m_bindClassPattern = Pattern.compile("\\((Ljava/util/Map;)?L([^;]+);\\)V");
+
+ // Pattern used to parse classes from class descriptors;
+ private final static Pattern m_classPattern = Pattern.compile("L([^;]+);");
+
+ // Pattern used to check if a method is void and does not take any params
+ private final static Pattern m_voidMethodPattern = Pattern.compile("\\(\\)V");
+
+ // Pattern used to check if a method returns an array of Objects
+ private final static Pattern m_methodCompoPattern = Pattern.compile("\\(\\)\\[Ljava/lang/Object;");
+
+ // List of component descriptor entry types
+ enum EntryTypes {
+ Service,
+ ServiceDependency,
+ ConfigurationDependency,
+ };
+
+ // List of component descriptor parameters
+ enum Params {
+ init,
+ start,
+ stop,
+ destroy,
+ impl,
+ provide,
+ properties,
+ factory,
+ factoryMethod,
+ composition,
+ service,
+ filter,
+ defaultImpl,
+ required,
+ added,
+ changed,
+ removed,
+ autoConfig,
+ pid,
+ propagate,
+ updated;
+ };
+
+ /**
+ * This class represents a parsed DependencyManager component descriptor entry.
+ * (either a Service, a ServiceDependency, or a ConfigurationDependency descriptor entry).
+ */
+ private class Info
+ {
+ EntryTypes m_entry;
+ Map<Params, Object> m_params = new HashMap<Params, Object>();
+
+ Info(EntryTypes entry)
+ {
+ m_entry = entry;
+ }
+
+ @Override
+ public String toString()
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.append(m_entry);
+ sb.append(":").append(" ");
+ for (Map.Entry<Params, Object> e : m_params.entrySet())
+ {
+ sb.append(e.getKey());
+ sb.append("=");
+ sb.append(e.getValue());
+ sb.append("; ");
+ }
+ return sb.toString();
+ }
+
+ void addParam(Params param, String value)
+ {
+ String old = (String) m_params.get(param);
+ if (old != null)
+ {
+ value = old + "," + value;
+ }
+ m_params.put(param, value);
+ }
+
+ void addParam(Annotation annotation, Params param, Object def)
+ {
+ Object value = annotation.get(param.toString());
+ if (value == null && def != null)
+ {
+ value = def;
+ }
+ if (value != null)
+ {
+ if (value instanceof Object[])
+ {
+ for (Object v : ((Object[]) value))
+ {
+ addParam(param, v.toString());
+ }
+ }
+ else
+ {
+ addParam(param, value.toString());
+ }
+ }
+ }
+
+ void addClassParam(Annotation annotation, Params param, Object def,
+ Pattern pattern, int group)
+ {
+ Object value = annotation.get(param.toString());
+ if (value == null && def != null)
+ {
+ value = def;
+ pattern = null;
+ }
+ if (value != null)
+ {
+ if (value instanceof Object[])
+ {
+ for (Object v : ((Object[]) value))
+ {
+ if (pattern != null)
+ {
+ v = parseClass(v.toString(), pattern, group);
+ }
+ addParam(param, v.toString());
+ }
+ }
+ else
+ {
+ if (pattern != null)
+ {
+ value = parseClass(value.toString(), pattern, group);
+ }
+ addParam(param, value.toString());
+ }
+ }
+
+ }
+ }
+
+ public AnnotationCollector(Reporter reporter)
+ {
+ m_reporter = reporter;
+ m_infos.add(new Info(EntryTypes.Service));
+ }
+
+ public Reporter getReporter()
+ {
+ return m_reporter;
+ }
+
+ @Override
+ public void classBegin(int access, String name)
+ {
+ m_className = name.replace('/', '.');
+ m_reporter.trace("class name: " + m_className);
+ }
+
+ @Override
+ public void implementsInterfaces(String[] interfaces)
+ {
+ m_interfaces = new String[interfaces.length];
+ for (int i = 0; i < interfaces.length; i++)
+ {
+ m_interfaces[i] = interfaces[i].replace('/', '.');
+ }
+ m_reporter.trace("implements: %s", Arrays.toString(m_interfaces));
+ }
+
+ /**
+ * Parses a method. Always invoked BEFORE eventual method annotation.
+ */
+ @Override
+ public void method(int access, String name, String descriptor)
+ {
+ m_reporter.trace("Parsed method %s, descriptor=%s", name, descriptor);
+ m_isField = false;
+ m_method = name;
+ m_descriptor = descriptor;
+ m_methods.add(name + descriptor);
+ }
+
+ /**
+ * Parses a field. Always invoked BEFORE eventual field annotation
+ */
+ @Override
+ public void field(int access, String name, String descriptor)
+ {
+ m_reporter.trace("Parsed field %s, descriptor=%s", name, descriptor);
+ m_isField = true;
+ m_field = name;
+ m_descriptor = descriptor;
+ }
+
+ /**
+ * An annotation has been parsed. Always invoked AFTER the "method"/"field"/"classBegin" callbacks.
+ */
+ @Override
+ public void annotation(Annotation annotation)
+ {
+ m_reporter.trace("Parsed annotation: %s", annotation);
+ if (annotation.getName().equals(A_INIT))
+ {
+ checkMethod(m_voidMethodPattern);
+ m_infos.get(0).m_params.put(Params.init, m_method);
+ }
+ else if (annotation.getName().equals(A_START))
+ {
+ checkMethod(m_voidMethodPattern);
+ m_infos.get(0).m_params.put(Params.start, m_method);
+ }
+ else if (annotation.getName().equals(A_STOP))
+ {
+ checkMethod(m_voidMethodPattern);
+ m_infos.get(0).m_params.put(Params.stop, m_method);
+ }
+ else if (annotation.getName().equals(A_DESTROY))
+ {
+ checkMethod(m_voidMethodPattern);
+ m_infos.get(0).m_params.put(Params.destroy, m_method);
+ }
+ else if (annotation.getName().equals(A_COMPOSITION))
+ {
+ checkMethod(m_methodCompoPattern);
+ m_infos.get(0).m_params.put(Params.composition, m_method);
+ }
+ else if (annotation.getName().equals(A_SERVICE))
+ {
+ parseServiceAnnotation(annotation);
+ }
+ else if (annotation.getName().equals(A_SERVICE_DEP))
+ {
+ parseServiceDependencyAnnotation(annotation);
+ }
+ else if (annotation.getName().equals(A_CONFIGURATION_DEPENDENCY))
+ {
+ parseConfigurationDependencyAnnotation(annotation);
+ }
+ }
+
+ private void parseServiceAnnotation(Annotation annotation)
+ {
+ Info info = m_infos.get(0);
+ // impl attribute
+ info.addParam(Params.impl, m_className);
+
+ // properties attribute
+ Object[] properties = annotation.get(Params.properties.toString());
+ if (properties != null)
+ {
+ for (Object property : properties)
+ {
+ String prop = property.toString().replace("=", ":");
+ info.addParam(Params.properties, prop);
+ }
+ }
+
+ // provide attribute
+ info.addClassParam(annotation, Params.provide, m_interfaces, m_classPattern, 1);
+
+ // factory attribute
+ info.addClassParam(annotation, Params.factory, null, m_classPattern, 1);
+
+ // factoryMethod attribute
+ info.addParam(annotation, Params.factoryMethod, null);
+ }
+
+ private void parseServiceDependencyAnnotation(Annotation annotation)
+ {
+ Info info = new Info(EntryTypes.ServiceDependency);
+ m_infos.add(info);
+
+ // service attribute
+ String service = annotation.get(Params.service.toString());
+ if (service != null)
+ {
+ service = parseClass(service, m_classPattern, 1);
+ }
+ else
+ {
+ if (m_isField)
+ {
+ service = parseClass(m_descriptor, m_classPattern, 1);
+ }
+ else
+ {
+ service = parseClass(m_descriptor, m_bindClassPattern, 2);
+ }
+ }
+ info.addParam(Params.service, service);
+
+ // autoConfig attribute
+ if (m_isField)
+ {
+ info.addParam(Params.autoConfig, m_field);
+ }
+
+ // filter attribute
+ String filter = annotation.get(Params.filter.toString());
+ if (filter != null)
+ {
+ Verifier.verifyFilter(filter, 0);
+ info.addParam(Params.filter, filter);
+ }
+
+ // defaultImpl attribute
+ info.addClassParam(annotation, Params.defaultImpl, null, m_classPattern, 1);
+
+ // required attribute
+ info.addParam(annotation, Params.required, null);
+
+ // added callback
+ info.addParam(annotation, Params.added, (!m_isField) ? m_method : null);
+
+ // changed callback
+ info.addParam(annotation, Params.changed, null);
+
+ // removed callback
+ info.addParam(annotation, Params.removed, null);
+ }
+
+ private void parseConfigurationDependencyAnnotation(Annotation annotation)
+ {
+ Info info = new Info(EntryTypes.ConfigurationDependency);
+ m_infos.add(info);
+
+ // pid attribute
+ info.addParam(annotation, Params.pid, m_className);
+
+ // propagate attribute
+ info.addParam(annotation, Params.propagate, null);
+ }
+
+ private String parseClass(String clazz, Pattern pattern, int group)
+ {
+ Matcher matcher = pattern.matcher(clazz);
+ if (matcher.matches())
+ {
+ return matcher.group(group).replace("/", ".");
+ }
+ else
+ {
+ m_reporter.warning("Invalid class descriptor: %s", clazz);
+ throw new IllegalArgumentException("Invalid class descriptor: " + clazz);
+ }
+ }
+
+ private void checkMethod(Pattern pattern)
+ {
+ Matcher matcher = pattern.matcher(m_descriptor);
+ if (!matcher.matches())
+ {
+ m_reporter.warning("Invalid method %s : wrong signature: %s", m_method, m_descriptor);
+ throw new IllegalArgumentException("Invalid method " + m_method + ", wrong signature: "
+ + m_descriptor);
+ }
+ }
+
+ public void finish()
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Parsed annotation for class ");
+ sb.append(m_className);
+ for (Info info : m_infos)
+ {
+ sb.append("\n\t").append(info.toString());
+ }
+ m_reporter.warning(sb.toString());
+ }
+
+ public void writeTo(PrintWriter pw)
+ {
+ for (Info info : m_infos)
+ {
+ pw.println(info.toString());
+ }
+ }
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationPlugin.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationPlugin.java
new file mode 100644
index 0000000..a907f8c
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/AnnotationPlugin.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.plugin.bnd;
+
+import java.util.Map;
+
+import aQute.bnd.service.AnalyzerPlugin;
+import aQute.lib.osgi.Analyzer;
+import aQute.lib.osgi.Resource;
+
+/**
+ * This class is a BND plugin. It will scans the target bundle for DependencyManager annotations.
+ * It can be directly used when using ant and can be referenced inside the ".bnd" descriptor, using
+ * the "-plugin" parameter.
+ */
+public class AnnotationPlugin implements AnalyzerPlugin
+{
+ /**
+ * This plugin is called after analysis of the JAR but before manifest
+ * generation. When some DM annotations are found, the plugin will add the corresponding
+ * DM component descriptors under OSGI-INF/ directory. It will also set the
+ * "DependencyManager-Component" manifest header (which references the descriptors paths).
+ *
+ * @param analyzer the object that is used to retrieve classes containing DM annotations.
+ * @return true if the classpace has been modified so that the bundle classpath must be reanalyzed
+ * @throws Exception on any errors.
+ */
+ public boolean analyzeJar(Analyzer analyzer) throws Exception
+ {
+ // We'll do the actual parsing using a DescriptorGenerator object.
+ DescriptorGenerator generator = new DescriptorGenerator(analyzer);
+ if (generator.execute())
+ {
+ // We have parsed some annotations: set the OSGi "DependencyManager-Component" header in the target bundle.
+ analyzer.setProperty("DependencyManager-Component", generator.getDescriptorPaths());
+
+ // And insert the generated descriptors into the target bundle.
+ Map<String, Resource> resources = generator.getDescriptors();
+ for (Map.Entry<String, Resource> entry : resources.entrySet())
+ {
+ analyzer.getJar().putResource(entry.getKey(), entry.getValue());
+ }
+ }
+ return false;
+ }
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/DescriptorGenerator.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/DescriptorGenerator.java
new file mode 100644
index 0000000..82f0705
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/bnd/DescriptorGenerator.java
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.plugin.bnd;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.dm.annotation.api.Service;
+
+import aQute.lib.osgi.Analyzer;
+import aQute.lib.osgi.Clazz;
+import aQute.lib.osgi.EmbeddedResource;
+import aQute.lib.osgi.Processor;
+import aQute.lib.osgi.Resource;
+import aQute.lib.osgi.Clazz.QUERY;
+
+/**
+ * This helper parses all classes which contain DM annotations, and generates the corresponding component descriptors.
+ */
+public class DescriptorGenerator extends Processor
+{
+ /**
+ * This is the bnd analyzer used to lookup classes containing DM annotations.
+ */
+ private Analyzer m_analyzer;
+
+ /**
+ * This is the generated Dependency Manager descriptors. The hashtable key is the path
+ * to a descriptor. The value is a bnd Resource object which contains the content of a
+ * descriptor.
+ */
+ Map<String, Resource> m_resources = new HashMap<String, Resource>();
+
+ /**
+ * Creates a new descriptor generator.
+ * @param analyzer The bnd analyzer used to lookup classes containing DM annotations.
+ */
+ public DescriptorGenerator(Analyzer analyzer)
+ {
+ super(analyzer);
+ m_analyzer = analyzer;
+ }
+
+ /**
+ * Starts the scanning.
+ * @return true if some annotations were successfully parsed, false if not. corresponding generated
+ * descriptors can then be retrieved by invoking the getDescriptors/getDescriptorPaths methods.
+ */
+ public boolean execute()
+ {
+ boolean annotationsFound = false;
+ Clazz clazz = null;
+ try
+ {
+ // Try to locate any classes in the wildcarded universe
+ // that are annotated with the DependencyManager "Service" annotations.
+ Collection<Clazz> expanded = m_analyzer.getClasses("",
+ // Then limit the ones with component annotations.
+ QUERY.ANNOTATION.toString(), Service.class.getName(),
+ // Parse everything
+ QUERY.NAMED.toString(), "*");
+
+ for (Clazz c : expanded)
+ {
+ clazz = c;
+ // Let's parse all annotations from that class !
+ AnnotationCollector reader = new AnnotationCollector(this);
+ c.parseClassFileWithCollector(reader);
+ reader.finish();
+ // And store the generated component descriptors in our resource list.
+ String name = c.getFQN();
+ Resource resource = createComponentResource(reader);
+ m_resources.put("OSGI-INF/" + name + ".dm", resource);
+ annotationsFound = true;
+ }
+
+ return annotationsFound;
+ }
+ catch (Throwable err)
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Error while scanning annotations");
+ if (clazz != null)
+ {
+ sb.append(" from class " + clazz);
+ }
+ sb.append(": ");
+ sb.append(err.toString());
+ error(sb.toString(), err.getCause());
+ return false;
+ }
+
+ finally
+ {
+ // Collect all logs (warns/errors) from our processor and store them into the analyze.
+ // Bnd will log them, if necessary.
+ m_analyzer.getInfo(this, "DependencyManager: ");
+ close();
+ }
+ }
+
+ /**
+ * Returns the path of the descriptor.
+ * @return the path of the generated descriptors.
+ */
+ public String getDescriptorPaths()
+ {
+ StringBuilder descriptorPaths = new StringBuilder();
+ String del = "";
+ for (Map.Entry<String, Resource> entry : m_resources.entrySet())
+ {
+ descriptorPaths.append(del);
+ descriptorPaths.append(entry.getKey());
+ del = ",";
+ }
+ return descriptorPaths.toString();
+ }
+
+ /**
+ * Returns the list of the generated descriptors.
+ * @return the list of the generated descriptors.
+ */
+ public Map<String, Resource> getDescriptors()
+ {
+ return m_resources;
+ }
+
+ /**
+ * Creates a bnd resource that contains the generated dm descriptor.
+ * @param collector
+ * @return
+ * @throws IOException
+ */
+ private Resource createComponentResource(AnnotationCollector collector) throws IOException
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
+ collector.writeTo(pw);
+ pw.close();
+ byte[] data = out.toByteArray();
+ out.close();
+ return new EmbeddedResource(data, 0);
+ }
+}
diff --git a/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/mvn/AnnotationMojo.java b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/mvn/AnnotationMojo.java
new file mode 100644
index 0000000..3b9e410
--- /dev/null
+++ b/dependencymanager/annotation/src/main/java/org/apache/felix/dm/annotation/plugin/mvn/AnnotationMojo.java
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.dm.annotation.plugin.mvn;
+
+import java.io.File;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.felix.dm.annotation.plugin.bnd.DescriptorGenerator;
+import org.apache.maven.model.Build;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+
+import aQute.lib.osgi.Analyzer;
+import aQute.lib.osgi.Jar;
+import aQute.lib.osgi.Resource;
+
+/**
+ * The <code>AnnotationMojo</code>
+ * generates a Dependency Manager component descriptor file based on annotations found from java classes.
+ *
+ * @goal scan
+ * @phase package
+ * @description Build DependencyManager component descriptors from class annotations.
+ * @requiresDependencyResolution compile
+ */
+public class AnnotationMojo extends AbstractMojo
+{
+ /**
+ * The Maven project.
+ *
+ * @parameter expression="${project}"
+ * @required
+ * @readonly
+ */
+ private MavenProject m_project;
+
+ /**
+ * The target extension
+ *
+ * @parameter default-value="jar"
+ * @required
+ */
+ private String m_artifactExtension;
+
+ /**
+ * Executes this mojo. We'll use the bnd library in order to scan classes from our target bundle.
+ */
+ public void execute() throws MojoExecutionException
+ {
+ Analyzer analyzer = null;
+ File output = null;
+ Jar jar = null;
+
+ try
+ {
+ // Get the name of our target bundle we are parsing for annotations.
+ File target = getBundleName();
+ getLog().info("Generating DM component descriptors for bundle " + target);
+
+ // Create a bnd analyzer and analyze our target bundle classes.
+ analyzer = new Analyzer();
+ analyzer.setJar(target);
+ analyzer.analyze();
+
+ // This helper class will parse classes using the analyzer we just created.
+ DescriptorGenerator generator = new DescriptorGenerator(analyzer);
+
+ // Start scanning
+ if (generator.execute())
+ {
+ // Some annotation has been parsed.
+ // Add the list of generated component descriptors in our special header.
+ jar = analyzer.getJar();
+ jar.getManifest().getMainAttributes().putValue("DependencyManager-Component",
+ generator.getDescriptorPaths());
+
+ // Add generated descriptors into the target bundle (we'll use a temp file).
+ Map<String, Resource> resources = generator.getDescriptors();
+ for (Map.Entry<String, Resource> entry : resources.entrySet())
+ {
+ jar.putResource(entry.getKey(), entry.getValue());
+ }
+ output = File.createTempFile(getClass().getName(), m_artifactExtension);
+ jar.write(output);
+ jar.close();
+ output.renameTo(target);
+ }
+
+ // Check if some errors have to be logged.
+ if (analyzer.getErrors().size() != 0)
+ {
+ for (Iterator<String> e = analyzer.getErrors().iterator(); e.hasNext();)
+ {
+ getLog().error(e.next());
+ }
+ throw new MojoExecutionException("Errors while generating dm descriptors");
+ }
+
+ // Check if some warnings have to be logged.
+ if (analyzer.getWarnings().size() != 0)
+ {
+ for (Iterator<String> e = analyzer.getWarnings().iterator(); e.hasNext();)
+ {
+ getLog().warn(e.next());
+ }
+ }
+ }
+
+ catch (MojoExecutionException e)
+ {
+ throw e;
+ }
+
+ catch (Throwable t)
+ {
+ getLog().warn("Exception while scanning annotation", t);
+ throw new MojoExecutionException(t.getMessage(), t.getCause());
+ }
+
+ finally
+ {
+ if (output != null && output.exists())
+ {
+ output.delete();
+ }
+
+ if (jar != null)
+ {
+ jar.close();
+ }
+ }
+ }
+
+ /**
+ * Returns the target name of this maven project.
+ * @return the target name of this maven project.
+ */
+ private File getBundleName()
+ {
+ Build build = m_project.getBuild();
+ return new File(build.getDirectory() + File.separator + build.getFinalName() + "."
+ + m_artifactExtension);
+ }
+}
\ No newline at end of file