@Retention(value=CLASS)
@Target(value=ANNOTATION_TYPE)
public @interface PropertyType
Property
annotation on your component class name.
Now, you can also define your own component property type interfaces and apply them directly on your components (instead of the
Property
annotation). The PropertyType annotation is closely similar to standard OSGi r7 declarative service @ComponentPropertyType
(which is also supported by dependency manager annotations).
Then you can first define your own annotation (but you could also reuse the default annotations provided by the jaxrs whiteboard r7 api): (notice that in the annotation, you can define default service property values):
In the above, the underscore is mapped to ".". Then you can apply the above annotation on top of your component like this:@PropertyType @interface ServletContext { String osgi_http_whiteboard_context_name() default AppServletContext.NAME; String osgi_http_whiteboard_context_path(); }
You can also use configuration admin service in order to override the default service properties:@Component @ServletContext(osgi_http_whiteboard_context_path="/game") public class AppServletContext extends ServletContextHelper { }
You can also define multiple property type annotations, and possibly single valued annotation. In this case, you can use the standard R7 PREFIX_ constants in order to specify the property prefix, and the property name will be derived from the single valued annotation (using camel case convention):@Component @ServletContext(osgi_http_whiteboard_context_path="/game") public class AppServletContext extends ServletContextHelper { @ConfigurationDependency(propagate=true, pid="my.pid") void updated(ServletContext cnf) { // if some properties are not present in the configuration, then the ones used in the annotation will be used. // The configuration admin properties, if defined, will override the default configurations defined in the annotations } }
Same example as above, but also using configuration admin service in order to override default service properties: Here, as in OSGi r7 declarative service, you can define a callback method which accepts as arguments all (or some of) the defined property types:@PropertyType @interface ContextName { // will map to "osgi.http.whiteboard.context.name" property name String PREFIX="osgi.http.whiteboard."; String value(); } @PropertyType @interface ContextPath { // will map to "osgi.http.whiteboard.context.path" property name String PREFIX="osgi.http.whiteboard."; String value(); } @Component @ContextName(AppServletContext.NAME) @ContextPath("/game") public class AppServletContext extends ServletContextHelper { }
The following is the same example as above, but this time the configuration callback can also define a Dictionary in the first argument (in case you want to also get the raw configuration dictionary:@Component @ContextName(AppServletContext.NAME) @ContextPath("/game") public class AppServletContext extends ServletContextHelper { @ConfigurationDependency(propagate=true, pid="my.pid") void updated(ContextName ctxName, ContextPath ctxPath) { // if some properties are not present in the configuration, then the ones used in the annotation will be used. // The configuration admin properties, if defined, will override the default configurations defined in the annotations } }
@Component @ContextName(AppServletContext.NAME) @ContextPath("/game") public class AppServletContext extends ServletContextHelper { @ConfigurationDependency(propagate=true, pid="my.pid") void updated(Dictionary<String, Object> rawConfig, ContextName ctxName) { // if some properties are not present in the configuration, then the ones used in the annotation will be used. // The configuration admin properties, if defined, will override the default configurations defined in the annotations } }