use of org.apache.felix.scrplugin.description.PropertyDescription in project felix by apache.
the class SlingAnnotationProcessor method generateStringPropertyDescriptor.
/**
* Generates a property descriptor of type {@link PropertyType#String}
*/
private void generateStringPropertyDescriptor(final ClassAnnotation cad, final ClassDescription classDescription, final boolean metatype, String annotationName, String propertyDescriptorName) {
final String[] values = (String[]) cad.getValue(annotationName);
if (values == null) {
return;
}
final PropertyDescription pd = new PropertyDescription(cad);
pd.setName(propertyDescriptorName);
pd.setMultiValue(values);
pd.setType(PropertyType.String);
if (metatype) {
pd.setPrivate(true);
}
classDescription.add(pd);
}
use of org.apache.felix.scrplugin.description.PropertyDescription in project felix by apache.
the class SlingAnnotationProcessor method processSlingFilter.
/**
* Process SlingFilter
*/
private void processSlingFilter(final ClassAnnotation cad, final ClassDescription classDescription) {
// generate ComponentDescription if required
final boolean generateComponent = cad.getBooleanValue("generateComponent", true);
final boolean metatype = cad.getBooleanValue("metatype", !generateComponent);
if (generateComponent) {
final ComponentDescription cd = new ComponentDescription(cad);
cd.setName(cad.getStringValue("name", classDescription.getDescribedClass().getName()));
cd.setConfigurationPolicy(ComponentConfigurationPolicy.OPTIONAL);
cd.setLabel(cad.getStringValue("label", null));
cd.setDescription(cad.getStringValue("description", null));
cd.setCreateMetatype(metatype);
classDescription.add(cd);
}
// generate ServiceDescription if required
final boolean generateService = cad.getBooleanValue("generateService", true);
if (generateService) {
final ServiceDescription sd = new ServiceDescription(cad);
sd.addInterface("javax.servlet.Filter");
classDescription.add(sd);
}
// generate PropertyDescriptions
// property order = service.ranking
final int order = cad.getIntegerValue("order", 0);
final PropertyDescription pd = new PropertyDescription(cad);
pd.setName("service.ranking");
pd.setValue(String.valueOf(order));
pd.setType(PropertyType.Integer);
if (metatype) {
pd.setPrivate(true);
}
classDescription.add(pd);
// property pattern = sling.filter.pattern
final String pattern = cad.getStringValue("pattern", "");
if (!StringUtils.isEmpty(pattern)) {
final PropertyDescription pdPattern = new PropertyDescription(cad);
pdPattern.setName("sling.filter.pattern");
pdPattern.setValue(pattern);
pdPattern.setType(PropertyType.String);
if (metatype) {
pdPattern.setPrivate(true);
}
classDescription.add(pdPattern);
}
// property scope
final String[] scopes;
final Object val = cad.getValue("scope");
if (val != null) {
if (val instanceof String[]) {
final String[] arr = (String[]) val;
scopes = new String[arr.length / 2];
int i = 0;
int index = 0;
while (i < arr.length) {
scopes[index] = arr[i];
i += 2;
index++;
}
} else if (val instanceof String[][]) {
final String[][] arr = (String[][]) val;
scopes = new String[arr.length];
int index = 0;
while (index < arr.length) {
scopes[index] = arr[index][1];
index++;
}
} else {
scopes = new String[] { val.toString() };
}
} else {
scopes = new String[] { SlingFilterScope.REQUEST.getScope() };
}
final PropertyDescription pd2 = new PropertyDescription(cad);
pd2.setName("sling.filter.scope");
if (scopes.length == 1) {
pd2.setValue(scopes[0]);
} else {
pd2.setMultiValue(scopes);
}
pd2.setType(PropertyType.String);
if (metatype) {
pd2.setPrivate(true);
}
classDescription.add(pd2);
}
use of org.apache.felix.scrplugin.description.PropertyDescription in project felix by apache.
the class SCRDescriptorGenerator method processProperties.
/**
* Process property directives
*/
private void processProperties(final ClassDescription current, final ComponentContainer component, final MetatypeContainer ocd) {
for (final PropertyDescription pd : current.getDescriptions(PropertyDescription.class)) {
if (this.testProperty(current, component.getProperties(), pd, current == component.getClassDescription())) {
final String name = pd.getName();
if (org.osgi.framework.Constants.SERVICE_ID.equals(name)) {
iLog.addError("Class " + current.getDescribedClass().getName() + " is declaring " + "the protected property 'service.id'.", current.getSource());
continue;
}
if (ocd != null) {
// metatype - is this property private?
final boolean isPrivate;
if (pd.isPrivate() != null) {
isPrivate = pd.isPrivate();
} else {
if (isPrivateProperty(name)) {
isPrivate = true;
} else {
isPrivate = false;
}
}
if (!isPrivate) {
final MetatypeAttributeDefinition ad = new MetatypeAttributeDefinition();
ocd.getProperties().add(ad);
ad.setId(pd.getName());
ad.setType(pd.getType().name());
if (pd.getLabel() != null) {
ad.setName(pd.getLabel());
}
if (pd.getDescription() != null) {
ad.setDescription(pd.getDescription());
}
if (pd.getUnbounded() == PropertyUnbounded.DEFAULT) {
if (pd.getCardinality() != 0) {
ad.setCardinality(pd.getCardinality());
}
} else if (pd.getUnbounded() == PropertyUnbounded.ARRAY) {
// unlimited array
ad.setCardinality(new Integer(Integer.MAX_VALUE));
} else {
// unlimited vector
ad.setCardinality(new Integer(Integer.MIN_VALUE));
}
ad.setDefaultValue(pd.getValue());
ad.setDefaultMultiValue(pd.getMultiValue());
// check options
final String[] parameters = pd.getOptions();
if (parameters != null && parameters.length > 0) {
final Map<String, String> options = new LinkedHashMap<String, String>();
for (int j = 0; j < parameters.length; j = j + 2) {
final String optionLabel = parameters[j];
final String optionValue = (j < parameters.length - 1) ? parameters[j + 1] : null;
if (optionValue != null) {
options.put(optionLabel, optionValue);
}
}
ad.setOptions(options);
}
}
} else {
// additional metatype checks (FELIX-4033)
if (pd.isPrivate() != null && pd.isPrivate()) {
iLog.addWarning("Property " + pd.getName() + " in class " + current.getDescribedClass().getName() + " is set as private. " + "This is redundant as no metatype will be generated.", current.getSource());
}
}
}
}
}
use of org.apache.felix.scrplugin.description.PropertyDescription in project felix by apache.
the class ComponentDescriptorIO method generateXML.
/**
* Write the xml for a Component
*
* @param component
* @param contentHandler
* @throws SAXException
*/
private static void generateXML(final String namespace, final DescriptionContainer module, final ComponentContainer container, final ContentHandler contentHandler, final int indent) throws SAXException {
final ComponentDescription component = container.getComponentDescription();
final AttributesImpl ai = new AttributesImpl();
IOUtils.addAttribute(ai, COMPONENT_ATTR_ENABLED, component.getEnabled());
IOUtils.addAttribute(ai, COMPONENT_ATTR_IMMEDIATE, component.getImmediate());
IOUtils.addAttribute(ai, ATTR_NAME, component.getName());
IOUtils.addAttribute(ai, COMPONENT_ATTR_FACTORY, component.getFactory());
// attributes new in 1.1
if (module.getOptions().getSpecVersion().ordinal() >= SpecVersion.VERSION_1_1.ordinal()) {
if (component.getConfigurationPolicy() != null && component.getConfigurationPolicy() != ComponentConfigurationPolicy.OPTIONAL) {
IOUtils.addAttribute(ai, COMPONENT_ATTR_POLICY, component.getConfigurationPolicy().name().toLowerCase());
}
IOUtils.addAttribute(ai, COMPONENT_ATTR_ACTIVATE, component.getActivate());
IOUtils.addAttribute(ai, COMPONENT_ATTR_DEACTIVATE, component.getDeactivate());
IOUtils.addAttribute(ai, COMPONENT_ATTR_MODIFIED, component.getModified());
}
// attributes new in 1.2
if (module.getOptions().getSpecVersion().ordinal() >= SpecVersion.VERSION_1_2.ordinal()) {
if (component.getConfigurationPid() != null && !component.getConfigurationPid().equals(component.getName())) {
IOUtils.addAttribute(ai, COMPONENT_ATTR_CONFIGURATION_PID, component.getConfigurationPid());
}
}
IOUtils.indent(contentHandler, indent);
contentHandler.startElement(namespace, ComponentDescriptorIO.COMPONENT, ComponentDescriptorIO.COMPONENT_QNAME, ai);
IOUtils.newline(contentHandler);
for (final PropertyDescription property : container.getProperties().values()) {
generatePropertyXML(property, contentHandler, indent + 1);
}
if (container.getServiceDescription() != null) {
generateServiceXML(container.getServiceDescription(), contentHandler, indent + 1);
}
for (final ReferenceDescription reference : container.getReferences().values()) {
generateReferenceXML(component, module, reference, contentHandler, indent + 1);
}
generateImplementationXML(container, contentHandler, indent + 1);
IOUtils.indent(contentHandler, indent);
contentHandler.endElement(namespace, ComponentDescriptorIO.COMPONENT, ComponentDescriptorIO.COMPONENT_QNAME);
IOUtils.newline(contentHandler);
}
Aggregations