/*
 * Copyright 2011 eBay Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.raptorjs.resources.osgi;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

import org.osgi.framework.Bundle;
import org.raptorjs.resources.Resource;
import org.raptorjs.resources.SearchPathEntry;
import org.raptorjs.resources.URLResource;
import org.raptorjs.resources.ResourceManager.ResourceCallback;

public class BundleSearchPathEntry extends SearchPathEntry {

    private Bundle bundle = null;
    private String basePath = null;
    
    public BundleSearchPathEntry(Bundle bundle, String basePath) {
        this.bundle = bundle;
        this.basePath = basePath;
    }
    
    @Override
    public Resource findResource(String path) {
        
        if (this.bundle.getState() != Bundle.ACTIVE) return null;
        
        String fullPath = ("/".equals(this.basePath) ? "" : this.basePath) + path;
        
        URL resourceURL = this.bundle.getResource(fullPath);
        if (resourceURL != null) {
            return this.createBundleResource(path, fullPath, resourceURL);
        }
        return null;
    }
    
    @Override
    public void forEachResource(String path, ResourceCallback callback) {
    	String fullPath = this.basePath + path;
    	
    	Enumeration<URL> urls;
		try {
			urls = this.bundle.getResources(fullPath);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		
		if (urls != null) {
			while (urls.hasMoreElements()) {
	    		URL url = urls.nextElement();
	    		Resource resource = new URLResource(path, this, fullPath, url, true);
	    		callback.resourceFound(resource);
	    	}
		}
    }

    @Override
    public String toString() {
        return "BundleSearchPathEntry [bundle=" + bundle.getSymbolicName() + " (" + bundle.getBundleId() + ")" + ", basePath="
                + basePath + "]";
    }
    
    protected Resource createBundleResource(String path, String fullPath, URL url) {
    	return new BundleResource(path, this, this.bundle, fullPath, url);
    }

	public Bundle getBundle() {
		return bundle;
	}

	public String getBasePath() {
		return basePath;
	}
    
    
}
