Alien-SVN
view release on metacpan or search on metacpan
src/subversion/subversion/bindings/javahl/native/JNIUtil.cpp view on Meta::CPAN
/**
* @copyright
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
* @endcopyright
*
* @file JNIUtil.cpp
* @brief Implementation of the class JNIUtil
*/
#include "JNIUtil.h"
#include "Array.h"
#include <sstream>
#include <vector>
#include <locale.h>
#include <apr_strings.h>
#include <apr_tables.h>
#include <apr_general.h>
#include <apr_lib.h>
#include "svn_pools.h"
#include "svn_fs.h"
#include "svn_ra.h"
#include "svn_utf.h"
#include "svn_wc.h"
#include "svn_dso.h"
#include "svn_path.h"
#include "svn_cache_config.h"
#include <apr_file_info.h>
#include "svn_private_config.h"
#ifdef WIN32
/* FIXME: We're using an internal APR header here, which means we
have to build Subversion with APR sources. This being Win32-only,
that should be fine for now, but a better solution must be found in
combination with issue #850. */
extern "C" {
#include <arch/win32/apr_arch_utf8.h>
};
#endif
#include "SVNBase.h"
#include "JNIMutex.h"
#include "JNICriticalSection.h"
#include "JNIThreadData.h"
#include "JNIStringHolder.h"
#include "Pool.h"
// Static members of JNIUtil are allocated here.
apr_pool_t *JNIUtil::g_pool = NULL;
std::list<SVNBase*> JNIUtil::g_finalizedObjects;
JNIMutex *JNIUtil::g_finalizedObjectsMutex = NULL;
JNIMutex *JNIUtil::g_logMutex = NULL;
bool JNIUtil::g_initException;
bool JNIUtil::g_inInit;
JNIEnv *JNIUtil::g_initEnv;
char JNIUtil::g_initFormatBuffer[formatBufferSize];
int JNIUtil::g_logLevel = JNIUtil::noLog;
std::ofstream JNIUtil::g_logStream;
/**
* Initialize the environment for all requests.
* @param env the JNI environment for this request
*/
bool JNIUtil::JNIInit(JNIEnv *env)
{
// Clear all standing exceptions.
env->ExceptionClear();
// Remember the env parameter for the remainder of the request.
setEnv(env);
// Lock the list of finalized objects.
JNICriticalSection cs(*g_finalizedObjectsMutex) ;
if (isExceptionThrown())
return false;
// Delete all finalized, but not yet deleted objects.
for (std::list<SVNBase*>::iterator it = g_finalizedObjects.begin();
it != g_finalizedObjects.end();
++it)
{
delete *it;
}
g_finalizedObjects.clear();
return true;
}
/**
* Initialize the environment for all requests.
* @param env the JNI environment for this request
*/
bool JNIUtil::JNIGlobalInit(JNIEnv *env)
{
// This method has to be run only once during the run a program.
static bool run = false;
svn_error_t *err;
src/subversion/subversion/bindings/javahl/native/JNIUtil.cpp view on Meta::CPAN
// time window when two threads create their first SVNClient and
// SVNAdmin at the same time, but I do not see a better option
// without APR already initialized
if (g_inInit)
return false;
g_inInit = true;
g_initEnv = env;
apr_status_t status;
/* Initialize the APR subsystem, and register an atexit() function
* to Uninitialize that subsystem at program exit. */
status = apr_initialize();
if (status)
{
if (stderr)
{
char buf[1024];
apr_strerror(status, buf, sizeof(buf) - 1);
fprintf(stderr,
"%s: error: cannot initialize APR: %s\n",
"svnjavahl", buf);
}
return FALSE;
}
/* This has to happen before any pools are created. */
if ((err = svn_dso_initialize2()))
{
if (stderr && err->message)
fprintf(stderr, "%s", err->message);
svn_error_clear(err);
return FALSE;
}
if (0 > atexit(apr_terminate))
{
if (stderr)
fprintf(stderr,
"%s: error: atexit registration failed\n",
"svnjavahl");
return FALSE;
}
/* Create our top-level pool. */
g_pool = svn_pool_create(NULL);
apr_allocator_t* allocator = apr_pool_allocator_get(g_pool);
if (allocator)
{
/* Keep a maximum of 1 free block, to release memory back to the JVM
(and other modules). */
apr_allocator_max_free_set(allocator, 1);
}
svn_utf_initialize2(FALSE, g_pool); /* Optimize character conversions */
svn_fs_initialize(g_pool); /* Avoid some theoretical issues */
svn_ra_initialize(g_pool);
/* We shouldn't fill the JVMs memory with FS cache data unless explictly
requested. */
{
svn_cache_config_t settings = *svn_cache_config_get();
settings.cache_size = 0;
settings.file_handle_count = 0;
settings.single_threaded = FALSE;
svn_cache_config_set(&settings);
}
#ifdef ENABLE_NLS
#ifdef WIN32
{
WCHAR ucs2_path[MAX_PATH];
char *utf8_path;
const char *internal_path;
apr_pool_t *pool;
apr_status_t apr_err;
apr_size_t inwords, outbytes;
unsigned int outlength;
pool = svn_pool_create(g_pool);
/* get dll name - our locale info will be in '../share/locale' */
inwords = sizeof(ucs2_path) / sizeof(ucs2_path[0]);
HINSTANCE moduleHandle = GetModuleHandle("libsvnjavahl-1");
GetModuleFileNameW(moduleHandle, ucs2_path, inwords);
inwords = lstrlenW(ucs2_path);
outbytes = outlength = 3 * (inwords + 1);
utf8_path = reinterpret_cast<char *>(apr_palloc(pool, outlength));
apr_err = apr_conv_ucs2_to_utf8((const apr_wchar_t *) ucs2_path,
&inwords, utf8_path, &outbytes);
if (!apr_err && (inwords > 0 || outbytes == 0))
apr_err = APR_INCOMPLETE;
if (apr_err)
{
if (stderr)
fprintf(stderr, "Can't convert module path to UTF-8");
return FALSE;
}
utf8_path[outlength - outbytes] = '\0';
internal_path = svn_dirent_internal_style(utf8_path, pool);
/* get base path name */
internal_path = svn_dirent_dirname(internal_path, pool);
internal_path = svn_dirent_join(internal_path, SVN_LOCALE_RELATIVE_PATH,
pool);
bindtextdomain(PACKAGE_NAME, internal_path);
svn_pool_destroy(pool);
}
#else
bindtextdomain(PACKAGE_NAME, SVN_LOCALE_DIR);
#endif
#endif
#if defined(WIN32) || defined(__CYGWIN__)
/* See http://svn.apache.org/repos/asf/subversion/trunk/notes/asp-dot-net-hack.txt */
/* ### This code really only needs to be invoked by consumers of
### the libsvn_wc library, which basically means SVNClient. */
if (getenv("SVN_ASP_DOT_NET_HACK"))
{
err = svn_wc_set_adm_dir("_svn", g_pool);
if (err)
{
if (stderr)
{
fprintf(stderr,
"%s: error: SVN_ASP_DOT_NET_HACK failed: %s\n",
"svnjavahl", err->message);
}
svn_error_clear(err);
return FALSE;
}
}
#endif
svn_error_set_malfunction_handler(svn_error_raise_on_malfunction);
// Build all mutexes.
g_finalizedObjectsMutex = new JNIMutex(g_pool);
if (isExceptionThrown())
return false;
g_logMutex = new JNIMutex(g_pool);
if (isExceptionThrown())
return false;
// initialized the thread local storage
if (!JNIThreadData::initThreadData())
return false;
setEnv(env);
if (isExceptionThrown())
return false;
g_initEnv = NULL;
g_inInit = false;
return true;
}
/**
* Returns the global (not request specific) pool.
* @return global pool
src/subversion/subversion/bindings/javahl/native/JNIUtil.cpp view on Meta::CPAN
return NULL;
// Access the bytes.
jbyte *retdata = env->GetByteArrayElements(ret, NULL);
if (isJavaExceptionThrown())
return NULL;
// Copy the bytes.
memcpy(retdata, data, length);
// Release the bytes.
env->ReleaseByteArrayElements(ret, retdata, 0);
if (isJavaExceptionThrown())
return NULL;
return ret;
}
/**
* Create a Java byte array from an svn_string_t.
* @param str the string
*/
jbyteArray JNIUtil::makeJByteArray(const svn_string_t *str)
{
return JNIUtil::makeJByteArray(str->data, static_cast<int>(str->len));
}
/**
* Build the error message from the svn error into buffer. This
* method calls itselft recursively for all the chained errors
*
* @param err the subversion error
* @param depth the depth of the call, used for formating
* @param parent_apr_err the apr of the previous level, used for formating
* @param buffer the buffer where the formated error message will
* be stored
*/
void JNIUtil::assembleErrorMessage(svn_error_t *err, int depth,
apr_status_t parent_apr_err,
std::string &buffer)
{
// buffer for a single error message
char errbuf[256];
/* Pretty-print the error */
/* Note: we can also log errors here someday. */
/* When we're recursing, don't repeat the top-level message if its
* the same as before. */
if (depth == 0 || err->apr_err != parent_apr_err)
{
/* Is this a Subversion-specific error code? */
if ((err->apr_err > APR_OS_START_USEERR)
&& (err->apr_err <= APR_OS_START_CANONERR))
buffer.append(svn_strerror(err->apr_err, errbuf, sizeof(errbuf)));
/* Otherwise, this must be an APR error code. */
else
{
/* Messages coming from apr_strerror are in the native
encoding, it's a good idea to convert them to UTF-8. */
const char* utf8_message;
apr_strerror(err->apr_err, errbuf, sizeof(errbuf));
svn_error_t* utf8_err = svn_utf_cstring_to_utf8(
&utf8_message, errbuf, err->pool);
if (utf8_err)
{
/* Use fuzzy transliteration instead. */
svn_error_clear(utf8_err);
utf8_message = svn_utf_cstring_from_utf8_fuzzy(errbuf, err->pool);
}
buffer.append(utf8_message);
}
buffer.append("\n");
}
if (err->message)
buffer.append(_("svn: ")).append(err->message).append("\n");
if (err->child)
assembleErrorMessage(err->child, depth + 1, err->apr_err, buffer);
}
/**
* Throw a Java NullPointerException. Used when input parameters
* which should not be null are that.
*
* @param message the name of the parameter that is null
*/
void JNIUtil::throwNullPointerException(const char *message)
{
if (getLogLevel() >= errorLog)
logMessage("NullPointerException thrown");
JNIEnv *env = getEnv();
jclass clazz = env->FindClass("java/lang/NullPointerException");
if (isJavaExceptionThrown())
return;
env->ThrowNew(clazz, message);
setExceptionThrown();
env->DeleteLocalRef(clazz);
}
svn_error_t *JNIUtil::preprocessPath(const char *&path, apr_pool_t *pool)
{
/* URLs and wc-paths get treated differently. */
if (svn_path_is_url(path))
{
/* No need to canonicalize a URL's case or path separators. */
/* Convert to URI. */
path = svn_path_uri_from_iri(path, pool);
/* Auto-escape some ASCII characters. */
path = svn_path_uri_autoescape(path, pool);
/* The above doesn't guarantee a valid URI. */
if (! svn_path_is_uri_safe(path))
return svn_error_createf(SVN_ERR_BAD_URL, NULL,
_("URL '%s' is not properly URI-encoded"),
path);
/* Verify that no backpaths are present in the URL. */
if (svn_path_is_backpath_present(path))
return svn_error_createf(SVN_ERR_BAD_URL, NULL,
_("URL '%s' contains a '..' element"),
path);
/* strip any trailing '/' */
path = svn_uri_canonicalize(path, pool);
}
( run in 1.327 second using v1.01-cache-2.11-cpan-3d66aa2751a )