distributed programming in java - carleton...

11
1 Distributed Programming in Java Networking (5) 2/31 ClassLoaders Responsible for finding and loading class files at run time Every object has an associated class loader One provided with system Applets use them Provide ability to manage class namespace 3/31 ClassLoader Example Web Server Java ClassLoader Web Browser Classes

Upload: others

Post on 20-Apr-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

1

DistributedProgramming in Java

Networking (5)

2/31

ClassLoaders

•Responsible for finding and loading class filesat run time

•Every object has an associated class loader

•One provided with system

•Applets use them

•Provide ability to manage class namespace

3/31

ClassLoader Example

Web Server

Java

ClassLoader

Web Browser

Classes

Page 2: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

2

4/31

Why Custom ClassLoaders?

•Manage the class namespace in special ways•Load edu.carleton.Foo more than once•Deal with “jar hell” -- versioning issues

•Load non-local classes:•Transfer bytecodes across the network

•Dynamically construct classes:•Compile classes on the fly

5/31

ClassLoader Purpose

•Service a request for a class• JVM needs a class, it asks ClassLoader, by

name, for it.•ClassLoader returns a Class object

representing the class

•Custom classloaders created by overridingjava.lang.ClassLoader

6/31

ClassLoader Hierarchy

System classloader

Custom classloader

Custom classloader

edu.carleton.Foo edu.carleton.Foo’

Page 3: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

3

7/31

ClassLoader API

•Class loadClass(String name, boolean resolve)•Name is fully specified; e.g. edu.carleton.Foo•Resolve tells method whether class is to

resolved

•Resolution?•Ensure that class is fully prepared for execution

8/31

ClassLoader API

•defineClass(String name, byte[] b, int o, int l)•Name - class name•B - byte codes for class•O - offset to start of class bytes•L - length of bytes defining class

•Central to class definition•Many complex and implementation

dependent issues (magic!)

9/31

ClassLoader API

•findSystemClass(String name)•Loads files from local filesystem•Returns Class object using name•Often use this after checking remote site

•findLoadedClass(String name)•Serves as a cache for named class•Use this first!

Page 4: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

4

10/31

ClassLoader API

•getParent•Returns the ClassLoader object that

created your custom loader!•Allows delegation to parent for sharing of

classes

11/31

Protocol

The right thing to do is to create adelegating class loader -- defer to system

loader for java.* and other sharedapplication classes

12/31

Example: NetworkClassLoader

Defines classNew instance

Page 5: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

5

13/31

NetworkClassLoader Details

14/31

NetworkClassLoader details

15/31

NetworkClassLoader

Page 6: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

6

16/31

NetworkClassLoader details

17/31

Server Logic (Object-Oriented II) dispatch to registered handlers according to type

of request

Or define readCommand() insubclass

18/31

Protocol for loading “acrossthe wire”

•So, read a serialized object off the wire•Obj o = s.readObject()•De-serialization will fail•Handle exception•Create new custom classloader•Connect to remote site, transfer bytes•Define new class

•Cache classloader for session with client

Page 7: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

7

19/31

Finally …

•Lots of security issues not discussed here

•ClassLoaders are essential for networkcomputing

•URLs•http://www.javaworld.com/javaworld/jw-

10-1996/jw-10-indepth.html•http://72.5.124.55/developer/TechTips/200

0/tt1027.html•http://www.javalobby.org/java/forums/t183

45.html

20/31

Reflection

•Reflection used by programs requiringthe ability to examine or modify theruntime behavior of applications runningin JVM

•Often used in conjunction with classloading

•Advanced feature of language!

21/31

Why?

•Extensibility Features•An application may make use of

external, user-defined classes bycreating instances of extensibilityobjects using their fully-qualifiednames.

Page 8: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

8

22/31

Why?

•Class Browsers and VisualDevelopment Environments•A class browser needs to be able to

enumerate the members of classes.Visual development environmentscan benefit from making use of typeinformation available in reflection toaid the developer in writing correctcode.

23/31

Why?

•Debuggers and Test Tools•Debuggers need to be able to

examine private members onclasses. Test harnesses can makeuse of reflection to systematically calla discoverable set APIs defined on aclass, to insure a high level of codecoverage in a test suite.

24/31

When?

•Reflection is powerful, but should not beused indiscriminately!

• If it is possible to perform an operationwithout using reflection, then it ispreferable to avoid using it

Page 9: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

9

25/31

Drawbacks

• Performance Overhead• Because reflection involves types that are

dynamically resolved, certain Java virtualmachine optimizations can not be performed.

• Security Restrictions• Reflection requires a runtime permission

which may not be present when runningunder a security manager

26/31

Drawbacks

• Exposure of Internals• Since reflection allows code to perform operations

that would be illegal in non-reflective code, such asaccessing private fields and methods, the use ofreflection can result in unexpected side-effects,which may render code dysfunctional and maydestroy portability• Reflective code breaks abstractions andtherefore may change behavior withupgrades of the platform

27/31

Overview

• Use java.lang.reflect, can manipulate:• Class: instantiation, declaration and contents• Member: find fields, members and constructors• Field: static or instance• Method: information about method• Good tutorial at:• http://java.sun.com/docs/books/tutorial/reflect/in

dex.html

Page 10: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

10

28/31

Class reflection API

•Class c = “edu.carleton.Foo”.getClass();•Returns java.lang.String

•Class F = Class.forName(“edu.carleton.Foo”);•Returns class for edu.carleton.Foo•May involve loading of class

•Creating an instance:•Foo foo = F.newInstance();

29/31

Using Class Reflection API

• Services.cfg file:

• Program:• Reads properties file• Looks for specific keys• Finds classes (Class reflection API)• Instantiates instance (Class reflection API)

Register = edu.carleton.RegisterDeregister = edu.carleton.DeregisterMessage.1 = edu.carleton.MessageHeaderMessage.2 = edu.carleton.MessageBody

30/31

Example Services Class

Page 11: Distributed Programming in Java - Carleton Universitypeople.scs.carleton.ca/~arpwhite/courses/4104/... · under a security manager 26/31 Drawbacks • Exposure of Internals • Since

11

31/31

Later …

•Classes now instantiatable•Assignment 2

•Will want to construct messages later•Will revisit reflection API