001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *  http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.xbean.osgi.bundle.util.equinox;
021
022import java.io.IOException;
023import java.net.URL;
024import java.util.Enumeration;
025
026import org.apache.xbean.osgi.bundle.util.BundleResourceHelper;
027import org.eclipse.osgi.service.urlconversion.URLConverter;
028import org.osgi.framework.Bundle;
029import org.osgi.framework.BundleContext;
030import org.osgi.framework.ServiceReference;
031
032/**
033 * Equinox implementation of BundleResourceHelper.
034 * <br/>
035 * This implementation of the {@link BundleResourceHelper} converts resource URLs
036 * returned by {@link #getResource(String)} and {@link #getResources(String)} into <tt>jar</tt> URLs
037 * using Equinox's <tt>URLConverter</tt> service.
038 * 
039 * @version $Rev: 1067587 $ $Date: 2011-02-06 06:15:22 +0100 (Sun, 06 Feb 2011) $
040 */
041public class EquinoxBundleResourceHelper extends BundleResourceHelper {
042
043    private URLConverter converter;
044    
045    public EquinoxBundleResourceHelper(Bundle bundle, boolean searchWiredBundles, boolean convertResourceUrls) {
046        super(bundle, searchWiredBundles, convertResourceUrls);
047        init();
048    }
049
050    private void init() {
051        BundleContext context = bundle.getBundleContext();
052        if (context != null) {
053            ServiceReference urlReference = context.getServiceReference(URLConverter.class.getName());
054            if (urlReference != null) { 
055                converter = (URLConverter) context.getService(urlReference);
056            }
057        }
058    }
059    
060    @Override
061    public URL getResource(String name) {
062        if (convertResourceUrls) {
063            return (converter == null) ? convertedFindResource(name) : findResource(name);            
064        } else {
065            return findResource(name);
066        }
067    }
068    
069    @Override
070    public Enumeration<URL> getResources(String name) throws IOException {
071        if (convertResourceUrls) {
072            return (converter == null) ? convertedFindResources(name) : findResources(name);   
073        } else {
074            return findResources(name);
075        }
076    }
077          
078    @Override
079    protected URL convert(URL url) {
080        try {
081            URL convertedURL = converter.resolve(url);
082            return convertedURL;
083        } catch (IOException e) {
084            e.printStackTrace();
085            return url;
086        }
087    }
088       
089}