gpt4 book ai didi

java - 使用Sonar的Web服务java客户端来保存属性

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:05 25 4
gpt4 key购买 nike

如何保留 Sonar 属性 using Sonar's web service java client

[编辑:我现在意识到 Web 服务客户端不适用于开发插件;相反,应该使用 Sonar API 中的其他类。请参阅我接受的答案。]

我打算为 Sonar 制作一个插件。与此同时,我正在熟悉 Sonar 的 API,特别是 Sonar 的 Web 服务 Java 客户端。我正在尝试找出如何保留 Sonar 属性。我编写了以下代码:

    package testers;

import org.sonar.wsclient.Sonar;
import org.sonar.wsclient.services.Property;
import org.sonar.wsclient.services.PropertyCreateQuery;
import org.sonar.wsclient.services.PropertyQuery;

public class PropertyPersister {

public static Sonar localSonar;
public static Property sonarStartTime;
public static PropertyQuery findStartTime;
public static Property testProperty;
public static PropertyQuery findTestProperty;
public static String testKey = "testKey";
public static String testValue = "testValue";


/**
* @param args
*/
public static void main(String[] args) {

//localSonar = Sonar.create("http://localhost:9000");//pointed to my instance of Sonar

//EDIT: using this line instead, but it still gives the same stack trace.
localSonar = Sonar.create("http://localhost:9000", "admin", "admin");//pointed to my instance of Sonar

findStartTime = PropertyQuery.createForKey("sonar.core.startTime");//creating query for a key I know exists
sonarStartTime = localSonar.find(findStartTime);//retrieve property object from my Sonar's database
System.out.println(sonarStartTime);//print out this object

PropertyCreateQuery testCreateQuery = new PropertyCreateQuery(testKey, testValue);//Query to create test property


localSonar.create(testCreateQuery);//With this line, I'm trying to persist my test property

findTestProperty = PropertyQuery.createForKey(testKey);//creating query for retrieving test property
testProperty = localSonar.find(findTestProperty);//line 36: retrieve property object from my Sonar's database
System.out.println(testProperty);//print test property
}
}

此代码打印出已经存在的 sonarStartTime 属性:

[sonar.core.startTime:2013-03-14T08:05:42-0700]

然后是倒数第二行抛出的空指针异常。异常消息包括:

 org.sonar.wsclient.unmarshallers.UnmarshalException: Can not parse the response of query /api/properties/testKey?: {"err_code":404,"err_msg":"Property not found: testKey"}

使用 MySQL Workbench,我确认我的测试属性确实从未被持久化。显然,我的处理方式是错误的。因此,重申我的问题,我如何在 Sonar 中保留属性using Sonar's web service java client

[编辑]这是带有堆栈跟踪的完整控制台输出:

[sonar.core.startTime:2013-03-14T08:05:42-0700]
Exception in thread "main" org.sonar.wsclient.unmarshallers.UnmarshalException: Can not parse the response of query /api/properties/testKey?: {"err_code":404,"err_msg":"Property not found: testKey"}

at org.sonar.wsclient.Sonar.find(Sonar.java:56)
at testers.PropertyPersister.main(PropertyPersister.java:36)
Caused by: java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to java.util.ArrayList
at org.sonar.wsclient.JdkUtils.getArraySize(JdkUtils.java:87)
at org.sonar.wsclient.unmarshallers.AbstractUnmarshaller.toModel(AbstractUnmarshaller.java:34)
at org.sonar.wsclient.Sonar.find(Sonar.java:54)
... 1 more

最佳答案

Sonar 简要解释了如何重用其 API 的现有组件 here

您可以简单地在尝试保留属性的类的构造函数中声明 DatabaseSession。例如:

import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.configuration.Property;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;

public class PropertyPersister{

private DatabaseSession session;
private Project project;
private Property newProperty;

public PropertyPersister (DatabaseSession session, Project project){
this.session = session;
this.project = project;
}

private void persistProperty (Resource resource) {
int resourceId= resource.getId();
newProperty = new Property("sonar.myFabulousProperty", "someValue", resourceId);
session.save(newProperty);
}
}

关于java - 使用Sonar的Web服务java客户端来保存属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15443096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com