Source Code Cross Referenced for ParallelWorldClassLoader.java in  » 6.0-JDK-Modules » jaxb-xjc » com » sun » istack » tools » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » jaxb xjc » com.sun.istack.tools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.sun.istack.tools;
002:
003:        import java.io.InputStream;
004:        import java.io.ByteArrayOutputStream;
005:        import java.io.IOException;
006:        import java.net.URL;
007:        import java.net.MalformedURLException;
008:        import java.util.Enumeration;
009:
010:        /**
011:         * Load classes/resources from a side folder, so that
012:         * classes of the same package can live in a single jar file.
013:         *
014:         * <p>
015:         * For example, with the following jar file:
016:         * <pre>
017:         *  /
018:         *  +- foo
019:         *     +- X.class
020:         *  +- bar
021:         *     +- X.class
022:         * </pre>
023:         * <p>
024:         * {@link ParallelWorldClassLoader}("foo/") would load <tt>X.class<tt> from
025:         * <tt>/foo/X.class</tt> (note that X is defined in the root package, not
026:         * <tt>foo.X</tt>.
027:         *
028:         * <p>
029:         * This can be combined with  {@link MaskingClassLoader} to mask classes which are loaded by the parent
030:         * class loader so that the child class loader
031:         * classes living in different folders are loaded
032:         * before the parent class loader loads classes living the jar file publicly
033:         * visible
034:         * For example, with the following jar file:
035:         * <pre>
036:         *  /
037:         *  +- foo
038:         *     +- X.class
039:         *  +- bar
040:         *     +-foo
041:         *        +- X.class
042:         * </pre>
043:         * <p>
044:         * {@link ParallelWorldClassLoader}(MaskingClassLoader.class.getClassLoader()) would load <tt>foo.X.class<tt> from
045:         * <tt>/bar/foo.X.class</tt> not the <tt>foo.X.class<tt> in the publicly visible place in the jar file, thus
046:         * masking the parent classLoader from loading the class from  <tt>foo.X.class<tt>
047:         * (note that X is defined in the  package foo, not
048:         * <tt>bar.foo.X</tt>.
049:         *
050:         * @author Kohsuke Kawaguchi
051:         */
052:        public class ParallelWorldClassLoader extends ClassLoader {
053:
054:            /**
055:             * Strings like "prefix/", "abc/", or "" to indicate
056:             * classes should be loaded normally.
057:             */
058:            private final String prefix;
059:
060:            public ParallelWorldClassLoader(ClassLoader parent, String prefix) {
061:                super (parent);
062:                this .prefix = prefix;
063:            }
064:
065:            protected Class findClass(String name)
066:                    throws ClassNotFoundException {
067:                StringBuffer sb = new StringBuffer(name.length()
068:                        + prefix.length() + 6);
069:                sb.append(prefix).append(name.replace('.', '/')).append(
070:                        ".class");
071:
072:                InputStream is = getParent().getResourceAsStream(sb.toString());
073:                if (is == null)
074:                    throw new ClassNotFoundException(name);
075:
076:                try {
077:                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
078:                    byte[] buf = new byte[1024];
079:                    int len;
080:                    while ((len = is.read(buf)) >= 0)
081:                        baos.write(buf, 0, len);
082:
083:                    buf = baos.toByteArray();
084:                    int packIndex = name.lastIndexOf('.');
085:                    if (packIndex != -1) {
086:                        String pkgname = name.substring(0, packIndex);
087:                        // Check if package already loaded.
088:                        Package pkg = getPackage(pkgname);
089:                        if (pkg == null) {
090:                            definePackage(pkgname, null, null, null, null,
091:                                    null, null, null);
092:                        }
093:                    }
094:                    return defineClass(name, buf, 0, buf.length);
095:                } catch (IOException e) {
096:                    throw new ClassNotFoundException(name, e);
097:                }
098:            }
099:
100:            protected URL findResource(String name) {
101:                return getParent().getResource(prefix + name);
102:            }
103:
104:            protected Enumeration<URL> findResources(String name)
105:                    throws IOException {
106:                return getParent().getResources(prefix + name);
107:            }
108:
109:            /**
110:             * Given the URL inside jar, returns the URL to the jar itself.
111:             */
112:            public static URL toJarUrl(URL res) throws ClassNotFoundException,
113:                    MalformedURLException {
114:                String url = res.toExternalForm();
115:                if (!url.startsWith("jar:"))
116:                    throw new ClassNotFoundException("Loaded outside a jar "
117:                            + url);
118:                url = url.substring(4); // cut off jar:
119:                url = url.substring(0, url.lastIndexOf('!')); // cut off everything after '!'
120:                return new URL(url);
121:            }
122:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.