use of org.osgi.service.deploymentadmin.spi.ResourceProcessorException in project felix by apache.
the class StoreResourceTask method process.
public void process(String name, InputStream stream) throws ResourceProcessorException {
m_log.log(LogService.LOG_DEBUG, "processing " + name);
// initial validation
assertInDeploymentSession("Can not process resource without a Deployment Session");
Map<String, List<AutoConfResource>> toBeInstalled;
synchronized (m_lock) {
toBeInstalled = new HashMap<String, List<AutoConfResource>>(m_toBeInstalled);
}
MetaData data = parseAutoConfResource(stream);
// process resources
Filter filter = getFilter(data);
// add to session data
if (!toBeInstalled.containsKey(name)) {
toBeInstalled.put(name, new ArrayList<AutoConfResource>());
}
List<Designate> designates = data.getDesignates();
if (designates == null || designates.isEmpty()) {
// if there are no designates, there's nothing to process
m_log.log(LogService.LOG_INFO, "No designates found in the resource, so there's nothing to process.");
return;
}
Map<String, OCD> localOcds = data.getObjectClassDefinitions();
if (localOcds == null) {
localOcds = Collections.emptyMap();
}
for (Designate designate : designates) {
// check object
DesignateObject objectDef = designate.getObject();
if (objectDef == null) {
throw new ResourceProcessorException(CODE_OTHER_ERROR, "Designate Object child missing or invalid");
}
// check attributes
if (objectDef.getAttributes() == null || objectDef.getAttributes().isEmpty()) {
throw new ResourceProcessorException(CODE_OTHER_ERROR, "Object Attributes child missing or invalid");
}
// check ocdRef
String ocdRef = objectDef.getOcdRef();
if (ocdRef == null || "".equals(ocdRef)) {
throw new ResourceProcessorException(CODE_OTHER_ERROR, "Object ocdRef attribute missing or invalid");
}
// determine OCD
ObjectClassDefinition ocd = null;
OCD localOcd = localOcds.get(ocdRef);
// ask meta type service for matching OCD if no local OCD has been defined
ocd = (localOcd != null) ? new ObjectClassDefinitionImpl(localOcd) : getMetaTypeOCD(data, designate);
if (ocd == null) {
throw new ResourceProcessorException(CODE_OTHER_ERROR, "No Object Class Definition found with id=" + ocdRef);
}
// determine configuration data based on the values and their type definition
Dictionary dict = MetaTypeUtil.getProperties(designate, ocd);
if (dict == null) {
// designate does not match it's definition, but was marked optional, ignore it
continue;
}
AutoConfResource resource = new AutoConfResource(name, designate.getPid(), designate.getFactoryPid(), designate.getBundleLocation(), designate.isMerge(), dict, filter);
toBeInstalled.get(name).add(resource);
}
synchronized (m_lock) {
m_toBeInstalled.putAll(toBeInstalled);
}
m_log.log(LogService.LOG_DEBUG, "processing " + name + " done");
}
use of org.osgi.service.deploymentadmin.spi.ResourceProcessorException in project felix by apache.
the class StoreResourceTask method run.
public void run(PersistencyManager persistencyManager, ConfigurationAdmin configAdmin) throws Exception {
String name = m_resource.getName();
Dictionary properties = m_resource.getProperties();
String bundleLocation = m_resource.getBundleLocation();
Configuration configuration = null;
List existingResources = null;
try {
existingResources = persistencyManager.load(name);
} catch (IOException ioe) {
throw new ResourceProcessorException(ResourceProcessorException.CODE_PREPARE, "Unable to read existing resources for resource " + name, ioe);
}
// update configuration
if (m_resource.isFactoryConfig()) {
// check if this is an factory config instance update
for (Iterator i = existingResources.iterator(); i.hasNext(); ) {
AutoConfResource existingResource = (AutoConfResource) i.next();
if (m_resource.equalsTargetConfiguration(existingResource)) {
// existing instance found
configuration = configAdmin.getConfiguration(existingResource.getGeneratedPid(), bundleLocation);
existingResources.remove(existingResource);
break;
}
}
if (configuration == null) {
// no existing instance, create new
configuration = configAdmin.createFactoryConfiguration(m_resource.getFactoryPid(), bundleLocation);
}
m_resource.setGeneratedPid(configuration.getPid());
} else {
for (Iterator i = existingResources.iterator(); i.hasNext(); ) {
AutoConfResource existingResource = (AutoConfResource) i.next();
if (m_resource.getPid().equals(existingResource.getPid())) {
// existing resource found
existingResources.remove(existingResource);
break;
}
}
configuration = configAdmin.getConfiguration(m_resource.getPid(), bundleLocation);
if (!bundleLocation.equals(configuration.getBundleLocation())) {
// an existing configuration exists that is bound to a different location, which is not allowed
throw new ResourceProcessorException(ResourceProcessorException.CODE_PREPARE, "Existing configuration was bound to " + configuration.getBundleLocation() + " instead of " + bundleLocation);
}
}
if (m_resource.isMerge()) {
Dictionary existingProperties = configuration.getProperties();
if (existingProperties != null) {
Enumeration keys = existingProperties.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
properties.put(key, existingProperties.get(key));
}
}
}
configuration.update(properties);
}
use of org.osgi.service.deploymentadmin.spi.ResourceProcessorException in project felix by apache.
the class MetaTypeUtil method getValue.
/**
* Determines the value of an attribute based on an attribute definition
*
* @param attribute The attribute containing value(s)
* @param ad The attribute definition
* @return An <code>Object</code> reflecting what was specified in the attribute and it's definition or <code>null</code> if the value did not match it's definition.
* @throws ResourceProcessorException in case we're unable to parse the value of an attribute.
*/
private static Object getValue(Attribute attribute, AttributeDefinition ad) throws ResourceProcessorException {
if (attribute == null || ad == null || !attribute.getAdRef().equals(ad.getID())) {
// wrong attribute or definition
return null;
}
String[] content = attribute.getContent();
// verify correct type of the value(s)
int type = ad.getType();
Object[] typedContent = null;
try {
for (int i = 0; i < content.length; i++) {
String value = content[i];
switch(type) {
case AttributeDefinition.BOOLEAN:
typedContent = (typedContent == null) ? new Boolean[content.length] : typedContent;
typedContent[i] = Boolean.valueOf(value);
break;
case AttributeDefinition.BYTE:
typedContent = (typedContent == null) ? new Byte[content.length] : typedContent;
typedContent[i] = Byte.valueOf(value);
break;
case AttributeDefinition.CHARACTER:
typedContent = (typedContent == null) ? new Character[content.length] : typedContent;
char[] charArray = value.toCharArray();
if (charArray.length == 1) {
typedContent[i] = new Character(charArray[0]);
} else {
throw new ResourceProcessorException(CODE_OTHER_ERROR, "Unable to parse value for definition: adref=" + ad.getID());
}
break;
case AttributeDefinition.DOUBLE:
typedContent = (typedContent == null) ? new Double[content.length] : typedContent;
typedContent[i] = Double.valueOf(value);
break;
case AttributeDefinition.FLOAT:
typedContent = (typedContent == null) ? new Float[content.length] : typedContent;
typedContent[i] = Float.valueOf(value);
break;
case AttributeDefinition.INTEGER:
typedContent = (typedContent == null) ? new Integer[content.length] : typedContent;
typedContent[i] = Integer.valueOf(value);
break;
case AttributeDefinition.LONG:
typedContent = (typedContent == null) ? new Long[content.length] : typedContent;
typedContent[i] = Long.valueOf(value);
break;
case AttributeDefinition.SHORT:
typedContent = (typedContent == null) ? new Short[content.length] : typedContent;
typedContent[i] = Short.valueOf(value);
break;
case AttributeDefinition.STRING:
typedContent = (typedContent == null) ? new String[content.length] : typedContent;
typedContent[i] = value;
break;
default:
// unsupported type
throw new ResourceProcessorException(CODE_OTHER_ERROR, "Unsupported value-type for definition: adref=" + ad.getID());
}
}
} catch (NumberFormatException nfe) {
throw new ResourceProcessorException(CODE_OTHER_ERROR, "Unable to parse value for definition: adref=" + ad.getID());
}
// verify cardinality of value(s)
int cardinality = ad.getCardinality();
Object result = null;
if (cardinality == 0) {
if (typedContent.length == 1) {
result = typedContent[0];
} else {
result = null;
}
} else if (cardinality == Integer.MIN_VALUE) {
result = new Vector(Arrays.asList(typedContent));
} else if (cardinality == Integer.MAX_VALUE) {
result = typedContent;
} else if (cardinality < 0) {
if (typedContent.length <= Math.abs(cardinality)) {
result = new Vector(Arrays.asList(typedContent));
} else {
result = null;
}
} else if (cardinality > 0) {
if (typedContent.length <= cardinality) {
result = typedContent;
} else {
result = null;
}
}
return result;
}
use of org.osgi.service.deploymentadmin.spi.ResourceProcessorException in project felix by apache.
the class MetaTypeUtil method getProperties.
/**
* Determines the actual configuration data based on the specified designate and object class definition
*
* @param designate The designate object containing the values for the properties
* @param ocd The object class definition
* @return A dictionary containing data as described in the designate and ocd objects, or <code>null</code> if the designate does not match it's
* definition and the designate was marked as optional.
* @throws ResourceProcessorException If the designate does not match the ocd and the designate is not marked as optional.
*/
public static Dictionary getProperties(Designate designate, ObjectClassDefinition ocd) throws ResourceProcessorException {
Dictionary properties = new Hashtable();
AttributeDefinition[] attributeDefs = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);
List<Attribute> attributes = designate.getObject().getAttributes();
for (Attribute attribute : attributes) {
String adRef = attribute.getAdRef();
boolean found = false;
for (int j = 0; j < attributeDefs.length; j++) {
AttributeDefinition ad = attributeDefs[j];
if (adRef.equals(ad.getID())) {
// found attribute definition
Object value = getValue(attribute, ad);
if (value == null) {
if (designate.isOptional()) {
properties = null;
break;
} else {
throw new ResourceProcessorException(CODE_OTHER_ERROR, "Could not match attribute to it's definition: adref=" + adRef);
}
}
properties.put(adRef, value);
found = true;
break;
}
}
if (!found) {
if (designate.isOptional()) {
properties = null;
break;
} else {
throw new ResourceProcessorException(CODE_OTHER_ERROR, "Could not find attribute definition: adref=" + adRef);
}
}
}
return properties;
}
use of org.osgi.service.deploymentadmin.spi.ResourceProcessorException in project felix by apache.
the class AutoConfResourceProcessorTest method testMissingMandatoryValueInConfig.
/**
* Go through a simple session, containing two empty configurations.
*/
public void testMissingMandatoryValueInConfig() throws Throwable {
AutoConfResourceProcessor p = createAutoConfRP();
createNewSession(p);
String config = "<MetaData xmlns:metatype='http://www.osgi.org/xmlns/metatype/v1.1.0' filter='(id=42)'>\n" + " <OCD name='ocd' id='ocd'>\n" + " <AD id='name' type='Integer' />\n" + " </OCD>\n" + " <Designate pid='simple' bundle='osgi-dp:location'>\n" + " <Object ocdref='ocd'>\n" + " <Attribute adref='name'>\n" + " <Value><![CDATA[]]></Value>\n" + " </Attribute>\n" + " </Object>\n" + " </Designate>\n" + "</MetaData>\n";
try {
p.process("missing-value", new ByteArrayInputStream(config.getBytes()));
fail("Expected ResourceProcessorException for missing value!");
} catch (ResourceProcessorException e) {
// Ok; expected...
assertEquals("Unable to parse value for definition: adref=name", e.getMessage());
}
}
Aggregations