Search in sources :

Example 1 with StringMap

use of org.apache.felix.framework.util.StringMap in project felix by apache.

the class Service method init.

public void init(InstallationLayout suppliedLayout, String[] args) throws Exception {
    if (!(suppliedLayout instanceof FelixLayout)) {
        this.layout = new FelixLayout(suppliedLayout);
    } else {
        this.layout = (FelixLayout) suppliedLayout;
    }
    configationProperties = readConfigProperties();
    instance = new Felix(new StringMap(configationProperties, false), null);
}
Also used : StringMap(org.apache.felix.framework.util.StringMap) Felix(org.apache.felix.framework.Felix)

Example 2 with StringMap

use of org.apache.felix.framework.util.StringMap in project felix by apache.

the class JarRevision method getManifestHeader.

public Map<String, Object> getManifestHeader() throws Exception {
    // Read and parse headers into a case insensitive map of manifest attributes and return it.
    ZipEntry manifestEntry = m_zipFile.getEntry("META-INF/MANIFEST.MF");
    Map<String, Object> manifest = manifestEntry != null ? BundleCache.getMainAttributes(new StringMap(), m_zipFile.getInputStream(manifestEntry), manifestEntry.getSize()) : null;
    return manifest;
}
Also used : StringMap(org.apache.felix.framework.util.StringMap) ZipEntry(java.util.zip.ZipEntry)

Example 3 with StringMap

use of org.apache.felix.framework.util.StringMap in project felix by apache.

the class DirectoryRevision method getManifestHeader.

public synchronized Map<String, Object> getManifestHeader() throws Exception {
    // Read the header file from the reference directory.
    InputStream is = null;
    try {
        // Open manifest file.
        is = BundleCache.getSecureAction().getFileInputStream(new File(m_refDir, "META-INF/MANIFEST.MF"));
        // Error if no jar file.
        if (is == null) {
            throw new IOException("No manifest file found.");
        }
        // Get manifest.
        Manifest mf = new Manifest(is);
        // Create a case insensitive map of manifest attributes.
        return new StringMap(mf.getMainAttributes());
    } finally {
        if (is != null)
            is.close();
    }
}
Also used : StringMap(org.apache.felix.framework.util.StringMap) InputStream(java.io.InputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) File(java.io.File)

Example 4 with StringMap

use of org.apache.felix.framework.util.StringMap in project felix by apache.

the class BundleImpl method getCurrentLocalizedHeader.

Map getCurrentLocalizedHeader(String locale) {
    Map result = null;
    // Spec says empty local returns raw headers.
    if (locale.length() == 0) {
        result = new StringMap(adapt(BundleRevisionImpl.class).getHeaders());
    }
    // If we have no result, try to get it from the cached headers.
    if (result == null) {
        synchronized (m_cachedHeaders) {
            // the spec.
            if (m_uninstalledHeaders != null) {
                result = m_uninstalledHeaders;
            } else // If the bundle has been updated, clear the cached headers.
            if (getLastModified() > m_cachedHeadersTimestamp) {
                m_cachedHeaders.clear();
            } else // Otherwise, returned the cached headers if they exist.
            {
                // Check if headers for this locale have already been resolved
                if (m_cachedHeaders.containsKey(locale)) {
                    result = (Map) m_cachedHeaders.get(locale);
                }
            }
        }
    }
    // If the requested locale is not cached, then try to create it.
    if (result == null) {
        // Get a modifiable copy of the raw headers.
        Map headers = new StringMap(adapt(BundleRevisionImpl.class).getHeaders());
        // Assume for now that this will be the result.
        result = headers;
        // Check to see if we actually need to localize anything
        boolean localize = false;
        for (Iterator it = headers.values().iterator(); !localize && it.hasNext(); ) {
            if (((String) it.next()).startsWith("%")) {
                localize = true;
            }
        }
        if (!localize) {
            // If localization is not needed, just cache the headers and return
            // them as-is. Not sure if this is useful
            updateHeaderCache(locale, headers);
        } else {
            // Do localization here and return the localized headers
            String basename = (String) headers.get(Constants.BUNDLE_LOCALIZATION);
            if (basename == null) {
                basename = Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME;
            }
            // Create ordered list of revisions to search for localization
            // property resources.
            List<BundleRevision> revisionList = createLocalizationRevisionList(adapt(BundleRevisionImpl.class));
            // Create ordered list of files to load properties from
            List<String> resourceList = createLocalizationResourceList(basename, locale);
            // Create a merged props file with all available props for this locale
            boolean found = false;
            Properties mergedProperties = new Properties();
            for (BundleRevision br : revisionList) {
                for (String res : resourceList) {
                    URL temp = ((BundleRevisionImpl) br).getEntry(res + ".properties");
                    if (temp != null) {
                        found = true;
                        try {
                            mergedProperties.load(temp.openConnection().getInputStream());
                        } catch (IOException ex) {
                        // File doesn't exist, just continue loop
                        }
                    }
                }
            }
            // return the default localization.
            if (!found && !locale.equals(Locale.getDefault().toString())) {
                result = getCurrentLocalizedHeader(Locale.getDefault().toString());
            } else // Otherwise, perform the localization based on the discovered
            // properties and cache the result.
            {
                // Resolve all localized header entries
                for (Iterator it = headers.entrySet().iterator(); it.hasNext(); ) {
                    Map.Entry entry = (Map.Entry) it.next();
                    String value = (String) entry.getValue();
                    if (value.startsWith("%")) {
                        String newvalue;
                        String key = value.substring(value.indexOf("%") + 1);
                        newvalue = mergedProperties.getProperty(key);
                        if (newvalue == null) {
                            newvalue = key;
                        }
                        entry.setValue(newvalue);
                    }
                }
                updateHeaderCache(locale, headers);
            }
        }
    }
    return result;
}
Also used : StringMap(org.apache.felix.framework.util.StringMap) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL) Iterator(java.util.Iterator) BundleRevision(org.osgi.framework.wiring.BundleRevision) HashMap(java.util.HashMap) Map(java.util.Map) StringMap(org.apache.felix.framework.util.StringMap)

Aggregations

StringMap (org.apache.felix.framework.util.StringMap)4 IOException (java.io.IOException)2 File (java.io.File)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Manifest (java.util.jar.Manifest)1 ZipEntry (java.util.zip.ZipEntry)1 Felix (org.apache.felix.framework.Felix)1 BundleRevision (org.osgi.framework.wiring.BundleRevision)1