Alien-SVN

 view release on metacpan or  search on metacpan

src/subversion/subversion/bindings/javahl/native/Prompter.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 Prompter.cpp
 * @brief Implementation of the class Prompter
 */

#include "Prompter.h"
#include "Pool.h"
#include "JNIUtil.h"
#include "JNIStringHolder.h"
#include "../include/org_apache_subversion_javahl_callback_UserPasswordCallback.h"
#include <apr_strings.h>
#include "svn_auth.h"
#include "svn_error.h"
#include "svn_error_codes.h"
#include "svn_private_config.h"

/**
 * Constructor
 * @param jprompter     a global reference to the Java callback object
 */
Prompter::Prompter(jobject jprompter)
{
  m_prompter = jprompter;
}

Prompter::~Prompter()
{
  if (m_prompter!= NULL)
    {
      // Since the reference to the Java object is a global one, it
      // has to be deleted.
      JNIEnv *env = JNIUtil::getEnv();
      env->DeleteGlobalRef(m_prompter);
    }
}

/**
 * Create a C++ peer object for the Java callback object
 *
 * @param jprompter     Java callback object
 * @return              C++ peer object
 */
Prompter *Prompter::makeCPrompter(jobject jprompter)
{
  // If we have no Java object, we need no C++ object.
  if (jprompter == NULL)
    return NULL;

  JNIEnv *env = JNIUtil::getEnv();

  // Create a local frame for our references
  env->PushLocalFrame(LOCAL_FRAME_SIZE);
  if (JNIUtil::isJavaExceptionThrown())
    return NULL;

  // Sanity check that the Java object implements UserPasswordCallback.
  jclass clazz = env->FindClass(JAVA_PACKAGE"/callback/UserPasswordCallback");
  if (JNIUtil::isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  if (!env->IsInstanceOf(jprompter, clazz))
    POP_AND_RETURN_NULL;

  // Create a new global ref for the Java object, because it is
  // longer used that this call.
  jobject myPrompt = env->NewGlobalRef(jprompter);
  if (JNIUtil::isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  env->PopLocalFrame(NULL);

  // Create the C++ peer.
  return new Prompter(myPrompt);
}

/**
 * Retrieve the username from the Java object
 * @return Java string for the username or NULL
 */
jstring Prompter::username()
{
  JNIEnv *env = JNIUtil::getEnv();

  // Create a local frame for our references
  env->PushLocalFrame(LOCAL_FRAME_SIZE);
  if (JNIUtil::isJavaExceptionThrown())
    return NULL;

  // The method id will not change during the time this library is
  // loaded, so it can be cached.
  static jmethodID mid = 0;

  if (mid == 0)
    {
      jclass clazz = env->FindClass(JAVA_PACKAGE"/callback/UserPasswordCallback");
      if (JNIUtil::isJavaExceptionThrown())
        POP_AND_RETURN_NULL;

      mid = env->GetMethodID(clazz, "getUsername", "()Ljava/lang/String;");
      if (JNIUtil::isJavaExceptionThrown() || mid == 0)
        POP_AND_RETURN_NULL;
    }

  jstring ret = static_cast<jstring>(env->CallObjectMethod(m_prompter, mid));
  if (JNIUtil::isJavaExceptionThrown())
    POP_AND_RETURN_NULL;

  return (jstring) env->PopLocalFrame(ret);
}

/**
 * Retrieve the password from the Java object
 * @return Java string for the password or NULL
 */
jstring Prompter::password()
{
  JNIEnv *env = JNIUtil::getEnv();

  // Create a local frame for our references
  env->PushLocalFrame(LOCAL_FRAME_SIZE);
  if (JNIUtil::isJavaExceptionThrown())
    return NULL;

  // The method id will not change during the time this library is



( run in 0.518 second using v1.01-cache-2.11-cpan-97f6503c9c8 )