Скачать презентацию Class Loading Survival Guide Peter Keavney Andrea O Скачать презентацию Class Loading Survival Guide Peter Keavney Andrea O

001ac59c23cef879002b846af2151cba.ppt

  • Количество слайдов: 59

Class. Loading Survival Guide Peter Keavney Andrea O. K. Wright Chariot Solutions www. chariotsolutions. Class. Loading Survival Guide Peter Keavney Andrea O. K. Wright Chariot Solutions www. chariotsolutions. com 1 | 2004 Java. One. SM Conference | BOF-2519 java. sun. com/javaone/sf

Class. Loading Survival Guide Understanding Java Class. Loading With an understanding of how Class. Class. Loading Survival Guide Understanding Java Class. Loading With an understanding of how Class. Loading works, you will be able to debug and avoid common pitfalls. We will not only look at ways to prevent Class. Loading from breaking an application, we will also talk about how applications may be enhanced with custom Class. Loaders. 2 | 2004 Java. One. SM Conference | BOF-2509

Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and 1. 4 Specs App Server Class. Loading Policies Diagnosing Common Class. Loading Problems Enhancing Applications with Techniques Involving Class. Loaders 3 | 2004 Java. One. SM Conference | BOF-2509

Java Class. Loading Basics • What is a class loader? – mechanism for loading Java Class. Loading Basics • What is a class loader? – mechanism for loading classes into the JVM – subclass of java. lang. Class. Loader • Hierarchical structure – Parent-child relationships • Uses delegation – Load requests delegated to parent 4 | 2004 Java. One. SM Conference | BOF-2509 Class. Loader-A Class-X Class. Loader-B Class-Y Class-Z Class. Loader-C Class-Z

Java Class. Loading Basics • Unidirectional visibility – Parent-loaded classes cannot see child-loaded classes Java Class. Loading Basics • Unidirectional visibility – Parent-loaded classes cannot see child-loaded classes – Thread. get. Context. Classloader() Class. Loader-A Class-X • Unique Namespaces – Classes referenced as classloader. classname Class. Loader-B Class-Y Class-Z Class. Loader-C Class-R Class-D • Variety of class file locations – File system – Databases – Remote server locations File system Database Remote Server 5 | 2004 Java. One. SM Conference | BOF-2509

Java Class. Loading Basics • Bootstrap Class. Loader ─ aka Primordial Class. Loader ─ Java Class. Loading Basics • Bootstrap Class. Loader ─ aka Primordial Class. Loader ─ Created by the JVM ─ Loads the JDK classes included in the JVM (including java. lang. Class. Loader) Bootstrap Class. Loader • Extensions Class. Loader ─ Loads JRE classes • System Class. Loader ─ loads classes specified in the classpath ─ Loads applications, shared libraries, jars, etc. 6 | 2004 Java. One. SM Conference | BOF-2509 Extensions Class. Loader System Class. Loader Application JAR Shared Lib

Java Class. Loading Basics Typical Loading Scenario Determine Initial Class. Loader Delegate to Parent Java Class. Loading Basics Typical Loading Scenario Determine Initial Class. Loader Delegate to Parent Class. Loader Bootstrap Class. Loader Extension Class. Loader System Class. Loader Class Loaded? No – Delegate to Parent Lastly – If class is not loaded in parent hierarchy then the Initial loader will attempt to load the class itself. 7 | 2004 Java. One. SM Conference | BOF-2509 Class Loader A Class Loader B Class Loader C My. Class

Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and 1. 4 Specs App Server Class. Loading Policies Diagnosing Common Class. Loading Problems Enhancing Applications with Techniques Involving Class. Loaders 8 | 2004 Java. One. SM Conference | BOF-2509

J 2 EE Specification Requirements • J 2 EE v 1. 3 – section J 2 EE Specification Requirements • J 2 EE v 1. 3 – section 8. 1. 1. 2 mandates application servers must load dependent JAR files listed in the Manifest Class-Path entry of a primary JAR file (typically an EJB JAR). This requirement is not mandated for EAR or WAR files. • J 2 EE v 1. 4 – section 8. 3. 1 states the following: – Classes loaded by lower-level class loaders must be able to discover the top-level application class loader used to dynamically load application classes. – Applications, deployed as archives, must ensure that the Java classes in the application are in a separate namespace from classes in other Java applications. • J 2 EE containers must provide a context class loader per thread that can be used to load top-level application classes. 9 | 2004 Java. One. SM Conference | BOF-2509

Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and 1. 4 Specs App Server Class. Loading Policies Diagnosing Common Class. Loading Problems Enhancing Applications with Techniques Involving Class. Loaders 10 | 2004 Java. One. SM Conference | BOF-2509

