[demo] / insanejava / trunk / rest-demo / src / main / java / no / brodwall / insanejava / rest / RESTClientRepository.java Repository:
ViewVC logotype

View of /insanejava/trunk/rest-demo/src/main/java/no/brodwall/insanejava/rest/RESTClientRepository.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 187 - (download) (as text) (annotate)
Thu Aug 30 22:15:18 2007 UTC (3 years ago) by johannes
File size: 2729 byte(s)
Decreased use of generics
package no.brodwall.insanejava.rest;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.LazyLoader;


public class RESTClientRepository implements Repository {

    private class KeyLazyLoader implements LazyLoader {
        private final Serializable key;

        public KeyLazyLoader(Serializable key) {
            this.key = key;
        }

        public Object loadObject() throws Exception {
            return eagerRetrieve(key);
        }

    }

    private RESTHttpClient httpClient;

    private EntityToXmlMapper mapper;

    private URL baseUrl;

    private final Class<?> entityType;

    public RESTClientRepository(Class<?> entityType, URL baseUrl, RESTHttpClient httpClient, EntityToXmlMapper mapper) {
        this.entityType = entityType;
        this.baseUrl = baseUrl;
        this.httpClient = httpClient;
        this.mapper = mapper;
    }

    public Serializable insert(Object entity) {
        URL location = httpClient.doPostAndReturnLocation(resourceUrl(""), mapper.entityToWriter(entity));
        return location;
    }

    @SuppressWarnings("unchecked")
    public Object retrieve(Serializable key) {
        return Enhancer.create(entityType, new KeyLazyLoader(key));
    }

    private Object eagerRetrieve(Serializable key) {
        return mapper.xmlToEntity(httpClient.doGet(resourceUrl(key)));
    }

    public void update(Serializable key, Object entity) {
        httpClient.doPut(resourceUrl(key), mapper.entityToWriter(entity));
    }

    public void delete(Serializable key) {
        httpClient.doDelete(resourceUrl(key));
    }

    public Serializable getKey(Object entity) {
        if (!(entity instanceof Factory)) {
            return null;
        }
        for (Callback callback : ((Factory)entity).getCallbacks()) {
            if (callback instanceof RESTClientRepository.KeyLazyLoader) {
                return ((KeyLazyLoader)callback).key;
            }
        }
        return null;
    }

    private URL resourceUrl(Serializable key) {
        try {
            return new URL(baseUrl, key.toString());
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Invalid url " + key.toString());
        }
    }


    public void discardChanges() {
        throw new UnsupportedOperationException("Use a SessionCachedRepository");
    }

    public void writeChanges() {
        throw new UnsupportedOperationException("Use a SessionCachedRepository");
    }

}

Johannes Brodwall
ViewVC Help
Powered by ViewVC 1.0.0