Dynamic Class Loading

Similar documents
Java Security. Compiler. Compiler. Hardware. Interpreter. The virtual machine principle: Abstract Machine Code. Source Code

Security SYSTEM SOFTWARE 1

Wednesday, June 23, JBoss Users & Developers Conference. Boston:2010

Introflection. Dave Landers BEA Systems, Inc.

The Dark Sides of Integration. Anton Arhipov JRebel,

Understanding ClassLoaders WebSphere 5.1, 6.0 and 6.1

Java Class Loading and Bytecode Verification

Java Code Coverage Mechanics

Java Code Coverage Mechanics Evgeny Mandrikov Marc Hoffmann #JokerConf 2017, Saint-Petersburg

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Java Code Coverage Mechanics. by Evgeny Mandrikov at EclipseCon Europe 2017

JBoss Tattletale 1.1 Developer's Guide

Module Road Map. 7. Version Control with Subversion Introduction Terminology

Do you really get classloaders?

What is Groovy? Almost as cool as me!

Techniques for Building J2EE Applications

CS 11 java track: lecture 1

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Before you start working with Java, you need to set up a Java development

Architecture of so-ware systems

Javac and Eclipse tutorial

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

The Java Technical Details. ICW Lecture 3 Tom Chothia

Analysis Tool Project

HPE Security Fortify Plugins for Eclipse

MAVEN INTERVIEW QUESTIONS

Efficient Java (with Stratosphere) Arvid Heise, Large Scale Duplicate Detection

The Terminator to Android Hardening Services. Yueqian Zhang, Xiapu Luo, Haoyang Yin Department of Computing The Hong Kong Polytechnic University

1Integrate Built-in Function Programmer Guide

Background. Reflection. The Class Class. How Objects Work

USING THE OOSIML/JAVA. With a Terminal Window

Pace University. Fundamental Concepts of CS121 1

How are classes loaded into the Java Virtual Machine (JVM)? from the local file system (CLASSPATH). by an instance of ClassLoader

Database Explorer Quickstart

Reflection (in fact, Java introspection)

Packaging and Deploying Java Based Solutions to WebSphere Message Broker V7

Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

What s NetBeans? Like Eclipse:

JAVA. Reflection API. Java, summer semester

Developing Android applications in Windows

Notes of the course - Advanced Programming. Barbara Russo

Choosing output format. Dynamically choosing format

RMI Example RMI. CmpE 473 Internet Programming RMI

Android Sdk Install Documentation Eclipse. Ubuntu >>>CLICK HERE<<<

Advanced Enterprise Debugging

JDB - QUICK GUIDE JDB - INTRODUCTION

Reverse Engineering of Managed Languages

Zero Turnaround in Java Jevgeni Kabanov

FindBugs review of Glassfish v2 b09

Classloader J2EE rakendusserveris (Bea Weblogic Server, IBM WebSphere)

BEA WebLogic Server R Using FastSwap TM to Minimize Redeployment

Selenium with Java Syllabus

Google App Engine: Java Technology In The Cloud

Agenda. Why OSGi. What is OSGi. How OSGi Works. Apache projects related to OSGi Progress Software Corporation. All rights reserved.

Schema Null Cannot Be Resolved For Table Jpa

15CS45 : OBJECT ORIENTED CONCEPTS

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

jgap freetts gruntspud jedit columba jfreechart

Writing a Client Application for Genesis II Contributors: Chris Sosa Last Modified: 06/05/2007

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

Why Java is practical for modern operating systems. JNode.org

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java

Adding a Module System to Java

CS506 Web Design & Development Final Term Solved MCQs with Reference

[ANALYSIS ASSIGNMENT 10]

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: reflection

This tutorial is designed for all Java enthusiasts who want to learn document type detection and content extraction using Apache Tika.

Java Security HotJava to Netscape and Beyond

Module 3: Installing Eclipse

Core JAVA Training Syllabus FEE: RS. 8000/-

Introspec*on and reflexivity

Jigsaw and OSGi: What the Heck Happens Now?

