src/JavaClassRegistry.st
author vranyj1
Fri, 28 Oct 2011 20:15:40 +0000
branchjk_new_structure
changeset 1058 1db06119bf3e
parent 1057 9031c415cacf
child 1059 22fe06c9e367
permissions -rw-r--r--
Temporary commit (fixing class loaders - classes gets registered twice)

"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger
 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 Parts of the code written by Claus Gittinger are under following
 license:

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 Parts of the code written at SWING Reasearch Group [1] are MIT licensed:

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

 [1] Code written at SWING Research Group contain a signature
     of one of the above copright owners.
"
"{ Package: 'stx:libjava' }"

Object subclass:#JavaClassRegistry
	instanceVariableNames:'classLoadersAndClasses systemClassLoader'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Support'
!

!JavaClassRegistry class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger
 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 Parts of the code written by Claus Gittinger are under following
 license:

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 Parts of the code written at SWING Reasearch Group [1] are MIT licensed:

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

 [1] Code written at SWING Research Group contain a signature
     of one of the above copright owners.

"
!

documentation
"
    documentation to be added.

    [author:]
        Jan Vrany <jan.vrany@fit.cvut.cz>

    [instance variables:]

    [class variables:]

    [see also:]

"
! !

!JavaClassRegistry class methodsFor:'instance creation'!

new
^ self basicNew initialize.
! !

!JavaClassRegistry methodsFor:'accessing'!

classForName: className loader: classLoader ifAbsent: aBlock 
    "Get class loaded by given classLoader from registry or evaluate aBlock if class is not yet registered"

    | classes |

    classes := classLoadersAndClasses at: classLoader ifAbsent: [^aBlock value].
    ^classes at: className ifAbsent: aBlock

    "Modified: / 21-10-2011 / 12:39:04 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Created: / 23-10-2011 / 11:40:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

classForName: className loader: classLoader ifAbsentPut: block 

    "Get class loaded by classLoader from registry. if absent block is evaluated and resulting class in registered in registry, "
    
    | classes |

    classes := classLoadersAndClasses at: classLoader ifAbsent: nil.
    classes isNil ifTrue:[
        classes := classLoadersAndClasses at: classLoader put: Dictionary new        
    ].
    ^classes at: className ifAbsent: [
        | class |

        class := block value.
        class notNil ifTrue:[
            self registerClass: class.
        ].
        class "return it"
    ]

    "Created: / 21-10-2011 / 12:00:30 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Created: / 23-10-2011 / 11:36:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

classNamed: className

    "Return a class for given classname loaded by 'current classloader' or 
     nil if not yet loaded"

    ^self classNamed: className loader: JavaClassReader classLoaderQuerySignal query

    "Created: / 23-10-2011 / 12:23:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

classNamed: className loader: classLoader

    "Return a class for given classname loaded by given classloader or 
     nil if not yet loaded"

    ^self classForName: className loader: classLoader ifAbsent:[nil].

    "Created: / 23-10-2011 / 12:23:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

classes

    ^Iterator on:[:whatToDo|self classesDo: whatToDo]

    "Created: / 23-10-2011 / 20:14:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'accessing-obsolete'!

at: className 

     <resource: #obsolete> 

    self breakPoint:#jv info: 'Use #classForName:'.
    self breakPoint:#mh info: 'Use #classForName:'.

    ^ self classNamed: className

    "Modified: / 23-10-2011 / 12:23:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

at: classLoader at: className

    <resource: #obsolete> 

    self breakPoint:#jv info: 'Use #classForName:loader:'.
    self breakPoint:#mh info: 'Use #classForName:loader:'.

    ^self classNamed: className loader: classLoader

    "Modified: / 23-10-2011 / 12:23:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

at: classLoader at: className ifAbsent: aBlock 

    <resource: #obsolete>

    self breakPoint:#mh info: 'Use #classForName:loader:isAbsent:'.
    self breakPoint:#jv info: 'Use #classForName:loader:isAbsent:'.

    ^self classForName: className loader: classLoader ifAbsent: aBlock.

    "Modified: / 21-10-2011 / 12:39:04 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 23-10-2011 / 11:47:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

