001    /*
002     * Copyright (C) 2010 Evgeny Mandrikov
003     *
004     * Sonar-IDE is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Lesser General Public
006     * License as published by the Free Software Foundation; either
007     * version 3 of the License, or (at your option) any later version.
008     *
009     * Sonar-IDE is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with Sonar-IDE; if not, write to the Free Software
016     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
017     */
018    
019    package org.sonar.ide.client;
020    
021    import java.util.List;
022    
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.sonar.ide.wsclient.HttpClient3ConnectorFactory;
026    import org.sonar.wsclient.Host;
027    import org.sonar.wsclient.Sonar;
028    import org.sonar.wsclient.connectors.ConnectionException;
029    import org.sonar.wsclient.services.Model;
030    import org.sonar.wsclient.services.Query;
031    import org.sonar.wsclient.services.Server;
032    import org.sonar.wsclient.services.ServerQuery;
033    
034    /**
035     * @author Evgeny Mandrikov
036     * @deprecated since 0.2, use {@link org.sonar.ide.api.SourceCodeSearchEngine} instead of it
037     */
038    @Deprecated
039    public class SonarClient extends Sonar {
040      private static final Logger LOG = LoggerFactory.getLogger(SonarClient.class);
041    
042      private boolean available;
043      private int serverTrips = 0;
044    
045      public SonarClient(String host) {
046        this(host, "", "");
047      }
048    
049      public SonarClient(String host, String username, String password) {
050        super(HttpClient3ConnectorFactory.createConnector(new Host(host, username, password)));
051        connect();
052      }
053    
054      private void connect() {
055        try {
056          LOG.info("Connect");
057          ServerQuery serverQuery = new ServerQuery();
058          Server server = find(serverQuery);
059          available = checkVersion(server);
060          LOG.info(available ? "Connected to " + server.getId() + "(" + server.getVersion() + ")" : "Unable to connect");
061        } catch (ConnectionException e) {
062          available = false;
063          LOG.error("Unable to connect", e);
064        }
065      }
066    
067      private boolean checkVersion(Server server) {
068        if (server == null) {
069          return false;
070        }
071        String version = server.getVersion();
072        return version != null && version.startsWith("2.");
073      }
074    
075      @Override
076      public <MODEL extends Model> MODEL find(Query<MODEL> query) {
077        serverTrips++;
078        LOG.info("find : {}", query.getUrl());
079        MODEL model = super.find(query);
080        LOG.info(model.toString());
081        return model;
082      }
083    
084      @Override
085      public <MODEL extends Model> List<MODEL> findAll(Query<MODEL> query) {
086        serverTrips++;
087        LOG.info("find : {}", query.getUrl());
088        List<MODEL> result = super.findAll(query);
089        LOG.info("Retrieved {} elements.", result.size());
090        return result;
091      }
092    
093      public int getServerTrips() {
094        return serverTrips;
095      }
096    
097      public boolean isAvailable() {
098        return available;
099      }
100    }