25. DECUS Symposium THE Application Development Environment for OpenVMS

Objects, Distribution, and the Internet. Update on Java. Introduction, fundamentals and basic concepts. Outline

Brekeke PBX Version 2 ARS Plug-in Developer s Guide Brekeke Software, Inc.

HPE Security Fortify Plugins for Eclipse Software Version: Installation and Usage Guide

Compiling Techniques

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

Reladomo Test Resource

JPA - INSTALLATION. Java version "1.7.0_60" Java TM SE Run Time Environment build b19

Banner Frequently Asked Questions (FAQs)

AutoVue Integration SDK & Sample Integration for Filesys DMS

Scripting Languages in OSGi. Thursday, November 8, 12

Cours / TP. Tutorial to Java Reflection API. Class / ClassLoader / Field-Method Introspection.

Dynamic code downloading using Java TM (Using the java.rmi.server.codebase Property)

I/O and Parsing Tutorial

Before you start with this tutorial, you need to know basic Java programming.

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

FlowTwist: Efficient Context-Sensitive Inside- Out Taint Analysis for Large Codebases

r = obj2.m( 0, 1 ); s = obj2.f; r = obj2.m( ); r = obj2.anothermethod( 0, 1 ); s = obj2.anotherfield;

Equinox Framework: How to get Hooked

Mastering WDK Developer Tips and Tricks. A.J. Whitney

INTROSPECTION. We need to begin with a more basic concept called type introspection

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE

Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat

Nothing to see here...

SELENIUM TRAINING COURSE CONTENT

New Features Overview

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Communication Manager API Release Notes Release 2.1, Build , May

Transcription:

Dynamic Class Loading Philippe Collet Partially based on notes from Michel Buffa Master 1 IFI Interna,onal 2012-2013 h4p://dep,nfo.unice.fr/twiki/bin/view/minfo/soceng1213 P. Collet 1

Agenda Principle of a ClassLoader Writing a custom ClassLoader Architecture to load plugins How to reload plugins P. Collet 2

Definition of a ClassLoader The JVM contains a ClassLoader ClassLoaders allows for loading classes from the filesystem, but also from multiple locations (DB, network, etc.) Role: convert a class name in an array of bytes representing a class Class c = loadclass(string classname, boolean resolveit); P. Collet 3

On ClassLoaders All JVMs have a «system» ClassLoader This default CL implements a method loadclass() that searches in the CLASSPATH for.class,.jar or.zip files One can create new CL by deriving from class ClassLoader and by redefining loadclass() and the methods it uses (findclassfromclass, ResolveClass ) P. Collet 4

Time of class loading It depends! In general when: Foo f = new Foo(); Static references such as: System.out, Foo.class, forname("foo"); P. Collet 5

Writing your own ClassLoader? Interest? Load classes from the web Load classes from a DB Load classes «differently» Don t work with applets! From JDK1.2 on, an URLClassLoader is provided P. Collet 6

Steps for writing a ClassLoader Sub-class ClassLoader and implement the method loadclass 1. Check the class name, determining whether it has already been loaded 2. Check whether this is a system class 3. Attempt to load it 4. Define the class for the VM 5. Resolve it by loading its dependencies 6. Return the class From JDK1.3 on: sub-class SecureClassLoader to respect recommendations relative to the new Java security policy P. Collet 7

