Download Jini and .NET

Document related concepts
no text concepts found
Transcript
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
 Co-founder & President of Codemesh, Inc.
 16 years of C++ development experience
 7 years of Java development experience
 2 years of .NET experience
 5 years of integration of Java with other
languages
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Agenda
 Why should I care about .NET?
 General Integration Approaches
What makes Jini a special case?
 Integration Details
Type Mapping
Integration Architecture
P/Invoke & JNI
 Demonstration
 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?
 Because .NET is becoming the
de-facto standard for Microsoft
centric development
 Because .NET has nothing like Jini
 Because .NET does have some
redeeming features
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Jini and .NET ... Why?
 Because we’re not religious about
technology.
We mix and match where appropriate!
 Because we want Jini to succeed,
and for that it can’t be seen as Java-only
 Because we benefit from promoting
Java “guts” technologies to .NET
users
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Jini and .NET ... Why?
 Because not everyone develops
software in Java
 Because I would hate to see distributed
computing dominated by J2EE and
Web Services
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
What does an integration look like?
Example 1:
JavaSpaces
VB.NET Client
Java Servers
JavaSpace
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?
JVM Spec
Language Spec
Class Libraries
Runtime Environment
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Definition of .NET
What is the .NET platform?
CLR
C#
Language
Spec
VB.NET
Language
Spec
CLR: Common Language
Runtime
Host
Spec
CIL:
Common Intermediate
Language
CTS: Common Type System
CTS
CLS: Common Language
Specification
CIL
CLS
mC++
Language
Spec
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
What we really want
Define the goals
 Write C#, VB, C++, etc. source code that
“links with” arbitrary Jini code easily
 Write C#, VB, C++, etc. source code that
allows natural use of all native features
 Use all important Jini features
from .NET, including callbacks
 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, “MyICtx” );
InitialContext
Context
ictx = new InitialContext( 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, “MyICtx” );
InitialContext
Context
ictx = new InitialContext( 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
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
 Must accommodate downloaded code
 Must support RMI
 Must support Java and Jini security
 Must support callbacks
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Integrating Jini with .NET ... How?
Architectural Alternatives
 Serialization/Message-based:
CORBA, Webservices, MOM, custom
 Translation-based:
Java to C#, Bytecode to IL
 Proxy-based:
Java Native Interface
managed C++ or PInvoke
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Serialization/Message-Based
 CORBA
 WebServices
 MOM
 Custom
- database
- filesystem
- sockets
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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 and out-of-date
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Proxy-Based Solution
Jini Example
Proxy’
Proxy
Service
Proxy’
Proxy
Service
Runtime
Library
JVM
.NET Client
Client Process
Java Server
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
 Design and Implementation have
huge impact on performance
 0-200% overhead, depending on application
 Negligible overhead in most applications
 up to 25% overhead in most others
 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 MyRunnable();
…
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
 Both platforms have object models
 Slightly incompatible specifications
 Both platforms have class libraries
 Typenames are duplicated
 Different Naming Policies
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Object Model Mapping
Overall a good match
 class to class
 interface to interface
 constructor to constructor
 method to method
 field to property
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Object Model Mapping
Object Lifecycle
 both sides use garbage collection
 JNI references need to be freed
explicitly
 IDisposable together with finalization
offer what we need
 Care required with callbacks!
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Object Model Mapping
Classes
String
Java
.NET
Object
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
Object
Custom Type
Custom Type
Custom Type
String
Exception
System
Exception
Application
Exception
Throwable
Error
Neither Strings nor Throwables
are Java Objects anymore!
Custom
Custom
Exception
Custom
Exception
Exception
Exception
Runtime
Exception
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Object Model Mapping
String Requirements
 .NET string literals must be usable in
places where Java strings are expected
String getProperty( String name )
 .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
 java.lang.String must map to
System.String because of literals
 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
 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)
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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?
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Object Model Mapping
Array Requirements
 Allow subscript operator to access element
arrInt[ 42 ] += 5;
 Allow built-in semantic uses
foreach( int i in arrInt ) sum += i;
 Use native arrays and proxy arrays
interchangably
int[]
arrNative = new int[ 5 ];
result = MyProxyClass.sumItUp( arrNative );
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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
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 ContextImpl :
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->SetIntField(
obj, fid, value );
C
Runtime DLL
JNIHelper.set( obj, value );
.NET
.NET
Generated Code
public int x
{
set { fx.set( value ); }
}
Runtime Assembly
PInvokeHelper.set(
fx, value );
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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.
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Object Model Mapping
Virtual Methods
 All non-static Java methods are
implicitly virtual
 .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
 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.)
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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!
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
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 );
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Using JNI (Example)
jclass cl = env->FindClass( “com.codemesh.MyClass” );
if( cl == 0 ) {
jthrowable t = env->ExceptionOccurred();
env->ExceptionClear();
return;
}
jmethodID mid = env->GetMethodID( cl, “<init>”,
“(Ljava/lang/String;IIZ)V” );
if( mid == 0 ) {
jthrowable t = env->ExceptionOccurred();
env->ExceptionClear();
env->DeleteLocalRef( cl );
return;
}
jstring
arg1 = env->NewStringUTF( “test” );
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Using JNI (Example) cont.
jobject temp = 0, result = 0;
temp = env->NewObject( cl, mid, arg1, 1, 2,
JNI_TRUE );
if( temp == 0 ) {
jthrowable t = env->ExceptionOccurred();
env->ExceptionClear();
env->DeleteLocalRef( cl );
return;
}
result = env->NewGlobalRef( temp );
env->DeleteLocalRef( temp );
Copyright 2004 by Codemesh, Inc.
All Rights Reserved
Demonstration
 A Jini example:
hello in C#
 A look at the demo source code
 A look at particular features of the
enabling source code
 A look at a code generator
 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
[email protected]
Copyright 2004 by Codemesh, Inc.
All Rights Reserved