jdk/src/solaris/native/sun/nio/fs/GnomeFileTypeDetector.c
changeset 2057 3acf8e5e2ca0
child 5506 202f599c92aa
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 #include "jni.h"
       
    27 #include "jni_util.h"
       
    28 #include "jvm.h"
       
    29 #include "jlong.h"
       
    30 
       
    31 #include <stdlib.h>
       
    32 #include <dlfcn.h>
       
    33 #include <link.h>
       
    34 
       
    35 #ifdef __solaris__
       
    36 #include <strings.h>
       
    37 #endif
       
    38 
       
    39 #ifdef __linux__
       
    40 #include <string.h>
       
    41 #endif
       
    42 
       
    43 /* Definitions for GIO */
       
    44 
       
    45 #define G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "standard::content-type"
       
    46 
       
    47 typedef void* gpointer;
       
    48 typedef struct _GFile GFile;
       
    49 typedef struct _GFileInfo GFileInfo;
       
    50 typedef struct _GCancellable GCancellable;
       
    51 typedef struct _GError GError;
       
    52 
       
    53 typedef enum {
       
    54   G_FILE_QUERY_INFO_NONE = 0
       
    55 } GFileQueryInfoFlags;
       
    56 
       
    57 typedef void (*g_type_init_func)(void);
       
    58 typedef void (*g_object_unref_func)(gpointer object);
       
    59 typedef GFile* (*g_file_new_for_path_func)(const char* path);
       
    60 typedef GFileInfo* (*g_file_query_info_func)(GFile *file,
       
    61     const char *attributes, GFileQueryInfoFlags flags,
       
    62     GCancellable *cancellable, GError **error);
       
    63 typedef char* (*g_file_info_get_content_type_func)(GFileInfo *info);
       
    64 
       
    65 static g_type_init_func g_type_init;
       
    66 static g_object_unref_func g_object_unref;
       
    67 static g_file_new_for_path_func g_file_new_for_path;
       
    68 static g_file_query_info_func g_file_query_info;
       
    69 static g_file_info_get_content_type_func g_file_info_get_content_type;
       
    70 
       
    71 
       
    72 /* Definitions for GNOME VFS */
       
    73 
       
    74 typedef int gboolean;
       
    75 
       
    76 typedef gboolean (*gnome_vfs_init_function)(void);
       
    77 typedef const char* (*gnome_vfs_mime_type_from_name_function)
       
    78     (const char* filename);
       
    79 
       
    80 static gnome_vfs_init_function gnome_vfs_init;
       
    81 static gnome_vfs_mime_type_from_name_function gnome_vfs_mime_type_from_name;
       
    82 
       
    83 
       
    84 #include "sun_nio_fs_GnomeFileTypeDetector.h"
       
    85 
       
    86 
       
    87 JNIEXPORT jboolean JNICALL
       
    88 Java_sun_nio_fs_GnomeFileTypeDetector_initializeGio
       
    89     (JNIEnv* env, jclass this)
       
    90 {
       
    91     void* gio_handle;
       
    92 
       
    93     gio_handle = dlopen("libgio-2.0.so", RTLD_LAZY);
       
    94     if (gio_handle == NULL) {
       
    95         gio_handle = dlopen("libgio-2.0.so.0", RTLD_LAZY);
       
    96         if (gio_handle == NULL) {
       
    97             return JNI_FALSE;
       
    98         }
       
    99     }
       
   100 
       
   101     g_type_init = (g_type_init_func)dlsym(gio_handle, "g_type_init");
       
   102     (*g_type_init)();
       
   103 
       
   104     g_object_unref = (g_object_unref_func)dlsym(gio_handle, "g_object_unref");
       
   105 
       
   106     g_file_new_for_path =
       
   107         (g_file_new_for_path_func)dlsym(gio_handle, "g_file_new_for_path");
       
   108 
       
   109     g_file_query_info =
       
   110         (g_file_query_info_func)dlsym(gio_handle, "g_file_query_info");
       
   111 
       
   112     g_file_info_get_content_type = (g_file_info_get_content_type_func)
       
   113         dlsym(gio_handle, "g_file_info_get_content_type");
       
   114 
       
   115 
       
   116     if (g_type_init == NULL ||
       
   117         g_object_unref == NULL ||
       
   118         g_file_new_for_path == NULL ||
       
   119         g_file_query_info == NULL ||
       
   120         g_file_info_get_content_type == NULL)
       
   121     {
       
   122         dlclose(gio_handle);
       
   123         return JNI_FALSE;
       
   124     }
       
   125 
       
   126     (*g_type_init)();
       
   127     return JNI_TRUE;
       
   128 }
       
   129 
       
   130 JNIEXPORT jbyteArray JNICALL
       
   131 Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGio
       
   132     (JNIEnv* env, jclass this, jlong pathAddress)
       
   133 {
       
   134     char* path = (char*)jlong_to_ptr(pathAddress);
       
   135     GFile* gfile;
       
   136     GFileInfo* gfileinfo;
       
   137     jbyteArray result = NULL;
       
   138 
       
   139     gfile = (*g_file_new_for_path)(path);
       
   140     gfileinfo = (*g_file_query_info)(gfile, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
       
   141         G_FILE_QUERY_INFO_NONE, NULL, NULL);
       
   142     if (gfileinfo != NULL) {
       
   143         const char* mime = (*g_file_info_get_content_type)(gfileinfo);
       
   144         if (mime != NULL) {
       
   145             jsize len = strlen(mime);
       
   146             result = (*env)->NewByteArray(env, len);
       
   147             if (result != NULL) {
       
   148                 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
       
   149             }
       
   150         }
       
   151         (*g_object_unref)(gfileinfo);
       
   152     }
       
   153     (*g_object_unref)(gfile);
       
   154 
       
   155     return result;
       
   156 }
       
   157 
       
   158 JNIEXPORT jboolean JNICALL
       
   159 Java_sun_nio_fs_GnomeFileTypeDetector_initializeGnomeVfs
       
   160     (JNIEnv* env, jclass this)
       
   161 {
       
   162     void* vfs_handle;
       
   163 
       
   164     vfs_handle = dlopen("libgnomevfs-2.so", RTLD_LAZY);
       
   165     if (vfs_handle == NULL) {
       
   166         vfs_handle = dlopen("libgnomevfs-2.so.0", RTLD_LAZY);
       
   167     }
       
   168     if (vfs_handle == NULL) {
       
   169         return JNI_FALSE;
       
   170     }
       
   171 
       
   172     gnome_vfs_init = (gnome_vfs_init_function)dlsym(vfs_handle, "gnome_vfs_init");
       
   173     gnome_vfs_mime_type_from_name = (gnome_vfs_mime_type_from_name_function)
       
   174         dlsym(vfs_handle, "gnome_vfs_mime_type_from_name");
       
   175 
       
   176     if (gnome_vfs_init == NULL ||
       
   177         gnome_vfs_mime_type_from_name == NULL)
       
   178     {
       
   179         dlclose(vfs_handle);
       
   180         return JNI_FALSE;
       
   181     }
       
   182 
       
   183     (*gnome_vfs_init)();
       
   184     return JNI_TRUE;
       
   185 }
       
   186 
       
   187 JNIEXPORT jbyteArray JNICALL
       
   188 Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGnomeVfs
       
   189     (JNIEnv* env, jclass this, jlong pathAddress)
       
   190 {
       
   191     char* path = (char*)jlong_to_ptr(pathAddress);
       
   192     const char* mime = (*gnome_vfs_mime_type_from_name)(path);
       
   193 
       
   194     if (mime == NULL) {
       
   195         return NULL;
       
   196     } else {
       
   197         jbyteArray result;
       
   198         jsize len = strlen(mime);
       
   199         result = (*env)->NewByteArray(env, len);
       
   200         if (result != NULL) {
       
   201             (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
       
   202         }
       
   203         return result;
       
   204     }
       
   205 }