All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.tweeenveertig.openstack.client.mock.OnFileObjectStoreLoader Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package nl.tweeenveertig.openstack.client.mock;

import nl.tweeenveertig.openstack.model.Account;
import nl.tweeenveertig.openstack.model.Container;
import nl.tweeenveertig.openstack.model.StoredObject;

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

public class OnFileObjectStoreLoader {

    private ClassLoader classLoader = OnFileObjectStoreLoader.class.getClassLoader();

    public void createContainers(Account account, String onFileObjectStore) throws IOException, URISyntaxException {
        Enumeration urls = classLoader.getResources(onFileObjectStore);
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            for (File containerFile : listFiles(url)) {
                Container container = account.getContainer(containerFile.getName());
                container.create();
                storeObjects(container, onFileObjectStore + "/" + containerFile.getName());
            }
        }
    }

    public void storeObjects(Container container, String containerLocation) throws IOException, URISyntaxException {
        Enumeration containerUrls = classLoader.getResources(containerLocation);
        while (containerUrls.hasMoreElements()) {
            URL containerURL = containerUrls.nextElement();
            for (File objectFile : listFiles(containerURL)) {
                StoredObject object = container.getObject(objectFile.getName());
                object.uploadObject(objectFile);
            }
        }
    }

    public File[] listFiles(URL url) throws URISyntaxException {
        if (url != null && url.getProtocol().equals("file")) {
            File file = new File(url.toURI());
            if (file.isDirectory()) {
                return new File(url.toURI()).listFiles();
            }
        }
        return new File[] {};
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy