1 / 55

Welcome

Welcome. WebTech Meeting, Waltham December 16 th. Java/.NET Interoperability. by Alexander Krapf. Speaker. Co-founder & President of Codemesh, Inc. 15 years of C++ development experience 7 years of Java development experience 1 year of .NET experience

jaafar
Download Presentation

Welcome

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Welcome WebTech Meeting, Waltham December 16th Java/.NET Interoperability by Alexander Krapf

  2. Speaker • Co-founder & President of Codemesh, Inc. • 15 years of C++ development experience • 7 years of Java development experience • 1 year of .NET experience • 5 years of integration of Java with other languages

  3. Agenda • Why Integrate? • General Solution Approaches • Integration Details • Type Mapping Integration Architecture • P/Invoke & JNI • Demonstration • Q & A

  4. Integrating Java and .NET ... Why? • .NET is becoming the de-facto standard for Microsoft-centric development • Java has a huge installed base on the server side (J2EE) • Enterprises are more and more agnostic regarding server architectures

  5. Compliance Trader .NET Supervisor Integrating Java and .NET ... Why? Example 1: Enterprise Messaging using JMS Matching Engine Trader Trader Audit Java Reporting

  6. Client 3rd Party App .NET Integrating Java and .NET ... Why? Example 2: J2EE Client Server Applications J2EE Server Client Client Web Java

  7. Application #2 Application #1 Integrating Java and .NET ... Why? Example 3: Reusing Existing Infrastructure Application Configuration /Discovery Data AccessObjects Logging 3rd PartyLibrary JNDI JDBC / JDO IO / NIO Java .NET

  8. Integrating Java and .NET ... Why? Example 5: Workflow with Backoffice Apps Excel populate data EJB Word JMS Word publish report Word

  9. Definition of Java What is the Java platform?

  10. Definition of .NET What is the .NET platform? CLR CLR: Common Language Runtime CIL: Common Intermediate Language CTS: Common Type System CLS: Common Language Specification C#LanguageSpec HostSpec VB.NETLanguageSpec CTS CIL CLS mC++LanguageSpec

  11. What we really want Define the goals • Write C#, VB, C++, etc. source code that “links with” arbitrary Java code easily • Write C#, VB, C++, etc. source code that allows natural use of all native features • Use all important Java language features from .NET, including callbacks • Impose no prohibitive performance penalty

  12. 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, “MyICtx” ); InitialContext ictx = new InitialContext( ht ); Context ctx = (Context)ictx.lookup( “test” ); … }

  13. 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, “MyICtx” ); InitialContext ictx = new InitialContext( ht ); Context ctx = (Context)ictx.lookup( “test” ); … }

  14. Integrating Java with .NET ... How? Architectural Alternatives • Translation-based: Java to C#, Bytecode to IL • Serialization/Message-based: CORBA, Webservices, MOM, custom • Proxy-based: Java Native Interface managed C++ or PInvoke

  15. Translation-Based Integration Promising at first, but very problematic • Large set of classes involved • transitive closure for Object: ~ 260 classes • Still need JRE or reimplementation of all native libraries • IL is very different from JVM bytecode • Interface/Implementation designs are problematic • Reflection, Class.forName() are problematic • “Stale bits” are uncheckable

  16. Serialization/Message-Based • CORBA • WebServices • MOM • Custom - database - filesystem - sockets

  17. Serialization/Message-Based • 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

  18. WebService Architecture UDDI Registry   HTTP Server   Java ApplicationWebservice .NET Webservice client  ServerProcesses

  19. WebService Development Process • Tools - JAX APIs for class to message and message to class mapping - proprietary webservice vendor tools

  20. WebService Summary • appropriate for WebService and SOA use • distributed • loosely coupled • unknown participants • inappropriate for mere language integration • weakly typed • not all types expressible • wrong modularity • really bad performance

  21. Serialization/Message-Based • Pros - usable without JRE • Cons - big performance penalty - complex deployment - difficult callback design - not 100% reliable - not 100% interoperable

  22. C C# JNI PInvoke API DLL assembly Proxy-Based Solution Architectural Diagram JVM GeneratedProxyClasses(C#) .NETUser

  23. Proxy-Based Solution EJB Example .NET Client RuntimeLibrary JVM J2EE Server Home Home EJBBean Home Home EJBBean Server Client

  24. Proxy-Based Solution Performance Characteristics • Design and Implementation have huge impact on performance • 0-25% overhead depending on application • Negligible penalty in most applications • Strings can be relatively costly

  25. Proxy-Based Solution xcopy Deployment <myapp> bin application binaries and config files lib application Java classes (jar,zip) jre private Java runtime environment

  26. Proxy-Based Solution Threading Model public static void main(String[] args ){ Runnable r = new MyRunnable(); … new Thread( r ).start(); … } How does this code behave in Java and in .NET ?

  27. 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?

  28. Type Mapping General Issues • Both platforms have object models • Slightly incompatible specifications • Both platforms have class libraries • Typenames are duplicated • Different Naming Policies

  29. Object Model Mapping Overall a good match • class to class • interface to interface • constructor to constructor • method to method • field to property

  30. Object Model Mapping But there are problems • conflicting Array models • conflicting Exception models • cannot declare static members for interfaces (CLS) • CLS has more restrictive naming than Java

  31. Object Model Mapping Object Lifecycle • both sides use garbage collection • JNI references need to be freed explicitly • IDisposable offers what we need • Care required with callbacks!

  32. Object Model Mapping ClassesJava .NET Object Object String Throwable String Exception Error Exception SystemException ApplicationException RuntimeException

  33. Object Custom Type Throwable Custom Type Custom Type Error Exception CustomException CustomException Neither Strings nor Throwablesare Java Objects anymore! CustomException RuntimeException Type System Mapping .NET with mapped Java Types Object String String Exception SystemException ApplicationException

  34. Object Model Mapping Object Requirements • Java proxy types should extend proxy Object type (with exceptions) • Every proxy type must be usable in all places where Java Object is legal

  35. Object Model Mapping String Requirements • .NET string literals must be usable in places where Java strings are expected • .NET string literals must be usable in places where Java strings are legal String getProperty( String name ) Object put( Object key, Object value )

  36. Object Model Mapping String Mapping • java.lang.String must map toSystem.String because of literals • Large impact on Object-type elements Object put( Object key, Object value ) becomes object put( object key, object value )

  37. Object Model Mapping Interface Requirements • interface types must be usable in all places where Java uses the Object type • interface inheritance (multiple interfaces, directly and indirectly) • static interface elements • polymorphic use (concrete object used through interface type)

  38. Object Model Mapping Interface Mapping • Every Java interface maps to a .NET interface and a .NET class • .NET interface type used for declarations • .NET class type used for instantiation of proxy objects and to hold static elements Which one gets the natural name?

  39. Object Model Mapping Array Requirements • Allow subscript operator to access element • Allow built-in semantic uses • Use native arrays and proxy arrays interchangably arrInt[ 42 ] += 5; foreach( int i in arrInt ) sum += i; int[] arrNative = new int[ 5 ];result = MyProxyClass.sumItUp( arrNative );

  40. Object Model Mapping Array Mapping • Cannot extend System.Array, so fully functional .NET arrays are not possible • Proxy array type declares index operator for 90% of desired functionality • Conversion operators provide ability to use .NET array types interchangeably

  41. Object Model Mapping Exceptions • No exception specification clause in .NET method declarations! • Need to extend System.Exception or subclass • Cannot extend java.lang.Object proxy class! • May need special serialization support

  42. Object Model Mapping Fields • Java fields map extremely well to .NET properties • Care needs to be taken with static interface fields (Impl classes) • final fields translate to read-only properties

  43. Object Model Mapping Fields Java .NET interface Context { } public class ContextImpl : Context { public static string PROVIDER_URL { get { return …; } } } interface Context { public static final String PROVIDER_URL = “…”; }

  44. C JVM .NET Generated Code env->SetIntField( obj, fid, value ); public int x { set { fx.set( value ); } } C Runtime DLL JNIHelper.set( obj, value ); .NET Runtime Assembly PInvokeHelper.set( fx, value ); Object Model Mapping Anatomy of a Field Access .NET Application Code f.x = 5;

  45. Object Model Mapping Methods and Constructors • because we’re dealing with managed code, we can directly map all constructors • no throws clause • Special cases for methods like toString(),equals(Object), etc.

  46. Object Model Mapping Virtual Methods • All non-static Java methods are implicitly virtual • .NET methods can choosevirtual is explicit, optional keyword Which virtual resolution mechanism should we use?

  47. Specification Incompatibilities • Corresponding methods of same name but different specification • Corresponding methods with different names but identical specification • CLS Type restrictions • Naming rules (CLS generally more restrictive than JLS) • Different APIs, same purpose (Iterator vs. IEnumerable etc.)

  48. Naming Policy What’s the right way to name .NET proxies for Java types? • Very important for usability Offer alternatives: • Keep Java names or • Uppercase first character or • Prefix I for interface names

  49. Naming Policy Take care with CLS constraints! • No identifiers that only differ in case at same scope • No elements of same name but different element type (method and field of same name, class and method of same name) • Don’t break contracts when changing names!

  50. PInvoke A way to call unmanaged code from managed code Managed Code (in assembly) class MyInvoker { [DllImport(“myunmanaged.dll”)] public static extern int callMe( int val ); } Unmanaged Code (in myunmanaged.dll) extern “C” __declspec(dllexport) int __stdcall callMe( int val );

More Related