1 /*===========================================================================
2 *
3 *                            PUBLIC DOMAIN NOTICE
4 *               National Center for Biotechnology Information
5 *
6 *  This software/database is a "United States Government Work" under the
7 *  terms of the United States Copyright Act.  It was written as part of
8 *  the author's official duties as a United States Government employee and
9 *  thus cannot be copyrighted.  This software/database is freely available
10 *  to the public for use. The National Library of Medicine and the U.S.
11 *  Government have not placed any restriction on its use or reproduction.
12 *
13 *  Although all reasonable efforts have been taken to ensure the accuracy
14 *  and reliability of the software and data, the NLM and the U.S.
15 *  Government do not and cannot warrant the performance or results that
16 *  may be obtained by using this software or data. The NLM and the U.S.
17 *  Government disclaim all warranties, express or implied, including
18 *  warranties of performance, merchantability or fitness for any particular
19 *  purpose.
20 *
21 *  Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26 
27 #include "jni_Manager.h"
28 #include "jni_ErrorMsg.h"
29 #include "jni_String.h"
30 
31 #include <kdb/manager.h> /* KDBManager */
32 
33 #include <kfc/ctx.h>
34 #include <kfc/rsrc.h>
35 #include <kfc/except.h>
36 #include <kfc/xc.h>
37 #include <kfc/rc.h>
38 
39 #include <kfc/rsrc-global.h>
40 
41 #include <kns/manager.h>
42 #include <klib/ncbi-vdb-version.h> /* GetPackageVersion */
43 
44 #include <vfs/manager.h> /* VFSManager */
45 #include <vfs/path.h> /* VPath */
46 
47 #include "NGS_ReadCollection.h"
48 #include "NGS_ReferenceSequence.h"
49 #include "../klib/release-vers.h"
50 
51 #include <assert.h>
52 
53 static bool have_user_version_string;
54 
55 /*
56  * Class:     gov_nih_nlm_ncbi_ngs_Manager
57  * Method:    Initialize
58  * Signature: ()Ljava/lang/String;
59  */
Java_gov_nih_nlm_ncbi_ngs_Manager_Initialize(JNIEnv * jenv,jclass jcls)60 JNIEXPORT jstring JNICALL Java_gov_nih_nlm_ncbi_ngs_Manager_Initialize
61     ( JNIEnv * jenv, jclass jcls )
62 {
63     DECLARE_FUNC_LOC ( rcSRA, rcMgr, rcConstructing );
64 
65     KCtx local_ctx, * ctx = & local_ctx;
66     TRY ( KRsrcGlobalInit ( & local_ctx, & s_func_loc, true ) )
67     {
68         return NULL;
69     }
70 
71     return JStringMake ( ctx, jenv, "KRsrcGlobalInit failed with rc = %R", local_ctx . rc );
72 }
73 
74 /*
75  * Class:     gov_nih_nlm_ncbi_ngs_Manager
76  * Method:    Shutdown
77  * Signature: ()V
78  */
Java_gov_nih_nlm_ncbi_ngs_Manager_Shutdown(JNIEnv * jenv,jclass jcls)79 JNIEXPORT void JNICALL Java_gov_nih_nlm_ncbi_ngs_Manager_Shutdown
80     ( JNIEnv * jenv, jclass jcls )
81 {
82     HYBRID_FUNC_ENTRY ( rcSRA, rcMgr, rcDestroying );
83     KRsrcGlobalWhack ( ctx );
84 }
85 
86 static
set_app_version_string(const char * app_version)87 void set_app_version_string ( const char * app_version )
88 {
89     // get a KNSManager
90     KNSManager * kns;
91     rc_t rc = KNSManagerMake ( & kns );
92     if ( rc == 0 )
93     {
94         have_user_version_string = true;
95         KNSManagerSetUserAgent ( kns, "ncbi-ngs.%V %s", RELEASE_VERS, app_version );
96         KNSManagerRelease ( kns );
97     }
98 }
99 
100 /*
101  * Class:     gov_nih_nlm_ncbi_ngs_Manager
102  * Method:    SetAppVersionString
103  * Signature: (Ljava/lang/String;)V
104  */
Java_gov_nih_nlm_ncbi_ngs_Manager_SetAppVersionString(JNIEnv * jenv,jclass jcls,jstring japp_version)105 JNIEXPORT void JNICALL Java_gov_nih_nlm_ncbi_ngs_Manager_SetAppVersionString
106     ( JNIEnv * jenv, jclass jcls, jstring japp_version )
107 {
108     HYBRID_FUNC_ENTRY ( rcSRA, rcMgr, rcUpdating );
109 
110     const char * app_version = JStringData ( japp_version, ctx, jenv );
111 
112     set_app_version_string ( app_version );
113 }
114 
115 /*
116  * Class:     gov_nih_nlm_ncbi_ngs_Manager
117  * Method:    OpenReadCollection
118  * Signature: (Ljava/lang/String;)J
119  */
Java_gov_nih_nlm_ncbi_ngs_Manager_OpenReadCollection(JNIEnv * jenv,jclass jcls,jstring jspec)120 JNIEXPORT jlong JNICALL Java_gov_nih_nlm_ncbi_ngs_Manager_OpenReadCollection
121     ( JNIEnv * jenv, jclass jcls, jstring jspec )
122 {
123     HYBRID_FUNC_ENTRY ( rcSRA, rcMgr, rcConstructing );
124 
125     NGS_ReadCollection * new_ref = NULL;
126     const char * spec = JStringData ( jspec, ctx, jenv );
127 
128     if ( ! have_user_version_string )
129         set_app_version_string ( "ncbi-ngs: unknown-application" );
130 
131     new_ref = NGS_ReadCollectionMake ( ctx, spec );
132     if ( FAILED () )
133     {
134         ErrorMsgThrow ( jenv, ctx, __LINE__, "failed to create ReadCollection from spec '%s'"
135                          , spec
136             );
137         JStringReleaseData ( jspec, ctx, jenv, spec );
138         return 0;
139     }
140 
141     JStringReleaseData ( jspec, ctx, jenv, spec );
142 
143     assert ( new_ref != NULL );
144     return ( jlong ) ( size_t ) new_ref;
145 }
146 
147 /*
148  * Class:     gov_nih_nlm_ncbi_ngs_Manager
149  * Method:    OpenReferenceSequence
150  * Signature: (Ljava/lang/String;)J
151  */
Java_gov_nih_nlm_ncbi_ngs_Manager_OpenReferenceSequence(JNIEnv * jenv,jclass jcls,jstring jspec)152 JNIEXPORT jlong JNICALL Java_gov_nih_nlm_ncbi_ngs_Manager_OpenReferenceSequence
153     ( JNIEnv * jenv, jclass jcls, jstring jspec )
154 {
155     HYBRID_FUNC_ENTRY ( rcSRA, rcMgr, rcConstructing );
156 
157     NGS_ReferenceSequence* new_ref = NULL;
158     const char * spec = JStringData ( jspec, ctx, jenv );
159 
160     if ( ! have_user_version_string )
161         set_app_version_string ( "ncbi-ngs: unknown-application" );
162 
163     new_ref = NGS_ReferenceSequenceMake ( ctx, spec );
164     if ( FAILED () )
165     {
166         ErrorMsgThrow ( jenv, ctx, __LINE__, "failed to create ReferenceSequence from spec '%s'"
167                          , spec
168             );
169         JStringReleaseData ( jspec, ctx, jenv, spec );
170         return 0;
171     }
172 
173     JStringReleaseData ( jspec, ctx, jenv, spec );
174 
175     assert ( new_ref != NULL );
176     return ( jlong ) ( size_t ) new_ref;
177 }
178 
179 /*
180  * Class:     gov_nih_nlm_ncbi_ngs_Manager
181  * Method:    IsValid
182  * Signature: (Ljava/lang/String;)Z
183  */
Java_gov_nih_nlm_ncbi_ngs_Manager_IsValid(JNIEnv * jenv,jclass jcls,jstring jspec)184 JNIEXPORT jboolean JNICALL Java_gov_nih_nlm_ncbi_ngs_Manager_IsValid
185   ( JNIEnv * jenv, jclass jcls, jstring jspec )
186 {
187     HYBRID_FUNC_ENTRY ( rcSRA, rcMgr, rcAccessing );
188 
189     jboolean result = false;
190 
191     VFSManager * vfs = NULL;
192     rc_t rc = VFSManagerMake ( & vfs );
193 
194     if ( rc == 0 ) {
195         const char * spec = JStringData ( jspec, ctx, jenv );
196 
197         VPath * path = NULL;
198         rc = VFSManagerMakePath ( vfs, & path, spec );
199 
200         if ( rc == 0 ) {
201             const KDBManager * kdb = NULL;
202             rc = KDBManagerMakeRead ( & kdb, NULL );
203 
204             if ( rc == 0 ) {
205                 KPathType t = KDBManagerPathTypeVP ( kdb, path );
206                 if (t == kptDatabase || t == kptTable) {
207                     result = true;
208                 }
209 
210                 KDBManagerRelease ( kdb );
211                 kdb = NULL;
212             }
213 
214             VPathRelease ( path );
215             path = NULL;
216         }
217 
218         VFSManagerRelease ( vfs );
219         vfs = NULL;
220     }
221 
222     return result;
223 }
224 
225 /*
226  * Class:     gov_nih_nlm_ncbi_ngs_Manager
227  * Method:    release
228  * Signature: (J)V
229  */
Java_gov_nih_nlm_ncbi_ngs_Manager_release(JNIEnv * jenv,jclass jcls,jlong jref)230 JNIEXPORT void JNICALL Java_gov_nih_nlm_ncbi_ngs_Manager_release
231     ( JNIEnv * jenv, jclass jcls, jlong jref )
232 {
233     if ( jref != 0 )
234     {
235         HYBRID_FUNC_ENTRY ( rcSRA, rcRefcount, rcReleasing );
236         NGS_RefcountRelease ( ( void* ) ( size_t ) jref, ctx );
237     }
238 }
239 
240 /*
241  * Class:     gov_nih_nlm_ncbi_ngs_Manager
242  * Method:    Version
243  * Signature: ()Ljava/lang/String;
244  */
Java_gov_nih_nlm_ncbi_ngs_Manager_Version(JNIEnv * jenv,jclass jcls)245 JNIEXPORT jstring JNICALL Java_gov_nih_nlm_ncbi_ngs_Manager_Version
246   (JNIEnv *jenv, jclass jcls)
247 {
248     HYBRID_FUNC_ENTRY ( rcSRA, rcMgr, rcAccessing );
249     return JStringMake(ctx, jenv, GetPackageVersion());
250 }
251