#*****************************************************************************
#   Copyright 2004-2008 Steve Menard
#
#   Licensed 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.
#
#*****************************************************************************
import sys

import _jpype

from . import _jclass
from . import _jarray
from . import _jwrapper
from . import _jproxy
from . import _jexception
from . import _jcollection
from . import _jobject
from . import _jio
from . import _properties
from . import nio
from . import reflect
from . import _refdaemon

_usePythonThreadForDaemon = False

def setUsePythonThreadForDeamon(v):
    global _usePythonThreadForDaemon
    _usePythonThreadForDaemon = v

_initializers=[]

def registerJVMInitializer(func):
    """Register a function to be called after jvm is started"""
    _initializers.append(func)

def _initialize():
    _properties._initialize()
    _jclass._initialize()
    _jarray._initialize()
    _jwrapper._initialize()
    _jproxy._initialize()
    _jexception._initialize()
    _jcollection._initialize()
    _jobject._initialize()
    nio._initialize()
    reflect._initialize()
    for func in _initializers:
        func()

def isJVMStarted() :
    return _jpype.isStarted()

def startJVM(jvm=None, *args, **kwargs):
    """
    Starts a Java Virtual Machine.  Typical usage
    startJVM(getDefaultJVMPath(), classpath=getClassPath())

    Args:
      jvm (str):  Path to the jvm library file (libjvm.so, jvm.dll, ...)
        default=None, will use jpype.get_default_jvm_path()
      *args (str[]): Arguments to give to the JVM
      classpath (Optional[string]): set the classpath for the jvm.
        This will override any classpath supplied in the arguments
        list.
      ignoreUnrecognized (Optional[bool]): option to jvm to ignore
        invalid jvm arguments.  (Default False)
    """
    if jvm is None:
        jvm = get_default_jvm_path()

    if 'ignoreUnrecognized' not in kwargs:
        kwargs['ignoreUnrecognized']=False

    # Classpath handling
    args = list(args)
    if 'classpath' in kwargs:
        args.append('-Djava.class.path=%s'%(kwargs['classpath']))

    _jpype.startup(jvm, tuple(args), kwargs['ignoreUnrecognized'])
    _initialize()

    # start the reference daemon thread
    if _usePythonThreadForDaemon:
        _refdaemon.startPython()
    else:
        _refdaemon.startJava()

def attachToJVM(jvm):
    _jpype.attach(jvm)
    _initialize()

def shutdownJVM():
    _refdaemon.stop()
    _jpype.shutdown()

def isThreadAttachedToJVM():
    return _jpype.isThreadAttachedToJVM()

def attachThreadToJVM():
    _jpype.attachThreadToJVM()

def detachThreadFromJVM():
    _jpype.detachThreadFromJVM()


def get_default_jvm_path():
    """
    Retrieves the path to the default or first found JVM library

    :return: The path to the JVM shared library file
    :raise ValueError: No JVM library found
    """
    if sys.platform in ("win32", "cygwin"):
        # Windows or Cygwin
        from ._windows import WindowsJVMFinder
        finder = WindowsJVMFinder()
    elif sys.platform == "darwin":
        # Mac OS X
        from ._darwin import DarwinJVMFinder
        finder = DarwinJVMFinder()
    else:
        # Use the Linux way for other systems
        from ._linux import LinuxJVMFinder
        finder = LinuxJVMFinder()

    return finder.get_jvm_path()

# Naming compatibility
getDefaultJVMPath = get_default_jvm_path


class ConversionConfigClass(object):
    def __init__(self):
        self._convertString = 1

    def _getConvertString(self):
        return self._convertString

    def _setConvertString(self, value):
        if value:
            self._convertString = 1
        else:
            self._convertString = 0

        _jpype.setConvertStringObjects(self._convertString)

    string = property(_getConvertString, _setConvertString, None)

ConversionConfig = ConversionConfigClass()

@@ -1056,16 +1056,41 @@
 efdaemon
+%0Afrom . import _classpath
 %0A%0A_usePy
@@ -1772,162 +1772,435 @@
 def 
-startJVM(jvm=None, *args, **kwargs):%0A    %22%22%22%0A    Starts a Java Virtual Machine.  Typical usage%0A    startJVM(getDefaultJVMPath(), classpath=getClassPath())
+_hasClassPath(args):%0A    for i in args:%0A        if i.startswith('-Djava.class.path'):%0A            return True%0A    return False%0A%0Adef startJVM(jvm=None, *args, **kwargs):%0A    %22%22%22%0A    Starts a Java Virtual Machine.  Without options it will start%0A    the JVM with the default classpath and jvm.  The default classpath%0A    will be determined by jpype.getClassPath().  The default JVM is %0A    determined by jpype.getDefaultJVMPath().
 %0A%0A  
@@ -2300,17 +2300,16 @@
 ult=None
-,
  will us
@@ -2319,30 +2319,27 @@
 pype.get
-_d
+D
 efault
-_jvm_p
+JVMP
 ath()%0A  
@@ -2711,24 +2711,306 @@
 jvm_path()%0A%0A
+        # Check to see that the user has not set the classpath%0A        # Otherwise use the default if not specified%0A        if not _hasClassPath(args) and 'classpath' not in kwargs:%0A           kwargs%5B'classpath'%5D=_classpath.getClassPath()%0A           print(%22Use default classpath%22)%0A%0A
     if 'igno
@@ -3151,32 +3151,62 @@
 spath' in kwargs
+ and kwargs%5B'classpath'%5D!=None
 :%0A        args.a
@@ -3256,16 +3256,47 @@
 path'%5D))
+%0A        print(%22Set classpath%22)
 %0A%0A    _j

Work on classpath