Web. Logic • System Class. Loader – Loads Web. Logic Server classes – Loads Web. Logic • System Class. Loader – Loads Web. Logic Server classes – Loads deployed applications – Loads resources shared across applications Bootstrap Class. Loader Extension Class. Loader System Class. Loader • Individual Applications – Load EJB JARs – Load resources shared within application – Load Web Applications • Web Applications – Load servlets, JSPs, etc 11 | 2004 Java. One. SM Conference | BOF-2509 EAR JAR RAR EJB JAR WAR

Web. Sphere • Web. Sphere Class. Loader – Loads Web. Sphere run-time – Loads Web. Sphere • Web. Sphere Class. Loader – Loads Web. Sphere run-time – Loads required J 2 EE classes Bootstrap Class. Loader Extension Class. Loader • Application Extensions Class. Loader – Loads application(s) into a single namespace when Policy is set to SINGLE • Module Class. Loader – Loads Web Applications when Isolation Policy set to Module 12 | 2004 Java. One. SM Conference | BOF-2509 System Class. Loader Web. Sphere Class. Loader Application Ext Class. Loader EAR Module Class. Loader WAR

Oracle i. AS • System Class. Loader – Loads OC 4 J System Classes Oracle i. AS • System Class. Loader – Loads OC 4 J System Classes • Global Connectors Class. Loader – Loads connectors resources shared across applications Bootstrap Class. Loader Extension Class. Loader System Class. Loader Global Connectors Class. Loader Global Applications Class. Loader • Global Applications Class. Loader EAR – Loads applications EAR 13 | 2004 Java. One. SM Conference | BOF-2509 EAR

JBoss • Unified. Class. Loader 3 – Extends java. net. URLClass. Loader – Loads JBoss • Unified. Class. Loader 3 – Extends java. net. URLClass. Loader – Loads classes and resources into Unified. Loader. Repository 3 overriding the standard parent delegation model. • Unified. Loader. Repository 3 – Registers Multiple Unified. Class. Loaders Bootstrap Class. Loader Extensions Class. Loader System Class. Loader Unified. Class. Loader 3 Unified Loader Repository 3 EAR 14 | 2004 Java. One. SM Conference | BOF-2509 EJB EAR WAR JAR

Web. Logic Oracle i. AS System Class. Loader EAR JAR System Class. Loader Global Web. Logic Oracle i. AS System Class. Loader EAR JAR System Class. Loader Global Connectors Class. Loader WAR Global Applications Class. Loader RAR EAR EJB JAR EAR WAR EAR System Class. Loader Web. Sphere Class. Loader Unified. Class. Loader 3 Application Ext Class. Loader Module Class. Loader EAR Web. Sphere 15 | 2004 Java. One. SM Conference | BOF-2509 JBoss Unified Loader Repository

Server Configuration Application Isolation Control Web. Logic Delegation Control Placing modules in the App-Inflib Server Configuration Application Isolation Control Web. Logic Delegation Control Placing modules in the App-Inflib directory enables sharing between them. Setting Prefer. Web. Inf to “true” subverts normal hierarchy delegation Web. Sphere Setting Class. Loader Policy to SINGLE will load all applications from a single Class. Loader. Setting Policy to MULTIPLE gives each app it’s own Class. Loader. Setting Application Mode to PARENT_FIRST is the default behavior Setting mode to PARENT_LAST subverts normal delegation. Oracle i. AS Specifying allows cross application sharing of “X”s children. subverts normal delegation for Web Applications. JBoss dot. com: loader=archive dot. com: loader=-archive Java 2 Parent. Delegaton=false 16 | 2004 Java. One. SM Conference | BOF-2509

Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and 1. 4 Specs App Server Class. Loading Policies Diagnosing Common Class. Loading Problems Enhancing Applications with Techniques Involving Class. Loaders 17 | 2004 Java. One. SM Conference | BOF-2509

The Class Could Not Be Found EJB. JAR STRUTS. JAR WAR A App. AForm The Class Could Not Be Found EJB. JAR STRUTS. JAR WAR A App. AForm 18 | 2004 Java. One. SM Conference | BOF-2509 WAR B App. BForm

The Class Could Not Be Found • Class. Not. Found. Exception ─ Used String The Class Could Not Be Found • Class. Not. Found. Exception ─ Used String representation (e. g. Class. Loader. Load. Class(class. Name)) ─ Typically, dependent Class is only visible to children • No. Class. Def. Found. Error ─ Class existed at compile time ─ Typically, dependent Class is only visible to children • No. Class. Def. Found. Exception ─ An interesting phenomenon: this Exception is not in the JDK, but it appears in dozens of topics in developer forums 19 | 2004 Java. One. SM Conference | BOF-2509

The Same Class Was Loaded By Multiple Class. Loaders EJB JAR public void utility. The Same Class Was Loaded By Multiple Class. Loaders EJB JAR public void utility. Method (Object o) { ((Apple)o). do. Something(); } CLASSCASTEXCEPTION Apple WAR A Apple 20 | 2004 Java. One. SM Conference | BOF-2509 WAR B Apple

