162ceda9e8f98d8046d2a0aeb020ce70.ppt
- Количество слайдов: 56
Welcome Jini Community Meeting Cambridge March 23 -35, 2004 Jini in a. NET Environment by Alexander Krapf Copyright 2004 by Codemesh, Inc. All Rights Reserved
Speaker u Co-founder & President of Codemesh, Inc. u 16 years of C++ development experience u 7 years of Java development experience u 2 years of. NET experience u 5 years of integration of Java with other languages Copyright 2004 by Codemesh, Inc. All Rights Reserved
Agenda u Why should I care about. NET? u General Integration Approaches What makes Jini a special case? u Integration Details Type Mapping Integration Architecture P/Invoke & JNI u Demonstration u Q&A Copyright 2004 by Codemesh, Inc. All Rights Reserved
Jini and. NET Why would I want to do that ? Copyright 2004 by Codemesh, Inc. All Rights Reserved
Jini and. NET. . . Why? u Because. NET is becoming the de-facto standard for Microsoft centric development u Because. NET has nothing like Jini u Because. NET does have some redeeming features Copyright 2004 by Codemesh, Inc. All Rights Reserved
Jini and. NET. . . Why? u Because we’re not religious about technology. We mix and match where appropriate! u Because we want Jini to succeed, and for that it can’t be seen as Java-only u Because we benefit from promoting Java “guts” technologies to. NET users Copyright 2004 by Codemesh, Inc. All Rights Reserved
Jini and. NET. . . Why? u Because not everyone develops software in Java u Because I would hate to see distributed computing dominated by J 2 EE and Web Services Copyright 2004 by Codemesh, Inc. All Rights Reserved
What does an integration look like? Example 1: Java. Spaces VB. NET Client Java Servers Java. Space C# Client Java Client Copyright 2004 by Codemesh, Inc. All Rights Reserved
What does an integration look like? Example 2: Service Oriented Architecture C# Client Jini Service Java Client VB Client Not a very exciting slide, but an exciting design! Copyright 2004 by Codemesh, Inc. All Rights Reserved
Jini and. NET OK, I buy the occasional need, but what exactly does “Jini and. NET” mean ? Copyright 2004 by Codemesh, Inc. All Rights Reserved
Definition of Java What is the Java platform? Copyright 2004 by Codemesh, Inc. All Rights Reserved
Definition of. NET What is the. NET platform? CLR C# Language Spec VB. NET Language Spec m. C++ Language Spec CLR: Common Language Runtime Host Spec CIL: Common Intermediate Language CTS: Common Type System CTS CLS: Common Language Specification CIL CLS Copyright 2004 by Codemesh, Inc. All Rights Reserved
What we really want Define the goals u Write C#, VB, C++, etc. source code that “links with” arbitrary Jini code easily u Write C#, VB, C++, etc. source code that allows natural use of all native features u Use all important Jini features from. NET, including callbacks u Impose no prohibitive performance penalty Copyright 2004 by Codemesh, Inc. All Rights Reserved
What we really want (cont. ) Write C# code like this: [STAThread] static void Main(string[] args) { Hashtable ht = new Hashtable(); ht. put( Context. PROVIDER_URL, “file: //mydir” ); ht. put( Context. INITIAL_CONTEXT_FACTORY, “My. ICtx” ); Initial. Context ictx = new Initial. Context( ht ); ctx = (Context)ictx. lookup( “test” ); … } Copyright 2004 by Codemesh, Inc. All Rights Reserved
What we really want (cont. ) Lots of Problems in a short snippet! [STAThread] static void Main(string[] args) { Hashtable ht = new Hashtable(); ht. put( Context. PROVIDER_URL, “file: //mydir” ); ht. put( Context. INITIAL_CONTEXT_FACTORY, “My. ICtx” ); Initial. Context ictx = new Initial. Context( ht ); ctx = (Context)ictx. lookup( “test” ); … } Copyright 2004 by Codemesh, Inc. All Rights Reserved
Jini and. NET The Jini twist on the Java/. NET integration problem… Copyright 2004 by Codemesh, Inc. All Rights Reserved
Why it is a hard problem… Jini leverages Java! Application #1 Application #2 JINI RMI/ code mobility Security/ Classloaders Platform independence Copyright 2004 by Codemesh, Inc. All Rights Reserved
Integrating Jini with. NET. . . How? Technical Requirements u Must accommodate downloaded code u Must support RMI u Must support Java and Jini security u Must support callbacks Copyright 2004 by Codemesh, Inc. All Rights Reserved
Integrating Jini with. NET. . . How? Architectural Alternatives u Serialization/Message-based: CORBA, Webservices, MOM, custom u Translation-based: Java to C#, Bytecode to IL u Proxy-based: Java Native Interface managed C++ or PInvoke Copyright 2004 by Codemesh, Inc. All Rights Reserved
Serialization/Message-Based u CORBA u Web. Services u MOM u Custom - database - filesystem - sockets Copyright 2004 by Codemesh, Inc. All Rights Reserved
Serialization/Message-Based u Different technologies, same principles - object/service discovery through naming service, UDDI, custom registries - argument/result serialization through IIOP, SOAP, XML, . . . - several processes or “objects” involved Java process, . NET process, file locks, table rows, helpers Copyright 2004 by Codemesh, Inc. All Rights Reserved
Translation-Based Integration Promising at first, but very problematic u Large set of classes involved transitive closure for Object: ~ 260 classes u Still need JRE or reimplementation of all native libraries u IL is very different from JVM bytecode u Interface/Implementation designs are problematic u Reflection, Class. for. Name() are problematic u “Stale bits” are uncheckable and out-of-date Copyright 2004 by Codemesh, Inc. All Rights Reserved
Proxy-Based Solution Jini Example Proxy’ Proxy Service Runtime Library JVM . NET Client Process Java Server Process Copyright 2004 by Codemesh, Inc. All Rights Reserved
Proxy-Based Solution Architectural Diagram JVM C JNI C# PInvoke DLL Generated Proxy Classes (C#) . NET User API assembly Copyright 2004 by Codemesh, Inc. All Rights Reserved
Proxy-Based Solution Performance Characteristics u Design and Implementation have huge impact on performance u 0 -200% overhead, depending on application u Negligible overhead in most applications u up to 25% overhead in most others u Strings can be relatively costly Copyright 2004 by Codemesh, Inc. All Rights Reserved
Proxy-Based Solution xcopy Deployment <myapp> bin application binaries and config files lib application Java classes (jar, zip) jre private Java runtime environment Copyright 2004 by Codemesh, Inc. All Rights Reserved
Proxy-Based Solution Threading Model public static void main(String[] args ) { Runnable r = new My. Runnable(); … new Thread( r ). start(); … } How does this code behave in Java and in. NET ? Copyright 2004 by Codemesh, Inc. All Rights Reserved
Type Mapping The Common Language Runtime provides access to the. NET class library or libraries that are based upon it using the language bindings of your choice. What about the Java class library? Copyright 2004 by Codemesh, Inc. All Rights Reserved
Type Mapping General Issues u Both platforms have object models u Slightly incompatible specifications u Both platforms have class libraries u Typenames are duplicated u Different Naming Policies Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Overall a good match u class to class u interface to interface u constructor to constructor u method to method u field to property Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping But there are problems u conflicting Array models u conflicting Exception models u cannot declare static members for interfaces (CLS) u CLS has more restrictive naming than Java Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Object Lifecycle u both sides use garbage collection u JNI references need to be freed explicitly u IDisposable together with finalization offer what we need u Care required with callbacks! Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Classes Java Object String . NET Object Throwable Error Exception String Exception System Exception Application Exception Runtime Exception Copyright 2004 by Codemesh, Inc. All Rights Reserved
Type System Mapping. NET with mapped Java Types Object Custom Type String Exception System Exception Application Exception Throwable Error Neither Strings nor Throwables are Java Objects anymore! Custom Exception Exception Runtime Exception Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Object Requirements u Java proxy types should extend proxy Object type (with exceptions) u Every proxy type must be usable in all places where Java Object is legal Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping String Requirements u. NET string literals must be usable in places where Java strings are expected String get. Property( String name ) u. NET string literals must be usable in places where Java strings are legal Object put( Object key, Object value ) Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping String Mapping u java. lang. String must map to System. String because of literals u Large impact on Object-type elements Object put( Object key, Object value ) becomes object put( object key, object value ) Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Interface Requirements u interface types must be usable in all places where Java uses the Object type u interface inheritance (multiple interfaces, directly and indirectly) u static interface elements u polymorphic use (concrete object used through interface type) Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Interface Mapping u Every Java interface maps to a. NET interface and a. NET class u. NET interface type used for declarations u. NET class type used for instantiation of proxy objects and to hold static elements Which one gets the natural name? Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Array Requirements u Allow subscript operator to access element arr. Int[ 42 ] += 5; u Allow built-in semantic uses foreach( int i in arr. Int ) sum += i; u Use native arrays and proxy arrays interchangably int[] arr. Native = new int[ 5 ]; result = My. Proxy. Class. sum. It. Up( arr. Native ); Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Array Mapping u Cannot extend System. Array, so fully functional. NET arrays are not possible u Proxy array type declares index operator for 90% of desired functionality u Conversion operators provide ability to use. NET array types interchangeably Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Exceptions u No exception specification clause in. NET method declarations! u Need to extend System. Exception or subclass u Cannot extend java. lang. Object proxy class! u May need special serialization support Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Fields u Java fields map extremely well to. NET properties u Care needs to be taken with static interface fields (Impl classes) u final fields translate to read-only properties Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Fields Java . NET interface Context { } interface Context { public static final String PROVIDER_URL = “…”; } public class Context. Impl : Context { public static string PROVIDER_URL { get { return …; } } } Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Anatomy of a Field Access. NET Application Code f. x = 5; C JVM env->Set. Int. Field( obj, fid, value ); C Runtime DLL JNIHelper. set( obj, value ); . NET Generated Code public int x { set { fx. set( value ); } } Runtime Assembly PInvoke. Helper. set( fx, value ); Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Methods and Constructors u because we’re dealing with managed code, we can directly map all constructors u no throws clause u Special cases for methods like to. String(), equals(Object), etc. Copyright 2004 by Codemesh, Inc. All Rights Reserved
Object Model Mapping Virtual Methods u All non-static Java methods are implicitly virtual u. NET methods can choose virtual is explicit, optional keyword Which virtual resolution mechanism should we use? Copyright 2004 by Codemesh, Inc. All Rights Reserved
Specification Incompatibilities u Corresponding methods of same name but different specification u Corresponding methods with different names but identical specification u CLS Type restrictions u Naming rules (CLS generally more restrictive than JLS) u Different APIs, same purpose (Iterator vs. IEnumerable etc. ) Copyright 2004 by Codemesh, Inc. All Rights Reserved
Naming Policy What’s the right way to name. NET proxies for Java types? u Very important for usability Offer alternatives: u Keep Java names or u Uppercase first character or u Prefix I for interface names Copyright 2004 by Codemesh, Inc. All Rights Reserved
Naming Policy Take care with CLS constraints! u No identifiers that only differ in case at same scope u No elements of same name but different element type (method and field of same name, class and method of same name) u Don’t break contracts when changing names! Copyright 2004 by Codemesh, Inc. All Rights Reserved
PInvoke A way to call unmanaged code from managed code Managed Code (in assembly) class My. Invoker { [Dll. Import(“myunmanaged. dll”)] public static extern int call. Me( int val ); } Unmanaged Code (in myunmanaged. dll) extern “C” __declspec(dllexport) int __stdcall. Me( int val ); Copyright 2004 by Codemesh, Inc. All Rights Reserved
Using JNI (Example) jclass cl = env->Find. Class( “com. codemesh. My. Class” ); if( cl == 0 ) { jthrowable t = env->Exception. Occurred(); env->Exception. Clear(); return; } jmethod. ID mid = env->Get. Method. ID( cl, “<init>”, “(Ljava/lang/String; IIZ)V” ); if( mid == 0 ) { jthrowable t = env->Exception. Occurred(); env->Exception. Clear(); env->Delete. Local. Ref( cl ); return; } jstring arg 1 = env->New. String. UTF( “test” ); Copyright 2004 by Codemesh, Inc. All Rights Reserved
Using JNI (Example) cont. jobject temp = 0, result = 0; temp = env->New. Object( cl, mid, arg 1, 1, 2, JNI_TRUE ); if( temp == 0 ) { jthrowable t = env->Exception. Occurred(); env->Exception. Clear(); env->Delete. Local. Ref( cl ); return; } result = env->New. Global. Ref( temp ); env->Delete. Local. Ref( temp ); Copyright 2004 by Codemesh, Inc. All Rights Reserved
Demonstration u A Jini example: hello in C# u A look at the demo source code u A look at particular features of the enabling source code u A look at a code generator u Q&A Copyright 2004 by Codemesh, Inc. All Rights Reserved
Literature/References The Java Native Interface, Programmer’s Guide and Specification by Sheng Liang, Addison-Wesley, ISBN 0 -201 -32577 -2 Essential JNI by Rob Gordon, Prentice Hall, ISBN 0 -136 -79895 -0 . NET and COM, The Complete Interoperability Guide by Adam Nathan, SAMS, ISBN 0 -672 -32170 -X JNI Documentation http: //java. sun. com/products/jdk/1. 2/docs/guide/jni/ JGuru http: //www. jguru. com Stu Halloway’s Java Interop site http: //staff. develop. com/halloway Copyright 2004 by Codemesh, Inc. All Rights Reserved
Thank you! I’ll be around for more questions! Codemesh, Inc. P. O. Box 620 Carlisle, MA 01741 http: //www. codemesh. com info@codemesh. com Copyright 2004 by Codemesh, Inc. All Rights Reserved
162ceda9e8f98d8046d2a0aeb020ce70.ppt