├── .gitignore ├── LICENSE-2.0.txt ├── LICENSE.txt ├── README.md ├── pom.xml └── src ├── main ├── aspect │ └── org │ │ └── codehaus │ │ └── plexus │ │ └── classworlds │ │ └── event │ │ └── ListenerAspect.aj └── java │ └── org │ └── codehaus │ ├── classworlds │ ├── BytesURLConnection.java │ ├── BytesURLStreamHandler.java │ ├── ClassRealm.java │ ├── ClassRealmAdapter.java │ ├── ClassRealmReverseAdapter.java │ ├── ClassWorld.java │ ├── ClassWorldAdapter.java │ ├── ClassWorldException.java │ ├── ClassWorldReverseAdapter.java │ ├── ConfigurationException.java │ ├── Configurator.java │ ├── ConfiguratorAdapter.java │ ├── DefaultClassRealm.java │ ├── DuplicateRealmException.java │ ├── Launcher.java │ └── NoSuchRealmException.java │ └── plexus │ └── classworlds │ ├── ClassWorld.java │ ├── ClassWorldException.java │ ├── ClassWorldListener.java │ ├── UrlUtils.java │ ├── launcher │ ├── ConfigurationException.java │ ├── ConfigurationHandler.java │ ├── ConfigurationParser.java │ ├── Configurator.java │ └── Launcher.java │ ├── realm │ ├── ClassRealm.java │ ├── DuplicateRealmException.java │ ├── Entry.java │ └── NoSuchRealmException.java │ └── strategy │ ├── AbstractStrategy.java │ ├── OsgiBundleStrategy.java │ ├── ParentFirstStrategy.java │ ├── SelfFirstStrategy.java │ ├── Strategy.java │ └── StrategyFactory.java ├── site └── site.xml └── test ├── java └── org │ └── codehaus │ └── plexus │ └── classworlds │ ├── AbstractClassWorldsTestCase.java │ ├── ClassView.java │ ├── ClassWorldTest.java │ ├── TestUtil.java │ ├── UrlUtilsTest.java │ ├── launcher │ ├── ConfigurationParserTest.java │ ├── ConfiguratorTest.java │ └── LauncherTest.java │ ├── realm │ ├── ClassRealmImplTest.java │ ├── DefaultClassRealmTest.java │ └── EntryTest.java │ └── strategy │ └── StrategyTest.java └── test-data ├── a.jar ├── a.properties ├── b.jar ├── b_old.jar ├── c.jar ├── circular-0.1.jar ├── component0-1.0.jar ├── component1-1.0.jar ├── component2-1.0.jar ├── component3-1.0.jar ├── component4-1.0.jar ├── component5-1.0.jar ├── component5-2.0.jar ├── d.jar ├── deadlock.jar ├── dupe-main.conf ├── dupe-realm.conf ├── early-import.conf ├── inheritance.conf ├── launch-noclass.conf ├── launch-nomethod.conf ├── nested.jar ├── nested.properties ├── optionally-existent.conf ├── optionally-nonexistent.conf ├── realm-syntax.conf ├── resources ├── classworlds.conf └── werkflow.jar ├── set-using-existent.conf ├── set-using-existent.properties ├── set-using-missing.conf ├── set-using-nonexistent.conf ├── unhandled.conf ├── valid-enh-launch-exitCode.conf ├── valid-enh-launch.conf ├── valid-launch-exitCode.conf ├── valid-launch.conf └── valid.conf /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .project 3 | .classpath 4 | .settings/ 5 | bin 6 | .idea 7 | *.iml 8 | .java-version 9 | -------------------------------------------------------------------------------- /LICENSE-2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | $Id$ 5 | 6 | Copyright 2002 (C) The Codehaus. All Rights Reserved. 7 | 8 | Redistribution and use of this software and associated documentation 9 | ("Software"), with or without modification, are permitted provided 10 | that the following conditions are met: 11 | 12 | 1. Redistributions of source code must retain copyright 13 | statements and notices. Redistributions must also contain a 14 | copy of this document. 15 | 16 | 2. Redistributions in binary form must reproduce the 17 | above copyright notice, this list of conditions and the 18 | following disclaimer in the documentation and/or other 19 | materials provided with the distribution. 20 | 21 | 3. The name "classworlds" must not be used to endorse or promote 22 | products derived from this Software without prior written 23 | permission of The Codehaus. For written permission, please 24 | contact bob@codehaus.org. 25 | 26 | 4. Products derived from this Software may not be called "classworlds" 27 | nor may "classworlds" appear in their names without prior written 28 | permission of The Codehaus. "classworlds" is a registered 29 | trademark of The Codehaus. 30 | 31 | 5. Due credit should be given to The Codehaus. 32 | (http://classworlds.codehaus.org/). 33 | 34 | THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS 35 | ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 36 | NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 37 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 38 | THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 39 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 40 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 41 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 43 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 44 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 45 | OF THE POSSIBILITY OF SUCH DAMAGE. 46 | 47 | */ 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # plexus-classworlds 2 | Current master is now at https://github.com/codehaus-plexus/plexus-classworlds 3 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.codehaus.plexus 23 | plexus 24 | 3.3.1 25 | 26 | 27 | plexus-classworlds 28 | 2.5.3-SNAPSHOT 29 | bundle 30 | 31 | Plexus Classworlds 32 | A class loader framework 33 | 2002 34 | 35 | 36 | scm:git:git@github.com:sonatype/plexus-classworlds.git 37 | scm:git:git@github.com:sonatype/plexus-classworlds.git 38 | http://github.com/sonatype/plexus-classworlds 39 | HEAD 40 | 41 | 42 | 43 | 44 | adm-site 45 | scm:git:git@github.com:sonatype/plexus-classworlds.git 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.apache.maven.plugins 54 | maven-scm-publish-plugin 55 | 1.0 56 | 57 | gh-pages 58 | 59 | 60 | 61 | org.apache.maven.plugins 62 | maven-site-plugin 63 | 3.3 64 | 65 | gh-pages 66 | 67 | 68 | 69 | org.apache.maven.plugins 70 | maven-release-plugin 71 | 2.5 72 | 73 | true 74 | false 75 | false 76 | deploy 77 | -Pplexus-release 78 | 79 | 80 | 81 | 82 | 83 | 84 | org.apache.felix 85 | maven-bundle-plugin 86 | true 87 | 88 | 89 | <_nouses>true 90 | org.codehaus.classworlds.*;org.codehaus.plexus.classworlds.* 91 | 92 | 93 | 94 | 95 | org.apache.maven.plugins 96 | maven-jar-plugin 97 | 98 | 99 | 100 | org.codehaus.plexus.classworlds.launcher.Launcher 101 | 102 | 103 | 104 | 105 | 106 | org.apache.maven.plugins 107 | maven-surefire-plugin 108 | 109 | true 110 | -ea:org.codehaus.classworlds:org.codehaus.plexus.classworlds 111 | once 112 | 113 | 114 | 115 | org.apache.maven.plugins 116 | maven-compiler-plugin 117 | 118 | 1.6 119 | 1.6 120 | 121 | org/codehaus/plexus/classworlds/event/* 122 | 123 | 124 | 125 | 126 | org.apache.maven.plugins 127 | maven-dependency-plugin 128 | 2.0 129 | 130 | 131 | generate-test-resources 132 | 133 | copy 134 | 135 | 136 | 137 | 138 | org.apache.ant 139 | ant 140 | 1.9.0 141 | 142 | 143 | commons-logging 144 | commons-logging 145 | 1.0.3 146 | 147 | 148 | xml-apis 149 | xml-apis 150 | 1.3.02 151 | 152 | 153 | ${project.build.directory}/test-lib 154 | 155 | 156 | 157 | 158 | 159 | org.apache.maven.plugins 160 | maven-enforcer-plugin 161 | 1.3.1 162 | 163 | 164 | enforce-java 165 | 166 | enforce 167 | 168 | 169 | 170 | 171 | 1.7.0 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | org.apache.maven.plugins 186 | maven-project-info-reports-plugin 187 | 2.7 188 | 189 | false 190 | 191 | 192 | 193 | 194 | summary 195 | index 196 | dependencies 197 | issue-tracking 198 | scm 199 | 200 | 201 | 202 | 203 | 204 | org.apache.maven.plugins 205 | maven-javadoc-plugin 206 | 2.9.1 207 | 208 | utf-8 209 | true 210 | true 211 | 212 | 213 | 214 | 215 | 216 | 217 | -------------------------------------------------------------------------------- /src/main/aspect/org/codehaus/plexus/classworlds/event/ListenerAspect.aj: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.event; 2 | 3 | import java.net.URL; 4 | 5 | import org.codehaus.plexus.classworlds.strategy.Strategy; 6 | 7 | /** 8 | * A simple aspect to hook event code in when compiling with debug enabled 9 | * 10 | * @uthor: Andrew Williams 11 | * @since: 1.2-alpha-15 12 | * @version: $Id$ 13 | */ 14 | aspect ListenerAspect 15 | { 16 | // TODO: here we want a proper listener registering system, not just a debugger 17 | private ClassEventDebug classDebugger = new ClassEventDebug(); 18 | private ResourceEventDebug resourceDebugger = new ResourceEventDebug(); 19 | 20 | pointcut loadClass( String name, Strategy strategy ): 21 | args( name ) && target( strategy ) && 22 | call( Class Strategy.loadClass( String ) ); 23 | 24 | before( String name, Strategy strategy ): 25 | loadClass( name, strategy ) 26 | { 27 | classDebugger.lookup( name, strategy ); 28 | } 29 | 30 | after( String name, Strategy strategy ) returning( Class result ): 31 | loadClass( name, strategy ) 32 | { 33 | classDebugger.found( name, strategy, result ); 34 | } 35 | 36 | after( String name, Strategy strategy ) throwing( Exception e ): 37 | loadClass( name, strategy ) 38 | { 39 | classDebugger.failed( name, strategy, e ); 40 | } 41 | 42 | pointcut getResource( String name, Strategy strategy ): 43 | args( name ) && target( strategy ) && 44 | call( URL Strategy.getResource( String ) ); 45 | 46 | before( String name, Strategy strategy ): 47 | getResource( name, strategy ) 48 | { 49 | resourceDebugger.lookup( name, strategy ); 50 | } 51 | 52 | after( String name, Strategy strategy ) returning( URL result ): 53 | getResource( name, strategy ) 54 | { 55 | if ( result == null ) 56 | { 57 | resourceDebugger.failed( name, strategy ); 58 | } 59 | else 60 | { 61 | resourceDebugger.found( name, strategy, result ); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/BytesURLConnection.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.net.URLConnection; 20 | import java.net.URL; 21 | import java.io.InputStream; 22 | import java.io.ByteArrayInputStream; 23 | 24 | /** 25 | * @author Hani Suleiman (hani@formicary.net) 26 | *

27 | * Date: Oct 20, 2003 28 | * Time: 12:46:01 AM 29 | */ 30 | @Deprecated 31 | public class BytesURLConnection extends URLConnection 32 | { 33 | protected byte[] content; 34 | 35 | protected int offset; 36 | 37 | protected int length; 38 | 39 | public BytesURLConnection( URL url, byte[] content ) 40 | { 41 | super( url ); 42 | this.content = content; 43 | } 44 | 45 | public void connect() 46 | { 47 | } 48 | 49 | public InputStream getInputStream() 50 | { 51 | return new ByteArrayInputStream( content ); 52 | } 53 | } -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/BytesURLStreamHandler.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.net.URLStreamHandler; 20 | import java.net.URLConnection; 21 | import java.net.URL; 22 | 23 | /** 24 | * @author Hani Suleiman (hani@formicary.net) 25 | * Date: Oct 20, 2003 26 | * Time: 12:45:18 AM 27 | */ 28 | @Deprecated 29 | public class BytesURLStreamHandler extends URLStreamHandler 30 | { 31 | byte[] content; 32 | 33 | int offset; 34 | 35 | int length; 36 | 37 | public BytesURLStreamHandler( byte[] content ) 38 | { 39 | this.content = content; 40 | } 41 | 42 | public URLConnection openConnection( URL url ) 43 | { 44 | return new BytesURLConnection( url, content ); 45 | } 46 | } -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/ClassRealm.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | 5 | Copyright 2002 (C) The Werken Company. All Rights Reserved. 6 | 7 | Redistribution and use of this software and associated documentation 8 | ("Software"), with or without modification, are permitted provided 9 | that the following conditions are met: 10 | 11 | 1. Redistributions of source code must retain copyright 12 | statements and notices. Redistributions must also contain a 13 | copy of this document. 14 | 15 | 2. Redistributions in binary form must reproduce the 16 | above copyright notice, this list of conditions and the 17 | following disclaimer in the documentation and/or other 18 | materials provided with the distribution. 19 | 20 | 3. The name "classworlds" must not be used to endorse or promote 21 | products derived from this Software without prior written 22 | permission of The Werken Company. For written permission, 23 | please contact bob@werken.com. 24 | 25 | 4. Products derived from this Software may not be called "classworlds" 26 | nor may "classworlds" appear in their names without prior written 27 | permission of The Werken Company. "classworlds" is a registered 28 | trademark of The Werken Company. 29 | 30 | 5. Due credit should be given to The Werken Company. 31 | (http://classworlds.werken.com/). 32 | 33 | THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 34 | ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 35 | NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 36 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 37 | THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 38 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 39 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 40 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 42 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 43 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 44 | OF THE POSSIBILITY OF SUCH DAMAGE. 45 | 46 | */ 47 | 48 | import java.io.IOException; 49 | import java.io.InputStream; 50 | import java.net.URL; 51 | import java.util.Enumeration; 52 | 53 | /** 54 | * Autonomous sub-portion of a ClassWorld. 55 | *

56 | *

57 | * This class most closed maps to the ClassLoader 58 | * role from Java and in facts can provide a ClassLoader 59 | * view of itself using {@link #getClassLoader}. 60 | *

61 | * 62 | * @author bob mcwhirter 63 | * @author Jason van Zyl 64 | */ 65 | @Deprecated 66 | public interface ClassRealm 67 | { 68 | String getId(); 69 | 70 | ClassWorld getWorld(); 71 | 72 | void importFrom( String realmId, String pkgName ) 73 | throws NoSuchRealmException; 74 | 75 | void addConstituent( URL constituent ); 76 | 77 | ClassRealm locateSourceRealm( String className ); 78 | 79 | void setParent( ClassRealm classRealm ); 80 | 81 | ClassRealm createChildRealm( String id ) 82 | throws DuplicateRealmException; 83 | 84 | ClassLoader getClassLoader(); 85 | 86 | ClassRealm getParent(); 87 | 88 | URL[] getConstituents(); 89 | 90 | // ---------------------------------------------------------------------- 91 | // Classloading 92 | // ---------------------------------------------------------------------- 93 | 94 | Class loadClass( String name ) 95 | throws ClassNotFoundException; 96 | 97 | // ---------------------------------------------------------------------- 98 | // Resource handling 99 | // ---------------------------------------------------------------------- 100 | 101 | URL getResource( String name ); 102 | 103 | Enumeration findResources( String name ) 104 | throws IOException; 105 | 106 | InputStream getResourceAsStream( String name ); 107 | 108 | void display(); 109 | } 110 | 111 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/ClassRealmAdapter.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.net.URL; 20 | import java.util.Enumeration; 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | 24 | /** 25 | * An adapter for ClassRealms 26 | * 27 | * @author Andrew Williams 28 | */ 29 | @Deprecated 30 | public class ClassRealmAdapter 31 | implements ClassRealm 32 | { 33 | 34 | public static ClassRealmAdapter getInstance( org.codehaus.plexus.classworlds.realm.ClassRealm newRealm ) 35 | { 36 | ClassRealmAdapter adapter = new ClassRealmAdapter( newRealm ); 37 | 38 | return adapter; 39 | } 40 | 41 | private org.codehaus.plexus.classworlds.realm.ClassRealm realm; 42 | 43 | private ClassRealmAdapter( org.codehaus.plexus.classworlds.realm.ClassRealm newRealm ) 44 | { 45 | this.realm = newRealm; 46 | } 47 | 48 | public String getId() 49 | { 50 | return realm.getId(); 51 | } 52 | 53 | public ClassWorld getWorld() 54 | { 55 | return ClassWorldAdapter.getInstance( realm.getWorld() ); 56 | } 57 | 58 | public void importFrom( String realmId, 59 | String pkgName ) 60 | throws NoSuchRealmException 61 | { 62 | try 63 | { 64 | realm.importFrom( realmId, pkgName ); 65 | } 66 | catch ( org.codehaus.plexus.classworlds.realm.NoSuchRealmException e ) 67 | { 68 | throw new NoSuchRealmException( getWorld(), e.getId() ); 69 | } 70 | } 71 | 72 | public void addConstituent( URL constituent ) 73 | { 74 | realm.addURL( constituent ); 75 | } 76 | 77 | public ClassRealm locateSourceRealm( String className ) 78 | { 79 | ClassLoader importLoader = realm.getImportClassLoader( className ); 80 | 81 | if ( importLoader instanceof org.codehaus.plexus.classworlds.realm.ClassRealm ) 82 | { 83 | return ClassRealmAdapter.getInstance( (org.codehaus.plexus.classworlds.realm.ClassRealm) importLoader ); 84 | } 85 | else 86 | { 87 | return null; 88 | } 89 | } 90 | 91 | public void setParent( ClassRealm classRealm ) 92 | { 93 | if ( classRealm != null ) 94 | { 95 | realm.setParentRealm( ClassRealmReverseAdapter.getInstance( classRealm ) ); 96 | } 97 | } 98 | 99 | public ClassRealm createChildRealm( String id ) 100 | throws DuplicateRealmException 101 | { 102 | try 103 | { 104 | return ClassRealmAdapter.getInstance( realm.createChildRealm( id ) ); 105 | } 106 | catch ( org.codehaus.plexus.classworlds.realm.DuplicateRealmException e ) 107 | { 108 | throw new DuplicateRealmException( getWorld(), e.getId() ); 109 | } 110 | } 111 | 112 | public ClassLoader getClassLoader() 113 | { 114 | return realm; 115 | } 116 | 117 | public ClassRealm getParent() 118 | { 119 | return ClassRealmAdapter.getInstance( realm.getParentRealm() ); 120 | } 121 | 122 | public ClassRealm getParentRealm() 123 | { 124 | return ClassRealmAdapter.getInstance( realm.getParentRealm() ); 125 | } 126 | 127 | public URL[] getConstituents() 128 | { 129 | return realm.getURLs(); 130 | } 131 | 132 | public Class loadClass( String name ) 133 | throws ClassNotFoundException 134 | { 135 | return realm.loadClass( name ); 136 | } 137 | 138 | public URL getResource( String name ) 139 | { 140 | return realm.getResource( trimLeadingSlash( name ) ); 141 | } 142 | 143 | public Enumeration findResources( String name ) 144 | throws IOException 145 | { 146 | return realm.findResources( trimLeadingSlash( name ) ); 147 | } 148 | 149 | public InputStream getResourceAsStream( String name ) 150 | { 151 | return realm.getResourceAsStream( trimLeadingSlash( name ) ); 152 | } 153 | 154 | public void display() 155 | { 156 | realm.display(); 157 | } 158 | 159 | public boolean equals(Object o) 160 | { 161 | if ( !( o instanceof ClassRealm ) ) 162 | return false; 163 | 164 | return getId().equals( ( (ClassRealm) o ).getId() ); 165 | } 166 | 167 | /** 168 | * Provides backward-compat with the old classworlds which accepted resource names with leading slashes. 169 | */ 170 | private String trimLeadingSlash( String resource ) 171 | { 172 | if ( resource != null && resource.startsWith( "/" ) ) 173 | { 174 | return resource.substring( 1 ); 175 | } 176 | else 177 | { 178 | return resource; 179 | } 180 | } 181 | 182 | } 183 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/ClassRealmReverseAdapter.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.util.Enumeration; 20 | import java.net.URL; 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | 24 | /** 25 | * A reverse adapter for ClassRealms 26 | * 27 | * @author Andrew Williams 28 | */ 29 | @Deprecated 30 | public class ClassRealmReverseAdapter 31 | extends org.codehaus.plexus.classworlds.realm.ClassRealm 32 | { 33 | 34 | public static ClassRealmReverseAdapter getInstance( ClassRealm oldRealm ) 35 | { 36 | ClassRealmReverseAdapter adapter = new ClassRealmReverseAdapter( oldRealm ); 37 | 38 | return adapter; 39 | } 40 | 41 | private ClassRealm realm; 42 | 43 | private ClassRealmReverseAdapter( ClassRealm oldRealm ) 44 | { 45 | super( ClassWorldReverseAdapter.getInstance( oldRealm.getWorld() ), 46 | oldRealm.getId(), oldRealm.getClassLoader() ); 47 | this.realm = oldRealm; 48 | } 49 | 50 | public String getId() 51 | { 52 | return realm.getId(); 53 | } 54 | 55 | public org.codehaus.plexus.classworlds.ClassWorld getWorld() 56 | { 57 | return ClassWorldReverseAdapter.getInstance( realm.getWorld() ); 58 | } 59 | 60 | public void importFrom( String realmId, 61 | String pkgName ) 62 | throws org.codehaus.plexus.classworlds.realm.NoSuchRealmException 63 | { 64 | try 65 | { 66 | realm.importFrom( realmId, pkgName ); 67 | } 68 | catch ( NoSuchRealmException e ) 69 | { 70 | throw new org.codehaus.plexus.classworlds.realm.NoSuchRealmException( getWorld(), e.getId() ); 71 | } 72 | } 73 | 74 | public void addURL( URL constituent ) 75 | { 76 | realm.addConstituent( constituent ); 77 | } 78 | 79 | public org.codehaus.plexus.classworlds.realm.ClassRealm locateSourceRealm( String className ) 80 | { 81 | return getInstance( realm.locateSourceRealm( 82 | className ) ); 83 | } 84 | 85 | public void setParentRealm( org.codehaus.plexus.classworlds.realm.ClassRealm classRealm ) 86 | { 87 | realm.setParent( ClassRealmAdapter.getInstance( classRealm ) ); 88 | } 89 | 90 | public org.codehaus.plexus.classworlds.realm.ClassRealm createChildRealm( String id ) 91 | throws org.codehaus.plexus.classworlds.realm.DuplicateRealmException 92 | { 93 | try 94 | { 95 | return getInstance( realm.createChildRealm( id ) ); 96 | } 97 | catch ( DuplicateRealmException e ) 98 | { 99 | throw new org.codehaus.plexus.classworlds.realm.DuplicateRealmException( getWorld(), e.getId() ); 100 | } 101 | } 102 | 103 | public ClassLoader getClassLoader() 104 | { 105 | return realm.getClassLoader(); 106 | } 107 | 108 | public org.codehaus.plexus.classworlds.realm.ClassRealm getParentRealm() 109 | { 110 | return getInstance( realm.getParent() ); 111 | } 112 | 113 | public URL[] getURLs() 114 | { 115 | return realm.getConstituents(); 116 | } 117 | 118 | public Class loadClass( String name ) 119 | throws ClassNotFoundException 120 | { 121 | return realm.loadClass( name ); 122 | } 123 | 124 | public URL getResource( String name ) 125 | { 126 | return realm.getResource( name ); 127 | } 128 | 129 | public Enumeration findResources( String name ) 130 | throws IOException 131 | { 132 | return realm.findResources( name ); 133 | } 134 | 135 | public InputStream getResourceAsStream( String name ) 136 | { 137 | return realm.getResourceAsStream( name ); 138 | } 139 | 140 | public void display() 141 | { 142 | realm.display(); 143 | } 144 | 145 | public boolean equals(Object o) 146 | { 147 | if ( !( o instanceof ClassRealm ) ) 148 | return false; 149 | 150 | return getId().equals( ( (ClassRealm) o ).getId() ); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/ClassWorld.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.util.Collection; 20 | 21 | /** 22 | * A compatibility wrapper for org.codehaus.plexus.classworlds.ClassWorld 23 | * provided for legacy code 24 | * 25 | * @author Andrew Williams 26 | */ 27 | @Deprecated 28 | public class ClassWorld 29 | { 30 | private ClassWorldAdapter adapter; 31 | 32 | public ClassWorld( String realmId, 33 | ClassLoader classLoader ) 34 | { 35 | adapter = ClassWorldAdapter.getInstance( 36 | new org.codehaus.plexus.classworlds.ClassWorld( realmId, classLoader ) ); 37 | } 38 | 39 | public ClassWorld() 40 | { 41 | adapter = ClassWorldAdapter.getInstance( 42 | new org.codehaus.plexus.classworlds.ClassWorld( ) ); 43 | } 44 | 45 | public ClassWorld( boolean ignore ) 46 | { 47 | /* fake */ 48 | } 49 | 50 | public ClassRealm newRealm( String id ) 51 | throws DuplicateRealmException 52 | { 53 | return adapter.newRealm( id ); 54 | } 55 | 56 | public ClassRealm newRealm( String id, 57 | ClassLoader classLoader ) 58 | throws DuplicateRealmException 59 | { 60 | return adapter.newRealm( id, classLoader ); 61 | } 62 | 63 | public void disposeRealm( String id ) 64 | throws NoSuchRealmException 65 | { 66 | adapter.disposeRealm( id ); 67 | } 68 | 69 | public ClassRealm getRealm( String id ) 70 | throws NoSuchRealmException 71 | { 72 | return adapter.getRealm( id ); 73 | } 74 | 75 | public Collection getRealms() 76 | { 77 | return adapter.getRealms(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/ClassWorldAdapter.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.util.Collection; 20 | import java.util.Vector; 21 | import java.util.Iterator; 22 | 23 | /** 24 | * An adapter for ClassWorlds 25 | * 26 | * @author Andrew Williams 27 | */ 28 | @Deprecated 29 | public class ClassWorldAdapter 30 | extends ClassWorld 31 | { 32 | 33 | public static ClassWorldAdapter getInstance( org.codehaus.plexus.classworlds.ClassWorld newWorld ) 34 | { 35 | ClassWorldAdapter adapter = new ClassWorldAdapter( newWorld ); 36 | 37 | return adapter; 38 | } 39 | 40 | private org.codehaus.plexus.classworlds.ClassWorld world; 41 | 42 | private ClassWorldAdapter( org.codehaus.plexus.classworlds.ClassWorld newWorld ) 43 | { 44 | super( false ); 45 | this.world = newWorld; 46 | } 47 | 48 | public ClassRealm newRealm( String id ) 49 | throws DuplicateRealmException 50 | { 51 | try 52 | { 53 | return ClassRealmAdapter.getInstance( world.newRealm( id ) ); 54 | } 55 | catch ( org.codehaus.plexus.classworlds.realm.DuplicateRealmException e ) 56 | { 57 | throw new DuplicateRealmException( this, e.getId() ); 58 | } 59 | } 60 | 61 | public ClassRealm newRealm( String id, 62 | ClassLoader classLoader ) 63 | throws DuplicateRealmException 64 | { 65 | try 66 | { 67 | return ClassRealmAdapter.getInstance( world.newRealm( id, 68 | classLoader ) ); 69 | } 70 | catch ( org.codehaus.plexus.classworlds.realm.DuplicateRealmException e ) 71 | { 72 | throw new DuplicateRealmException( this, e.getId() ); 73 | } 74 | } 75 | 76 | public void disposeRealm( String id ) 77 | throws NoSuchRealmException 78 | { 79 | try 80 | { 81 | world.disposeRealm( id ); 82 | } 83 | catch ( org.codehaus.plexus.classworlds.realm.NoSuchRealmException e ) 84 | { 85 | throw new NoSuchRealmException( this, e.getId() ); 86 | } 87 | } 88 | 89 | public ClassRealm getRealm( String id ) 90 | throws NoSuchRealmException 91 | { 92 | try 93 | { 94 | return ClassRealmAdapter.getInstance( world.getRealm( id ) ); 95 | } 96 | catch ( org.codehaus.plexus.classworlds.realm.NoSuchRealmException e ) 97 | { 98 | throw new NoSuchRealmException( this, e.getId() ); 99 | } 100 | } 101 | 102 | public Collection getRealms() 103 | { 104 | Collection realms = world.getRealms(); 105 | Vector ret = new Vector(); 106 | 107 | Iterator it = realms.iterator(); 108 | while ( it.hasNext() ) 109 | { 110 | org.codehaus.plexus.classworlds.realm.ClassRealm realm = 111 | (org.codehaus.plexus.classworlds.realm.ClassRealm) it.next(); 112 | ret.add( ClassRealmAdapter.getInstance( realm ) ); 113 | } 114 | 115 | return ret; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/ClassWorldException.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | 5 | Copyright 2002 (C) The Werken Company. All Rights Reserved. 6 | 7 | Redistribution and use of this software and associated documentation 8 | ("Software"), with or without modification, are permitted provided 9 | that the following conditions are met: 10 | 11 | 1. Redistributions of source code must retain copyright 12 | statements and notices. Redistributions must also contain a 13 | copy of this document. 14 | 15 | 2. Redistributions in binary form must reproduce the 16 | above copyright notice, this list of conditions and the 17 | following disclaimer in the documentation and/or other 18 | materials provided with the distribution. 19 | 20 | 3. The name "classworlds" must not be used to endorse or promote 21 | products derived from this Software without prior written 22 | permission of The Werken Company. For written permission, 23 | please contact bob@werken.com. 24 | 25 | 4. Products derived from this Software may not be called "classworlds" 26 | nor may "classworlds" appear in their names without prior written 27 | permission of The Werken Company. "classworlds" is a registered 28 | trademark of The Werken Company. 29 | 30 | 5. Due credit should be given to The Werken Company. 31 | (http://classworlds.werken.com/). 32 | 33 | THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 34 | ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 35 | NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 36 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 37 | THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 38 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 39 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 40 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 42 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 43 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 44 | OF THE POSSIBILITY OF SUCH DAMAGE. 45 | 46 | */ 47 | 48 | /** 49 | * Base exception for ClassWorld errors. 50 | * 51 | * @author bob mcwhirter 52 | */ 53 | @Deprecated 54 | public class ClassWorldException extends Exception 55 | { 56 | // ------------------------------------------------------------ 57 | // Instance members 58 | // ------------------------------------------------------------ 59 | 60 | /** 61 | * The world. 62 | */ 63 | private ClassWorld world; 64 | 65 | // ------------------------------------------------------------ 66 | // Constructors 67 | // ------------------------------------------------------------ 68 | 69 | /** 70 | * Construct. 71 | * 72 | * @param world The world. 73 | */ 74 | public ClassWorldException( final ClassWorld world ) 75 | { 76 | this.world = world; 77 | } 78 | 79 | /** 80 | * Construct. 81 | * 82 | * @param world The world. 83 | * @param msg The detail message. 84 | */ 85 | public ClassWorldException( final ClassWorld world, final String msg ) 86 | { 87 | super( msg ); 88 | this.world = world; 89 | } 90 | 91 | // ------------------------------------------------------------ 92 | // Instance methods 93 | // ------------------------------------------------------------ 94 | 95 | /** 96 | * Retrieve the world. 97 | * 98 | * @return The world. 99 | */ 100 | public ClassWorld getWorld() 101 | { 102 | return this.world; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/ClassWorldReverseAdapter.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.util.HashMap; 20 | import java.util.Collection; 21 | import java.util.Vector; 22 | import java.util.Iterator; 23 | 24 | /** 25 | * A reverse adapter for ClassWorlds 26 | * 27 | * @author Andrew Williams 28 | */ 29 | @Deprecated 30 | public class ClassWorldReverseAdapter 31 | extends org.codehaus.plexus.classworlds.ClassWorld 32 | { 33 | private static HashMap instances = new HashMap(); 34 | 35 | public static ClassWorldReverseAdapter getInstance( ClassWorld oldWorld ) 36 | { 37 | if ( instances.containsKey( oldWorld ) ) 38 | return (ClassWorldReverseAdapter) instances.get( oldWorld ); 39 | 40 | ClassWorldReverseAdapter adapter = new ClassWorldReverseAdapter( oldWorld ); 41 | instances.put( oldWorld, adapter ); 42 | 43 | return adapter; 44 | } 45 | 46 | private ClassWorld world; 47 | 48 | private ClassWorldReverseAdapter( ClassWorld newWorld ) 49 | { 50 | super(); 51 | this.world = newWorld; 52 | } 53 | 54 | public org.codehaus.plexus.classworlds.realm.ClassRealm newRealm( String id ) 55 | throws org.codehaus.plexus.classworlds.realm.DuplicateRealmException 56 | { 57 | try 58 | { 59 | return ClassRealmReverseAdapter.getInstance( world.newRealm( id ) ); 60 | } 61 | catch ( DuplicateRealmException e ) 62 | { 63 | throw new org.codehaus.plexus.classworlds.realm.DuplicateRealmException( this, e.getId() ); 64 | } 65 | } 66 | 67 | public org.codehaus.plexus.classworlds.realm.ClassRealm newRealm( String id, 68 | ClassLoader classLoader ) 69 | throws org.codehaus.plexus.classworlds.realm.DuplicateRealmException 70 | { 71 | try 72 | { 73 | return ClassRealmReverseAdapter.getInstance( world.newRealm( id, 74 | classLoader ) ); 75 | } 76 | catch ( DuplicateRealmException e ) 77 | { 78 | throw new org.codehaus.plexus.classworlds.realm.DuplicateRealmException( this, e.getId() ); 79 | } 80 | } 81 | 82 | public void disposeRealm( String id ) 83 | throws org.codehaus.plexus.classworlds.realm.NoSuchRealmException 84 | { 85 | try 86 | { 87 | world.disposeRealm( id ); 88 | } 89 | catch ( NoSuchRealmException e ) 90 | { 91 | throw new org.codehaus.plexus.classworlds.realm.NoSuchRealmException( this, e.getId() ); 92 | } 93 | } 94 | 95 | public org.codehaus.plexus.classworlds.realm.ClassRealm getRealm( String id ) 96 | throws org.codehaus.plexus.classworlds.realm.NoSuchRealmException 97 | { 98 | try 99 | { 100 | return ClassRealmReverseAdapter.getInstance( world.getRealm( id ) ); 101 | } 102 | catch ( NoSuchRealmException e ) 103 | { 104 | throw new org.codehaus.plexus.classworlds.realm.NoSuchRealmException( this, e.getId() ); 105 | } 106 | } 107 | 108 | public Collection getRealms() 109 | { 110 | Collection realms = world.getRealms(); 111 | Vector ret = new Vector(); 112 | 113 | Iterator it = realms.iterator(); 114 | while ( it.hasNext() ) 115 | { 116 | ClassRealm realm = (ClassRealm) it.next(); 117 | ret.add( ClassRealmReverseAdapter.getInstance( realm ) ); 118 | } 119 | 120 | return ret; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/ConfigurationException.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | 5 | Copyright 2002 (C) The Werken Company. All Rights Reserved. 6 | 7 | Redistribution and use of this software and associated documentation 8 | ("Software"), with or without modification, are permitted provided 9 | that the following conditions are met: 10 | 11 | 1. Redistributions of source code must retain copyright 12 | statements and notices. Redistributions must also contain a 13 | copy of this document. 14 | 15 | 2. Redistributions in binary form must reproduce the 16 | above copyright notice, this list of conditions and the 17 | following disclaimer in the documentation and/or other 18 | materials provided with the distribution. 19 | 20 | 3. The name "classworlds" must not be used to endorse or promote 21 | products derived from this Software without prior written 22 | permission of The Werken Company. For written permission, 23 | please contact bob@werken.com. 24 | 25 | 4. Products derived from this Software may not be called "classworlds" 26 | nor may "classworlds" appear in their names without prior written 27 | permission of The Werken Company. "classworlds" is a registered 28 | trademark of The Werken Company. 29 | 30 | 5. Due credit should be given to The Werken Company. 31 | (http://classworlds.werken.com/). 32 | 33 | THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 34 | ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 35 | NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 36 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 37 | THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 38 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 39 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 40 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 42 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 43 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 44 | OF THE POSSIBILITY OF SUCH DAMAGE. 45 | 46 | */ 47 | 48 | /** 49 | * Indicates an error during Launcher configuration. 50 | * 51 | * @author bob mcwhirter 52 | */ 53 | @Deprecated 54 | public class ConfigurationException extends Exception 55 | { 56 | /** 57 | * Construct. 58 | * 59 | * @param msg The message. 60 | */ 61 | public ConfigurationException( String msg ) 62 | { 63 | super( msg ); 64 | } 65 | 66 | /** 67 | * Construct. 68 | * 69 | * @param msg The message. 70 | * @param lineNo The number of configuraton line where the problem occured. 71 | * @param line The configuration line where the problem occured. 72 | */ 73 | public ConfigurationException( String msg, int lineNo, String line ) 74 | { 75 | super( msg + " (" + lineNo + "): " + line ); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/Configurator.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.FileNotFoundException; 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.net.MalformedURLException; 23 | 24 | /** 25 | * A compatibility wrapper for org.codehaus.plexus.classworlds.launcher.Configurator 26 | * provided for legacy code 27 | * 28 | * @author Andrew Williams 29 | */ 30 | @Deprecated 31 | public class Configurator 32 | { 33 | private ConfiguratorAdapter config; 34 | 35 | /** Construct. 36 | * 37 | * @param launcher The launcher to configure. 38 | */ 39 | public Configurator( Launcher launcher ) 40 | { 41 | config = ConfiguratorAdapter.getInstance( 42 | new org.codehaus.plexus.classworlds.launcher.Configurator( launcher ), launcher ); 43 | } 44 | 45 | /** Construct. 46 | * 47 | * @param world The classWorld to configure. 48 | */ 49 | public Configurator( ClassWorld world ) 50 | { 51 | config = ConfiguratorAdapter.getInstance( 52 | new org.codehaus.plexus.classworlds.launcher.Configurator( 53 | ClassWorldReverseAdapter.getInstance( world ) ), world ); 54 | } 55 | 56 | /** set world. 57 | * this setter is provided so you can use the same configurator to configure several "worlds" 58 | * 59 | * @param world The classWorld to configure. 60 | */ 61 | public void setClassWorld( ClassWorld world ) 62 | { 63 | config.setClassWorld( world ); 64 | } 65 | 66 | /** 67 | * Configure from a file. 68 | * 69 | * @param is The config input stream 70 | * @throws IOException If an error occurs reading the config file. 71 | * @throws MalformedURLException If the config file contains invalid URLs. 72 | * @throws ConfigurationException If the config file is corrupt. 73 | * @throws DuplicateRealmException If the config file defines two realms with the same id. 74 | * @throws NoSuchRealmException If the config file defines a main entry point in 75 | * a non-existent realm. 76 | */ 77 | public void configure( InputStream is ) 78 | throws IOException, MalformedURLException, ConfigurationException, DuplicateRealmException, NoSuchRealmException 79 | { 80 | config.configureAdapter( is ); 81 | } 82 | 83 | /** 84 | * Associate parent realms with their children. 85 | */ 86 | protected void associateRealms() 87 | { 88 | config.associateRealms(); 89 | } 90 | 91 | /** 92 | * Load a glob into the specified classloader. 93 | * 94 | * @param line The path configuration line. 95 | * @param realm The realm to populate 96 | * @throws MalformedURLException If the line does not represent 97 | * a valid path element. 98 | * @throws FileNotFoundException If the line does not represent 99 | * a valid path element in the filesystem. 100 | */ 101 | protected void loadGlob( String line, ClassRealm realm ) 102 | throws MalformedURLException, FileNotFoundException 103 | { 104 | loadGlob( line, realm, false ); 105 | } 106 | 107 | /** 108 | * Load a glob into the specified classloader. 109 | * 110 | * @param line The path configuration line. 111 | * @param realm The realm to populate 112 | * @param optionally Whether the path is optional or required 113 | * @throws MalformedURLException If the line does not represent 114 | * a valid path element. 115 | * @throws FileNotFoundException If the line does not represent 116 | * a valid path element in the filesystem. 117 | */ 118 | protected void loadGlob( String line, ClassRealm realm, boolean optionally ) 119 | throws MalformedURLException, FileNotFoundException 120 | { 121 | config.loadGlob( line, realm, optionally ); 122 | } 123 | 124 | /** 125 | * Filter a string for system properties. 126 | * 127 | * @param text The text to filter. 128 | * @return The filtered text. 129 | * @throws ConfigurationException If the property does not 130 | * exist or if there is a syntax error. 131 | */ 132 | protected String filter( String text ) 133 | throws ConfigurationException 134 | { 135 | return config.filter( text ); 136 | } 137 | 138 | } 139 | 140 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/ConfiguratorAdapter.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.InputStream; 20 | import java.io.IOException; 21 | import java.net.MalformedURLException; 22 | 23 | /** 24 | * Created by IntelliJ IDEA. 25 | * 26 | * @uthor: Andrew Williams 27 | * @since: Nov 25, 2006 28 | */ 29 | @Deprecated 30 | public class ConfiguratorAdapter 31 | extends Configurator 32 | { 33 | 34 | public static ConfiguratorAdapter getInstance( org.codehaus.plexus.classworlds.launcher.Configurator newConfig, 35 | Launcher launcher ) 36 | { 37 | ConfiguratorAdapter adapter = new ConfiguratorAdapter( newConfig, launcher ); 38 | 39 | return adapter; 40 | } 41 | 42 | public static ConfiguratorAdapter getInstance( org.codehaus.plexus.classworlds.launcher.Configurator newConfig, 43 | ClassWorld world ) 44 | { 45 | ConfiguratorAdapter adapter = new ConfiguratorAdapter( newConfig, world ); 46 | 47 | return adapter; 48 | } 49 | 50 | private org.codehaus.plexus.classworlds.launcher.Configurator config; 51 | 52 | private ConfiguratorAdapter( org.codehaus.plexus.classworlds.launcher.Configurator config, Launcher launcher ) 53 | { 54 | super( launcher ); 55 | this.config = config; 56 | } 57 | 58 | private ConfiguratorAdapter( org.codehaus.plexus.classworlds.launcher.Configurator config, ClassWorld world ) 59 | { 60 | super( world ); 61 | this.config = config; 62 | } 63 | 64 | public void associateRealms() 65 | { 66 | config.associateRealms(); 67 | } 68 | 69 | public void configureAdapter( InputStream is ) 70 | throws IOException, MalformedURLException, ConfigurationException, DuplicateRealmException, NoSuchRealmException 71 | { 72 | try 73 | { 74 | config.configure( is ); 75 | } 76 | catch ( org.codehaus.plexus.classworlds.launcher.ConfigurationException e ) 77 | { 78 | throw new ConfigurationException( e.getMessage() ); 79 | } 80 | catch ( org.codehaus.plexus.classworlds.realm.DuplicateRealmException e ) 81 | { 82 | throw new DuplicateRealmException( ClassWorldAdapter.getInstance( e.getWorld() ), e.getId() ); 83 | } 84 | catch ( org.codehaus.plexus.classworlds.realm.NoSuchRealmException e ) 85 | { 86 | throw new NoSuchRealmException( ClassWorldAdapter.getInstance( e.getWorld() ), e.getId() ); 87 | } 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/DefaultClassRealm.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * A compatibility wrapper for org.codehaus.plexus.classworlds.realm.ClassRealm 21 | * provided for legacy code 22 | * 23 | * @author Andrew Williams 24 | * @version $Id$ 25 | */ 26 | 27 | import java.io.File; 28 | import java.io.IOException; 29 | import java.io.InputStream; 30 | import java.net.URL; 31 | import java.util.Enumeration; 32 | 33 | @Deprecated 34 | public class DefaultClassRealm 35 | implements ClassRealm 36 | { 37 | private ClassRealmAdapter adapter; 38 | 39 | public DefaultClassRealm( ClassWorld world, String id ) 40 | { 41 | this( world, id, null ); 42 | } 43 | 44 | public DefaultClassRealm( ClassWorld world, String id, ClassLoader foreignClassLoader ) 45 | { 46 | this.adapter = ClassRealmAdapter.getInstance( 47 | new org.codehaus.plexus.classworlds.realm.ClassRealm( 48 | ClassWorldReverseAdapter.getInstance( world ), id, foreignClassLoader ) ); 49 | } 50 | 51 | public URL[] getConstituents() 52 | { 53 | return adapter.getConstituents(); 54 | } 55 | 56 | public ClassRealm getParent() 57 | { 58 | return adapter.getParentRealm(); 59 | } 60 | 61 | public void setParent( ClassRealm parent ) 62 | { 63 | adapter.setParent( parent ); 64 | } 65 | 66 | public String getId() 67 | { 68 | return adapter.getId(); 69 | } 70 | 71 | public ClassWorld getWorld() 72 | { 73 | return adapter.getWorld(); 74 | } 75 | 76 | public void importFrom( String realmId, String packageName ) 77 | throws NoSuchRealmException 78 | { 79 | adapter.importFrom( realmId, packageName ); 80 | } 81 | 82 | public void addConstituent( URL constituent ) 83 | { 84 | adapter.addConstituent( constituent ); 85 | } 86 | 87 | /** 88 | * Adds a byte[] class definition as a constituent for locating classes. 89 | * Currently uses BytesURLStreamHandler to hold a reference of the byte[] in memory. 90 | * This ensures we have a unifed URL resource model for all constituents. 91 | * The code to cache to disk is commented out - maybe a property to choose which method? 92 | * 93 | * @param constituent class name 94 | * @param b the class definition as a byte[] 95 | */ 96 | public void addConstituent(String constituent, 97 | byte[] b) throws ClassNotFoundException 98 | { 99 | try 100 | { 101 | File path, file; 102 | if (constituent.lastIndexOf('.') != -1) 103 | { 104 | path = new File("byteclass/" + constituent.substring(0, constituent.lastIndexOf('.') + 1).replace('.', File.separatorChar)); 105 | 106 | file = new File(path, constituent.substring(constituent.lastIndexOf('.') + 1) + ".class"); 107 | } 108 | else 109 | { 110 | path = new File("byteclass/"); 111 | 112 | file = new File(path, constituent + ".class"); 113 | } 114 | 115 | addConstituent( new URL( null, 116 | file.toURI().toURL().toExternalForm(), 117 | new BytesURLStreamHandler(b) ) ); 118 | } 119 | catch (java.io.IOException e) 120 | { 121 | throw new ClassNotFoundException( "Couldn't load byte stream.", e ); 122 | } 123 | } 124 | 125 | public ClassRealm locateSourceRealm( String classname ) 126 | { 127 | return adapter.locateSourceRealm( classname ); 128 | } 129 | 130 | public ClassLoader getClassLoader() 131 | { 132 | return adapter.getClassLoader(); 133 | } 134 | 135 | public ClassRealm createChildRealm( String id ) 136 | throws DuplicateRealmException 137 | { 138 | return adapter.createChildRealm( id ); 139 | } 140 | 141 | // ---------------------------------------------------------------------- 142 | // ClassLoader API 143 | // ---------------------------------------------------------------------- 144 | 145 | public Class loadClass( String name ) 146 | throws ClassNotFoundException 147 | { 148 | return adapter.loadClass( name ); 149 | } 150 | 151 | public URL getResource( String name ) 152 | { 153 | return adapter.getResource( name ); 154 | } 155 | 156 | public InputStream getResourceAsStream( String name ) 157 | { 158 | return adapter.getResourceAsStream( name ); 159 | } 160 | 161 | public Enumeration findResources(String name) 162 | throws IOException 163 | { 164 | return adapter.findResources( name ); 165 | } 166 | 167 | public void display() 168 | { 169 | adapter.display(); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/DuplicateRealmException.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | 5 | Copyright 2002 (C) The Werken Company. All Rights Reserved. 6 | 7 | Redistribution and use of this software and associated documentation 8 | ("Software"), with or without modification, are permitted provided 9 | that the following conditions are met: 10 | 11 | 1. Redistributions of source code must retain copyright 12 | statements and notices. Redistributions must also contain a 13 | copy of this document. 14 | 15 | 2. Redistributions in binary form must reproduce the 16 | above copyright notice, this list of conditions and the 17 | following disclaimer in the documentation and/or other 18 | materials provided with the distribution. 19 | 20 | 3. The name "classworlds" must not be used to endorse or promote 21 | products derived from this Software without prior written 22 | permission of The Werken Company. For written permission, 23 | please contact bob@werken.com. 24 | 25 | 4. Products derived from this Software may not be called "classworlds" 26 | nor may "classworlds" appear in their names without prior written 27 | permission of The Werken Company. "classworlds" is a registered 28 | trademark of The Werken Company. 29 | 30 | 5. Due credit should be given to The Werken Company. 31 | (http://classworlds.werken.com/). 32 | 33 | THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 34 | ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 35 | NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 36 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 37 | THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 38 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 39 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 40 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 42 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 43 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 44 | OF THE POSSIBILITY OF SUCH DAMAGE. 45 | 46 | */ 47 | 48 | /** 49 | * Indicates an attempt to add a ClassRealm to a 50 | * ClassWorld with a duplicate id. 51 | * 52 | * @author bob mcwhirter 53 | */ 54 | @Deprecated 55 | public class DuplicateRealmException extends ClassWorldException 56 | { 57 | // ------------------------------------------------------------ 58 | // Instance members 59 | // ------------------------------------------------------------ 60 | 61 | /** 62 | * The realm id. 63 | */ 64 | private String id; 65 | 66 | // ------------------------------------------------------------ 67 | // Constructors 68 | // ------------------------------------------------------------ 69 | 70 | /** 71 | * Construct. 72 | * 73 | * @param world The world. 74 | * @param id The realm id. 75 | */ 76 | public DuplicateRealmException( ClassWorld world, 77 | String id ) 78 | { 79 | super( world, id ); 80 | this.id = id; 81 | } 82 | 83 | // ------------------------------------------------------------ 84 | // Instance methods 85 | // ------------------------------------------------------------ 86 | 87 | /** 88 | * Retrieve the duplicate realm id. 89 | * 90 | * @return The id. 91 | */ 92 | public String getId() 93 | { 94 | return this.id; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/Launcher.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * A compatibility wrapper for org.codehaus.plexus.classworlds.launcher.Launcher 21 | * provided for legacy code 22 | * 23 | * @author Andrew Williams 24 | */ 25 | @Deprecated 26 | public class Launcher 27 | extends org.codehaus.plexus.classworlds.launcher.Launcher 28 | { 29 | public Launcher() 30 | { 31 | } 32 | 33 | 34 | // ------------------------------------------------------------ 35 | // Class methods 36 | // ------------------------------------------------------------ 37 | 38 | /** 39 | * Launch the launcher from the command line. 40 | * Will exit using System.exit with an exit code of 0 for success, 100 if there was an unknown exception, 41 | * or some other code for an application error. 42 | * 43 | * @param args The application command-line arguments. 44 | */ 45 | public static void main( String[] args ) 46 | { 47 | org.codehaus.plexus.classworlds.launcher.Launcher.main( args ); 48 | } 49 | 50 | /** 51 | * Launch the launcher. 52 | * 53 | * @param args The application command-line arguments. 54 | * @return an integer exit code 55 | * @throws Exception If an error occurs. 56 | */ 57 | public static int mainWithExitCode( String[] args ) 58 | throws Exception 59 | { 60 | return org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode( args ); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/classworlds/NoSuchRealmException.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.classworlds; 2 | 3 | /* 4 | 5 | Copyright 2002 (C) The Werken Company. All Rights Reserved. 6 | 7 | Redistribution and use of this software and associated documentation 8 | ("Software"), with or without modification, are permitted provided 9 | that the following conditions are met: 10 | 11 | 1. Redistributions of source code must retain copyright 12 | statements and notices. Redistributions must also contain a 13 | copy of this document. 14 | 15 | 2. Redistributions in binary form must reproduce the 16 | above copyright notice, this list of conditions and the 17 | following disclaimer in the documentation and/or other 18 | materials provided with the distribution. 19 | 20 | 3. The name "classworlds" must not be used to endorse or promote 21 | products derived from this Software without prior written 22 | permission of The Werken Company. For written permission, 23 | please contact bob@werken.com. 24 | 25 | 4. Products derived from this Software may not be called "classworlds" 26 | nor may "classworlds" appear in their names without prior written 27 | permission of The Werken Company. "classworlds" is a registered 28 | trademark of The Werken Company. 29 | 30 | 5. Due credit should be given to The Werken Company. 31 | (http://classworlds.werken.com/). 32 | 33 | THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 34 | ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 35 | NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 36 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 37 | THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 38 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 39 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 40 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 42 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 43 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 44 | OF THE POSSIBILITY OF SUCH DAMAGE. 45 | 46 | */ 47 | 48 | /** 49 | * Indicates an attempt to retrieve a ClassRealm from a 50 | * ClassWorld with an invalid id. 51 | * 52 | * @author bob mcwhirter 53 | */ 54 | @Deprecated 55 | public class NoSuchRealmException extends ClassWorldException 56 | { 57 | // ------------------------------------------------------------ 58 | // Instance members 59 | // ------------------------------------------------------------ 60 | 61 | /** 62 | * The realm id. 63 | */ 64 | private String id; 65 | 66 | // ------------------------------------------------------------ 67 | // Constructors 68 | // ------------------------------------------------------------ 69 | 70 | /** 71 | * Construct. 72 | * 73 | * @param world The world. 74 | * @param id The realm id. 75 | */ 76 | public NoSuchRealmException( ClassWorld world, 77 | String id ) 78 | { 79 | super( world, id ); 80 | this.id = id; 81 | } 82 | 83 | // ------------------------------------------------------------ 84 | // Instance methods 85 | // ------------------------------------------------------------ 86 | 87 | /** 88 | * Retrieve the invalid realm id. 89 | * 90 | * @return The id. 91 | */ 92 | public String getId() 93 | { 94 | return this.id; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/ClassWorld.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.Closeable; 20 | import java.io.IOException; 21 | import java.util.ArrayList; 22 | import java.util.Collection; 23 | import java.util.Collections; 24 | import java.util.LinkedHashMap; 25 | import java.util.List; 26 | import java.util.Map; 27 | 28 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 29 | import org.codehaus.plexus.classworlds.realm.DuplicateRealmException; 30 | import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; 31 | 32 | /** 33 | * A collection of ClassRealms, indexed by id. 34 | * 35 | * @author bob mcwhirter 36 | */ 37 | public class ClassWorld 38 | { 39 | private Map realms; 40 | 41 | private final List listeners = new ArrayList(); 42 | 43 | public ClassWorld( String realmId, ClassLoader classLoader ) 44 | { 45 | this(); 46 | 47 | try 48 | { 49 | newRealm( realmId, classLoader ); 50 | } 51 | catch ( DuplicateRealmException e ) 52 | { 53 | // Will never happen as we are just creating the world. 54 | } 55 | } 56 | 57 | public ClassWorld() 58 | { 59 | this.realms = new LinkedHashMap(); 60 | } 61 | 62 | public ClassRealm newRealm( String id ) 63 | throws DuplicateRealmException 64 | { 65 | return newRealm( id, getClass().getClassLoader() ); 66 | } 67 | 68 | public synchronized ClassRealm newRealm( String id, ClassLoader classLoader ) 69 | throws DuplicateRealmException 70 | { 71 | if ( realms.containsKey( id ) ) 72 | { 73 | throw new DuplicateRealmException( this, id ); 74 | } 75 | 76 | ClassRealm realm; 77 | 78 | realm = new ClassRealm( this, id, classLoader ); 79 | 80 | realms.put( id, realm ); 81 | 82 | for ( ClassWorldListener listener : listeners ) 83 | { 84 | listener.realmCreated( realm ); 85 | } 86 | 87 | return realm; 88 | } 89 | 90 | public synchronized void disposeRealm( String id ) 91 | throws NoSuchRealmException 92 | { 93 | ClassRealm realm = (ClassRealm) realms.remove( id ); 94 | 95 | if ( realm != null ) 96 | { 97 | closeIfJava7( realm ); 98 | for ( ClassWorldListener listener : listeners ) 99 | { 100 | listener.realmDisposed( realm ); 101 | } 102 | } 103 | } 104 | 105 | private void closeIfJava7( ClassRealm realm ) 106 | { 107 | try 108 | { 109 | //noinspection ConstantConditions 110 | if ( realm instanceof Closeable ) 111 | { 112 | //noinspection RedundantCast 113 | ( (Closeable) realm ).close(); 114 | } 115 | } 116 | catch ( IOException ignore ) 117 | { 118 | } 119 | } 120 | 121 | public synchronized ClassRealm getRealm( String id ) 122 | throws NoSuchRealmException 123 | { 124 | if ( realms.containsKey( id ) ) 125 | { 126 | return (ClassRealm) realms.get( id ); 127 | } 128 | 129 | throw new NoSuchRealmException( this, id ); 130 | } 131 | 132 | public synchronized Collection getRealms() 133 | { 134 | return Collections.unmodifiableList( new ArrayList( realms.values() ) ); 135 | } 136 | 137 | // from exports branch 138 | public synchronized ClassRealm getClassRealm( String id ) 139 | { 140 | if ( realms.containsKey( id ) ) 141 | { 142 | return realms.get( id ); 143 | } 144 | 145 | return null; 146 | } 147 | 148 | public synchronized void addListener( ClassWorldListener listener ) 149 | { 150 | // TODO ideally, use object identity, not equals 151 | if ( !listeners.contains( listener ) ) 152 | { 153 | listeners.add( listener ); 154 | } 155 | } 156 | 157 | public synchronized void removeListener( ClassWorldListener listener ) 158 | { 159 | listeners.remove( listener ); 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/ClassWorldException.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Base exception for ClassWorld errors. 21 | * 22 | * @author bob mcwhirter 23 | */ 24 | public class ClassWorldException 25 | extends Exception 26 | { 27 | // ------------------------------------------------------------ 28 | // Instance members 29 | // ------------------------------------------------------------ 30 | 31 | /** 32 | * The world. 33 | */ 34 | private ClassWorld world; 35 | 36 | // ------------------------------------------------------------ 37 | // Constructors 38 | // ------------------------------------------------------------ 39 | 40 | /** 41 | * Construct. 42 | * 43 | * @param world The world. 44 | */ 45 | public ClassWorldException( final ClassWorld world ) 46 | { 47 | this.world = world; 48 | } 49 | 50 | /** 51 | * Construct. 52 | * 53 | * @param world The world. 54 | * @param msg The detail message. 55 | */ 56 | public ClassWorldException( final ClassWorld world, final String msg ) 57 | { 58 | super( msg ); 59 | this.world = world; 60 | } 61 | 62 | // ------------------------------------------------------------ 63 | // Instance methods 64 | // ------------------------------------------------------------ 65 | 66 | /** 67 | * Retrieve the world. 68 | * 69 | * @return The world. 70 | */ 71 | public ClassWorld getWorld() 72 | { 73 | return this.world; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/ClassWorldListener.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 20 | 21 | public interface ClassWorldListener 22 | { 23 | public void realmCreated( ClassRealm realm ); 24 | 25 | public void realmDisposed( ClassRealm realm ); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/UrlUtils.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.net.URL; 20 | import java.net.URLClassLoader; 21 | import java.util.HashSet; 22 | import java.util.Set; 23 | 24 | /** 25 | * @author Jason van Zyl 26 | */ 27 | public class UrlUtils 28 | { 29 | public static String normalizeUrlPath( String name ) 30 | { 31 | if ( name.startsWith( "/" ) ) 32 | { 33 | name = name.substring( 1 ); 34 | } 35 | 36 | // Looking for org/codehaus/werkflow/personality/basic/../common/core-idioms.xml 37 | // | i | 38 | // +-------+ remove 39 | // 40 | int i = name.indexOf( "/.." ); 41 | 42 | // Can't be at the beginning because we have no root to refer to so 43 | // we start at 1. 44 | if ( i > 0 ) 45 | { 46 | int j = name.lastIndexOf( "/", i - 1 ); 47 | 48 | if ( j >= 0 ) 49 | { 50 | name = name.substring( 0, j ) + name.substring( i + 3 ); 51 | } 52 | } 53 | 54 | return name; 55 | } 56 | 57 | public static Set getURLs( URLClassLoader loader ) 58 | { 59 | Set ret = new HashSet(); 60 | 61 | for ( URL url : loader.getURLs() ) 62 | { 63 | ret.add( url ); 64 | } 65 | 66 | return ret; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/launcher/ConfigurationException.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.launcher; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Indicates an error during Launcher configuration. 21 | * 22 | * @author bob mcwhirter 23 | */ 24 | public class ConfigurationException 25 | extends Exception 26 | { 27 | /** 28 | * Construct. 29 | * 30 | * @param msg The message. 31 | */ 32 | public ConfigurationException( String msg ) 33 | { 34 | super( msg ); 35 | } 36 | 37 | /** 38 | * Construct. 39 | * 40 | * @param msg The message. 41 | * @param lineNo The number of configuraton line where the problem occured. 42 | * @param line The configuration line where the problem occured. 43 | */ 44 | public ConfigurationException( String msg, int lineNo, String line ) 45 | { 46 | super( msg + " (" + lineNo + "): " + line ); 47 | } 48 | 49 | protected ConfigurationException( Exception cause ) 50 | { 51 | super( cause ); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/launcher/ConfigurationHandler.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.launcher; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.File; 20 | import java.net.URL; 21 | 22 | import org.codehaus.plexus.classworlds.realm.DuplicateRealmException; 23 | import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; 24 | 25 | /** 26 | * Receive notification of the logical content of launcher configuration, independently from parsing. 27 | * 28 | * @author Igor Fedorenko 29 | */ 30 | public interface ConfigurationHandler 31 | { 32 | 33 | /** 34 | * Define the main class name 35 | * @param mainClassName the main class name 36 | * @param mainRealmName the main realm from which the main class is loaded 37 | */ 38 | void setAppMain( String mainClassName, String mainRealmName ); 39 | 40 | /** 41 | * Define a new realm 42 | * @param realmName the new realm name 43 | * @throws DuplicateRealmException 44 | */ 45 | void addRealm( String realmName ) 46 | throws DuplicateRealmException; 47 | 48 | /** 49 | * Add an import specification from a realm 50 | * @param relamName the realm name 51 | * @param importSpec the import specification 52 | * @throws NoSuchRealmException 53 | */ 54 | void addImportFrom( String relamName, String importSpec ) 55 | throws NoSuchRealmException; 56 | 57 | /** 58 | * Add a file to the realm 59 | * @param file the file to load content from 60 | */ 61 | void addLoadFile( File file ); 62 | 63 | /** 64 | * Add an URL to the realm 65 | * @param url the url to load content from 66 | */ 67 | void addLoadURL( URL url ); 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/launcher/ConfigurationParser.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.launcher; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.BufferedReader; 20 | import java.io.File; 21 | import java.io.FileInputStream; 22 | import java.io.FileNotFoundException; 23 | import java.io.FilenameFilter; 24 | import java.io.IOException; 25 | import java.io.InputStream; 26 | import java.io.InputStreamReader; 27 | import java.net.MalformedURLException; 28 | import java.net.URL; 29 | import java.util.Properties; 30 | 31 | import org.codehaus.plexus.classworlds.realm.DuplicateRealmException; 32 | import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; 33 | 34 | /** 35 | * Event based launcher configuration parser, delegating effective configuration handling to ConfigurationHandler. 36 | * 37 | * @author bob mcwhirter 38 | * @author Jason van Zyl 39 | * @author Igor Fedorenko 40 | * @see ConfigurationHandler 41 | */ 42 | public class ConfigurationParser 43 | { 44 | public static final String MAIN_PREFIX = "main is"; 45 | 46 | public static final String SET_PREFIX = "set"; 47 | 48 | public static final String IMPORT_PREFIX = "import"; 49 | 50 | public static final String LOAD_PREFIX = "load"; 51 | 52 | /** 53 | * Optionally spec prefix. 54 | */ 55 | public static final String OPTIONALLY_PREFIX = "optionally"; 56 | 57 | private ConfigurationHandler handler; 58 | 59 | private Properties systemProperties; 60 | 61 | public ConfigurationParser( ConfigurationHandler handler, Properties systemProperties ) 62 | { 63 | this.handler = handler; 64 | this.systemProperties = systemProperties; 65 | } 66 | 67 | /** 68 | * Parse launcher configuration file and send events to the handler. 69 | */ 70 | public void parse( InputStream is ) 71 | throws IOException, ConfigurationException, DuplicateRealmException, NoSuchRealmException 72 | { 73 | BufferedReader reader = new BufferedReader( new InputStreamReader( is, "UTF-8" ) ); 74 | 75 | String line = null; 76 | 77 | int lineNo = 0; 78 | 79 | boolean mainSet = false; 80 | 81 | String curRealm = null; 82 | 83 | while ( true ) 84 | { 85 | line = reader.readLine(); 86 | 87 | if ( line == null ) 88 | { 89 | break; 90 | } 91 | 92 | ++lineNo; 93 | line = line.trim(); 94 | 95 | if ( canIgnore( line ) ) 96 | { 97 | continue; 98 | } 99 | 100 | if ( line.startsWith( MAIN_PREFIX ) ) 101 | { 102 | if ( mainSet ) 103 | { 104 | throw new ConfigurationException( "Duplicate main configuration", lineNo, line ); 105 | } 106 | 107 | String conf = line.substring( MAIN_PREFIX.length() ).trim(); 108 | 109 | int fromLoc = conf.indexOf( "from" ); 110 | 111 | if ( fromLoc < 0 ) 112 | { 113 | throw new ConfigurationException( "Missing from clause", lineNo, line ); 114 | } 115 | 116 | String mainClassName = filter( conf.substring( 0, fromLoc ).trim() ); 117 | 118 | String mainRealmName = filter( conf.substring( fromLoc + 4 ).trim() ); 119 | 120 | this.handler.setAppMain( mainClassName, mainRealmName ); 121 | 122 | mainSet = true; 123 | } 124 | else if ( line.startsWith( SET_PREFIX ) ) 125 | { 126 | String conf = line.substring( SET_PREFIX.length() ).trim(); 127 | 128 | int usingLoc = conf.indexOf( " using" ) + 1; 129 | 130 | String property = null; 131 | 132 | String propertiesFileName = null; 133 | 134 | if ( usingLoc > 0 ) 135 | { 136 | property = conf.substring( 0, usingLoc ).trim(); 137 | 138 | propertiesFileName = filter( conf.substring( usingLoc + 5 ).trim() ); 139 | 140 | conf = propertiesFileName; 141 | } 142 | 143 | String defaultValue = null; 144 | 145 | int defaultLoc = conf.indexOf( " default" ) + 1; 146 | 147 | if ( defaultLoc > 0 ) 148 | { 149 | defaultValue = filter( conf.substring( defaultLoc + 7 ).trim() ); 150 | 151 | if ( property == null ) 152 | { 153 | property = conf.substring( 0, defaultLoc ).trim(); 154 | } 155 | else 156 | { 157 | propertiesFileName = conf.substring( 0, defaultLoc ).trim(); 158 | } 159 | } 160 | 161 | String value = systemProperties.getProperty( property ); 162 | 163 | if ( value != null ) 164 | { 165 | continue; 166 | } 167 | 168 | if ( propertiesFileName != null ) 169 | { 170 | File propertiesFile = new File( propertiesFileName ); 171 | 172 | if ( propertiesFile.exists() ) 173 | { 174 | Properties properties = new Properties(); 175 | 176 | try 177 | { 178 | properties.load( new FileInputStream( propertiesFileName ) ); 179 | 180 | value = properties.getProperty( property ); 181 | } 182 | catch ( Exception e ) 183 | { 184 | // do nothing 185 | } 186 | } 187 | } 188 | 189 | if ( value == null && defaultValue != null ) 190 | { 191 | value = defaultValue; 192 | } 193 | 194 | if ( value != null ) 195 | { 196 | value = filter( value ); 197 | systemProperties.setProperty( property, value ); 198 | } 199 | } 200 | else if ( line.startsWith( "[" ) ) 201 | { 202 | int rbrack = line.indexOf( "]" ); 203 | 204 | if ( rbrack < 0 ) 205 | { 206 | throw new ConfigurationException( "Invalid realm specifier", lineNo, line ); 207 | } 208 | 209 | String realmName = line.substring( 1, rbrack ); 210 | 211 | handler.addRealm( realmName ); 212 | 213 | curRealm = realmName; 214 | } 215 | else if ( line.startsWith( IMPORT_PREFIX ) ) 216 | { 217 | if ( curRealm == null ) 218 | { 219 | throw new ConfigurationException( "Unhandled import", lineNo, line ); 220 | } 221 | 222 | String conf = line.substring( IMPORT_PREFIX.length() ).trim(); 223 | 224 | int fromLoc = conf.indexOf( "from" ); 225 | 226 | if ( fromLoc < 0 ) 227 | { 228 | throw new ConfigurationException( "Missing from clause", lineNo, line ); 229 | } 230 | 231 | String importSpec = conf.substring( 0, fromLoc ).trim(); 232 | 233 | String relamName = conf.substring( fromLoc + 4 ).trim(); 234 | 235 | handler.addImportFrom( relamName, importSpec ); 236 | 237 | } 238 | else if ( line.startsWith( LOAD_PREFIX ) ) 239 | { 240 | String constituent = line.substring( LOAD_PREFIX.length() ).trim(); 241 | 242 | constituent = filter( constituent ); 243 | 244 | if ( constituent.indexOf( "*" ) >= 0 ) 245 | { 246 | loadGlob( constituent, false /*not optionally*/ ); 247 | } 248 | else 249 | { 250 | File file = new File( constituent ); 251 | 252 | if ( file.exists() ) 253 | { 254 | handler.addLoadFile( file ); 255 | } 256 | else 257 | { 258 | try 259 | { 260 | handler.addLoadURL( new URL( constituent ) ); 261 | } 262 | catch ( MalformedURLException e ) 263 | { 264 | throw new FileNotFoundException( constituent ); 265 | } 266 | } 267 | } 268 | } 269 | else if ( line.startsWith( OPTIONALLY_PREFIX ) ) 270 | { 271 | String constituent = line.substring( OPTIONALLY_PREFIX.length() ).trim(); 272 | 273 | constituent = filter( constituent ); 274 | 275 | if ( constituent.indexOf( "*" ) >= 0 ) 276 | { 277 | loadGlob( constituent, true /*optionally*/ ); 278 | } 279 | else 280 | { 281 | File file = new File( constituent ); 282 | 283 | if ( file.exists() ) 284 | { 285 | handler.addLoadFile( file ); 286 | } 287 | else 288 | { 289 | try 290 | { 291 | handler.addLoadURL( new URL( constituent ) ); 292 | } 293 | catch ( MalformedURLException e ) 294 | { 295 | // swallow 296 | } 297 | } 298 | } 299 | } 300 | else 301 | { 302 | throw new ConfigurationException( "Unhandled configuration", lineNo, line ); 303 | } 304 | } 305 | 306 | reader.close(); 307 | } 308 | 309 | /** 310 | * Load a glob into the specified classloader. 311 | * 312 | * @param line The path configuration line. 313 | * @param optionally Whether the path is optional or required 314 | * @throws MalformedURLException If the line does not represent 315 | * a valid path element. 316 | * @throws FileNotFoundException If the line does not represent 317 | * a valid path element in the filesystem. 318 | * @throws ConfigurationException 319 | */ 320 | protected void loadGlob( String line, 321 | boolean optionally ) 322 | throws MalformedURLException, FileNotFoundException, ConfigurationException 323 | { 324 | File globFile = new File( line ); 325 | 326 | File dir = globFile.getParentFile(); 327 | if ( !dir.exists() ) 328 | { 329 | if ( optionally ) 330 | { 331 | return; 332 | } 333 | else 334 | { 335 | throw new FileNotFoundException( dir.toString() ); 336 | } 337 | } 338 | 339 | String localName = globFile.getName(); 340 | 341 | int starLoc = localName.indexOf( "*" ); 342 | 343 | final String prefix = localName.substring( 0, starLoc ); 344 | 345 | final String suffix = localName.substring( starLoc + 1 ); 346 | 347 | File[] matches = dir.listFiles( new FilenameFilter() 348 | { 349 | public boolean accept( File dir, 350 | String name ) 351 | { 352 | if ( !name.startsWith( prefix ) ) 353 | { 354 | return false; 355 | } 356 | 357 | if ( !name.endsWith( suffix ) ) 358 | { 359 | return false; 360 | } 361 | 362 | return true; 363 | } 364 | } ); 365 | 366 | for ( File match : matches ) 367 | { 368 | handler.addLoadFile( match ); 369 | } 370 | } 371 | 372 | /** 373 | * Filter a string for system properties. 374 | * 375 | * @param text The text to filter. 376 | * @return The filtered text. 377 | * @throws ConfigurationException If the property does not 378 | * exist or if there is a syntax error. 379 | */ 380 | protected String filter( String text ) 381 | throws ConfigurationException 382 | { 383 | String result = ""; 384 | 385 | int cur = 0; 386 | int textLen = text.length(); 387 | 388 | int propStart = -1; 389 | int propStop = -1; 390 | 391 | String propName = null; 392 | String propValue = null; 393 | 394 | while ( cur < textLen ) 395 | { 396 | propStart = text.indexOf( "${", cur ); 397 | 398 | if ( propStart < 0 ) 399 | { 400 | break; 401 | } 402 | 403 | result += text.substring( cur, propStart ); 404 | 405 | propStop = text.indexOf( "}", propStart ); 406 | 407 | if ( propStop < 0 ) 408 | { 409 | throw new ConfigurationException( "Unterminated property: " + text.substring( propStart ) ); 410 | } 411 | 412 | propName = text.substring( propStart + 2, propStop ); 413 | 414 | propValue = systemProperties.getProperty( propName ); 415 | 416 | /* do our best if we are not running from surefire */ 417 | if ( propName.equals( "basedir" ) && ( propValue == null || propValue.equals( "" ) ) ) 418 | { 419 | propValue = ( new File( "" ) ).getAbsolutePath(); 420 | 421 | } 422 | 423 | if ( propValue == null ) 424 | { 425 | throw new ConfigurationException( "No such property: " + propName ); 426 | } 427 | result += propValue; 428 | 429 | cur = propStop + 1; 430 | } 431 | 432 | result += text.substring( cur ); 433 | 434 | return result; 435 | } 436 | 437 | /** 438 | * Determine if a line can be ignored because it is 439 | * a comment or simply blank. 440 | * 441 | * @param line The line to test. 442 | * @return true if the line is ignorable, 443 | * otherwise false. 444 | */ 445 | private boolean canIgnore( String line ) 446 | { 447 | return ( line.length() == 0 || line.startsWith( "#" ) ); 448 | } 449 | } 450 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/launcher/Configurator.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.launcher; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.File; 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.net.MalformedURLException; 23 | import java.net.URL; 24 | import java.util.ArrayList; 25 | import java.util.Collections; 26 | import java.util.Comparator; 27 | import java.util.HashMap; 28 | import java.util.List; 29 | import java.util.Map; 30 | 31 | import org.codehaus.plexus.classworlds.ClassWorld; 32 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 33 | import org.codehaus.plexus.classworlds.realm.DuplicateRealmException; 34 | import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; 35 | 36 | /** 37 | * Launcher configurator. 38 | * 39 | * @author bob mcwhirter 40 | * @author Jason van Zyl 41 | */ 42 | public class Configurator implements ConfigurationHandler 43 | { 44 | /** 45 | * The launcher to configure. 46 | */ 47 | private Launcher launcher; 48 | 49 | private ClassWorld world; 50 | 51 | /** 52 | * Processed Realms. 53 | */ 54 | private Map configuredRealms; 55 | 56 | /** 57 | * Current Realm. 58 | */ 59 | private ClassRealm curRealm; 60 | 61 | private ClassLoader foreignClassLoader = null; 62 | 63 | /** 64 | * Construct. 65 | * 66 | * @param launcher The launcher to configure. 67 | */ 68 | public Configurator( Launcher launcher ) 69 | { 70 | this.launcher = launcher; 71 | 72 | configuredRealms = new HashMap(); 73 | 74 | if ( launcher != null ) 75 | { 76 | this.foreignClassLoader = launcher.getSystemClassLoader(); 77 | } 78 | } 79 | 80 | /** 81 | * Construct. 82 | * 83 | * @param world The classWorld to configure. 84 | */ 85 | public Configurator( ClassWorld world ) 86 | { 87 | setClassWorld( world ); 88 | } 89 | 90 | /** 91 | * set world. 92 | * this setter is provided so you can use the same configurator to configure several "worlds" 93 | * 94 | * @param world The classWorld to configure. 95 | */ 96 | public void setClassWorld( ClassWorld world ) 97 | { 98 | this.world = world; 99 | 100 | configuredRealms = new HashMap(); 101 | } 102 | 103 | /** 104 | * Configure from a file. 105 | * 106 | * @param is The config input stream 107 | * @throws IOException If an error occurs reading the config file. 108 | * @throws MalformedURLException If the config file contains invalid URLs. 109 | * @throws ConfigurationException If the config file is corrupt. 110 | * @throws org.codehaus.plexus.classworlds.realm.DuplicateRealmException If the config file defines two realms with the same id. 111 | * @throws org.codehaus.plexus.classworlds.realm.NoSuchRealmException If the config file defines a main entry point in 112 | * a non-existent realm. 113 | */ 114 | public void configure( InputStream is ) 115 | throws IOException, ConfigurationException, DuplicateRealmException, NoSuchRealmException 116 | { 117 | if ( world == null ) 118 | { 119 | world = new ClassWorld(); 120 | } 121 | 122 | curRealm = null; 123 | 124 | foreignClassLoader = null; 125 | 126 | if ( this.launcher != null ) 127 | { 128 | foreignClassLoader = this.launcher.getSystemClassLoader(); 129 | } 130 | 131 | ConfigurationParser parser = new ConfigurationParser( this, System.getProperties() ); 132 | 133 | parser.parse( is ); 134 | 135 | // Associate child realms to their parents. 136 | associateRealms(); 137 | 138 | if ( this.launcher != null ) 139 | { 140 | this.launcher.setWorld( world ); 141 | } 142 | 143 | } 144 | 145 | // TODO return this to protected when the legacy wrappers can be removed. 146 | /** 147 | * Associate parent realms with their children. 148 | */ 149 | public void associateRealms() 150 | { 151 | List sortRealmNames = new ArrayList( configuredRealms.keySet() ); 152 | 153 | // sort by name 154 | Comparator comparator = new Comparator() 155 | { 156 | public int compare( String g1, 157 | String g2 ) 158 | { 159 | return g1.compareTo( g2 ); 160 | } 161 | }; 162 | 163 | Collections.sort( sortRealmNames, comparator ); 164 | 165 | // So now we have something like the following for defined 166 | // realms: 167 | // 168 | // root 169 | // root.maven 170 | // root.maven.plugin 171 | // 172 | // Now if the name of a realm is a superset of an existing realm 173 | // the we want to make child/parent associations. 174 | 175 | for ( String realmName : sortRealmNames ) 176 | { 177 | int j = realmName.lastIndexOf( '.' ); 178 | 179 | if ( j > 0 ) 180 | { 181 | String parentRealmName = realmName.substring( 0, j ); 182 | 183 | ClassRealm parentRealm = configuredRealms.get( parentRealmName ); 184 | 185 | if ( parentRealm != null ) 186 | { 187 | ClassRealm realm = configuredRealms.get( realmName ); 188 | 189 | realm.setParentRealm( parentRealm ); 190 | } 191 | } 192 | } 193 | } 194 | 195 | public void addImportFrom( String relamName, String importSpec ) 196 | throws NoSuchRealmException 197 | { 198 | curRealm.importFrom( relamName, importSpec ); 199 | } 200 | 201 | public void addLoadFile( File file ) 202 | { 203 | try 204 | { 205 | curRealm.addURL( file.toURI().toURL() ); 206 | } 207 | catch ( MalformedURLException e ) 208 | { 209 | // can't really happen... or can it? 210 | } 211 | } 212 | 213 | public void addLoadURL( URL url ) 214 | { 215 | curRealm.addURL( url ); 216 | } 217 | 218 | public void addRealm( String realmName ) 219 | throws DuplicateRealmException 220 | { 221 | curRealm = world.newRealm( realmName, foreignClassLoader ); 222 | 223 | // Stash the configured realm for subsequent association processing. 224 | configuredRealms.put( realmName, curRealm ); 225 | } 226 | 227 | public void setAppMain( String mainClassName, String mainRealmName ) 228 | { 229 | if ( this.launcher != null ) 230 | { 231 | this.launcher.setAppMain( mainClassName, mainRealmName ); 232 | } 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/realm/DuplicateRealmException.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.realm; 2 | 3 | import org.codehaus.plexus.classworlds.ClassWorld; 4 | import org.codehaus.plexus.classworlds.ClassWorldException; 5 | 6 | /* 7 | * Copyright 2001-2006 Codehaus Foundation. 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | */ 21 | 22 | /** 23 | * Indicates an attempt to add a ClassRealm to a 24 | * ClassWorld with a duplicate id. 25 | * 26 | * @author bob mcwhirter 27 | */ 28 | public class DuplicateRealmException 29 | extends ClassWorldException 30 | { 31 | // ------------------------------------------------------------ 32 | // Instance members 33 | // ------------------------------------------------------------ 34 | 35 | /** 36 | * The realm id. 37 | */ 38 | private String id; 39 | 40 | // ------------------------------------------------------------ 41 | // Constructors 42 | // ------------------------------------------------------------ 43 | 44 | /** 45 | * Construct. 46 | * 47 | * @param world The world. 48 | * @param id The realm id. 49 | */ 50 | public DuplicateRealmException( ClassWorld world, String id ) 51 | { 52 | super( world, id ); 53 | this.id = id; 54 | } 55 | 56 | // ------------------------------------------------------------ 57 | // Instance methods 58 | // ------------------------------------------------------------ 59 | 60 | /** 61 | * Retrieve the duplicate realm id. 62 | * 63 | * @return The id. 64 | */ 65 | public String getId() 66 | { 67 | return this.id; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/realm/Entry.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.realm; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Import description entry. 21 | * 22 | * @author bob mcwhirter 23 | */ 24 | class Entry 25 | implements Comparable 26 | { 27 | 28 | final ClassLoader classLoader; 29 | 30 | final String pkgName; 31 | 32 | Entry( ClassLoader realm, String pkgName ) 33 | { 34 | this.classLoader = realm; 35 | 36 | this.pkgName = pkgName; 37 | } 38 | 39 | // ------------------------------------------------------------ 40 | // Instance methods 41 | // ------------------------------------------------------------ 42 | 43 | /** 44 | * Retrieve the class loader. 45 | * 46 | * @return The class loader. 47 | */ 48 | ClassLoader getClassLoader() 49 | { 50 | return this.classLoader; 51 | } 52 | 53 | /** 54 | * Retrieve the package name. 55 | * 56 | * @return The package name. 57 | */ 58 | String getPackageName() 59 | { 60 | return this.pkgName; 61 | } 62 | 63 | /** 64 | * Determine if the class/resource name matches the package 65 | * described by this entry. 66 | * 67 | * @param name The class or resource name to test, must not be null. 68 | * @return true if this entry matches the 69 | * classname, otherwise false. 70 | */ 71 | boolean matches( String name ) 72 | { 73 | String pkg = getPackageName(); 74 | 75 | if ( pkg.endsWith( ".*" ) ) 76 | { 77 | String pkgName; 78 | 79 | if ( name.indexOf( '/' ) < 0 ) 80 | { 81 | // a binary class name, e.g. java.lang.Object 82 | 83 | int index = name.lastIndexOf( '.' ); 84 | pkgName = ( index < 0 ) ? "" : name.substring( 0, index ); 85 | } 86 | else 87 | { 88 | // a resource name, e.g. java/lang/Object.class 89 | 90 | int index = name.lastIndexOf( '/' ); 91 | pkgName = ( index < 0 ) ? "" : name.substring( 0, index ).replace( '/', '.' ); 92 | } 93 | 94 | return pkgName.length() == pkg.length() - 2 && pkg.regionMatches( 0, pkgName, 0, pkgName.length() ); 95 | } 96 | else if ( pkg.length() > 0 ) 97 | { 98 | if ( name.indexOf( '/' ) < 0 ) 99 | { 100 | // a binary class name, e.g. java.lang.Object 101 | 102 | if ( name.startsWith( pkg ) ) 103 | { 104 | if ( name.length() == pkg.length() ) 105 | { 106 | // exact match of class name 107 | return true; 108 | } 109 | else if ( name.charAt( pkg.length() ) == '.' ) 110 | { 111 | // prefix match of package name 112 | return true; 113 | } 114 | else if ( name.charAt( pkg.length() ) == '$' ) 115 | { 116 | // prefix match of enclosing type 117 | return true; 118 | } 119 | } 120 | } 121 | else 122 | { 123 | // a resource name, e.g. java/lang/Object.class 124 | 125 | if ( name.equals( pkg ) ) 126 | { 127 | // exact match of resource name 128 | return true; 129 | } 130 | 131 | pkg = pkg.replace( '.', '/' ); 132 | 133 | if ( name.startsWith( pkg ) && name.length() > pkg.length() ) 134 | { 135 | if ( name.charAt( pkg.length() ) == '/' ) 136 | { 137 | // prefix match of package directory 138 | return true; 139 | } 140 | else if ( name.charAt( pkg.length() ) == '$' ) 141 | { 142 | // prefix match of nested class file 143 | return true; 144 | } 145 | else if ( name.length() == pkg.length() + 6 && name.endsWith( ".class" ) ) 146 | { 147 | // exact match of class file 148 | return true; 149 | } 150 | } 151 | } 152 | 153 | return false; 154 | } 155 | else 156 | { 157 | return true; 158 | } 159 | } 160 | 161 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 162 | // java.lang.Comparable 163 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 164 | 165 | /** 166 | * Compare this entry to another for relative ordering. 167 | *

168 | *

169 | * The natural ordering of Entry objects is reverse-alphabetical 170 | * based upon package name. 171 | *

172 | * 173 | * @param thatObj The object to compare. 174 | * @return -1 if this object sorts before that object, 0 175 | * if they are equal, or 1 if this object sorts 176 | * after that object. 177 | */ 178 | public int compareTo( Entry that ) 179 | { 180 | // We are reverse sorting this list, so that 181 | // we get longer matches first: 182 | // 183 | // com.werken.foo.bar 184 | // com.werken.foo 185 | // com.werken 186 | 187 | return - ( getPackageName().compareTo( that.getPackageName() ) ); 188 | } 189 | 190 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 191 | // java.lang.Object 192 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 193 | 194 | /** 195 | * Test this entry for equality to another. 196 | *

197 | *

198 | * Consistent with {@link #compareTo}, this method tests 199 | * for equality purely on the package name. 200 | *

201 | * 202 | * @param thatObj The object to compare 203 | * @return true if the two objects are 204 | * semantically equivalent, otherwise false. 205 | */ 206 | public boolean equals( Object thatObj ) 207 | { 208 | Entry that = (Entry) thatObj; 209 | 210 | return getPackageName().equals( that.getPackageName() ); 211 | } 212 | 213 | /** 214 | *

215 | * Consistent with {@link #equals}, this method creates a hashCode 216 | * based on the packagename. 217 | *

218 | */ 219 | public int hashCode() 220 | { 221 | return getPackageName().hashCode(); 222 | } 223 | 224 | public String toString() 225 | { 226 | return "Entry[import " + getPackageName() + " from realm " + getClassLoader() + "]"; 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/realm/NoSuchRealmException.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.realm; 2 | 3 | import org.codehaus.plexus.classworlds.ClassWorld; 4 | import org.codehaus.plexus.classworlds.ClassWorldException; 5 | 6 | /* 7 | * Copyright 2001-2006 Codehaus Foundation. 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | */ 21 | 22 | /** 23 | * Indicates an attempt to retrieve a ClassRealm from a 24 | * ClassWorld with an invalid id. 25 | * 26 | * @author bob mcwhirter 27 | */ 28 | public class NoSuchRealmException 29 | extends ClassWorldException 30 | { 31 | // ------------------------------------------------------------ 32 | // Instance members 33 | // ------------------------------------------------------------ 34 | 35 | /** 36 | * The realm id. 37 | */ 38 | private String id; 39 | 40 | // ------------------------------------------------------------ 41 | // Constructors 42 | // ------------------------------------------------------------ 43 | 44 | /** 45 | * Construct. 46 | * 47 | * @param world The world. 48 | * @param id The realm id. 49 | */ 50 | public NoSuchRealmException( ClassWorld world, String id ) 51 | { 52 | super( world, id ); 53 | this.id = id; 54 | } 55 | 56 | // ------------------------------------------------------------ 57 | // Instance methods 58 | // ------------------------------------------------------------ 59 | 60 | /** 61 | * Retrieve the invalid realm id. 62 | * 63 | * @return The id. 64 | */ 65 | public String getId() 66 | { 67 | return this.id; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/strategy/AbstractStrategy.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.strategy; 2 | 3 | import java.net.URL; 4 | import java.util.Collection; 5 | import java.util.Collections; 6 | import java.util.Enumeration; 7 | import java.util.LinkedHashSet; 8 | 9 | import org.codehaus.plexus.classworlds.UrlUtils; 10 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 11 | 12 | /* 13 | * Copyright 2001-2006 Codehaus Foundation. 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); 16 | * you may not use this file except in compliance with the License. 17 | * You may obtain a copy of the License at 18 | * 19 | * http://www.apache.org/licenses/LICENSE-2.0 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, 23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 24 | * See the License for the specific language governing permissions and 25 | * limitations under the License. 26 | */ 27 | 28 | /** 29 | * @author Jason van Zyl 30 | */ 31 | public abstract class AbstractStrategy 32 | implements Strategy 33 | { 34 | 35 | protected ClassRealm realm; 36 | 37 | public AbstractStrategy( ClassRealm realm ) 38 | { 39 | this.realm = realm; 40 | } 41 | 42 | protected String getNormalizedResource( String name ) 43 | { 44 | return UrlUtils.normalizeUrlPath( name ); 45 | } 46 | 47 | protected Enumeration combineResources( Enumeration en1, Enumeration en2, Enumeration en3 ) 48 | { 49 | Collection urls = new LinkedHashSet(); 50 | 51 | addAll( urls, en1 ); 52 | addAll( urls, en2 ); 53 | addAll( urls, en3 ); 54 | 55 | return Collections.enumeration( urls ); 56 | } 57 | 58 | private void addAll( Collection target, Enumeration en ) 59 | { 60 | if ( en != null ) 61 | { 62 | while ( en.hasMoreElements() ) 63 | { 64 | target.add( en.nextElement() ); 65 | } 66 | } 67 | } 68 | 69 | public ClassRealm getRealm() 70 | { 71 | return realm; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/strategy/OsgiBundleStrategy.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.strategy; 2 | 3 | /* 4 | * Copyright 2001-2010 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.IOException; 20 | import java.net.URL; 21 | import java.util.Enumeration; 22 | 23 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 24 | 25 | public class OsgiBundleStrategy 26 | extends AbstractStrategy 27 | { 28 | 29 | // java.* from parent 30 | // imported packages [Import-Package header with explicit constraints on the exporter] 31 | // requires bundle [Required-Bundle] 32 | // self [Bundle-Classpath header] 33 | // attached fragments 34 | // 35 | // We need to trya and be OSGi r4 compliant in the loading of all the bundles so that we can try to 36 | // load eclipse without requiring equinox. Or any other OSGi container for that matter. 37 | public OsgiBundleStrategy( ClassRealm realm ) 38 | { 39 | super( realm ); 40 | } 41 | 42 | public Class loadClass( String name ) 43 | throws ClassNotFoundException 44 | { 45 | Class clazz = realm.loadClassFromImport( name ); 46 | 47 | if ( clazz == null ) 48 | { 49 | clazz = realm.loadClassFromSelf( name ); 50 | 51 | if ( clazz == null ) 52 | { 53 | clazz = realm.loadClassFromParent( name ); 54 | 55 | if ( clazz == null ) 56 | { 57 | throw new ClassNotFoundException( name ); 58 | } 59 | } 60 | } 61 | 62 | return clazz; 63 | } 64 | 65 | public URL getResource( String name ) 66 | { 67 | URL resource = realm.loadResourceFromImport( name ); 68 | 69 | if ( resource == null ) 70 | { 71 | resource = realm.loadResourceFromSelf( name ); 72 | 73 | if ( resource == null ) 74 | { 75 | resource = realm.loadResourceFromParent( name ); 76 | } 77 | } 78 | 79 | return resource; 80 | } 81 | 82 | public Enumeration getResources( String name ) 83 | throws IOException 84 | { 85 | Enumeration imports = realm.loadResourcesFromImport( name ); 86 | Enumeration self = realm.loadResourcesFromSelf( name ); 87 | Enumeration parent = realm.loadResourcesFromParent( name ); 88 | 89 | return combineResources( imports, self, parent ); 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/strategy/ParentFirstStrategy.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.strategy; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed under the License 12 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 13 | * or implied. See the License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | import java.io.IOException; 18 | import java.net.URL; 19 | import java.util.Enumeration; 20 | 21 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 22 | 23 | /** 24 | * @author Jason van Zyl 25 | */ 26 | public class ParentFirstStrategy 27 | extends AbstractStrategy 28 | { 29 | 30 | public ParentFirstStrategy( ClassRealm realm ) 31 | { 32 | super( realm ); 33 | } 34 | 35 | public Class loadClass( String name ) 36 | throws ClassNotFoundException 37 | { 38 | Class clazz = realm.loadClassFromImport( name ); 39 | 40 | if ( clazz == null ) 41 | { 42 | clazz = realm.loadClassFromParent( name ); 43 | 44 | if ( clazz == null ) 45 | { 46 | clazz = realm.loadClassFromSelf( name ); 47 | 48 | if ( clazz == null ) 49 | { 50 | throw new ClassNotFoundException( name ); 51 | } 52 | } 53 | } 54 | 55 | return clazz; 56 | } 57 | 58 | public URL getResource( String name ) 59 | { 60 | URL resource = realm.loadResourceFromImport( name ); 61 | 62 | if ( resource == null ) 63 | { 64 | resource = realm.loadResourceFromParent( name ); 65 | 66 | if ( resource == null ) 67 | { 68 | resource = realm.loadResourceFromSelf( name ); 69 | } 70 | } 71 | 72 | return resource; 73 | } 74 | 75 | public Enumeration getResources( String name ) 76 | throws IOException 77 | { 78 | Enumeration imports = realm.loadResourcesFromImport( name ); 79 | Enumeration parent = realm.loadResourcesFromParent( name ); 80 | Enumeration self = realm.loadResourcesFromSelf( name ); 81 | 82 | return combineResources( imports, parent, self ); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/strategy/SelfFirstStrategy.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.strategy; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed under the License 12 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 13 | * or implied. See the License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | import java.io.IOException; 18 | import java.net.URL; 19 | import java.util.Enumeration; 20 | 21 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 22 | 23 | /** 24 | * @author Jason van Zyl 25 | */ 26 | public class SelfFirstStrategy 27 | extends AbstractStrategy 28 | { 29 | 30 | public SelfFirstStrategy( ClassRealm realm ) 31 | { 32 | super( realm ); 33 | } 34 | 35 | public Class loadClass( String name ) 36 | throws ClassNotFoundException 37 | { 38 | Class clazz = realm.loadClassFromImport( name ); 39 | 40 | if ( clazz == null ) 41 | { 42 | clazz = realm.loadClassFromSelf( name ); 43 | 44 | if ( clazz == null ) 45 | { 46 | clazz = realm.loadClassFromParent( name ); 47 | 48 | if ( clazz == null ) 49 | { 50 | throw new ClassNotFoundException( name ); 51 | } 52 | } 53 | } 54 | 55 | return clazz; 56 | } 57 | 58 | public URL getResource( String name ) 59 | { 60 | URL resource = realm.loadResourceFromImport( name ); 61 | 62 | if ( resource == null ) 63 | { 64 | resource = realm.loadResourceFromSelf( name ); 65 | 66 | if ( resource == null ) 67 | { 68 | resource = realm.loadResourceFromParent( name ); 69 | } 70 | } 71 | 72 | return resource; 73 | } 74 | 75 | public Enumeration getResources( String name ) 76 | throws IOException 77 | { 78 | Enumeration imports = realm.loadResourcesFromImport( name ); 79 | Enumeration self = realm.loadResourcesFromSelf( name ); 80 | Enumeration parent = realm.loadResourcesFromParent( name ); 81 | 82 | return combineResources( imports, self, parent ); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/strategy/Strategy.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.strategy; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.IOException; 20 | import java.net.URL; 21 | import java.util.Enumeration; 22 | 23 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 24 | 25 | /** 26 | * A strategy is a class for defining how classes and resources are located 27 | * in classworlds. 28 | * 29 | * @uthor: Andrew Williams 30 | * @since: Nov 19, 2006 31 | */ 32 | public interface Strategy 33 | { 34 | 35 | Class loadClass( String name ) 36 | throws ClassNotFoundException; 37 | 38 | URL getResource( String name ); 39 | 40 | Enumeration getResources( String name ) 41 | throws IOException; 42 | 43 | ClassRealm getRealm(); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/codehaus/plexus/classworlds/strategy/StrategyFactory.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.strategy; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 20 | 21 | /** 22 | * StrategyFactory loads a strategy, either default or from a given hint. 23 | * 24 | * @uthor: Andrew Williams 25 | * @since: Nov 19, 2006 26 | */ 27 | public class StrategyFactory 28 | { 29 | 30 | public static Strategy getStrategy( ClassRealm realm ) 31 | { 32 | return getStrategy( realm, "default" ); 33 | } 34 | 35 | public static Strategy getStrategy( ClassRealm realm, String hint ) 36 | { 37 | // TODO: Here we shall check hint to load non-default strategies 38 | 39 | return new SelfFirstStrategy( realm ); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Plexus Classworlds 5 | 6 | 7 | org.apache.maven.skins 8 | maven-fluido-skin 9 | 1.3.0 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/AbstractClassWorldsTestCase.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds; 2 | 3 | import java.net.URL; 4 | 5 | import junit.framework.TestCase; 6 | 7 | /* 8 | * Copyright 2001-2006 Codehaus Foundation. 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | */ 22 | 23 | /** 24 | * @author Jason van Zyl 25 | */ 26 | public abstract class AbstractClassWorldsTestCase 27 | extends TestCase 28 | { 29 | 30 | public AbstractClassWorldsTestCase( String string ) 31 | { 32 | super( string ); 33 | } 34 | 35 | protected URL getJarUrl( String jarName ) 36 | { 37 | return TestUtil.getTestResourceUrl( jarName ); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/ClassView.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | public class ClassView 20 | { 21 | /** 22 | * * Formats Class information for debug output purposes. 23 | * * 24 | * * @param clz the Class to print information for 25 | * * 26 | * * @return a String describing the Class in detail 27 | */ 28 | public static String toString( Class clz ) 29 | { 30 | if ( clz.isPrimitive() ) 31 | { 32 | return clz.toString(); 33 | } 34 | else if ( clz.isArray() ) 35 | { 36 | return "Array of " + toString( clz.getComponentType() ); 37 | } 38 | else if ( clz.isInterface() ) 39 | { 40 | return toInterfaceString( clz, "" ); 41 | } 42 | else 43 | { 44 | return toClassString( clz, "" ); 45 | } 46 | } 47 | 48 | /** 49 | * * Formats Class information for debug output purposes. 50 | * * 51 | * * @param clz the Class to print information for 52 | * * @param sIndent the indentation to precede each line of output 53 | * * 54 | * * @return a String describing the Class in detail 55 | */ 56 | private static String toClassString( Class clz, String sIndent ) 57 | { 58 | StringBuffer sb = new StringBuffer(); 59 | sb.append( sIndent ) 60 | .append( "Class " ) 61 | .append( clz.getName() ) 62 | .append( " (" ) 63 | .append( toString( clz.getClassLoader() ) ) 64 | .append( ')' ); 65 | 66 | sIndent += " "; 67 | 68 | Class[] aclz = clz.getInterfaces(); 69 | for ( int i = 0, c = aclz.length; i < c; ++i ) 70 | { 71 | sb.append( '\n' ) 72 | .append( toInterfaceString( aclz[i], sIndent ) ); 73 | } 74 | 75 | clz = clz.getSuperclass(); 76 | if ( clz != null ) 77 | { 78 | sb.append( '\n' ) 79 | .append( toClassString( clz, sIndent ) ); 80 | } 81 | 82 | return sb.toString(); 83 | } 84 | 85 | /** 86 | * * Formats interface information for debug output purposes. 87 | * * 88 | * * @param clz the interface Class to print information for 89 | * * @param sIndent the indentation to precede each line of output 90 | * * 91 | * * @return a String describing the interface Class in detail 92 | */ 93 | private static String toInterfaceString( Class clz, String sIndent ) 94 | { 95 | StringBuffer sb = new StringBuffer(); 96 | sb.append( sIndent ) 97 | .append( "Interface " ) 98 | .append( clz.getName() ) 99 | .append( " (" ) 100 | .append( toString( clz.getClassLoader() ) ) 101 | .append( ')' ); 102 | 103 | Class[] aclz = clz.getInterfaces(); 104 | for ( int i = 0, c = aclz.length; i < c; ++i ) 105 | { 106 | clz = aclz[i]; 107 | 108 | sb.append( '\n' ) 109 | .append( toInterfaceString( clz, sIndent + " " ) ); 110 | } 111 | 112 | return sb.toString(); 113 | } 114 | 115 | /** 116 | * * Format a description for the specified ClassLoader object. 117 | * * 118 | * * @param loader the ClassLoader instance (or null) 119 | * * 120 | * * @return a String description of the ClassLoader 121 | */ 122 | private static String toString( ClassLoader loader ) 123 | { 124 | if ( loader == null ) 125 | { 126 | return "System ClassLoader"; 127 | } 128 | 129 | return "ClassLoader class=" + loader.getClass().getName() + ", hashCode=" + loader.hashCode(); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/ClassWorldTest.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.net.URL; 20 | import java.net.URLClassLoader; 21 | import java.util.Enumeration; 22 | 23 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 24 | import org.codehaus.plexus.classworlds.realm.DuplicateRealmException; 25 | import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; 26 | 27 | public class ClassWorldTest 28 | extends AbstractClassWorldsTestCase 29 | { 30 | private ClassWorld world; 31 | 32 | public ClassWorldTest( String name ) 33 | { 34 | super( name ); 35 | } 36 | 37 | public void setUp() 38 | { 39 | this.world = new ClassWorld(); 40 | } 41 | 42 | public void tearDown() 43 | { 44 | this.world = null; 45 | } 46 | 47 | public void testEmpty() 48 | { 49 | assertTrue( this.world.getRealms().isEmpty() ); 50 | } 51 | 52 | public void testNewRealm() 53 | throws Exception 54 | { 55 | ClassRealm realm = this.world.newRealm( "foo" ); 56 | 57 | assertNotNull( realm ); 58 | } 59 | 60 | public void testGetRealm() 61 | throws Exception 62 | { 63 | ClassRealm realm = this.world.newRealm( "foo" ); 64 | 65 | assertSame( realm, this.world.getRealm( "foo" ) ); 66 | } 67 | 68 | public void testNewRealm_Duplicate() 69 | throws Exception 70 | { 71 | try 72 | { 73 | this.world.newRealm( "foo" ); 74 | this.world.newRealm( "foo" ); 75 | 76 | fail( "throw DuplicateRealmException" ); 77 | } 78 | catch ( DuplicateRealmException e ) 79 | { 80 | // expected and correct 81 | 82 | assertSame( this.world, e.getWorld() ); 83 | 84 | assertEquals( "foo", e.getId() ); 85 | } 86 | } 87 | 88 | public void testGetRealm_NoSuch() 89 | throws Exception 90 | { 91 | try 92 | { 93 | this.world.getRealm( "foo" ); 94 | fail( "throw NoSuchRealmException" ); 95 | } 96 | catch ( NoSuchRealmException e ) 97 | { 98 | // expected and correct 99 | 100 | assertSame( this.world, e.getWorld() ); 101 | 102 | assertEquals( "foo", e.getId() ); 103 | } 104 | } 105 | 106 | public void testGetRealms() 107 | throws Exception 108 | { 109 | assertTrue( this.world.getRealms().isEmpty() ); 110 | 111 | ClassRealm foo = this.world.newRealm( "foo" ); 112 | 113 | assertEquals( 1, this.world.getRealms().size() ); 114 | 115 | assertTrue( this.world.getRealms().contains( foo ) ); 116 | 117 | ClassRealm bar = this.world.newRealm( "bar" ); 118 | 119 | assertEquals( 2, this.world.getRealms().size() ); 120 | 121 | assertTrue( this.world.getRealms().contains( bar ) ); 122 | } 123 | 124 | public void testPLX334() 125 | throws Exception 126 | { 127 | ClassLoader loader = new URLClassLoader( new URL[] { getJarUrl( "component1-1.0.jar" ) } ); 128 | world.newRealm( "netbeans", loader ); 129 | ClassRealm plexus = world.newRealm( "plexus" ); 130 | plexus.importFrom( "netbeans", "META-INF/plexus" ); 131 | plexus.importFrom( "netbeans", "org.codehaus.plexus" ); 132 | Enumeration e = plexus.getResources( "META-INF/plexus/components.xml" ); 133 | assertNotNull( e ); 134 | int resourceCount = 0; 135 | for ( Enumeration resources = e; resources.hasMoreElements(); ) 136 | { 137 | URL obj = resources.nextElement(); 138 | assertTrue( obj.getPath().indexOf( "/component1-1.0.jar!/META-INF/plexus/components.xml" ) >= 0 ); 139 | resourceCount++; 140 | } 141 | // assertEquals( 2, resourceCount ); 142 | // for some reason surefire-plugin 2.3 returned 2 items there: 143 | // for example: 144 | //jar:file:/home/mkleint/.m2/repository/org/codehaus/plexus/plexus-archiver/1.0-alpha-7/plexus-archiver-1.0-alpha-7.jar!/META-INF/plexus/components.xml 145 | //jar:file:/home/mkleint/src/plexus-trunk/plexus-classworlds/src/test-jars/component1-1.0.jar!/META-INF/plexus/components.xml 146 | // However only 1 is correct, which is actually returned by the 2.4 surefire-plugin 147 | 148 | assertEquals( 1, resourceCount ); 149 | Class c = plexus.loadClass( "org.codehaus.plexus.Component1" ); 150 | assertNotNull( c ); 151 | 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/TestUtil.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.File; 20 | import java.net.MalformedURLException; 21 | import java.net.URL; 22 | 23 | /** 24 | * @author Ben Walding 25 | */ 26 | public class TestUtil 27 | { 28 | 29 | public static URL getTestResourceUrl( String resourceName ) 30 | { 31 | File baseDir = new File( getBasedir() ); 32 | 33 | File testDir = new File( baseDir, "src/test/test-data" ); 34 | 35 | File resourceFile = new File( testDir, resourceName ); 36 | 37 | try 38 | { 39 | return resourceFile.toURI().toURL(); 40 | } 41 | catch ( MalformedURLException e ) 42 | { 43 | throw new RuntimeException( e ); 44 | } 45 | } 46 | 47 | public static String getBasedir() 48 | { 49 | String basedir = System.getProperty( "basedir" ); 50 | 51 | /* do our best if we are not running from surefire */ 52 | if ( basedir == null || basedir.length() <= 0 ) 53 | { 54 | basedir = ( new File( "" ) ).getAbsolutePath(); 55 | } 56 | 57 | return basedir; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/UrlUtilsTest.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import junit.framework.TestCase; 20 | 21 | public class UrlUtilsTest 22 | extends TestCase 23 | { 24 | 25 | public void testNormalizeUrlPath() 26 | { 27 | assertEquals( "org/codehaus/Test.class", UrlUtils.normalizeUrlPath( "org/codehaus/Test.class" ) ); 28 | assertEquals( "org/Test.class", UrlUtils.normalizeUrlPath( "org/codehaus/../Test.class" ) ); 29 | assertEquals( "../../some.jar/org/Test.class", UrlUtils.normalizeUrlPath( "../../some.jar/org/Test.class" ) ); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/launcher/ConfigurationParserTest.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.launcher; 2 | 3 | import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase; 4 | 5 | public class ConfigurationParserTest 6 | extends AbstractClassWorldsTestCase 7 | { 8 | 9 | ConfigurationParser configurator = new ConfigurationParser( null, System.getProperties() ); 10 | 11 | public ConfigurationParserTest( String string ) 12 | { 13 | super( string ); 14 | } 15 | 16 | public void testFilter_Unterminated() 17 | throws Exception 18 | { 19 | try 20 | { 21 | this.configurator.filter( "${cheese" ); 22 | fail( "throw ConfigurationException" ); 23 | } 24 | catch ( ConfigurationException e ) 25 | { 26 | // expected and correct 27 | assertTrue( e.getMessage().startsWith( "Unterminated" ) ); 28 | } 29 | } 30 | 31 | public void testFilter_Solitary() 32 | throws Exception 33 | { 34 | System.setProperty( "classworlds.test.prop", "test prop value" ); 35 | 36 | String result = this.configurator.filter( "${classworlds.test.prop}" ); 37 | 38 | assertEquals( "test prop value", result ); 39 | } 40 | 41 | public void testFilter_AtStart() 42 | throws Exception 43 | { 44 | System.setProperty( "classworlds.test.prop", "test prop value" ); 45 | 46 | String result = this.configurator.filter( "${classworlds.test.prop}cheese" ); 47 | 48 | assertEquals( "test prop valuecheese", result ); 49 | } 50 | 51 | public void testFilter_AtEnd() 52 | throws Exception 53 | { 54 | System.setProperty( "classworlds.test.prop", "test prop value" ); 55 | 56 | String result = this.configurator.filter( "cheese${classworlds.test.prop}" ); 57 | 58 | assertEquals( "cheesetest prop value", result ); 59 | } 60 | 61 | public void testFilter_Multiple() 62 | throws Exception 63 | { 64 | System.setProperty( "classworlds.test.prop.one", "test prop value one" ); 65 | 66 | System.setProperty( "classworlds.test.prop.two", "test prop value two" ); 67 | 68 | String result = 69 | this.configurator.filter( "I like ${classworlds.test.prop.one} and ${classworlds.test.prop.two} a lot" ); 70 | 71 | assertEquals( "I like test prop value one and test prop value two a lot", result ); 72 | } 73 | 74 | public void testFilter_NonExistent() 75 | throws Exception 76 | { 77 | try 78 | { 79 | this.configurator.filter( "${gollygeewillikers}" ); 80 | fail( "throw ConfigurationException" ); 81 | } 82 | catch ( ConfigurationException e ) 83 | { 84 | // expected and correct 85 | assertTrue( e.getMessage().startsWith( "No such property" ) ); 86 | } 87 | } 88 | 89 | public void testFilter_InMiddle() 90 | throws Exception 91 | { 92 | System.setProperty( "classworlds.test.prop", "test prop value" ); 93 | 94 | String result = this.configurator.filter( "cheese${classworlds.test.prop}toast" ); 95 | 96 | assertEquals( "cheesetest prop valuetoast", result ); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/launcher/ConfiguratorTest.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.launcher; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.File; 20 | import java.io.FileInputStream; 21 | import java.io.FileNotFoundException; 22 | import java.net.URL; 23 | import java.util.Collection; 24 | 25 | import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase; 26 | import org.codehaus.plexus.classworlds.ClassWorld; 27 | import org.codehaus.plexus.classworlds.TestUtil; 28 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 29 | import org.codehaus.plexus.classworlds.realm.DuplicateRealmException; 30 | 31 | public class ConfiguratorTest 32 | extends AbstractClassWorldsTestCase 33 | { 34 | private Launcher launcher; 35 | private Configurator configurator; 36 | 37 | public ConfiguratorTest( String name ) 38 | { 39 | super( name ); 40 | } 41 | 42 | public void setUp() 43 | { 44 | this.launcher = new Launcher(); 45 | this.configurator = new Configurator( this.launcher ); 46 | } 47 | 48 | public void tearDown() 49 | { 50 | this.launcher = null; 51 | this.configurator = null; 52 | System.getProperties().remove( "set.using.existent" ); 53 | System.getProperties().remove( "set.using.default" ); 54 | System.getProperties().remove( "set.using.nonexistent" ); 55 | System.getProperties().remove( "set.using.nonexistent.default" ); 56 | System.getProperties().remove( "set.using.missing" ); 57 | System.getProperties().remove( "set.using.filtered.default" ); 58 | } 59 | 60 | public void testConfigure_Nonexistent() 61 | throws Exception 62 | { 63 | try 64 | { 65 | this.configurator.configure( getConfigPath( "notfound.conf" ) ); 66 | fail( "throw FileNotFoundException" ); 67 | } 68 | catch ( FileNotFoundException e ) 69 | { 70 | // expected and correct 71 | } 72 | } 73 | 74 | public void testConfigure_DuplicateMain() 75 | throws Exception 76 | { 77 | try 78 | { 79 | this.configurator.configure( getConfigPath( "dupe-main.conf" ) ); 80 | fail( "throw ConfigurationException" ); 81 | } 82 | catch ( ConfigurationException e ) 83 | { 84 | // expected and correct 85 | assertTrue( e.getMessage().startsWith( "Duplicate main" ) ); 86 | } 87 | } 88 | 89 | public void testConfigure_DuplicateRealm() 90 | throws Exception 91 | { 92 | try 93 | { 94 | this.configurator.configure( getConfigPath( "dupe-realm.conf" ) ); 95 | fail( "throw DuplicateRealmException" ); 96 | } 97 | catch ( DuplicateRealmException e ) 98 | { 99 | // expected and correct 100 | assertEquals( "dupe.realm", e.getId() ); 101 | } 102 | } 103 | 104 | public void testConfigure_EarlyImport() 105 | throws Exception 106 | { 107 | try 108 | { 109 | this.configurator.configure( getConfigPath( "early-import.conf" ) ); 110 | fail( "throw ConfigurationException" ); 111 | } 112 | catch ( ConfigurationException e ) 113 | { 114 | // expected and correct 115 | assertTrue( e.getMessage().startsWith( "Unhandled import" ) ); 116 | } 117 | } 118 | 119 | public void testConfigure_RealmSyntax() 120 | throws Exception 121 | { 122 | try 123 | { 124 | this.configurator.configure( getConfigPath( "realm-syntax.conf" ) ); 125 | fail( "throw ConfigurationException" ); 126 | } 127 | catch ( ConfigurationException e ) 128 | { 129 | // expected and correct 130 | assertTrue( e.getMessage().startsWith( "Invalid realm" ) ); 131 | } 132 | } 133 | 134 | public void testConfigure_Valid() 135 | throws Exception 136 | { 137 | this.configurator.configure( getConfigPath( "valid.conf" ) ); 138 | 139 | assertEquals( "org.apache.maven.app.App", this.launcher.getMainClassName() ); 140 | 141 | assertEquals( "maven", this.launcher.getMainRealmName() ); 142 | 143 | ClassWorld world = this.launcher.getWorld(); 144 | 145 | Collection realms = world.getRealms(); 146 | 147 | assertEquals( 4, realms.size() ); 148 | 149 | assertNotNull( world.getRealm( "ant" ) ); 150 | assertNotNull( world.getRealm( "maven" ) ); 151 | assertNotNull( world.getRealm( "xml" ) ); 152 | 153 | ClassRealm antRealm = world.getRealm( "ant" ); 154 | ClassRealm mavenRealm = world.getRealm( "maven" ); 155 | ClassRealm xmlRealm = world.getRealm( "xml" ); 156 | ClassRealm globRealm = world.getRealm( "glob" ); 157 | 158 | assertSame( null, antRealm.getImportClassLoader( "org.apache.tools.Ant" ) ); 159 | 160 | assertSame( xmlRealm, antRealm.getImportClassLoader( "org.xml.sax.SAXException" ) ); 161 | 162 | assertSame( null, mavenRealm.getImportClassLoader( "org.apache.maven.app.App" ) ); 163 | 164 | assertSame( xmlRealm, mavenRealm.getImportClassLoader( "org.xml.sax.SAXException" ) ); 165 | 166 | URL[] urls = globRealm.getURLs(); 167 | 168 | String basedir = TestUtil.getBasedir(); 169 | assertArrayContains( urls, new File( basedir, "src/test/test-data/nested.jar" ).toURI().toURL() ); 170 | assertArrayContains( urls, new File( basedir, "src/test/test-data/a.jar" ).toURI().toURL() ); 171 | assertArrayContains( urls, new File( basedir, "src/test/test-data/b.jar" ).toURI().toURL() ); 172 | assertArrayContains( urls, new File( basedir, "src/test/test-data/c.jar" ).toURI().toURL() ); 173 | } 174 | 175 | public void testConfigure_Optionally_NonExistent() 176 | throws Exception 177 | { 178 | this.configurator.configure( getConfigPath( "optionally-nonexistent.conf" ) ); 179 | 180 | assertEquals( "org.apache.maven.app.App", this.launcher.getMainClassName() ); 181 | 182 | assertEquals( "opt", this.launcher.getMainRealmName() ); 183 | 184 | ClassWorld world = this.launcher.getWorld(); 185 | 186 | Collection realms = world.getRealms(); 187 | 188 | assertEquals( 1, realms.size() ); 189 | 190 | assertNotNull( world.getRealm( "opt" ) ); 191 | 192 | ClassRealm optRealm = world.getRealm( "opt" ); 193 | 194 | URL[] urls = optRealm.getURLs(); 195 | 196 | assertEquals( "no urls", 0, urls.length ); 197 | } 198 | 199 | public void testConfigure_Optionally_Existent() 200 | throws Exception 201 | { 202 | this.configurator.configure( getConfigPath( "optionally-existent.conf" ) ); 203 | 204 | assertEquals( "org.apache.maven.app.App", this.launcher.getMainClassName() ); 205 | 206 | assertEquals( "opt", this.launcher.getMainRealmName() ); 207 | 208 | ClassWorld world = this.launcher.getWorld(); 209 | 210 | Collection realms = world.getRealms(); 211 | 212 | assertEquals( 1, realms.size() ); 213 | 214 | assertNotNull( world.getRealm( "opt" ) ); 215 | 216 | ClassRealm optRealm = world.getRealm( "opt" ); 217 | 218 | URL[] urls = optRealm.getURLs(); 219 | 220 | assertEquals( "one url", 1, urls.length ); 221 | 222 | assertSame( null, optRealm.getImportClassLoader( "org.xml.sax.SAXException" ) ); 223 | } 224 | 225 | public void testConfigure_Unhandled() 226 | throws Exception 227 | { 228 | try 229 | { 230 | this.configurator.configure( getConfigPath( "unhandled.conf" ) ); 231 | fail( "throw ConfigurationException" ); 232 | } 233 | catch ( ConfigurationException e ) 234 | { 235 | // expected and correct 236 | assertTrue( e.getMessage().startsWith( "Unhandled configuration" ) ); 237 | } 238 | } 239 | 240 | public void testSet_Using_Existent() 241 | throws Exception 242 | { 243 | assertNull( System.getProperty( "set.using.existent" ) ); 244 | 245 | this.configurator.configure( getConfigPath( "set-using-existent.conf" ) ); 246 | 247 | assertEquals( "testSet_Using_Existent", System.getProperty( "set.using.existent" ) ); 248 | } 249 | 250 | public void testSet_Using_NonExistent() 251 | throws Exception 252 | { 253 | assertNull( System.getProperty( "set.using.nonexistent" ) ); 254 | 255 | this.configurator.configure( getConfigPath( "set-using-nonexistent.conf" ) ); 256 | 257 | assertNull( System.getProperty( "set.using.nonexistent" ) ); 258 | } 259 | 260 | public void testSet_Using_NonExistent_Default() 261 | throws Exception 262 | { 263 | assertNull( System.getProperty( "set.using.nonexistent.default" ) ); 264 | 265 | this.configurator.configure( getConfigPath( "set-using-nonexistent.conf" ) ); 266 | 267 | assertEquals( "testSet_Using_NonExistent_Default", System.getProperty( "set.using.nonexistent.default" ) ); 268 | } 269 | 270 | public void testSet_Using_NonExistent_Override() 271 | throws Exception 272 | { 273 | assertNull( System.getProperty( "set.using.default" ) ); 274 | System.setProperty( "set.using.default", "testSet_Using_NonExistent_Override" ); 275 | 276 | this.configurator.configure( getConfigPath( "set-using-nonexistent.conf" ) ); 277 | 278 | assertEquals( "testSet_Using_NonExistent_Override", System.getProperty( "set.using.default" ) ); 279 | } 280 | 281 | public void testSet_Using_Existent_Override() 282 | throws Exception 283 | { 284 | assertNull( System.getProperty( "set.using.existent" ) ); 285 | System.setProperty( "set.using.existent", "testSet_Using_Existent_Override" ); 286 | 287 | this.configurator.configure( getConfigPath( "set-using-existent.conf" ) ); 288 | 289 | assertEquals( "testSet_Using_Existent_Override", System.getProperty( "set.using.existent" ) ); 290 | } 291 | 292 | public void testSet_Using_Existent_Default() 293 | throws Exception 294 | { 295 | assertNull( System.getProperty( "set.using.default" ) ); 296 | 297 | this.configurator.configure( getConfigPath( "set-using-existent.conf" ) ); 298 | 299 | assertEquals( "testSet_Using_Existent_Default", System.getProperty( "set.using.default" ) ); 300 | } 301 | 302 | public void testSet_Using_Missing_Default() 303 | throws Exception 304 | { 305 | assertNull( System.getProperty( "set.using.missing" ) ); 306 | 307 | this.configurator.configure( getConfigPath( "set-using-missing.conf" ) ); 308 | 309 | assertEquals( "testSet_Using_Missing_Default", System.getProperty( "set.using.missing" ) ); 310 | } 311 | 312 | public void testSet_Using_Missing_Override() 313 | throws Exception 314 | { 315 | assertNull( System.getProperty( "set.using.missing" ) ); 316 | System.setProperty( "set.using.missing", "testSet_Using_Missing_Override" ); 317 | 318 | this.configurator.configure( getConfigPath( "set-using-missing.conf" ) ); 319 | 320 | assertEquals( "testSet_Using_Missing_Override", System.getProperty( "set.using.missing" ) ); 321 | } 322 | 323 | public void testSet_Using_Filtered_Default() 324 | throws Exception 325 | { 326 | assertNull( System.getProperty( "set.using.filtered.default" ) ); 327 | 328 | this.configurator.configure( getConfigPath( "set-using-missing.conf" ) ); 329 | 330 | assertEquals( System.getProperty( "user.home" ) + "/m2", System.getProperty( "set.using.filtered.default" ) ); 331 | } 332 | 333 | private FileInputStream getConfigPath( String name ) 334 | throws Exception 335 | { 336 | return new FileInputStream( 337 | new File( new File( TestUtil.getBasedir(), "src/test/test-data" ), name ) ); 338 | } 339 | 340 | private void assertArrayContains( URL[] array, 341 | URL url ) 342 | throws Exception 343 | { 344 | for ( int i = 0; i < array.length; ++i ) 345 | { 346 | if ( url.equals( array[i] ) ) 347 | { 348 | return; 349 | } 350 | } 351 | fail( "URL (" + url + ") not found in array of URLs" ); 352 | } 353 | } 354 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/launcher/LauncherTest.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.launcher; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.File; 20 | import java.io.FileInputStream; 21 | 22 | import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase; 23 | import org.codehaus.plexus.classworlds.TestUtil; 24 | 25 | public class LauncherTest 26 | extends AbstractClassWorldsTestCase 27 | { 28 | private Launcher launcher; 29 | 30 | public LauncherTest( String name ) 31 | { 32 | super( name ); 33 | } 34 | 35 | public void setUp() 36 | { 37 | System.setProperty( "java.protocol.handler.pkgs", "org.codehaus.classworlds.protocol" ); 38 | 39 | this.launcher = new Launcher(); 40 | 41 | this.launcher.setSystemClassLoader( Thread.currentThread().getContextClassLoader() ); 42 | } 43 | 44 | public void tearDown() 45 | { 46 | this.launcher = null; 47 | } 48 | 49 | public void testConfigure_Valid() 50 | throws Exception 51 | { 52 | launcher.configure( getConfigPath( "valid-launch.conf" ) ); 53 | 54 | Class mainClass = launcher.getMainClass(); 55 | 56 | assertNotNull( mainClass ); 57 | 58 | assertEquals( "a.A", mainClass.getName() ); 59 | 60 | assertEquals( "app", launcher.getMainRealm().getId() ); 61 | } 62 | 63 | public void testLaunch_ValidStandard() 64 | throws Exception 65 | { 66 | launcher.configure( getConfigPath( "valid-launch.conf" ) ); 67 | 68 | launcher.launch( new String[]{} ); 69 | } 70 | 71 | public void testLaunch_ValidStandardExitCode() 72 | throws Exception 73 | { 74 | launcher.configure( getConfigPath( "valid-launch-exitCode.conf" ) ); 75 | 76 | launcher.launch( new String[]{} ); 77 | 78 | assertEquals( "check exit code", 15, launcher.getExitCode() ); 79 | } 80 | 81 | public void testLaunch_ValidEnhanced() 82 | throws Exception 83 | { 84 | launcher.configure( getConfigPath( "valid-enh-launch.conf" ) ); 85 | 86 | launcher.launch( new String[]{} ); 87 | } 88 | 89 | public void testLaunch_ValidEnhancedExitCode() 90 | throws Exception 91 | { 92 | launcher.configure( getConfigPath( "valid-enh-launch-exitCode.conf" ) ); 93 | 94 | launcher.launch( new String[]{} ); 95 | 96 | assertEquals( "check exit code", 45, launcher.getExitCode() ); 97 | } 98 | 99 | public void testLaunch_NoSuchMethod() 100 | throws Exception 101 | { 102 | launcher.configure( getConfigPath( "launch-nomethod.conf" ) ); 103 | 104 | try 105 | { 106 | launcher.launch( new String[]{} ); 107 | fail( "should have thrown NoSuchMethodException" ); 108 | } 109 | catch ( NoSuchMethodException e ) 110 | { 111 | // expected and correct 112 | } 113 | } 114 | 115 | public void testLaunch_ClassNotFound() 116 | throws Exception 117 | { 118 | launcher.configure( getConfigPath( "launch-noclass.conf" ) ); 119 | 120 | try 121 | { 122 | launcher.launch( new String[]{} ); 123 | fail( "throw ClassNotFoundException" ); 124 | } 125 | catch ( ClassNotFoundException e ) 126 | { 127 | // expected and correct 128 | } 129 | } 130 | 131 | private FileInputStream getConfigPath( String name ) 132 | throws Exception 133 | { 134 | String basedir = TestUtil.getBasedir(); 135 | 136 | return new FileInputStream( new File( new File( basedir, "src/test/test-data" ), name ) ); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/realm/DefaultClassRealmTest.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.realm; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.net.URL; 20 | import java.net.URLClassLoader; 21 | import java.util.Collections; 22 | import java.util.concurrent.CountDownLatch; 23 | 24 | import org.codehaus.classworlds.ClassRealmAdapter; 25 | import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase; 26 | import org.codehaus.plexus.classworlds.ClassWorld; 27 | 28 | public class DefaultClassRealmTest 29 | extends AbstractClassWorldsTestCase 30 | { 31 | public DefaultClassRealmTest( String name ) 32 | { 33 | super( name ); 34 | } 35 | 36 | // ---------------------------------------------------------------------- 37 | // Class testing 38 | // ---------------------------------------------------------------------- 39 | 40 | public void testLoadClassFromRealm() 41 | throws Exception 42 | { 43 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "main", null ); 44 | 45 | mainRealm.addURL( getJarUrl( "component0-1.0.jar" ) ); 46 | 47 | loadClass( mainRealm, "org.codehaus.plexus.Component0" ); 48 | } 49 | 50 | public void testLoadClassFromChildRealmWhereClassIsLocatedInParentRealm() 51 | throws Exception 52 | { 53 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "main", null ); 54 | 55 | mainRealm.addURL( getJarUrl( "component0-1.0.jar" ) ); 56 | 57 | ClassRealm childRealm = mainRealm.createChildRealm( "child" ); 58 | 59 | loadClass( childRealm, "org.codehaus.plexus.Component0" ); 60 | } 61 | 62 | public void testLoadClassFromChildRealmWhereClassIsLocatedInGrantParentRealm() 63 | throws Exception 64 | { 65 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "main", null ); 66 | 67 | mainRealm.addURL( getJarUrl( "component0-1.0.jar" ) ); 68 | 69 | ClassRealm childRealm = mainRealm.createChildRealm( "child" ); 70 | 71 | ClassRealm grandchildRealm = childRealm.createChildRealm( "grandchild" ); 72 | 73 | loadClass( grandchildRealm, "org.codehaus.plexus.Component0" ); 74 | } 75 | 76 | public void testLoadClassFromChildRealmWhereClassIsLocatedInBothChildRealmAndParentRealm() 77 | throws Exception 78 | { 79 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "parent", null ); 80 | 81 | mainRealm.addURL( getJarUrl( "component5-1.0.jar" ) ); 82 | 83 | ClassRealm childRealm = mainRealm.createChildRealm( "child" ); 84 | 85 | childRealm.addURL( getJarUrl( "component5-2.0.jar" ) ); 86 | 87 | Class cls = loadClass( childRealm, "test.Component5" ); 88 | 89 | assertSame( childRealm, cls.getClassLoader() ); 90 | assertEquals( 1, cls.getMethods().length ); 91 | assertEquals( "printNew", cls.getMethods()[0].getName() ); 92 | } 93 | 94 | public void testLoadNonExistentClass() 95 | throws Exception 96 | { 97 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "main", null ); 98 | 99 | mainRealm.addURL( getJarUrl( "component0-1.0.jar" ) ); 100 | 101 | try 102 | { 103 | mainRealm.loadClass( "org.foo.bar.NonExistentClass" ); 104 | 105 | fail( "A ClassNotFoundException should have been thrown!" ); 106 | } 107 | catch ( ClassNotFoundException e ) 108 | { 109 | // expected 110 | } 111 | } 112 | 113 | public void testImport() 114 | throws Exception 115 | { 116 | ClassWorld world = new ClassWorld(); 117 | 118 | ClassRealm r0 = world.newRealm( "r0" ); 119 | 120 | ClassRealm r1 = world.newRealm( "r1" ); 121 | 122 | r0.addURL( getJarUrl( "component0-1.0.jar" ) ); 123 | 124 | r1.importFrom( "r0", "org.codehaus.plexus" ); 125 | 126 | loadClass( r1, "org.codehaus.plexus.Component0" ); 127 | } 128 | 129 | public void testParentImport() 130 | throws Exception 131 | { 132 | ClassWorld world = new ClassWorld(); 133 | 134 | ClassRealm parent = world.newRealm( "parent" ); 135 | 136 | ClassRealm child = world.newRealm( "child" ); 137 | 138 | parent.addURL( getJarUrl( "component0-1.0.jar" ) ); 139 | 140 | child.setParentRealm( parent ); 141 | 142 | Class type = loadClass( child, "org.codehaus.plexus.Component0" ); 143 | 144 | child.importFromParent( "non-existing" ); 145 | 146 | assertSame( null, loadClassOrNull( child, "org.codehaus.plexus.Component0" ) ); 147 | 148 | child.importFromParent( "org.codehaus.plexus" ); 149 | 150 | assertSame( type, loadClass( child, "org.codehaus.plexus.Component0" ) ); 151 | } 152 | 153 | public void testLoadClassFromBaseClassLoaderBeforeSelf() 154 | throws Exception 155 | { 156 | ClassWorld world = new ClassWorld(); 157 | 158 | ClassRealm base = world.newRealm( "base" ); 159 | 160 | base.addURL( getJarUrl( "a.jar" ) ); 161 | 162 | ClassRealm child = world.newRealm( "child", base ); 163 | 164 | child.addURL( getJarUrl( "a.jar" ) ); 165 | 166 | Class baseClass = loadClass( base, "a.A" ); 167 | Class childClass = loadClass( child, "a.A" ); 168 | 169 | assertSame( base, baseClass.getClassLoader() ); 170 | assertSame( base, childClass.getClassLoader() ); 171 | assertSame( baseClass, childClass ); 172 | } 173 | 174 | public void testLoadClassFromRealmWithCircularClassReferences() 175 | throws Exception 176 | { 177 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "main", null ); 178 | 179 | mainRealm.addURL( getJarUrl( "circular-0.1.jar" ) ); 180 | 181 | /* 182 | * This was reported to fail with a ClassCircularityError in IBM JDK 1.5.0-SR2, 1.5.0-SR7 and 1.6.0-SR2. It 183 | * works in IBM JDK 1.5.0-SR10 and 1.6.0-SR6. 184 | */ 185 | loadClass( mainRealm, "A$C" ); 186 | } 187 | 188 | // ---------------------------------------------------------------------- 189 | // Resource testing 190 | // ---------------------------------------------------------------------- 191 | 192 | public void testResource() 193 | throws Exception 194 | { 195 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "main", null ); 196 | 197 | mainRealm.addURL( getJarUrl( "component0-1.0.jar" ) ); 198 | 199 | getResource( mainRealm, "META-INF/plexus/components.xml" ); 200 | } 201 | 202 | public void testMalformedResource() 203 | throws Exception 204 | { 205 | URL jarUrl = getJarUrl( "component0-1.0.jar" ); 206 | 207 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "main", null ); 208 | 209 | mainRealm.addURL( jarUrl ); 210 | 211 | ClassLoader officialClassLoader = new URLClassLoader( new URL[] { jarUrl } ); 212 | 213 | String resource = "META-INF/plexus/components.xml"; 214 | 215 | assertNotNull( mainRealm.getResource( resource ) ); 216 | assertNotNull( officialClassLoader.getResource( resource ) ); 217 | 218 | /* 219 | * NOTE: Resource names with a leading slash are invalid when passed to a class loader and must not be found! 220 | * One can use a leading slash in Class.getResource() but not in ClassLoader.getResource(). 221 | */ 222 | 223 | assertSame( null, mainRealm.getResource( "/" + resource ) ); 224 | assertSame( null, officialClassLoader.getResource( "/" + resource ) ); 225 | 226 | /* 227 | * For backward-compat, legacy class realms have to support leading slashes. 228 | */ 229 | 230 | org.codehaus.classworlds.ClassRealm legacyRealm = ClassRealmAdapter.getInstance( mainRealm ); 231 | assertNotNull( legacyRealm.getResource( "/" + resource ) ); 232 | assertNotNull( legacyRealm.getResourceAsStream( "/" + resource ) ); 233 | assertTrue( legacyRealm.findResources( "/" + resource ).hasMoreElements() ); 234 | } 235 | 236 | public void testFindResourceOnlyScansSelf() 237 | throws Exception 238 | { 239 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "main", null ); 240 | 241 | mainRealm.addURL( getJarUrl( "a.jar" ) ); 242 | 243 | ClassRealm childRealm = mainRealm.createChildRealm( "child" ); 244 | 245 | childRealm.addURL( getJarUrl( "b.jar" ) ); 246 | 247 | assertNotNull( childRealm.getResource( "a.properties" ) ); 248 | assertNotNull( childRealm.getResource( "b.properties" ) ); 249 | 250 | assertNull( childRealm.findResource( "a.properties" ) ); 251 | 252 | assertNotNull( childRealm.findResource( "b.properties" ) ); 253 | } 254 | 255 | public void testFindResourcesOnlyScansSelf() 256 | throws Exception 257 | { 258 | ClassRealm mainRealm = new ClassRealm( new ClassWorld(), "main", null ); 259 | 260 | mainRealm.addURL( getJarUrl( "a.jar" ) ); 261 | 262 | ClassRealm childRealm = mainRealm.createChildRealm( "child" ); 263 | 264 | childRealm.addURL( getJarUrl( "b.jar" ) ); 265 | 266 | assertTrue( childRealm.getResources( "a.properties" ).hasMoreElements() ); 267 | assertTrue( childRealm.getResources( "b.properties" ).hasMoreElements() ); 268 | 269 | assertFalse( childRealm.findResources( "a.properties" ).hasMoreElements() ); 270 | 271 | assertTrue( childRealm.findResources( "b.properties" ).hasMoreElements() ); 272 | } 273 | 274 | /** Should never deadlock. Ever */ 275 | public void testParallelDeadlockClassRealm() 276 | throws InterruptedException 277 | { 278 | for (int i = 0; i < 100; i++){ 279 | doOneDeadlockAttempt(); 280 | } 281 | 282 | } 283 | 284 | private void doOneDeadlockAttempt() 285 | throws InterruptedException 286 | { 287 | // Deadlock sample graciously ripped from http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html 288 | final ClassRealm cl1 = new ClassRealm( new ClassWorld(), "cl1", null ); 289 | final ClassRealm cl2 = new ClassRealm( new ClassWorld(), "cl2", cl1 ); 290 | cl1.setParentRealm( cl2 ); 291 | cl1.addURL( getJarUrl( "deadlock.jar" ) ); 292 | cl2.addURL( getJarUrl( "deadlock.jar" ) ); 293 | final CountDownLatch latch = new CountDownLatch( 1 ); 294 | 295 | Runnable r1 = new Runnable() 296 | { 297 | public void run() 298 | { 299 | try 300 | { 301 | latch.await(); 302 | cl1.loadClass( "deadlock.A" ); 303 | } 304 | catch ( ClassNotFoundException e ) 305 | { 306 | throw new RuntimeException( e ); 307 | } 308 | catch ( InterruptedException e ) 309 | { 310 | throw new RuntimeException( e ); 311 | } 312 | } 313 | }; 314 | 315 | Runnable r2 = new Runnable() 316 | { 317 | public void run() 318 | { 319 | try 320 | { 321 | latch.await(); 322 | cl1.loadClass( "deadlock.C" ); 323 | } 324 | catch ( ClassNotFoundException e ) 325 | { 326 | throw new RuntimeException( e ); 327 | } 328 | catch ( InterruptedException e ) 329 | { 330 | throw new RuntimeException( e ); 331 | } 332 | } 333 | }; 334 | 335 | Thread thread = new Thread( r1 ); 336 | thread.start(); 337 | Thread thread1 = new Thread( r2 ); 338 | thread1.start(); 339 | latch.countDown(); 340 | thread.join(); 341 | thread1.join(); 342 | } 343 | 344 | // ---------------------------------------------------------------------- 345 | // 346 | // ---------------------------------------------------------------------- 347 | 348 | private Class loadClassOrNull( ClassRealm realm, String name ) 349 | { 350 | try 351 | { 352 | return loadClass( realm, name ); 353 | } 354 | catch ( ClassNotFoundException e ) 355 | { 356 | return null; 357 | } 358 | } 359 | 360 | private Class loadClass( ClassRealm realm, String name ) 361 | throws ClassNotFoundException 362 | { 363 | Class cls = realm.loadClass( name ); 364 | 365 | /* 366 | * NOTE: Load the class both directly from the realm and indirectly from an (ordinary) child class loader which 367 | * uses the specified class realm for parent delegation. The child class loader itself has no additional class 368 | * path entries but relies entirely on the provided class realm. Hence, the created child class loader should in 369 | * theory be able to load exactly the same classes/resources as the underlying class realm. In practice, it will 370 | * test that class realms properly integrate into the standard Java class loader hierarchy. 371 | */ 372 | ClassLoader childLoader = new URLClassLoader( new URL[0], realm ); 373 | assertEquals( cls, childLoader.loadClass( name ) ); 374 | 375 | return cls; 376 | } 377 | 378 | private void getResource( ClassRealm realm, String name ) 379 | throws Exception 380 | { 381 | ClassLoader childLoader = new URLClassLoader( new URL[0], realm ); 382 | assertNotNull( realm.getResource( name ) ); 383 | assertEquals( realm.getResource( name ), childLoader.getResource( name ) ); 384 | assertEquals( Collections.list( realm.getResources( name ) ), 385 | Collections.list( childLoader.getResources( name ) ) ); 386 | } 387 | 388 | } 389 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/realm/EntryTest.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.realm; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase; 20 | import org.codehaus.plexus.classworlds.ClassWorld; 21 | 22 | /** 23 | * @author Ben Walding 24 | */ 25 | public class EntryTest 26 | extends AbstractClassWorldsTestCase 27 | { 28 | 29 | /** 30 | * Constructor for EntryTest. 31 | * 32 | * @param name 33 | */ 34 | public EntryTest( String name ) 35 | { 36 | super( name ); 37 | } 38 | 39 | public void testCompareTo() 40 | throws Exception 41 | { 42 | ClassWorld cw = new ClassWorld(); 43 | ClassRealm r = cw.newRealm( "test1" ); 44 | 45 | Entry entry1 = new Entry( r, "org.test" ); 46 | Entry entry2 = new Entry( r, "org.test.impl" ); 47 | 48 | assertTrue( "org.test > org.test.impl", entry1.compareTo( entry2 ) > 0 ); 49 | } 50 | 51 | /** 52 | * Tests the equality is realm independant 53 | * 54 | * @throws Exception 55 | */ 56 | public void testEquals() 57 | throws Exception 58 | { 59 | ClassWorld cw = new ClassWorld(); 60 | ClassRealm r1 = cw.newRealm( "test1" ); 61 | ClassRealm r2 = cw.newRealm( "test2" ); 62 | 63 | Entry entry1 = new Entry( r1, "org.test" ); 64 | Entry entry2 = new Entry( r2, "org.test" ); 65 | 66 | assertTrue( "entry1 == entry2", entry1.equals( entry2 ) ); 67 | assertTrue( "entry1.hashCode() == entry2.hashCode()", entry1.hashCode() == entry2.hashCode() ); 68 | } 69 | 70 | public void testMatchesClassByPackageImport() 71 | throws Exception 72 | { 73 | ClassWorld cw = new ClassWorld(); 74 | ClassRealm r = cw.newRealm( "test1" ); 75 | 76 | Entry entry = new Entry( r, "org.test" ); 77 | 78 | assertTrue( entry.matches( "org.test.MyClass" ) ); 79 | assertTrue( entry.matches( "org.test.MyClass$NestedClass" ) ); 80 | assertTrue( entry.matches( "org.test.MyClassUtils" ) ); 81 | assertTrue( entry.matches( "org.test.impl.MyClass" ) ); 82 | assertFalse( entry.matches( "org.tests.AnotherClass" ) ); 83 | } 84 | 85 | public void testMatchesClassByClassImport() 86 | throws Exception 87 | { 88 | ClassWorld cw = new ClassWorld(); 89 | ClassRealm r = cw.newRealm( "test1" ); 90 | 91 | Entry entry = new Entry( r, "org.test.MyClass" ); 92 | 93 | assertTrue( entry.matches( "org.test.MyClass" ) ); 94 | assertTrue( entry.matches( "org.test.MyClass$NestedClass" ) ); 95 | assertFalse( entry.matches( "org.test.MyClassUtils" ) ); 96 | assertFalse( entry.matches( "org.test.AnotherClass" ) ); 97 | } 98 | 99 | public void testMatchesResourceByPackageImport() 100 | throws Exception 101 | { 102 | ClassWorld cw = new ClassWorld(); 103 | ClassRealm r = cw.newRealm( "test1" ); 104 | 105 | Entry entry = new Entry( r, "org.test" ); 106 | 107 | assertTrue( entry.matches( "org/test/MyClass.class" ) ); 108 | assertTrue( entry.matches( "org/test/MyClass$NestedClass.class" ) ); 109 | assertTrue( entry.matches( "org/test/MyClasses.properties" ) ); 110 | assertTrue( entry.matches( "org/test/impl/MyClass.class" ) ); 111 | assertFalse( entry.matches( "org/tests/AnotherClass.class" ) ); 112 | } 113 | 114 | public void testMatchesResourceByClassImport() 115 | throws Exception 116 | { 117 | ClassWorld cw = new ClassWorld(); 118 | ClassRealm r = cw.newRealm( "test1" ); 119 | 120 | Entry entry = new Entry( r, "org.test.MyClass" ); 121 | 122 | assertTrue( entry.matches( "org/test/MyClass.class" ) ); 123 | assertTrue( entry.matches( "org/test/MyClass$NestedClass.class" ) ); 124 | assertFalse( entry.matches( "org/test/MyClass.properties" ) ); 125 | assertFalse( entry.matches( "org/test/AnotherClass" ) ); 126 | } 127 | 128 | public void testMatchesAllImport() 129 | throws Exception 130 | { 131 | ClassWorld cw = new ClassWorld(); 132 | ClassRealm r = cw.newRealm( "test1" ); 133 | 134 | Entry entry = new Entry( r, "" ); 135 | 136 | assertTrue( entry.matches( "org.test.MyClass" ) ); 137 | assertTrue( entry.matches( "org.test.MyClass$NestedClass" ) ); 138 | assertTrue( entry.matches( "org/test/MyClass.class" ) ); 139 | assertTrue( entry.matches( "org/test/MyClass.properties" ) ); 140 | } 141 | 142 | public void testMatchesResourceByResourceImport() 143 | throws Exception 144 | { 145 | ClassWorld cw = new ClassWorld(); 146 | ClassRealm r = cw.newRealm( "test1" ); 147 | 148 | Entry entry1 = new Entry( r, "some.properties" ); 149 | 150 | assertTrue( entry1.matches( "some.properties" ) ); 151 | assertFalse( entry1.matches( "other.properties" ) ); 152 | 153 | Entry entry2 = new Entry( r, "org/test/some.properties" ); 154 | 155 | assertTrue( entry2.matches( "org/test/some.properties" ) ); 156 | assertFalse( entry2.matches( "org/test/other.properties" ) ); 157 | } 158 | 159 | public void testMatchesClassByExactPackageImport() 160 | throws Exception 161 | { 162 | ClassWorld cw = new ClassWorld(); 163 | ClassRealm r = cw.newRealm( "test1" ); 164 | 165 | Entry entry = new Entry( r, "org.test.*" ); 166 | 167 | assertTrue( entry.matches( "org.test.MyClass" ) ); 168 | assertTrue( entry.matches( "org.test.MyClass$NestedClass" ) ); 169 | assertTrue( entry.matches( "org.test.MyClassUtils" ) ); 170 | assertFalse( entry.matches( "org.test.impl.MyClass" ) ); 171 | assertFalse( entry.matches( "org.tests.AnotherClass" ) ); 172 | } 173 | 174 | public void testMatchesResourceByExactPackageImport() 175 | throws Exception 176 | { 177 | ClassWorld cw = new ClassWorld(); 178 | ClassRealm r = cw.newRealm( "test1" ); 179 | 180 | Entry entry = new Entry( r, "org.test.*" ); 181 | 182 | assertTrue( entry.matches( "org/test/MyClass.class" ) ); 183 | assertTrue( entry.matches( "org/test/MyClass$NestedClass.class" ) ); 184 | assertTrue( entry.matches( "org/test/MyClasses.properties" ) ); 185 | assertFalse( entry.matches( "org/test/impl/MyClass.class" ) ); 186 | assertFalse( entry.matches( "org/tests/AnotherClass.class" ) ); 187 | } 188 | 189 | } 190 | -------------------------------------------------------------------------------- /src/test/java/org/codehaus/plexus/classworlds/strategy/StrategyTest.java: -------------------------------------------------------------------------------- 1 | package org.codehaus.plexus.classworlds.strategy; 2 | 3 | /* 4 | * Copyright 2001-2006 Codehaus Foundation. 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import java.io.InputStream; 20 | import java.net.URL; 21 | import java.util.Enumeration; 22 | 23 | import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase; 24 | import org.codehaus.plexus.classworlds.ClassWorld; 25 | import org.codehaus.plexus.classworlds.realm.ClassRealm; 26 | 27 | // jars within jars 28 | // hierarchy vs graph 29 | 30 | public class StrategyTest 31 | extends AbstractClassWorldsTestCase 32 | { 33 | private ClassWorld world; 34 | 35 | private ClassRealm realm; 36 | 37 | private Strategy strategy; 38 | 39 | public StrategyTest( String name ) 40 | { 41 | super( name ); 42 | } 43 | 44 | public void setUp() 45 | throws Exception 46 | { 47 | this.world = new ClassWorld(); 48 | 49 | this.realm = this.world.newRealm( "realm" ); 50 | 51 | this.strategy = this.realm.getStrategy(); 52 | 53 | realm.addURL( getJarUrl( "component0-1.0.jar" ) ); 54 | } 55 | 56 | public void testLoadingOfApplicationClass() 57 | throws Exception 58 | { 59 | Class c = strategy.loadClass( "org.codehaus.plexus.Component0" ); 60 | 61 | assertNotNull( c ); 62 | } 63 | 64 | public void testLoadingOfApplicationClassThenDoingItAgain() 65 | throws Exception 66 | { 67 | Class c = strategy.loadClass( "org.codehaus.plexus.Component0" ); 68 | 69 | assertNotNull( c ); 70 | 71 | c = strategy.loadClass( "org.codehaus.plexus.Component0" ); 72 | 73 | assertNotNull( c ); 74 | } 75 | 76 | 77 | public void testLoadingOfSystemClass() 78 | throws Exception 79 | { 80 | Class c = strategy.getRealm().loadClass( "java.lang.Object" ); 81 | 82 | assertNotNull( c ); 83 | } 84 | 85 | public void testLoadingOfNonExistentClass() 86 | throws Exception 87 | { 88 | try 89 | { 90 | strategy.loadClass( "org.codehaus.plexus.NonExistentComponent" ); 91 | 92 | fail( "Should have thrown a ClassNotFoundException!" ); 93 | } 94 | catch ( ClassNotFoundException e ) 95 | { 96 | // do nothing 97 | } 98 | } 99 | 100 | public void testGetApplicationResource() 101 | throws Exception 102 | { 103 | URL resource = strategy.getResource( "META-INF/plexus/components.xml" ); 104 | 105 | assertNotNull( resource ); 106 | 107 | String content = getContent( resource.openStream() ); 108 | 109 | assertTrue( content.startsWith( "" ) ); 110 | } 111 | 112 | public void testGetSystemResource() 113 | throws Exception 114 | { 115 | URL resource = strategy.getRealm().getResource( "java/lang/Object.class" ); 116 | 117 | assertNotNull( resource ); 118 | } 119 | 120 | 121 | public void testFindResources() 122 | throws Exception 123 | { 124 | realm.addURL( getJarUrl( "component1-1.0.jar" ) ); 125 | 126 | Enumeration e = strategy.getResources( "META-INF/plexus/components.xml" ); 127 | 128 | assertNotNull( e ); 129 | 130 | int resourceCount = 0; 131 | 132 | for ( Enumeration resources = e; resources.hasMoreElements(); ) 133 | { 134 | resources.nextElement(); 135 | 136 | resourceCount++; 137 | } 138 | 139 | assertEquals( 2, resourceCount ); 140 | } 141 | 142 | protected String getContent( InputStream in ) 143 | throws Exception 144 | { 145 | byte[] buffer = new byte[1024]; 146 | 147 | int read = 0; 148 | 149 | StringBuffer content = new StringBuffer(); 150 | 151 | while ( ( read = in.read( buffer, 0, 1024 ) ) >= 0 ) 152 | { 153 | content.append( new String( buffer, 0, read ) ); 154 | } 155 | 156 | return content.toString(); 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /src/test/test-data/a.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/a.jar -------------------------------------------------------------------------------- /src/test/test-data/a.properties: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # 3 | # Copyright 2001-2006 The Codehaus Foundation. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | a properties 19 | -------------------------------------------------------------------------------- /src/test/test-data/b.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/b.jar -------------------------------------------------------------------------------- /src/test/test-data/b_old.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/b_old.jar -------------------------------------------------------------------------------- /src/test/test-data/c.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/c.jar -------------------------------------------------------------------------------- /src/test/test-data/circular-0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/circular-0.1.jar -------------------------------------------------------------------------------- /src/test/test-data/component0-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/component0-1.0.jar -------------------------------------------------------------------------------- /src/test/test-data/component1-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/component1-1.0.jar -------------------------------------------------------------------------------- /src/test/test-data/component2-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/component2-1.0.jar -------------------------------------------------------------------------------- /src/test/test-data/component3-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/component3-1.0.jar -------------------------------------------------------------------------------- /src/test/test-data/component4-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/component4-1.0.jar -------------------------------------------------------------------------------- /src/test/test-data/component5-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/component5-1.0.jar -------------------------------------------------------------------------------- /src/test/test-data/component5-2.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/component5-2.0.jar -------------------------------------------------------------------------------- /src/test/test-data/d.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/d.jar -------------------------------------------------------------------------------- /src/test/test-data/deadlock.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/deadlock.jar -------------------------------------------------------------------------------- /src/test/test-data/dupe-main.conf: -------------------------------------------------------------------------------- 1 | 2 | main is com.werken.Foo from foo 3 | main is com.werken.Bar from bar 4 | -------------------------------------------------------------------------------- /src/test/test-data/dupe-realm.conf: -------------------------------------------------------------------------------- 1 | 2 | [cheese] 3 | 4 | [dupe.realm] 5 | 6 | [potatoes] 7 | 8 | [dupe.realm] 9 | 10 | -------------------------------------------------------------------------------- /src/test/test-data/early-import.conf: -------------------------------------------------------------------------------- 1 | 2 | import org.xml.sax from root 3 | -------------------------------------------------------------------------------- /src/test/test-data/inheritance.conf: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------ 2 | # Define the main entry-point 3 | # ------------------------------------------------------------ 4 | 5 | main is org.apache.maven.app.App from root.maven 6 | 7 | # ------------------------------------------------------------ 8 | # Start defining realms 9 | # ------------------------------------------------------------ 10 | 11 | [root] 12 | load ${basedir}/src/test/test-data/a.jar 13 | 14 | [root.maven] 15 | load ${basedir}/src/test/test-data/b.jar 16 | 17 | [root.maven.plugin] 18 | load ${basedir}/src/test/test-data/c.jar 19 | -------------------------------------------------------------------------------- /src/test/test-data/launch-noclass.conf: -------------------------------------------------------------------------------- 1 | 2 | main is b.Goober from app 3 | 4 | [app] 5 | load ${basedir}/src/test/test-data/a.jar 6 | load ${basedir}/src/test/test-data/b.jar 7 | -------------------------------------------------------------------------------- /src/test/test-data/launch-nomethod.conf: -------------------------------------------------------------------------------- 1 | 2 | main is c.C from app 3 | 4 | [app] 5 | load ${basedir}/src/test/test-data/a.jar 6 | load ${basedir}/src/test/test-data/b.jar 7 | load ${basedir}/src/test/test-data/c.jar 8 | -------------------------------------------------------------------------------- /src/test/test-data/nested.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/nested.jar -------------------------------------------------------------------------------- /src/test/test-data/nested.properties: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # 3 | # Copyright 2001-2006 The Codehaus Foundation. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | nested.properties 19 | -------------------------------------------------------------------------------- /src/test/test-data/optionally-existent.conf: -------------------------------------------------------------------------------- 1 | 2 | # ------------------------------------------------------------ 3 | # Define the main entry-point 4 | # ------------------------------------------------------------ 5 | 6 | main is org.apache.maven.app.App from opt 7 | 8 | # ------------------------------------------------------------ 9 | # Start defining realms 10 | # ------------------------------------------------------------ 11 | 12 | [opt] 13 | optionally ${basedir}/target/test-lib/xml-apis-1.3.02.jar 14 | 15 | -------------------------------------------------------------------------------- /src/test/test-data/optionally-nonexistent.conf: -------------------------------------------------------------------------------- 1 | 2 | # ------------------------------------------------------------ 3 | # Define the main entry-point 4 | # ------------------------------------------------------------ 5 | 6 | main is org.apache.maven.app.App from opt 7 | 8 | # ------------------------------------------------------------ 9 | # Start defining realms 10 | # ------------------------------------------------------------ 11 | 12 | [opt] 13 | optionally /non/existant/path/*.jar 14 | -------------------------------------------------------------------------------- /src/test/test-data/realm-syntax.conf: -------------------------------------------------------------------------------- 1 | 2 | [foo 3 | ] 4 | -------------------------------------------------------------------------------- /src/test/test-data/resources/classworlds.conf: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------ 2 | # Define the main entry-point 3 | # ------------------------------------------------------------ 4 | 5 | main is foo from root 6 | 7 | # ------------------------------------------------------------ 8 | # Start defining realms 9 | # ------------------------------------------------------------ 10 | 11 | [root] 12 | load ${basedir}/src/test/test-data/resources/werkflow.jar 13 | -------------------------------------------------------------------------------- /src/test/test-data/resources/werkflow.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sonatype/plexus-classworlds/e869ac904400f0a966e64af7e28709ef2dd5dd3f/src/test/test-data/resources/werkflow.jar -------------------------------------------------------------------------------- /src/test/test-data/set-using-existent.conf: -------------------------------------------------------------------------------- 1 | 2 | # ------------------------------------------------------------ 3 | # Define the main entry-point 4 | # ------------------------------------------------------------ 5 | 6 | main is org.apache.maven.app.App from opt 7 | 8 | # ------------------------------------------------------------ 9 | # Set properties 10 | # ------------------------------------------------------------ 11 | set set.using.existent using ${basedir}/src/test/test-data/set-using-existent.properties 12 | set set.using.default using ${basedir}/src/test/test-data/set-using-existent.properties default testSet_Using_Existent_Default 13 | 14 | # ------------------------------------------------------------ 15 | # Start defining realms 16 | # ------------------------------------------------------------ 17 | 18 | [opt] 19 | 20 | -------------------------------------------------------------------------------- /src/test/test-data/set-using-existent.properties: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # 3 | # Copyright 2001-2006 The Codehaus Foundation. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | set.using.existent=testSet_Using_Existent 19 | 20 | -------------------------------------------------------------------------------- /src/test/test-data/set-using-missing.conf: -------------------------------------------------------------------------------- 1 | 2 | # ------------------------------------------------------------ 3 | # Define the main entry-point 4 | # ------------------------------------------------------------ 5 | 6 | main is org.apache.maven.app.App from opt 7 | 8 | # ------------------------------------------------------------ 9 | # Set properties 10 | # ------------------------------------------------------------ 11 | set set.using.missing default testSet_Using_Missing_Default 12 | set set.using.filtered.default default ${user.home}/m2 13 | 14 | # ------------------------------------------------------------ 15 | # Start defining realms 16 | # ------------------------------------------------------------ 17 | 18 | [opt] 19 | 20 | -------------------------------------------------------------------------------- /src/test/test-data/set-using-nonexistent.conf: -------------------------------------------------------------------------------- 1 | 2 | # ------------------------------------------------------------ 3 | # Define the main entry-point 4 | # ------------------------------------------------------------ 5 | 6 | main is org.apache.maven.app.App from opt 7 | 8 | # ------------------------------------------------------------ 9 | # Set properties 10 | # ------------------------------------------------------------ 11 | set set.using.nonexistent using ${basedir}/src/test/test-data/set-using-nonexistent.properties 12 | set set.using.nonexistent.default using ${basedir}/src/test/test-data/set-using-nonexistent.properties default testSet_Using_NonExistent_Default 13 | 14 | # ------------------------------------------------------------ 15 | # Start defining realms 16 | # ------------------------------------------------------------ 17 | 18 | [opt] 19 | 20 | -------------------------------------------------------------------------------- /src/test/test-data/unhandled.conf: -------------------------------------------------------------------------------- 1 | 2 | foo 3 | -------------------------------------------------------------------------------- /src/test/test-data/valid-enh-launch-exitCode.conf: -------------------------------------------------------------------------------- 1 | 2 | main is b.Bb from app 3 | 4 | [app] 5 | load ${basedir}/src/test/test-data/a.jar 6 | load ${basedir}/src/test/test-data/b.jar 7 | -------------------------------------------------------------------------------- /src/test/test-data/valid-enh-launch.conf: -------------------------------------------------------------------------------- 1 | 2 | main is b.B from app 3 | 4 | [app] 5 | load ${basedir}/src/test/test-data/a.jar 6 | load ${basedir}/src/test/test-data/b.jar 7 | -------------------------------------------------------------------------------- /src/test/test-data/valid-launch-exitCode.conf: -------------------------------------------------------------------------------- 1 | 2 | main is a.Aa from app 3 | 4 | [app] 5 | load ${basedir}/src/test/test-data/a.jar 6 | -------------------------------------------------------------------------------- /src/test/test-data/valid-launch.conf: -------------------------------------------------------------------------------- 1 | 2 | main is a.A from app 3 | 4 | [app] 5 | load ${basedir}/src/test/test-data/a.jar 6 | -------------------------------------------------------------------------------- /src/test/test-data/valid.conf: -------------------------------------------------------------------------------- 1 | 2 | # ------------------------------------------------------------ 3 | # Define the main entry-point 4 | # ------------------------------------------------------------ 5 | 6 | main is org.apache.maven.app.App from maven 7 | 8 | # ------------------------------------------------------------ 9 | # Start defining realms 10 | # ------------------------------------------------------------ 11 | 12 | [xml] 13 | load ${basedir}/target/test-lib/xml-apis-1.3.02.jar 14 | 15 | [ant] 16 | import org.xml.sax from xml 17 | load ${basedir}/target/test-lib/ant-1.9.0.jar 18 | 19 | [maven] 20 | import org.xml.sax from xml 21 | load ${basedir}/target/test-lib/commons-logging-1.0.3.jar 22 | 23 | [glob] 24 | load ${basedir}/src/test/test-data/*.jar 25 | --------------------------------------------------------------------------------