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