PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/nl/bitbrains/nebu/rest/client/VirtItemCacheLoader.java

https://github.com/deltaforge/nebu-core
Java | 58 lines | 37 code | 9 blank | 12 comment | 0 complexity | c7b9df0da144f8788874c8f104b76186 MD5 | raw file
 1package nl.bitbrains.nebu.rest.client;
 2
 3import java.text.ParseException;
 4
 5import javax.ws.rs.client.Invocation;
 6import javax.ws.rs.core.Response;
 7
 8import nl.bitbrains.nebu.common.VirtualMachine;
 9import nl.bitbrains.nebu.common.cache.CacheLoader;
10import nl.bitbrains.nebu.common.factories.VirtualMachineFactory;
11import nl.bitbrains.nebu.common.util.xml.XMLConverter;
12import nl.bitbrains.nebu.rest.RESTRequestException;
13
14import org.apache.logging.log4j.LogManager;
15import org.apache.logging.log4j.Logger;
16import org.jdom2.Element;
17import org.w3c.dom.Document;
18
19/**
20 * Can update the /virt/uuid resource.
21 * 
22 * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
23 * 
24 */
25public class VirtItemCacheLoader implements CacheLoader<Object> {
26
27    private final String uuid;
28    private static Logger logger = LogManager.getLogger();
29
30    /**
31     * Simple constructor, takes the uuid to fetch.
32     * 
33     * @param uuid
34     *            to fetch the information of.
35     */
36    public VirtItemCacheLoader(final String uuid) {
37        this.uuid = uuid;
38    }
39
40    @Override
41    public final VirtualMachine refresh() throws RESTRequestException {
42        VirtItemCacheLoader.logger.entry();
43        final Invocation.Builder builder = RequestBuilder.get()
44                .newGetVirtualMachineClient(this.uuid);
45        final Document doc = RequestSender.performGETRequestAndCheckResponse(builder,
46                                                                             Document.class);
47        final Element elem = XMLConverter.convertW3CDocumentJDOMElement(doc);
48        final VirtualMachineFactory factory = new VirtualMachineFactory();
49        try {
50            return VirtItemCacheLoader.logger.exit(factory.fromXML(elem).build());
51        } catch (final ParseException e) {
52            throw VirtItemCacheLoader.logger.throwing(new RESTRequestException(
53                    RequestSender.INVALID_RESPONSE, Response.Status.INTERNAL_SERVER_ERROR
54                            .getStatusCode(), e));
55        }
56    }
57
58}