The Same Class Was Loaded By Multiple Class. Loaders • Class. Cast. Exception ─ The Same Class Was Loaded By Multiple Class. Loaders • Class. Cast. Exception ─ This is usually a bug ─ But sometimes… same Class was loaded by two different Class. Loaders • Class. Circularity. Error ─ A Class and one of its dependents are both dependent on a third Class; different Class. Loaders are used to load that third Class 21 | 2004 Java. One. SM Conference | BOF-2509

Diagnostic Statements • • Class. get. Class. Loader() System. get. System. Class. Loader() Thread. Diagnostic Statements • • Class. get. Class. Loader() System. get. System. Class. Loader() Thread. current. Thread. get. Context. Class. Loader() Class. Loader. get. Parent() • Class. get. Protection. Domain(). get. Code. Source(). get. Location() • URLClass. Loader. get. URLs() • Printing out the contents of a JAR: Jar. Entry jar. Entry; Jar Input. Stream jar. Input. Stream = new Jar. Input. Stream(urls[i]. open. Connection(). get. Input. Stream()); while((jar. Entry=jar. Input. Stream. get. Next. Jar. Entry())!=null) { System. out. println(jar. Entry. get. Name()); } 22 | 2004 Java. One. SM Conference | BOF-2509

Getting Diagnostic Information • Use the -verbose option • Place diagnostic statements in the Getting Diagnostic Information • Use the -verbose option • Place diagnostic statements in the application code • Place diagnostic statements in a custom Class. Loader • Place diagnostic statements in the core Java Class. Loaders. The -Xbootclasspath option makes to possible to supersede core Classes in a development environment. C: >java -Xbootclasspath: C: debug; j 2 sdk 1. 4. 2_03jrelibrt. jar …[all other boot. class. path jars] app. java 23 | 2004 Java. One. SM Conference | BOF-2509