at: classLoader at: className ifAbsentPut: aBlock 

    <resource: #obsolete> 

    self breakPoint:#jv info: 'Use #classForName:loader:isAbsentPut:'.
    self breakPoint:#mh info: 'Use #classForName:loader:isAbsentPut:'.

    ^self classForName: className loader: classLoader ifAbsentPut: aBlock

    "Created: / 21-10-2011 / 12:00:30 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 23-10-2011 / 11:41:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'class loading'!

loadFile: aFilename 
    "reads a class from aFilename, installs and returns it."
    
    | aClass |

    self breakPoint: #mh.
    aClass := JavaClassReader readFile: aFilename ignoring: Set new.
    aClass isJavaClass ifFalse:[self breakPoint:#mh].
    self registerClass: aClass.
    ^ aClass.

    "Created: / 15-04-1996 / 14:58:53 / cg"
    "Modified: / 12-05-1998 / 22:06:52 / cg"
    "Modified: / 23-10-2011 / 11:55:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

loadStream: javaClassDataStream loader: aJavaClassLoader 
    "reads a class from aStream and returns it.
     The JavaClass is installed as global.
     If new classes are required to be loaded, aClassLoader is
     asked to do it."
    
    | javaClass |

    self breakPoint: #mh.
    JavaClassReader classLoaderQuerySignal answer: aJavaClassLoader
        do: [
            javaClass := JavaClassReader readStream: javaClassDataStream
                        ignoring: (Set new).
            javaClass 
                ifNil: [
                    Logger 
                        log: 'JavaClassReader was not able to read given data stream'
                        severity: #warn
                        facility: #JavaClassRegistry.
                    self breakPoint: #mh.
                    ^ nil.
                ].
            javaClass isJavaClass ifFalse: [ self breakPoint: #mh. ].
            javaClass classLoader: aJavaClassLoader.
            self registerClass: javaClass.
        ].
    ^ javaClass

    "Modified: / 23-10-2011 / 11:57:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'enumerating'!

classesDo: aBlock

    classLoadersAndClasses do:[:classes|
        classes do:[:class|
            aBlock value: class
        ]        
    ]

    "Created: / 23-10-2011 / 20:13:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'helpers'!

addClassLoader: aJavaClassLoader

classLoadersAndClasses at: aJavaClassLoader put: Dictionary new.
!

getClassesDefinedBy:classLoader 
    ^classLoadersAndClasses at: classLoader ifAbsent: [nil].
!

getSystemClassLoader
    systemClassLoader 
        ifNil: [
            systemClassLoader := (JavaVM classForName: 'java.lang.ClassLoader') 
                        perform: #'getSystemClassLoader()Ljava/lang/ClassLoader;'.
        ].
    ^ systemClassLoader.
!

removeClassLoader: aJavaClassLoader 
    classLoadersAndClasses removeKey: aJavaClassLoader.
! !

!JavaClassRegistry methodsFor:'initialization'!

flush

    self initialize.
    systemClassLoader := nil.

    "Modified: / 24-10-2011 / 16:41:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

initialize
    classLoadersAndClasses := WeakIdentityDictionary new.
    classLoadersAndClasses at: nil put: Dictionary new.

    "Modified: / 24-10-2011 / 16:42:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry methodsFor:'registering'!

registerClass: aJavaClass
    | classes |

    classLoadersAndClasses keysAndValuesDo:[:loader :classes|

        (classes includesKey: aJavaClass name) ifTrue:[
            loader == aJavaClass classLoader ifTrue:[
                (classes at: aJavaClass name) ~~ aJavaClass ifTrue:[
                    self error:'Trying to register class twice!!'
                ].
            ] ifFalse:[
                self breakPoint: #jv.
            ]
        ]                        
    ].

    classes := classLoadersAndClasses at: aJavaClass classLoader ifAbsent: nil.
    classes isNil ifTrue:[
        classes := classLoadersAndClasses at: aJavaClass classLoader put: Dictionary new.
    ].
    classes at: aJavaClass name put: aJavaClass.

    "Created: / 23-10-2011 / 11:53:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaClassRegistry class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !