Source Code Cross Referenced for TestHelper_ClassLoader.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » sql » tests » java » sql » 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 » Apache Harmony Java SE » org package » org.apache.harmony.sql.tests.java.sql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.harmony.sql.tests.java.sql;
019:
020:        import java.io.File;
021:        import java.io.FileInputStream;
022:        import java.io.IOException;
023:        import java.io.InputStream;
024:        import java.util.jar.JarEntry;
025:        import java.util.jar.JarFile;
026:
027:        public class TestHelper_ClassLoader extends ClassLoader {
028:
029:            public TestHelper_ClassLoader() {
030:                super (null);
031:            }
032:
033:            /**
034:             * Loads a class specified by its name
035:             * <p>
036:             * This classloader makes the assumption that any class it is asked to load
037:             * is in the current directory....
038:             */
039:            @Override
040:            public Class<?> findClass(String className)
041:                    throws ClassNotFoundException {
042:                Class<?> theClass = null;
043:
044:                if (!className
045:                        .equals("org.apache.harmony.sql.tests.java.sql.TestHelper_DriverManager")) {
046:                    return null;
047:                }
048:
049:                String classNameAsFile = className.replace('.', '/') + ".class";
050:                // System.out.println("findClass - class filename = " + classNameAsFile
051:                // );
052:
053:                String classPath = System.getProperty("java.class.path");
054:                // System.out.println("Test class loader - classpath = " + classPath );
055:
056:                String theSeparator = String.valueOf(File.pathSeparatorChar);
057:                String[] theClassPaths = classPath.split(theSeparator);
058:                for (int i = 0; (i < theClassPaths.length)
059:                        && (theClass == null); i++) {
060:                    // Ignore jar files...
061:                    if (theClassPaths[i].endsWith(".jar")) {
062:                        theClass = loadClassFromJar(theClassPaths[i],
063:                                className, classNameAsFile);
064:                    } else {
065:                        theClass = loadClassFromFile(theClassPaths[i],
066:                                className, classNameAsFile);
067:                    } // end if
068:                } // end for
069:
070:                return theClass;
071:            } // end method findClass( String )
072:
073:            @Override
074:            public Class<?> loadClass(String className)
075:                    throws ClassNotFoundException {
076:                // Allowed classes:
077:                String[] disallowedClasses = {
078:                        "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver1",
079:                        "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver2",
080:                        "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver4",
081:                        "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver5" };
082:
083:                Class<?> theClass;
084:
085:                theClass = findLoadedClass(className);
086:                if (theClass != null) {
087:                    return theClass;
088:                }
089:
090:                theClass = this .findClass(className);
091:
092:                if (theClass == null) {
093:                    for (String element : disallowedClasses) {
094:                        if (element.equals(className)) {
095:                            return null;
096:                        } // end if
097:                    } // end for
098:                    theClass = Class.forName(className);
099:                } // end if
100:
101:                return theClass;
102:            } // end method loadClass( String )
103:
104:            private Class<?> loadClassFromFile(String pathName,
105:                    String className, String classNameAsFile) {
106:                Class<?> theClass = null;
107:                FileInputStream theInput = null;
108:                File theFile = null;
109:                try {
110:                    theFile = new File(pathName, classNameAsFile);
111:                    if (theFile.exists()) {
112:                        int length = (int) theFile.length();
113:                        theInput = new FileInputStream(theFile);
114:                        byte[] theBytes = new byte[length + 100];
115:                        int dataRead = 0;
116:                        while (dataRead < length) {
117:                            int count = theInput.read(theBytes, dataRead,
118:                                    theBytes.length - dataRead);
119:                            if (count == -1) {
120:                                break;
121:                            }
122:                            dataRead += count;
123:                        }
124:
125:                        if (dataRead > 0) {
126:                            // Create the class from the bytes read in...
127:                            theClass = this .defineClass(className, theBytes, 0,
128:                                    dataRead);
129:                            ClassLoader testClassLoader = theClass
130:                                    .getClassLoader();
131:                            if (testClassLoader != this ) {
132:                                System.out
133:                                        .println("findClass - wrong classloader!!");
134:                            }
135:                        }
136:                    }
137:                } catch (Exception e) {
138:                    System.out
139:                            .println("findClass - exception reading class file.");
140:                    e.printStackTrace();
141:                } finally {
142:                    try {
143:                        if (theInput != null) {
144:                            theInput.close();
145:                        }
146:                    } catch (Exception e) {
147:                    }
148:                }
149:                return theClass;
150:            }
151:
152:            /*
153:             * Loads a named class from a specified JAR file
154:             */
155:            private Class<?> loadClassFromJar(String jarfileName,
156:                    String className, String classNameAsFile) {
157:                Class<?> theClass = null;
158:
159:                // First, try to open the Jar file
160:                JarFile theJar = null;
161:                try {
162:                    theJar = new JarFile(jarfileName);
163:                    JarEntry theEntry = theJar.getJarEntry(classNameAsFile);
164:
165:                    if (theEntry == null) {
166:                        // System.out.println("TestHelper_Classloader - did not find
167:                        // class file in Jar " + jarfileName );
168:                        return theClass;
169:                    } // end if
170:
171:                    theEntry.getMethod();
172:                    InputStream theStream = theJar.getInputStream(theEntry);
173:
174:                    long size = theEntry.getSize();
175:                    if (size < 0) {
176:                        size = 100000;
177:                    }
178:                    byte[] theBytes = new byte[(int) size + 100];
179:
180:                    int dataRead = 0;
181:                    while (dataRead < size) {
182:                        int count = theStream.read(theBytes, dataRead,
183:                                theBytes.length - dataRead);
184:                        if (count == -1) {
185:                            break;
186:                        }
187:                        dataRead += count;
188:                    } // end while
189:
190:                    // System.out.println("loadClassFromJar: read " + dataRead + " bytes
191:                    // from class file");
192:                    if (dataRead > 0) {
193:                        // Create the class from the bytes read in...
194:                        theClass = this .defineClass(className, theBytes, 0,
195:                                dataRead);
196:                        /* System.out.println("findClass: created Class object."); */
197:                        ClassLoader testClassLoader = theClass.getClassLoader();
198:                        if (testClassLoader != this ) {
199:                            System.out
200:                                    .println("findClass - wrong classloader!!");
201:                        } else {
202:                            System.out
203:                                    .println("Testclassloader loaded class from jar: "
204:                                            + className);
205:                        } // end if
206:                    } // end if
207:                } catch (IOException ie) {
208:                    System.out
209:                            .println("TestHelper_ClassLoader: IOException opening Jar "
210:                                    + jarfileName);
211:                } catch (Exception e) {
212:                    System.out
213:                            .println("TestHelper_ClassLoader: Exception loading class from Jar ");
214:                } catch (ClassFormatError ce) {
215:                    System.out
216:                            .println("TestHelper_ClassLoader: ClassFormatException loading class from Jar ");
217:                } finally {
218:                    try {
219:                        if (theJar != null) {
220:                            theJar.close();
221:                        }
222:                    } catch (Exception e) {
223:                    } // end try
224:                } // end try
225:
226:                return theClass;
227:            } // end method loadClassFromJar(
228:
229:        } // end class TestHelper_ClassLoader
www___.___j___a__v__a2___s_._c___o_m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.