1 | /** |
---|
2 | * @copyright |
---|
3 | * ==================================================================== |
---|
4 | * Copyright (c) 2003-2004 CollabNet. All rights reserved. |
---|
5 | * |
---|
6 | * This software is licensed as described in the file COPYING, which |
---|
7 | * you should have received as part of this distribution. The terms |
---|
8 | * are also available at http://subversion.tigris.org/license-1.html. |
---|
9 | * If newer versions of this license are posted there, you may use a |
---|
10 | * newer version instead, at your option. |
---|
11 | * |
---|
12 | * This software consists of voluntary contributions made by many |
---|
13 | * individuals. For exact contribution history, see the revision |
---|
14 | * history and logs, available at http://subversion.tigris.org/. |
---|
15 | * ==================================================================== |
---|
16 | * @endcopyright |
---|
17 | * |
---|
18 | * @file CommitMessage.cpp |
---|
19 | * @brief Implementation of the class CommitMessage |
---|
20 | */ |
---|
21 | |
---|
22 | #include "CommitMessage.h" |
---|
23 | #include "JNIUtil.h" |
---|
24 | #include <apr_tables.h> |
---|
25 | #include "svn_client.h" |
---|
26 | #include "../include/org_tigris_subversion_javahl_CommitItemStateFlags.h" |
---|
27 | |
---|
28 | CommitMessage::CommitMessage(jobject jcommitMessage) |
---|
29 | { |
---|
30 | m_jcommitMessage = jcommitMessage; |
---|
31 | } |
---|
32 | |
---|
33 | CommitMessage::~CommitMessage() |
---|
34 | { |
---|
35 | // Since the m_jcommitMessage is a global reference, it has to be |
---|
36 | // deleted to allow the Java garbage collector to reclaim the |
---|
37 | // object. |
---|
38 | if (m_jcommitMessage!= NULL) |
---|
39 | { |
---|
40 | JNIEnv *env = JNIUtil::getEnv(); |
---|
41 | env->DeleteGlobalRef(m_jcommitMessage); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | CommitMessage *CommitMessage::makeCCommitMessage(jobject jcommitMessage) |
---|
46 | { |
---|
47 | // If there is no object passed into this method, there is no need |
---|
48 | // for a C++ holding object. |
---|
49 | if (jcommitMessage == NULL) |
---|
50 | return NULL; |
---|
51 | |
---|
52 | // Sanity check, that the passed Java object implements the right |
---|
53 | // interface. |
---|
54 | JNIEnv *env = JNIUtil::getEnv(); |
---|
55 | jclass clazz = env->FindClass(JAVA_PACKAGE"/CommitMessage"); |
---|
56 | if (JNIUtil::isJavaExceptionThrown()) |
---|
57 | return NULL; |
---|
58 | |
---|
59 | if (!env->IsInstanceOf(jcommitMessage, clazz)) |
---|
60 | { |
---|
61 | env->DeleteLocalRef(clazz); |
---|
62 | return NULL; |
---|
63 | } |
---|
64 | env->DeleteLocalRef(clazz); |
---|
65 | if (JNIUtil::isJavaExceptionThrown()) |
---|
66 | return NULL; |
---|
67 | |
---|
68 | // Since the reference is longer needed then the duration of the |
---|
69 | // SVNClient.commtMessage, the local reference has to be converted |
---|
70 | // to a global reference. |
---|
71 | jobject myCommitMessage = env->NewGlobalRef(jcommitMessage); |
---|
72 | if (JNIUtil::isJavaExceptionThrown()) |
---|
73 | return NULL; |
---|
74 | |
---|
75 | // create & return the holding object |
---|
76 | return new CommitMessage(myCommitMessage); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Call the Java callback method to retrieve the commit message |
---|
81 | * @param commit_items the array of the items of this commit |
---|
82 | * @returns the commit message |
---|
83 | */ |
---|
84 | jstring |
---|
85 | CommitMessage::getCommitMessage(const apr_array_header_t *commit_items) |
---|
86 | { |
---|
87 | JNIEnv *env = JNIUtil::getEnv(); |
---|
88 | // create an Java array for the commit items |
---|
89 | jclass clazz = env->FindClass(JAVA_PACKAGE"/CommitItem"); |
---|
90 | if (JNIUtil::isExceptionThrown()) |
---|
91 | return NULL; |
---|
92 | |
---|
93 | int count = commit_items->nelts; |
---|
94 | jobjectArray jitems = env->NewObjectArray(count, clazz, NULL); |
---|
95 | if (JNIUtil::isExceptionThrown()) |
---|
96 | return NULL; |
---|
97 | |
---|
98 | // Java method ids will not change during the time this library is |
---|
99 | // loaded, so they can be cached. |
---|
100 | |
---|
101 | // Get the method id for the CommitItem constructor. |
---|
102 | static jmethodID midConstructor = 0; |
---|
103 | if (midConstructor == 0) |
---|
104 | { |
---|
105 | midConstructor = env->GetMethodID(clazz, "<init>", |
---|
106 | "(Ljava/lang/String;" |
---|
107 | "IILjava/lang/String;" |
---|
108 | "Ljava/lang/String;J)V"); |
---|
109 | if (JNIUtil::isExceptionThrown()) |
---|
110 | return NULL; |
---|
111 | } |
---|
112 | |
---|
113 | // get the method if for the CommitMessage callback method |
---|
114 | static jmethodID midCallback = 0; |
---|
115 | if (midCallback == 0) |
---|
116 | { |
---|
117 | jclass clazz2 = env->FindClass(JAVA_PACKAGE"/CommitMessage"); |
---|
118 | if (JNIUtil::isJavaExceptionThrown()) |
---|
119 | return NULL; |
---|
120 | |
---|
121 | midCallback = env->GetMethodID(clazz2, "getLogMessage", |
---|
122 | "([L"JAVA_PACKAGE"/CommitItem;)" |
---|
123 | "Ljava/lang/String;"); |
---|
124 | if (JNIUtil::isJavaExceptionThrown()) |
---|
125 | return NULL; |
---|
126 | |
---|
127 | env->DeleteLocalRef(clazz2); |
---|
128 | if (JNIUtil::isJavaExceptionThrown()) |
---|
129 | return NULL; |
---|
130 | } |
---|
131 | |
---|
132 | // create a Java CommitItem for each of the passed in commit items |
---|
133 | for (int i = 0; i < count; ++i) |
---|
134 | { |
---|
135 | svn_client_commit_item3_t *item = |
---|
136 | APR_ARRAY_IDX(commit_items, i, svn_client_commit_item3_t *); |
---|
137 | |
---|
138 | // convert the commit item members to the match Java members |
---|
139 | jstring jpath = JNIUtil::makeJString(item->path); |
---|
140 | |
---|
141 | jint jnodeKind = item->kind; |
---|
142 | |
---|
143 | jint jstateFlags = 0; |
---|
144 | if (item->state_flags & SVN_CLIENT_COMMIT_ITEM_ADD) |
---|
145 | jstateFlags |= |
---|
146 | org_tigris_subversion_javahl_CommitItemStateFlags_Add; |
---|
147 | if (item->state_flags & SVN_CLIENT_COMMIT_ITEM_DELETE) |
---|
148 | jstateFlags |= |
---|
149 | org_tigris_subversion_javahl_CommitItemStateFlags_Delete; |
---|
150 | if (item->state_flags & SVN_CLIENT_COMMIT_ITEM_TEXT_MODS) |
---|
151 | jstateFlags |= |
---|
152 | org_tigris_subversion_javahl_CommitItemStateFlags_TextMods; |
---|
153 | if (item->state_flags & SVN_CLIENT_COMMIT_ITEM_PROP_MODS) |
---|
154 | jstateFlags |= |
---|
155 | org_tigris_subversion_javahl_CommitItemStateFlags_PropMods; |
---|
156 | if (item->state_flags & SVN_CLIENT_COMMIT_ITEM_IS_COPY) |
---|
157 | jstateFlags |= |
---|
158 | org_tigris_subversion_javahl_CommitItemStateFlags_IsCopy; |
---|
159 | |
---|
160 | jstring jurl = JNIUtil::makeJString(item->url); |
---|
161 | |
---|
162 | jstring jcopyUrl = JNIUtil::makeJString(item->copyfrom_url); |
---|
163 | |
---|
164 | jlong jcopyRevision = item->revision; |
---|
165 | |
---|
166 | // create the Java object |
---|
167 | jobject jitem = env->NewObject(clazz, midConstructor, jpath, |
---|
168 | jnodeKind, jstateFlags, jurl, |
---|
169 | jcopyUrl, jcopyRevision); |
---|
170 | if (JNIUtil::isJavaExceptionThrown()) |
---|
171 | return NULL; |
---|
172 | |
---|
173 | // release the tempory Java objects |
---|
174 | env->DeleteLocalRef(jpath); |
---|
175 | if (JNIUtil::isJavaExceptionThrown()) |
---|
176 | return NULL; |
---|
177 | |
---|
178 | |
---|
179 | env->DeleteLocalRef(jurl); |
---|
180 | if (JNIUtil::isJavaExceptionThrown()) |
---|
181 | return NULL; |
---|
182 | |
---|
183 | env->DeleteLocalRef(jcopyUrl); |
---|
184 | if (JNIUtil::isJavaExceptionThrown()) |
---|
185 | return NULL; |
---|
186 | |
---|
187 | // store the Java object into the array |
---|
188 | env->SetObjectArrayElement(jitems, i, jitem); |
---|
189 | if (JNIUtil::isJavaExceptionThrown()) |
---|
190 | return NULL; |
---|
191 | } |
---|
192 | env->DeleteLocalRef(clazz); |
---|
193 | if (JNIUtil::isJavaExceptionThrown()) |
---|
194 | return NULL; |
---|
195 | |
---|
196 | // call the Java callback method |
---|
197 | jstring jmessage = (jstring)env->CallObjectMethod(m_jcommitMessage, |
---|
198 | midCallback, |
---|
199 | jitems); |
---|
200 | if (JNIUtil::isJavaExceptionThrown()) |
---|
201 | return NULL; |
---|
202 | |
---|
203 | // release the Java object array |
---|
204 | env->DeleteLocalRef(jitems); |
---|
205 | if (JNIUtil::isJavaExceptionThrown()) |
---|
206 | return NULL; |
---|
207 | |
---|
208 | return jmessage; |
---|
209 | } |
---|