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