cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nick Khamis <sym...@gmail.com>
Subject CXF and Maps
Date Tue, 05 Jan 2010 22:44:29 GMT
 Hello Everyone,

I have set up some code that uses a Map to load a result set returned by a
Sparql endpoint.  The data is there the map is getting filled but the
queryOntologyMethod that
returns the loaded map is returning n amount of records all that are the
last record in the dataset.  The size of the Map is correct, but the records
in the map are identical.
I think there is a problem with the marshilling of the Map not sure. But
this is code taken from the example that uses a Map Adapter. Please Help.
Your help is greatly appreciated.

Thank in Advanced,

Nick

@WebService
public interface Sparql {
    /**
     * Queries ontology on server and return result in Map Format.
     */
    @XmlJavaTypeAdapter(IntegerRecordMapAdapter.class)
    public Map<Integer, Record> queryOntology(String query) throws
WebServiceException;
}

@WebService(endpointInterface = "ca.symack.com.Sparql",
    serviceName = "SparqlImplService", targetNamespace="sparql")
public class SparqlImpl implements Sparql {
    public final static String defaultSesameServer = "
http://ca.symack.com/openrdf-sesame/";
    public final static String defaultRepoID = "SEAdvisorRepo";
    private final Log log = LogFactory.getLog(SparqlImpl.class);
    private final String sesameServer;
    private final String reporId;
    private static Map<Integer, Record> records = new LinkedHashMap<Integer,
Record>();

    public SparqlImpl() {
        this(defaultSesameServer, defaultRepoID);
    }

    public SparqlImpl(String sesameServer, String reporId) {
        this.sesameServer = sesameServer;
        this.reporId = reporId;
    }

    @Override
    public Map<Integer, Record> queryOntology(String query) throws
WebServiceException {
        RepositoryConnection connection = null;
        MutableTupleQueryResult queryResults = null;

        try {
            HTTPRepository sesameServerRepository = new HTTPRepository(
                    sesameServer, reporId);


            TupleQuery tupleQuery = sesameServerRepository.getConnection().
            prepareTupleQuery(QueryLanguage.SPARQL, query);
            queryResults = new
MutableTupleQueryResult(tupleQuery.evaluate());
            int i = 0;

            RecordImpl r = new RecordImpl();

            while(i < queryResults.size()){
                r.setRecord(queryResults.get(i).toString());
                this.addRecord(r);
                i += 1;
            }

            return records;
        }
        catch (Exception e) {
            log.error(e);
            throw new WebServiceException("Query failed: " + query, e);
        }
        finally {
            if (connection != null) {
                try {
                    connection.close();
                }
                catch (RepositoryException e) {
                    throw new WebServiceException(e);
                }
            }
        }
    }

    public void addRecord(Record r) {
        records.put(records.size() + 1, r);
    }
}


@XmlType(name = "IntegerRecordMap")
@XmlAccessorType(XmlAccessType.FIELD)
public class IntegerRecordMap {
    @XmlElement(nillable = false, name = "entry")
    List<IntegerRecordEntry> entries = new ArrayList<IntegerRecordEntry>();

    public List<IntegerRecordEntry> getEntries() {
        return entries;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "IdentifiedRecord")
    static class IntegerRecordEntry {
        //Map keys cannot be null
        @XmlElement(required = true, nillable = false)
        Integer id;
        Record record;

        public void setId(Integer k) {
            id = k;
        }
        public Integer getId() {
            return id;
        }

        public void setRecord(Record r) {
            record = r;
        }
        public Record getRecord() {
            return record;
        }
    }
}


public class IntegerRecordMapAdapter extends XmlAdapter<IntegerRecordMap,
Map<Integer, Record>> {
    public IntegerRecordMap marshal(Map<Integer, Record> v) throws Exception
{
        IntegerRecordMap map = new IntegerRecordMap();
        for (Map.Entry<Integer, Record> e : v.entrySet()) {
            IntegerRecordMap.IntegerRecordEntry iue = new
IntegerRecordMap.IntegerRecordEntry();
            iue.setRecord(e.getValue());
            iue.setId(e.getKey());
            map.getEntries().add(iue);
        }
        return map;
    }

    public Map<Integer, Record> unmarshal(IntegerRecordMap v) throws
Exception {
        Map<Integer, Record> map = new LinkedHashMap<Integer, Record>();
        for (IntegerRecordMap.IntegerRecordEntry e : v.getEntries()) {
            map.put(e.getId(), e.getRecord());
        }
        return map;
    }

}

@XmlJavaTypeAdapter(RecordAdapter.class)
public interface Record {
    String getRecord();
}

public class RecordAdapter extends XmlAdapter<RecordImpl, Record> {
    public RecordImpl marshal(Record v) throws Exception {
        if (v instanceof RecordImpl) {
            return (RecordImpl)v;
        }
        return new RecordImpl(v.getRecord());
    }

    public Record unmarshal(RecordImpl v) throws Exception {
        return v;
    }
}

@XmlType(name = "Record")
public class RecordImpl implements Record {
    String record;

    public RecordImpl() {
    }

    public RecordImpl(String record) {
        this.record = record;
    }

    public void setRecord(String record) {
        this.record = record;
    }

    public String getRecord() {
        return record;
    }
}

public final class Client {
    //Namespaces
    private static final QName SPARQL_QUERY_SERVICE_NAME
            = new QName("http://ca.symack.com/", "SparqlImplService");
    private static final QName SPARQL_QUERY_PORT_NAME
            = new QName("http://ca.symack.com/", "SparqlPort");

    public static void main(String args[]) throws Exception {
        Service sparqlQueryService =
            Service.create(new URL("http://localhost:9003/sparql?wsdl"),
                    new QName("sparql", "SparqlImplService") );

        // Endpoint Addresses
        String sparqlQueryAddress = "http://localhost:9003/Sparql";

        // Add a port to the Service
        sparqlQueryService.addPort(SPARQL_QUERY_PORT_NAME,
SOAPBinding.SOAP11HTTP_BINDING, sparqlQueryAddress);

        Sparql sparq = sparqlQueryService.getPort(Sparql.class);

        Map<Integer,Record> map = sparq.queryOntology("PREFIX svn:<http://
http://ca.symack.com/svn#> " +
        "SELECT ?Commit_Revision ?Commit_Message WHERE{?Commit_Revision
svn:commitMessage ?Commit_Message}");

        int i = 0;

        while(i < map.size())
        {
            System.out.println(map.get(1).getRecord());
            i+=1;
        }
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message