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.unmarshallers.Unmarshaller;
027 import org.sonar.wsclient.unmarshallers.Unmarshallers;
028
029 import java.util.Collections;
030 import java.util.List;
031
032 public class Sonar {
033
034 private Connector connector;
035
036 public Sonar(Connector connector) {
037 this.connector = connector;
038 }
039
040 public Connector getConnector() {
041 return connector;
042 }
043
044 public <MODEL extends Model> MODEL find(Query<MODEL> query) {
045 String json = connector.execute(query);
046 MODEL result = null;
047 if (json != null) {
048 Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass());
049 result = unmarshaller.toModel(json);
050 }
051 return result;
052 }
053
054 public <MODEL extends Model> List<MODEL> findAll(Query<MODEL> query) {
055 String json = connector.execute(query);
056 List<MODEL> result;
057 if (json == null) {
058 result = Collections.emptyList();
059 } else {
060 Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass());
061 result = unmarshaller.toModels(json);
062 }
063 return result;
064 }
065
066 public static Sonar create(String host) {
067 return new Sonar(ConnectorFactory.create(new Host(host)));
068 }
069
070 public static Sonar create(String host, String username, String password) {
071 return new Sonar(ConnectorFactory.create(new Host(host, username, password)));
072 }
073 }