Changeset 29875

Show
Ignore:
Timestamp:
02/02/08 18:13:22 (10 months ago)
Author:
gracinet
Message:

NXP 2039: New bundle lookup method based on bundle symbolic name

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • org.nuxeo.runtime/branches/1.4/nuxeo-runtime-test/src/main/java/org/nuxeo/runtime/test/NXRuntimeTestCase.java

    r28359 r29875  
    2323import java.net.URL; 
    2424import java.net.URLClassLoader; 
     25import java.util.HashMap; 
     26import java.util.HashSet; 
     27import java.util.Set; 
     28import java.util.jar.Attributes; 
     29import java.util.jar.Manifest; 
    2530 
    2631import org.apache.commons.logging.Log; 
     
    6772    private StandaloneBundleLoader bundleLoader; 
    6873 
     74    private Set<URL> readUrls; 
     75 
     76    private HashMap<String, BundleFile> bundles; 
     77 
    6978    @Override 
    7079    protected void setUp() throws Exception { 
     
    8594            FileUtils.deleteTree(workingDir); 
    8695        } 
     96        readUrls = null; 
     97        bundles = null; 
    8798        super.tearDown(); 
    8899    } 
     
    111122        bundleLoader.setExtractNestedJARs(false); 
    112123 
    113         BundleFile bundleFile = lookupBundle("nuxeo-runtime"); 
     124        BundleFile bundleFile = lookupBundle("org.nuxeo.runtime"); 
    114125        Bundle bundle = new RootRuntimeBundle(osgi, bundleFile, 
    115126                bundleLoader.getClass().getClassLoader(), true); 
     
    149160        } 
    150161        log.debug(sb.toString()); 
     162        readUrls = new HashSet<URL>(); 
     163        bundles = new HashMap<String, BundleFile>(); 
    151164    } 
    152165 
     
    194207    /** 
    195208     * Deploys a contribution file by looking for it in the class loader. 
    196      * 
     209     * <p>The first contribution file found by the class loader will be used. 
     210     * You have no guarantee in case of name collisions</p> 
     211     * 
     212     * @deprecated use the less ambiguous {@method deployContrib(bundleName, contrib)} 
    197213     * @param contrib the relative path to the contribution file 
    198214     */ 
     215    @Deprecated 
    199216    public void deployContrib(String contrib) { 
    200217        URL url = getResource(contrib); 
     
    289306    /** 
    290307     * Deploy a whole OSGI bundle. 
    291      * 
    292      * @param bundle the name of the bundle. 
     308     * <p>The lookup is first done on symbolic name, as set in <code>MANIFEST.MF</code> 
     309     * and then falls back to the bundle url (e.g., <code>nuxeo-platform-search-api</code>) 
     310     * for backwards compatibility. 
     311     * 
     312     * @param bundle the symbolic name 
    293313     * @throws Exception 
    294314     */ 
     
    299319    } 
    300320 
    301     protected BundleFile lookupBundle(String bundle) throws Exception { 
     321    protected String readSymbolicName(BundleFile bf) { 
     322        Manifest manifest = bf.getManifest(); 
     323        if (manifest == null) { 
     324            return null; 
     325        } 
     326        Attributes attrs = manifest.getMainAttributes(); 
     327        String name = (String) attrs.getValue("Bundle-SymbolicName"); 
     328        if (name == null) { 
     329            return null; 
     330        } 
     331        String[] sp = name.split(";", 2); 
     332        return sp[0]; 
     333    } 
     334 
     335    protected BundleFile lookupBundle(String bundleName) throws Exception { 
     336        BundleFile bundleFile = bundles.get(bundleName); 
     337        if (bundleFile != null) { 
     338            return bundleFile; 
     339        } 
     340        for (URL url: urls) { 
     341            if (readUrls.contains(url)) { 
     342                continue; 
     343            } 
     344            File file = new File(url.toURI()); 
     345            readUrls.add(url); 
     346            try { 
     347                if (file.isDirectory()) { 
     348                    bundleFile = new DirectoryBundleFile(file); 
     349                } else { 
     350                    bundleFile = new JarBundleFile(file); 
     351                } 
     352            } catch (IOException e) { 
     353                // no manifest => not a bundle 
     354                continue; 
     355            } 
     356            String symbolicName = readSymbolicName(bundleFile); 
     357            if (symbolicName != null) { 
     358                log.info(String.format("Bundle '%s' has URL %s", symbolicName, url)); 
     359                bundles.put(symbolicName, bundleFile); 
     360            } 
     361            if (bundleName.equals(symbolicName)) { 
     362                return bundleFile; 
     363            } 
     364        } 
     365        log.warn(String.format("No bundle with symbolic name '%s'; Falling back to deprecated url lookup scheme", bundleName)); 
     366        return oldLookupBundle(bundleName); 
     367    } 
     368 
     369    @Deprecated 
     370    protected BundleFile oldLookupBundle(String bundle) throws Exception { 
    302371        URL url = lookupBundleUrl(bundle); 
    303372        File file = new File(url.toURI()); 
     373        BundleFile bundleFile; 
    304374        if (file.isDirectory()) { 
    305             return new DirectoryBundleFile(file); 
     375            bundleFile = new DirectoryBundleFile(file); 
    306376        } else { 
    307             return new JarBundleFile(file); 
    308         } 
    309     } 
     377            bundleFile = new JarBundleFile(file); 
     378        } 
     379        log.warn(String.format( 
     380                "URL-based bundle lookup is deprecated. Please use the symbolic name from MANIFEST (%s) instead", 
     381                readSymbolicName(bundleFile)) 
     382                ); 
     383        return bundleFile; 
     384    } 
     385 
    310386 
    311387}