1

Sonar の Web サービス Java クライアントを使用して Sonar プロパティを永続化するにはどうすればよいですか?

[編集: 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]

その後、最後から 2 番目の行でヌル ポインター例外がスローされます。例外メッセージには次のものが含まれていました。

 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 ワークベンチを使用して、実際にテスト プロパティが永続化されていないことを確認しました。明らかに、私はこれについて間違った方法で行っています。質問を繰り返しますが、Sonar の Web サービス Java client を使用して Sonarでプロパティを永続化するにはどうすればよいですか?

[編集] スタック トレースを含む完全なコンソール出力は次のとおりです。

[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
4

2 に答える 2

0

データを Sonar にプッシュできるようにする場合は、管理者の資格情報が必要です。

したがって、次のように「org.sonar.wsclient.Sonar」インスタンスを作成する必要があります。

localSonar = Sonar.create("http://localhost:9000", "admin", "admin");
于 2013-03-18T08:14:48.737 に答える