Sample App for Debugging Top. Hat. java import import java. io. File; java. net. Sample App for Debugging Top. Hat. java import import java. io. File; java. net. Malformed. URLException; java. net. URLClass. Loader; java. net. URL; demo. Rabbit; /** * Instantiates and displays a Rabbit. */ public class Top. Hat { public static void main (String args[]) { try { // Create a Class. Loader that knows where to find demo. Rabbit URL rabbit. URL = new File("rabbit. jar"). to. URL(); URL[] urls = new URL[]{rabbit. URL}; URLClass. Loader rabbit. Class. Loader = new URLClass. Loader(urls, Thread. current. Thread(). get. Context. Class. Loader()); // Set the Context. Class. Loader for the current Thread so that it can find Rabbit. class Thread. current. Thread(). set. Context. Class. Loader(rabbit. Class. Loader); // Make a Rabbit appear. System. out. println(new Rabbit()); } catch (Malformed. URLException malformed. URLException) { malformed. URLException. print. Stack. Trace(); } } } 24 | 2004 Java. One. SM Conference | BOF-2509

Compile Time and Runtime Environments Rabbit. java package demo; public class Rabbit { } Compile Time and Runtime Environments Rabbit. java package demo; public class Rabbit { } rabbit. jar C: > jar tf rabbit. jar demo/ demo. Rabbit. class Compilation C: > javac –classpath rabbit. jar Top. Hat. java Command Line C: > java Top. Hat 25 | 2004 Java. One. SM Conference | BOF-2509

Desired Behavior vs. Actual Behavior Desired Behavior Tophat should instantiate and display a String Desired Behavior vs. Actual Behavior Desired Behavior Tophat should instantiate and display a String representation of a Rabbit, even though rabbit. jar is not referenced on the application classpath. Actual Behavior C: >java Top. Hat Exception in thread "main" java. lang. No. Class. Def. Found. Error: demo/Rabbit at Top. Hat. main(Top. Hat. java: 23) 26 | 2004 Java. One. SM Conference | BOF-2509

Top. Hat. java with Diagnostic Statements // Create a Class. Loader that knows where Top. Hat. java with Diagnostic Statements // Create a Class. Loader that knows where to find demo. Rabbit URL rabbit. URL = new File("rabbit. jar"). to. URL(); URL[] urls = new URL[]{rabbit. URL}; URLClass. Loader rabbit. Class. Loader = new URLClass. Loader(urls, Thread. current. Thread(). get. Context. Class. Loader()); System. out. println("---> Class. Loader for Top. Hat: " + Top. Hat. class. get. Class. Loader()); System. out. println("---> Before setting a custom Context. Class. Loader, Context. Class. Loader is: “); System. out. println( Thread. current. Thread(). get. Context. Class. Loader()); // Set the Context. Class. Loader for the current Thread so that it can find // Rabbit Thread. current. Thread(). set. Context. Class. Loader(rabbit. Class. Loader); System. out. println("---> After setting a custom Context. Class. Loader is: "); System. out. println( Thread. current. Thread(). get. Context. Class. Loader()); // Make a Rabbit appear. System. out. println(new Rabbit()); 27 | 2004 Java. One. SM Conference | BOF-2509

Output From Diagnostics in the Application C: >java Top. Hat ---> Class. Loader for Output From Diagnostics in the Application C: >java Top. Hat ---> Class. Loader for Top. Hat: sun. misc. Launcher$App. Class. Loader@e 80 a 59 ---> Before setting a custom Context. Class. Loader, Context. Class. Loader is: sun. misc. Launcher$App. Class. Loader@e 80 a 59 ---> After setting a custom Context. Class. Loader, Context. Class. Loader is: java. net. URLClass. Loader@eee 36 c Exception in thread "main" java. lang. No. Class. Def. Found. Error: demo/Rabbit at Top. Hat. main(Top. Hat. java: 28) 28 | 2004 Java. One. SM Conference | BOF-2509

Diagnostics in java. net. URLClass. Loader protected Class find. Class(final String name) throws Class. Diagnostics in java. net. URLClass. Loader protected Class find. Class(final String name) throws Class. Not. Found. Exception { try { return (Class) Access. Controller. do. Privileged(new Privileged. Exception. Action () { public Object run() throws Class. Not. Found. Exception { String path = name. replace('. ', '/'). concat(". class"); Resource res = ucp. get. Resource(path, false); if (res != null) { try { return define. Class(name, res); } catch ( IOException e) { throw new Class. Not. Found. Exception(name, e); } } else { System. out. println(“-- Looked in: ”); URL[] urls = get. URLs(); for (int i = 0; i

Diagnostics in java. lang. Class. Loader protected synchronized Class load. Class(String name, boolean resolve) Diagnostics in java. lang. Class. Loader protected synchronized Class load. Class(String name, boolean resolve) throws Class. Not. Found. Exception { Class c = find. Loaded. Class(name); if (c == null) { try { if (name. equals("demo. Rabbit")) System. out. println("---> " + this + "is asking parent (" + parent + ") to try to load " + name + ". "); if (parent != null) { c = parent. load. Class(name, false); } else { c = find. Bootstrap. Class 0(name); } } catch (Class. Not. Found. Exception e) { // If still not found, then call find. Class in order // to find the class. if (name. equals("demo. Rabbit")) System. out. println("---> " + name + " was not found by parent (" + parent + "), so " + this + " will try. "); c = find. Class(name); } } if (resolve) { resolve. Class(c); } return c; } void add. Class(Class c) { if (c. to. String(). equals("class demo. Rabbit")) System. out. println("---> " + this + "loaded " + c); classes. add. Element(c); } 30 | 2004 Java. One. SM Conference | BOF-2509 private synchronized Class load. Class. Internal(String name)throws Class. Not. Found. Exception { if (name. equals("demo. Rabbit")) { System. out. println("JVM is requesting that " + this +" load "+ name); } return load. Class(name); }

Output from Diagnostics in the Core Java Classes ---> JVM is requesting that sun. Output from Diagnostics in the Core Java Classes ---> JVM is requesting that sun. misc. Launcher$ App. Class. Loader@e 80 a 59 load demo. Rabbit ---> sun. misc. Launcher$App. Class. Loader@e 80 a 59 is asking parent (sun. misc. Launcher$ Ext. Class. Loader@1 ff 5 ea 7) to try to load demo. Rabbit. ---> sun. misc. Launcher$Ext. Class. Loader@1 ff 5 ea 7 is asking parent (null) to try to load demo. Rabbit. ---> demo. Rabbit was not found by parent (null), so sun. misc. Launcher$Ext. Class. Loader@1 ff 5 ea 7 will try. --> Looked in: file: /C: /j 2 sdk 1. 4. 2_03/jre/lib/ext/dnsns. jar file: /C: /j 2 sdk 1. 4. 2_03/jre/lib/ext/ldapsec. jar file: /C: /j 2 sdk 1. 4. 2_03/jre/lib/ext/localedata. jar file: /C: /j 2 sdk 1. 4. 2_03/jre/lib/ext/sunjce_provider. jar ---> demo. Rabbit was not found by parent (sun. misc. Launcher$ Ext. Class. Loader@1 ff 5 ea 7), so sun. misc. Launcher$App. Class. Loader@e 80 a 59 will try. --> Looked in: file: /C: / Exception in thread "main" java. lang. No. Class. Def. Found. Error: demo/Rabbit at Top. Hat. main(Top. Hat. java: 28) 31 | 2004 Java. One. SM Conference | BOF-2509

Analysis • Regardless of how the Context Class. Loader is set, the classloader that Analysis • Regardless of how the Context Class. Loader is set, the classloader that loaded the calling Class will always be used to load a requested Class when no Class. Loader is specified – new Class. Name() – Class. for. Name(“package. Class. Name”) • Context Class. Loaders are used by – JNDI – JAXP 32 | 2004 Java. One. SM Conference | BOF-2509

Possible Solutions Class. Loader loader = Thread. current. Thread(). get. Context. Class. Loader(); loader. Possible Solutions Class. Loader loader = Thread. current. Thread(). get. Context. Class. Loader(); loader. load. Class(“demo. Rabbit”). new. Instance(); – or – Class. Loader loader = Thread. current. Thread(). get. Context. Class. Loader(); Class. for. Name(“demo. Rabbit”, loader). new. Instance(); 33 | 2004 Java. One. SM Conference | BOF-2509

Rabbit Appears ---> java. net. URLClass. Loader@defa 1 a is asking parent (sun. misc. Rabbit Appears ---> java. net. URLClass. Loader@defa 1 a is asking parent (sun. misc. Launcher$App. Class. L oader@e 80 a 59) to try to load demo. Rabbit. ---> sun. misc. Launcher$App. Class. Loader@e 80 a 59 is asking parent (sun. misc. Launcher$ Ext. Class. Loader@1 ff 5 ea 7) to try to load demo. Rabbit. ---> sun. misc. Launcher$Ext. Class. Loader@1 ff 5 ea 7 is asking parent (null) to try to load demo. Rabbit. ---> demo. Rabbit was not found by parent (null), so sun. misc. Launcher$ Ext. Class. Lo ader@1 ff 5 ea 7 will try. --> Looked in: file: /C: /j 2 sdk 1. 4. 2_03/jre/lib/ext/dnsns. jar file: /C: /j 2 sdk 1. 4. 2_03/jre/lib/ext/ldapsec. jar file: /C: /j 2 sdk 1. 4. 2_03/jre/lib/ext/localedata. jar file: /C: /j 2 sdk 1. 4. 2_03/jre/lib/ext/sunjce_provider. jar ---> demo. Rabbit was not found by parent (sun. misc. Launcher$ Ext. Class. Loader@1 ff 5 e a 7), so sun. misc. Launcher$App. Class. Loader@e 80 a 59 will try. --> Looked in: file: /C: / ---> demo. Rabbit was not found by parent (sun. misc. Launcher$ App. Class. Loader@e 80 a 5 9), so java. net. URLClass. Loader@defa 1 a will try. ---> java. net. URLClass. Loader@defa 1 a loaded class demo. Rabbit@a 18 aa 2 34 | 2004 Java. One. SM Conference | BOF-2509

Sample Web App for Debugging Rabbit. java package webdemo; public class Rabbit { public Sample Web App for Debugging Rabbit. java package webdemo; public class Rabbit { public static boolean visible = true; } manifest. mf Manifest-Version: 1. 0 Class-Path: rabbit. jar C: >jar tf thinair. war META-INF/MANIFEST. MF thinair. jsp C: slides 5>jar tf magic. ear META-INF/application. xml rabbit. jar thinair. war tophat. war 35 | 2004 Java. One. SM Conference | BOF-2509 C: >jar tf tophat. war META-INF/MANIFEST. MF tophat. jsp

<% if (Rabbit. visible) %>" src="https://present5.com/presentation/001ac59c23cef879002b846af2151cba/image-36.jpg" alt="thinair. jsp <%@ page import=“webdemo. Rabbit" %> <% if (Rabbit. visible) %>" /> thinair. jsp <%@ page import=“webdemo. Rabbit" %> <% if (Rabbit. visible) %>

 ^ ^ / | / ^|  ^/ / -- / /  | 6 6 |  =@= /  o / /  
<% Rabbit. visible = !Rabbit. visible; %> 36 | 2004 Java. One. SM Conference | BOF-2509 tophat. jsp <%@ page import=“webdemo. Rabbit" %> <% if (Rabbit. visible) %>
 ^ ^ / | / ^|  ^/ / -- / /  | 6 6 |  =@= /  o / /  
<% else for (int i = 0; i < 9; i++) { %>
<% } Rabbit. visible = !Rabbit. visible; %>
 ========== |MWMWMWMWMW| |MWMWMW| |MWMWMM| 

37 | 2004 Java. One. SM Conference | BOF-2509 37 | 2004 Java. One. SM Conference | BOF-2509

38 | 2004 Java. One. SM Conference | BOF-2509 38 | 2004 Java. One. SM Conference | BOF-2509

39 | 2004 Java. One. SM Conference | BOF-2509 39 | 2004 Java. One. SM Conference | BOF-2509

40 | 2004 Java. One. SM Conference | BOF-2509 40 | 2004 Java. One. SM Conference | BOF-2509

41 | 2004 Java. One. SM Conference | BOF-2509 41 | 2004 Java. One. SM Conference | BOF-2509

42 | 2004 Java. One. SM Conference | BOF-2509 42 | 2004 Java. One. SM Conference | BOF-2509

The –verbose Option [Loaded [Loaded [Loaded [Loaded [Loaded [Loaded [Loaded 43 jsp_servlet. __thinair] weblogic. The –verbose Option [Loaded [Loaded [Loaded [Loaded [Loaded [Loaded [Loaded 43 jsp_servlet. __thinair] weblogic. utils. http. Query. Params] javax. servlet. jsp. Jsp. Writer] javax. servlet. jsp. tagext. Body. Content] weblogic. servlet. jsp. Byte. Writer] weblogic. servlet. jsp. Body. Content. Impl] weblogic. servlet. jsp. Jsp. Writer. Impl] weblogic. servlet. internal. Delegate. Chunk. Writer] weblogic. utils. Unsync. Hashtable] weblogic. utils. Unsync. HTEntry] javax. ejb. Handle] weblogic. servlet. internal. session. File. Session. Data] javax. servlet. http. Cookie] weblogic. servlet. internal. Chunk. Utils] demo. Rabbit] weblogic. servlet. internal. Cookie. Parser] weblogic. servlet. logging. Format. String. Buffer] weblogic. servlet. internal. Inet. Address. Cache. Record] weblogic. utils. concurrent. Lock] weblogic. utils. concurrent. Mutex] jsp_servlet. __tophat] demo. Rabbit] weblogic. drs. internal. Heartbeat. Message] | 2004 Java. One. SM Conference | BOF-2509

Diagnostics in Core Class. Loaders ---> demo. Rabbit was not found by parent (sun. Diagnostics in Core Class. Loaders ---> demo. Rabbit was not found by parent (sun. misc. Launcher$App. Class. Loader@f 38798), so weblogic. utils. classloaders. Generic. Class. Loader@6 b 51 d 8 finder: weblogic. utils. classloaders. Multi. Class. Finder@a 2 c 691 annotation: will try. ---> demo. Rabbit was not found by parent (weblogic. utils. classloaders. Generic. Class. Loader@6 b 51 d 8 finder: weblogic. utils. classloaders. Multi. Class. Finder@a 2 c 691 annotation: ), so weblogic. utils. classloaders. Generic. Class. Loader@6 d 4 f 30 finder: weblogic. utils. classloaders. Multi. Class. Finder@bf 9839 annotation: Application. Class. Loader@ will try. ---> demo. Rabbit was not found by parent (weblogic. utils. classloaders. Generic. Class. Loader@6 d 4 f 30 finder: weblogic. utils. classloaders. Multi. Class. Finder@bf 9839 annotation: Application. Class. Loader@), so weblogic. utils. classloaders. Generic. Class. Loader@7227 a 8 finder: weblogic. utils. classloaders. Multi. Class. Finder@98 c 92 c annotation: magic@ will try. ---> demo. Rabbit was not found by parent (weblogic. utils. classloaders. Generic. Class. Loader@7227 a 8 finder: weblogic. utils. classloaders. Multi. Class. Finder@98 c 92 c annotation: magic@), so weblogic. utils. classloaders. Change. Aware. Class. Loader@899 e 6 a finder: weblogic. utils. classloaders. Multi. Class. Finder@1444 b 03 annotation: magic@thinair will try. ---> weblogic. utils. classloaders. Change. Aware. Class. Loader@899 e 6 a finder: weblogic. utils. classloaders. Multi. Class. Finder@1444 b 03 annotation: magic@thinair loaded class demo. Rabbit ---> demo. Rabbit was not found by parent (sun. misc. Launcher$App. Class. Loader@f 38798), so weblogic. utils. classloaders. Generic. Class. Loader@6 b 51 d 8 finder: weblogic. utils. classloaders. Multi. Class. Finder@a 2 c 691 annotation: will try. ---> demo. Rabbit was not found by parent (weblogic. utils. classloaders. Generic. Class. Loader@6 b 51 d 8 finder: weblogic. utils. classloaders. Multi. Class. Finder@a 2 c 691 annotation: ), so weblogic. utils. classloaders. Generic. Class. Loader@6 d 4 f 30 finder: weblogic. utils. classloaders. Multi. Class. Finder@bf 9839 annotation: Application. Class. Loader@ will try. ---> demo. Rabbit was not found by parent (weblogic. utils. classloaders. Generic. Class. Loader@6 d 4 f 30 finder: weblogic. utils. classloaders. Multi. Class. Finder@bf 9839 annotation: Application. Class. Loader@), so weblogic. utils. classloaders. Generic. Class. Loader@7227 a 8 finder: weblogic. utils. classloaders. Multi. Class. Finder@98 c 92 c annotation: magic@ will try. ---> demo. Rabbit was not found by parent (weblogic. utils. classloaders. Generic. Class. Loader@7227 a 8 finder: weblogic. utils. classloaders. Multi. Class. Finder@98 c 92 c annotation: magic@), so weblogic. utils. classloaders. Change. Aware. Class. Loader@cdf 872 finder: weblogic. utils. classloaders. Multi. Class. Finder@7 d 0 e 5 e annotation: magic@tophat will try. ---> weblogic. utils. classloaders. Change. Aware. Class. Loader@cdf 872 finder: weblogic. utils. classloaders. Multi. Class. Finder@7 d 0 e 5 e annotation: magic@tophat loaded classdemo. Rabbit 44 | 2004 Java. One. SM Conference | BOF-2509

Possible Solutions • J 2 EE spec does not provide Class. Loading guidelines for Possible Solutions • J 2 EE spec does not provide Class. Loading guidelines for Classes specified using the class-path tag in a web app’s manifest ─ Web. Logic uses the web app’s Class. Loader • Server classpath ─ Downside: Server needs to be restarted whenever a library class changes • Use vendor-specific means of specifying a Class. Loader hierarchy. ─ Downside: Complicates porting to another server 45 | 2004 Java. One. SM Conference | BOF-2509

Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and Agenda Class. Loading Basics Class. Loading in the J 2 EE 1. 3 and 1. 4 Specs App Server Class. Loading Policies Diagnosing Common Class. Loading Problems Enhancing Applications with Techniques Involving Class. Loaders 46 | 2004 Java. One. SM Conference | BOF-2509

Enhancing Applications with Techniques Involving Class. Loaders • Customizing Security • Improving Performance • Enhancing Applications with Techniques Involving Class. Loaders • Customizing Security • Improving Performance • Providing Flexibility 47 | 2004 Java. One. SM Conference | BOF-2509

Modifying the Default Security Policy Using a Custom Class. Loader Default Permissions Checking: • Modifying the Default Security Policy Using a Custom Class. Loader Default Permissions Checking: • When a permissions check is requested, policy configuration files are consulted. • Permissions may be granted based on user and/or codebase • Sample policy file entries: grant principal "Max" { permission demo. Print. Back. Stage. Passes. Permission; }; grant code. Base “http: //honorable. com" { permission java. io. File. Permission "C: \secrets*", "read" }; 48 | 2004 Java. One. SM Conference | BOF-2509

Augmenting Permissions Granted By the Default Mechanism • Extend URLClass. Loader to override get. Augmenting Permissions Granted By the Default Mechanism • Extend URLClass. Loader to override get. Permissions(Code. Source codesource) • Call super. get. Permissions(codesource) ─ Adds File. Permissions or Connection. Permissions based on the location of the Class and the URL protocol • Authorization information may be obtained from the database • Permissions may be role-based protected Permission. Collection get. Permissions(Code. Source cs){ Permissions. Collection pc = super. get. Permissions(cs); if (check. Roles(username, "Magician's Assistant")) { pc. add(new demo. Print. Back. Stage. Passes. Permission()); } return pc; } 49 | 2004 Java. One. SM Conference | BOF-2509

A Custom Class. Loader that Helps Protect Against Decompilation • Encrypt Classes before deployment A Custom Class. Loader that Helps Protect Against Decompilation • Encrypt Classes before deployment • Extend URLClass. Loader, and override find. Class to decrypt the class before calling define. Class • If the encrypted Classes are delegated to the System Class. Loader, it will throw an Exception, so either… ─ ensure that the encrypted Classes are not on the classpath ─ or override load. Class so that only core Classes are delegated to the System loader • Caveat ─ Class data must eventually pass through the final java. lang. Class. Loader define. Class() ─ Using the -Xbootclasspath option we looked at earlier, a modified java. lang. Class. Loader could be installed. It's definition of define. Class could be modified to send the decrypted data to a file! 50 | 2004 Java. One. SM Conference | BOF-2509

Enhancing Applications with Techniques Involving Class. Loaders • Customizing Security • Improving Performance • Enhancing Applications with Techniques Involving Class. Loaders • Customizing Security • Improving Performance • Providing Flexibility 51 | 2004 Java. One. SM Conference | BOF-2509

Ensure that initialization-intensive Classes are not loaded until needed • All Classes will be Ensure that initialization-intensive Classes are not loaded until needed • All Classes will be loaded when the enclosing Class is loaded: Screen current. Screen; String command = action. Event. get. Command(); if (command. equals(PULL_RABBIT_OUT_OF_HAT)) { current. Screen = new Rabbit. Out. Of. Hat. Screen(); } else if (command. equals(ESCAPE_FROM_LOCKED_TRUNK)) { current. Screen = new Escape. From. Locked. Trunk. Screen(); } else if (command. equals(DISAPPEAR_IN_THIN_AIR)) current. Screen = new Disappear. In. Thin. Air. Screen(); } else … • Classes will be loaded on an as-needed basis: String command = action. Event. get. Command(); Class. Loader loader = Thread. current. Thread(). get. Context. Class. Loader(); String class. Name = “demo. ” + command + “Screen”; current. Screen = (Screen) loader. load. Class(class. Name). new. Instance(); 52 | 2004 Java. One. SM Conference | BOF-2509

Controlling Applet Class. Loading • If all the Applet classes are in a single Controlling Applet Class. Loading • If all the Applet classes are in a single JAR, startup may be unacceptably slow • Create a URL Class. Loader to load additional classes on an as-needed basis ─ Warning: Using new to invoke the URLClass. Loader will yield a security Exception ─ Even an unsigned applet can use URLClass. Loader. new. Instance(URL[] urls, Class. Loader parent) to set the URLs for a URLClass. Loader 53 | 2004 Java. One. SM Conference | BOF-2509

Controlling Applet Class. Loading, Cnt’d • The Screens classes will be loaded on an Controlling Applet Class. Loading, Cnt’d • The Screens classes will be loaded on an as-needed basis. Screen current. Screen; String command = action. Event. get. Command(); URLClass. Loader loader = URLClass. Loader. new. Instance(new URL[]{new URL("file: ///C: /screens")}, get. Class(). get. Class. Loader()); String class. Name = command + “Screen”; current. Screen = (Screen) loader. load. Class(class. Name). new. Instance(); • The Screens are not in magicshow. jar; they are not referenced the applet element. Magic Show Applet 54 | 2004 Java. One. SM Conference | BOF-2509

Enhancing Applications with Techniques Involving Class. Loaders • Customizing Security • Improving Performance • Enhancing Applications with Techniques Involving Class. Loaders • Customizing Security • Improving Performance • Providing Flexibility 55 | 2004 Java. One. SM Conference | BOF-2509

Pluggable, Hot-deployable Modules • Once a Class is Loaded by a Class. Loader, it Pluggable, Hot-deployable Modules • Once a Class is Loaded by a Class. Loader, it cannot be reloaded by that Class. Loader, so a new Class. Loader must be instantiated to load a new version of a Class. * • Classes cannot be hot deployed by the System Class. Loader – Ensure that hot-deployable Classes are not on the classpath A Pluggable “Volunteer from the Audience” URLClass. Loader loader; protected Volunteer get. Volunteer(String volunteer. Name) { //When a class loader is axed, all the classes it loaded are discarded URL[] plugin. Paths = new URL[]{new URL(“Any. Directory. Not. On. Classpath”)}; loader = new URLClass. Loader(plugin. Paths, get. Class. Loader()); return (Volunteer)loader. load. Class(volunteer. Name, true); } 56 | 2004 Java. One. SM Conference | BOF-2509

Other Cool Uses for Custom Class. Loaders • A Class. Loader that accepts a Other Cool Uses for Custom Class. Loaders • A Class. Loader that accepts a version number, checks out the code from CVS, and loads Classes from the target build directory ─ A New Era for Java Protocol Handlers by Brian Maso (http: //java. sun. com/developer/online. Training/protocolhandlers/) • A Class. Loader that constructs a JAR that only contains Classes that were actually used by the program ─ Minimize Java Distributions with an Automated, Custom JAR File by Greg Travis (http: //www. devx. com/Java/Article/1767) • A Class. Loader that knows how to apply patches ─ Clinton S. Bennion, Principal Software Engineer, Brooks Automation 57 | 2004 Java. One. SM Conference | BOF-2509

References • Classloading in Oracle 9 i. AS Containers for J 2 EE ─ References • Classloading in Oracle 9 i. AS Containers for J 2 EE ─ Bryan Atsatt and Debu Panda • Understanding Class. for. Name ─ Ted Neward • Making the most of Web. Logic Classloaders ─ John Musser • J 2 EE Class Loading Demystified ─ Tim de. Boer and Gary Karasiuk • J 2 EE Design and Development ─ Rod Johnson • The Class Loading Architecture of JBoss 3. 0. 7 and 3. 2 ─ The Jboss Group, LLC • Advanced Classloading in J 2 EE ─ David Bosschaert and Arne Koschel • Find a way out of the Class. Loader maze ─ Vladimir Roubtsov 58 | 2004 Java. One. SM Conference | BOF-2509

Q&A Peter Keavney Andrea O. K. Wright Chariot Solutions 59 | 2004 Java. One. Q&A Peter Keavney Andrea O. K. Wright Chariot Solutions 59 | 2004 Java. One. SM Conference | Session XXXX