Example of custom ClassLoader public synchronized Class loadclass(string classname, boolean resolveit) throws ClassNotFoundException { Class result; Byte []classdata; // 1) search in the cache whether the class has already been loaded result = (Class) classes.get(classname); if(result!= null) return result; // 2) find out whether this is a system class. Very important because one // could change the security manager! try { result = super.findsystemclass(classname); return result; } catch(classnotfoundexception e) { System.out.println("Pas une classe système!"); } // 3) load the class from OUR REPOSITORY (e.g., web ou DB) classdata = getimplfromdatabase(classname); if(classdata == null) { throw new ClassNotFOundException() } P. Collet 8

Example of custom ClassLoader (cont d)... // 4) Define the class (by parsing). Actually the method will check the // validity of Bytecode and make other verifications // The result is stored in a specific data structures within the JVM result = defineclass(classdata, 0, classdata.length); // 5) Resolve class, i.e. apply the same process to superclasses and dependent // classes. if(resolveit) resolveclass(result); // Before returning the final result, put the class into the cachehe classes.put(classname, result); // 6) return result Return result; } P. Collet 9

Example of a code that reads a class... byte [] result; try { FIS fis = new FIS("store\\ " + classname + ".impl"); result = new byte[fis.available()]; fis.read(result); } catch(exception e) { } return null; P. Collet 10

Example of a code that reads a class from the web... URL url = new URL(urlClassName); URLConnection uc = new URLConnection(url); int length = uc.getcontentlength(); IS is = uc.getinputstream(); byte []data = new byte[length]; is.read(data); is.close(); return data;... P. Collet 11

Using a custom ClassLoader Class c = ccl.loadclass("foo"); Object o = c.newinstance(); ((Foo) o).f(); Don t work, as only the new cl knows Foo! System ClassLoader ((Foo)o) ccl Class Foo P. Collet 12

The need for an interface Only the new CL knows Foo. If one wants to cast the object, a common Interface must be created and be known by the default (system) CL. For example, Plugin is known by the default CL (SystemClassLoader). System ClassLoader Interface MyPlugin ((MyPlugin)o) ccl Class loaded Form the classpath Class Foo P. Collet 13

Writing some plugins Simple rule: read plugins from a directory Define a usage interface on plugins Each plugin must implement this interface Each class will be a plugin Then, one simply needs: 1. Read the content of a directory 2. For each class name in the plugins directory Class c = Class.forName(className); Plugin p = (PluginDraw) c.newinstance(); p.draw(); // methode present in Plugin.java P. Collet 14

Plugins again... Difficulties when No default constructor Plugins are in a.jar or in a DB Look for ressources? Images, sounds, etc. Class.forName(className) is the equivalent of loadclass(classname) that was studied for the custom ClassLoader Plugins are everywhere!!! Photoshop, IE, Firefox, Chrome P. Collet 15

Plugins: ideal model Ideal model = A directory for plugins (.class files) and jar files in the directory The application «discovers» files and «absorbs» plugins that are inside Each jar contains A class that implements the interface Plugin.java Other classes it needs (ie depends on) Images, icons, sounds, docs, etc. that the plugin needs as well P. Collet 16

Plugins: ideal model But there is a problem when two many classes are present in the plugin How to test that a class is really a plugin without trying to load it, which is taking time Class.forName() is long Class.newInstance() too Eclipse, for example, contains more than 800 plugins that are loaded at startup Some plugins extend other plugins and thus depend on them The loading order is important for consistency P. Collet 17

How to manage plugins? The solution often uses a descriptor, a «map» of plugins Eclipse follows a specific format to describe plugins, their dependencies (with some XML files) Problem #1 with plugins is the loading time But advantages are numerous P. Collet 18

Developing an application extensible by plugins A SDK to develop plugins must be provided So that developers can compile and test plugins without the code of the main application What must be provided? Necessary classes and interfaces One or more exemple plugins, with a compiling aid (ant file for example) Documentation, tutorials, etc. P. Collet 19

Changing plugins without restarting the main application/server Principle of Servlet/Jsp servers: Classes and jars are added The server is not restarted Example Java application servers (large majority of front-end/ business tier on current linux based servers) 24/7 exploitation (never stopped nor rebooted) Plugins are dropped down and discovered on the fly P. Collet 20

Hot (Re-)Loading Principle : change the classloader for each plugin loading Each classloader manages its own «cache» of classes If the classloader is reinstantiated, one can load/reload plugins at runtime An URLClassLoader will be used for this implementation One only needs to re-scan every X (5 for example) seconds the plugin directory re-scan on-demand (a menu for example) P. Collet 21

P. Collet 22