001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
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.gwt;
021    
022    import com.google.gwt.core.client.JavaScriptObject;
023    import com.google.gwt.i18n.client.Dictionary;
024    import com.google.gwt.json.client.JSONObject;
025    import org.sonar.gwt.JsonUtils;
026    import org.sonar.wsclient.services.Model;
027    import org.sonar.wsclient.services.Query;
028    import org.sonar.wsclient.services.WSUtils;
029    import org.sonar.wsclient.unmarshallers.Unmarshaller;
030    import org.sonar.wsclient.unmarshallers.Unmarshallers;
031    
032    public class Sonar {
033      static {
034        WSUtils.setInstance(new GwtUtils());
035      }
036    
037      private static Sonar instance = null;
038    
039      private final String host;
040    
041      public Sonar(String host) {
042        this.host = host;
043      }
044    
045      /**
046       * To be used in Sonar extensions only, else use constructors.
047       */
048      public static Sonar getInstance() {
049        if (instance == null) {
050          Dictionary dic = Dictionary.getDictionary("config");
051          instance = new Sonar(dic.get("sonar_url"));
052        }
053        return instance;
054      }
055    
056      public <MODEL extends Model> void find(final Query<MODEL> query, final Callback<MODEL> callback) {
057        JsonUtils.requestJson(getUrl(query), new JsonUtils.JSONHandler() {
058          public void onResponse(JavaScriptObject obj) {
059            Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass());
060            String json = (new JSONObject(obj)).toString();
061            callback.onResponse(unmarshaller.toModel(json), obj);
062          }
063    
064          public void onTimeout() {
065            callback.onTimeout();
066          }
067    
068          public void onError(int errorCode, String errorMessage) {
069            callback.onError(errorCode, errorMessage);
070          }
071        });
072      }
073    
074      public <MODEL extends Model> void findAll(final Query<MODEL> query, final ListCallback<MODEL> callback) {
075        JsonUtils.requestJson(getUrl(query), new JsonUtils.JSONHandler() {
076          public void onResponse(JavaScriptObject obj) {
077            Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass());
078            String json = (new JSONObject(obj)).toString();
079            callback.onResponse(unmarshaller.toModels(json), obj);
080          }
081    
082          public void onTimeout() {
083            callback.onTimeout();
084          }
085    
086          public void onError(int errorCode, String errorMessage) {
087            callback.onError(errorCode, errorMessage);
088          }
089        });
090      }
091    
092      private String getUrl(Query query) {
093        return host + query.getUrl();
094      }
095    }