001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.wsclient;
021    
022    import org.sonar.wsclient.connectors.Connector;
023    import org.sonar.wsclient.connectors.ConnectorFactory;
024    import org.sonar.wsclient.services.Model;
025    import org.sonar.wsclient.services.Query;
026    import org.sonar.wsclient.services.WSUtils;
027    import org.sonar.wsclient.unmarshallers.Unmarshaller;
028    import org.sonar.wsclient.unmarshallers.Unmarshallers;
029    
030    import java.util.Collections;
031    import java.util.List;
032    
033    public class Sonar {
034    
035      static {
036        WSUtils.setInstance(new JdkUtils());
037      }
038      
039      private Connector connector;
040    
041      public Sonar(Connector connector) {
042        this.connector = connector;
043      }
044    
045      public Connector getConnector() {
046        return connector;
047      }
048    
049      public <MODEL extends Model> MODEL find(Query<MODEL> query) {
050        String json = connector.execute(query);
051        MODEL result = null;
052        if (json != null) {
053          Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass());
054          result = unmarshaller.toModel(json);
055        }
056        return result;
057      }
058    
059      public <MODEL extends Model> List<MODEL> findAll(Query<MODEL> query) {
060        String json = connector.execute(query);
061        List<MODEL> result;
062        if (json == null) {
063          result = Collections.emptyList();
064        } else {
065          Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass());
066          result = unmarshaller.toModels(json);
067        }
068        return result;
069      }
070    
071      public static Sonar create(String host) {
072        return new Sonar(ConnectorFactory.create(new Host(host)));
073      }
074    
075      public static Sonar create(String host, String username, String password) {
076        return new Sonar(ConnectorFactory.create(new Host(host, username, password)));
077      }
078    }