Search in sources :

Example 1 with BundleContextPropertyResolver

use of org.ops4j.pax.swissbox.property.BundleContextPropertyResolver in project karaf by apache.

the class FeaturesProcessingSerializer method read.

/**
 * Reads {@link FeaturesProcessing features processing model} from input stream
 * @param stream
 * @param versions additional properties to resolve placeholders in features processing XML
 * @return
 */
public FeaturesProcessing read(InputStream stream, Properties versions) throws Exception {
    Unmarshaller unmarshaller = FEATURES_PROCESSING_CONTEXT.createUnmarshaller();
    UnmarshallerHandler handler = unmarshaller.getUnmarshallerHandler();
    // BundleContextPropertyResolver gives access to e.g., ${karaf.base}
    final PropertyResolver resolver = bundleContext == null ? new DictionaryPropertyResolver(versions) : new DictionaryPropertyResolver(versions, new BundleContextPropertyResolver(bundleContext));
    // indirect unmarshaling with property resolution inside XML attribute values and CDATA
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
    xmlReader.setContentHandler(new ResolvingContentHandler(new Properties() {

        @Override
        public String getProperty(String key) {
            return resolver.get(key);
        }

        @Override
        public String getProperty(String key, String defaultValue) {
            String value = resolver.get(key);
            return value == null ? defaultValue : value;
        }
    }, handler));
    xmlReader.parse(new InputSource(stream));
    return (FeaturesProcessing) handler.getResult();
}
Also used : BundleContextPropertyResolver(org.ops4j.pax.swissbox.property.BundleContextPropertyResolver) InputSource(org.xml.sax.InputSource) UnmarshallerHandler(javax.xml.bind.UnmarshallerHandler) DictionaryPropertyResolver(org.ops4j.util.property.DictionaryPropertyResolver) Unmarshaller(javax.xml.bind.Unmarshaller) DictionaryPropertyResolver(org.ops4j.util.property.DictionaryPropertyResolver) PropertyResolver(org.ops4j.util.property.PropertyResolver) BundleContextPropertyResolver(org.ops4j.pax.swissbox.property.BundleContextPropertyResolver) Properties(java.util.Properties) XMLReader(org.xml.sax.XMLReader) FeaturesProcessing(org.apache.karaf.features.internal.model.processing.FeaturesProcessing) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

Properties (java.util.Properties)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 UnmarshallerHandler (javax.xml.bind.UnmarshallerHandler)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 FeaturesProcessing (org.apache.karaf.features.internal.model.processing.FeaturesProcessing)1 BundleContextPropertyResolver (org.ops4j.pax.swissbox.property.BundleContextPropertyResolver)1 DictionaryPropertyResolver (org.ops4j.util.property.DictionaryPropertyResolver)1 PropertyResolver (org.ops4j.util.property.PropertyResolver)1 InputSource (org.xml.sax.InputSource)1 XMLReader (org.xml.sax.XMLReader)1