⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bundlerepositoryserviceimpl.java

📁 OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        return sources[0];    }    /**     * Returns an array of bundle records that resolve the supplied     * package declaration.     * @param target the package declaration to resolve.     * @return an array of bundle records that resolve the package     *         declaration or <tt>null</tt> if none are found.    **/    private synchronized BundleRecord[] findResolvingBundles(PackageDeclaration targetPkg)    {        // Create a list for storing bundles that can resolve package.        ArrayList resolveList = new ArrayList();        // Get the exporter list from the export package map for this package.        ArrayList exporterList = (ArrayList) m_exportPackageMap.get(targetPkg.getName());        // Loop through each exporter and see if it satisfies the target.        for (int i = 0; (exporterList != null) && (i < exporterList.size()); i++)        {            // Get the export info from the exporter list.            Object[] exportInfo = (Object[]) exporterList.get(i);            // Get the export package from the export info.            PackageDeclaration exportPkg = (PackageDeclaration)                exportInfo[EXPORT_PACKAGE_IDX];            // Get the export bundle from the export info.            BundleRecord exportBundle = (BundleRecord)                exportInfo[EXPORT_BUNDLE_IDX];            // See if the export package satisfies the target package.            if (exportPkg.doesSatisfy(targetPkg))            {                // Add it to the list of resolving bundles.                resolveList.add(exportBundle);            }        }        // If no resolving bundles were found, return null.        if (resolveList.size() == 0)        {            return null;        }        // Otherwise, return an array containing resolving bundles.        return (BundleRecord[]) resolveList.toArray(new BundleRecord[resolveList.size()]);    }    private void initialize()    {        m_initialized = true;        m_bundleList.clear();        m_exportPackageMap.clear();        for (int urlIdx = 0; (m_urls != null) && (urlIdx < m_urls.length); urlIdx++)        {            parseRepositoryFile(m_hopCount, m_urls[urlIdx]);        }    }        private void parseRepositoryFile(int hopCount, String urlStr)    {        InputStream is = null;        InputStreamReader isr = null;        BufferedReader br = null;        try        {            // Do it the manual way to have a chance to             // set request properties as proxy auth (EW).            URL url = new URL(urlStr);            URLConnection conn = url.openConnection();             // Support for http proxy authentication            String auth = System.getProperty("http.proxyAuth");            if ((auth != null) && (auth.length() > 0))            {                if ("http".equals(url.getProtocol()) ||                    "https".equals(url.getProtocol()))                {                    String base64 = Util.base64Encode(auth);                    conn.setRequestProperty(                        "Proxy-Authorization", "Basic " + base64);                }            }            is = conn.getInputStream();            // Create the parser Kxml            XmlCommonHandler handler = new XmlCommonHandler();            handler.addType("bundles", ArrayList.class);            handler.addType("repository", HashMap.class);            handler.addType("extern-repositories", ArrayList.class);            handler.addType("bundle", MultivalueMap.class);            handler.addType("import-package", HashMap.class);            handler.addType("export-package", HashMap.class);            handler.setDefaultType(String.class);            br = new BufferedReader(new InputStreamReader(is));            KXmlSAXParser parser;            parser = new KXmlSAXParser(br);            try            {                parser.parseXML(handler);            }            catch (Exception ex)            {                ex.printStackTrace();                return;            }            List root = (List) handler.getRoot();            for (int bundleIdx = 0; bundleIdx < root.size(); bundleIdx++)            {                Object obj = root.get(bundleIdx);                                // The elements of the root will either be a HashMap for                // the repository tag or a MultivalueMap for the bundle                // tag, as indicated above when we parsed the file.                                // If HashMap, then read repository information.                if (obj instanceof HashMap)                {                    // Create a case-insensitive map.                    Map repoMap = new TreeMap(new Comparator() {                        public int compare(Object o1, Object o2)                        {                            return o1.toString().compareToIgnoreCase(o2.toString());                        }                    });                    repoMap.putAll((Map) obj);                    // Process external repositories if hop count is                    // greater than zero.                    if (hopCount > 0)                    {                        // Get the external repository list.                        List externList = (List) repoMap.get(EXTERN_REPOSITORY_TAG);                        for (int i = 0; (externList != null) && (i < externList.size()); i++)                        {                            parseRepositoryFile(hopCount - 1, (String) externList.get(i));                        }                    }                }                // Else if mulitvalue map, then create a bundle record                // for the associated bundle meta-data.                else if (obj instanceof MultivalueMap)                {                    // Create a case-insensitive map.                    Map bundleMap = new TreeMap(new Comparator() {                        public int compare(Object o1, Object o2)                        {                            return o1.toString().compareToIgnoreCase(o2.toString());                        }                    });                    bundleMap.putAll((Map) obj);                                        // Convert any import package declarations                    // to PackageDeclaration objects.                    Object target = bundleMap.get(BundleRecord.IMPORT_PACKAGE);                    if (target != null)                    {                        // Overwrite the original package declarations.                        bundleMap.put(                            BundleRecord.IMPORT_PACKAGE,                            convertPackageDeclarations(target));                    }                    // Convert any export package declarations                    // to PackageDeclaration objects.                    target = bundleMap.get(BundleRecord.EXPORT_PACKAGE);                    if (target != null)                    {                        // Overwrite the original package declarations.                        bundleMap.put(                            BundleRecord.EXPORT_PACKAGE,                            convertPackageDeclarations(target));                    }                    // Create a bundle record using the map.                    BundleRecord record = new BundleRecord(bundleMap);                    // Try to put all exported packages from this bundle                    // into the export package map to simplify access.                    try                    {                        PackageDeclaration[] exportPkgs = (PackageDeclaration[])                            record.getAttribute(BundleRecord.EXPORT_PACKAGE);                        for (int exportIdx = 0;                            (exportPkgs != null) && (exportIdx < exportPkgs.length);                            exportIdx++)                        {                            // Check to see if this package is already in the                            // export package map.                            ArrayList exporterList = (ArrayList)                                m_exportPackageMap.get(exportPkgs[exportIdx].getName());                            // If the package is not in the map, create an                            // array list for it.                            if (exporterList == null)                            {                                exporterList = new ArrayList();                            }                            // Add the export info to the array list.                            Object[] exportInfo = new Object[2];                            exportInfo[EXPORT_PACKAGE_IDX] = exportPkgs[exportIdx];                            exportInfo[EXPORT_BUNDLE_IDX] = record;                            exporterList.add(exportInfo);                            // Put the array list containing the export info                            // into the export map, which will make it easy                            // to search for which bundles export what. Note,                            // if the exporterList already was in the map, this                            // will just overwrite it with the same value.                            m_exportPackageMap.put(                                exportPkgs[exportIdx].getName(), exporterList);                        }                        // TODO: Filter duplicates.                        m_bundleList.add(record);                    }                    catch (IllegalArgumentException ex)                    {                        // Ignore.                    }                }            }        }        catch (MalformedURLException ex)        {            System.err.println("Error: " + ex);        }        catch (IOException ex)        {            System.err.println("Error: " + ex);        }        finally        {            try            {                if (is != null) is.close();            }            catch (IOException ex)            {                // Not much we can do.            }        }        Collections.sort(m_bundleList, new Comparator() {            public int compare(Object o1, Object o2)            {                BundleRecord r1 = (BundleRecord) o1;                BundleRecord r2 = (BundleRecord) o2;                String name1 = (String) r1.getAttribute(BundleRecord.BUNDLE_NAME);                String name2 = (String) r2.getAttribute(BundleRecord.BUNDLE_NAME);                return name1.compareToIgnoreCase(name2);            }        });    }    private PackageDeclaration[] convertPackageDeclarations(Object target)    {        PackageDeclaration[] decls = null;        // If there is only one package it will be a        // Map as specified above when parsing.        if (target instanceof Map)        {            // Put package declaration into an array.            decls = new PackageDeclaration[1];            decls[0] = convertPackageMap((Map) target);        }        // If there is more than one package, then the        // MultivalueMap will convert them to a list of maps.        else if (target instanceof List)        {            List pkgList = (List) target;            decls = new PackageDeclaration[pkgList.size()];            for (int pkgIdx = 0; pkgIdx < decls.length; pkgIdx++)            {                decls[pkgIdx] = convertPackageMap(                    (Map) pkgList.get(pkgIdx));            }        }        return decls;    }        private PackageDeclaration convertPackageMap(Map map)    {        // Create a case-insensitive map.        Map pkgMap = new TreeMap(new Comparator() {            public int compare(Object o1, Object o2)            {                return o1.toString().compareToIgnoreCase(o2.toString());            }        });        pkgMap.putAll((Map) map);        // Get package name and version.        String name = (String) pkgMap.get(PackageDeclaration.PACKAGE_ATTR);        String version = (String) pkgMap.get(PackageDeclaration.VERSION_ATTR);        if (name != null)        {            return new PackageDeclaration(name, version);        }        return null;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -