├── JCL ├── jcl.jar ├── test-jcl.jar ├── lib │ ├── spring.jar │ ├── junit-4.4.jar │ ├── log4j-1.2.14.jar │ └── commons-logging-1.1.jar ├── docs │ └── web │ │ ├── js │ │ ├── clipboard.swf │ │ ├── shBrushJava.js │ │ └── shBrushXml.js │ │ ├── css │ │ ├── style.css │ │ └── SyntaxHighlighter.css │ │ └── index.html ├── src │ ├── log4j.properties │ └── xeus │ │ └── jcl │ │ ├── ResourceType.java │ │ ├── exception │ │ ├── JclException.java │ │ └── ResourceNotFoundException.java │ │ ├── loader │ │ └── Loader.java │ │ ├── Configuration.java │ │ ├── JclUtils.java │ │ ├── JclObjectFactory.java │ │ ├── JarResources.java │ │ ├── junit │ │ └── LoadTest.java │ │ ├── AbstractClassLoader.java │ │ ├── ClasspathResources.java │ │ └── JarClassLoader.java ├── test-src │ ├── log4j.properties │ └── xeus │ │ └── jcl │ │ └── test │ │ └── Test.java ├── spring-test.xml ├── build.xml └── LICENSE ├── JCL2 ├── core │ ├── src │ │ ├── test │ │ │ ├── resources │ │ │ │ ├── test │ │ │ │ │ └── test.properties │ │ │ │ ├── test.jar │ │ │ │ ├── lucene-core-5.3.1.jar │ │ │ │ └── jcl.xml │ │ │ └── java │ │ │ │ └── org │ │ │ │ └── xeustechnologies │ │ │ │ └── jcl │ │ │ │ ├── sample │ │ │ │ └── Test1.java │ │ │ │ ├── test │ │ │ │ ├── TestInterface.java │ │ │ │ └── TestLoader.java │ │ │ │ ├── SpiTest.java │ │ │ │ ├── DelegateProxyClassLoaderTest.java │ │ │ │ ├── ClasspathResourcesTest.java │ │ │ │ └── AbstractClassLoaderTest.java │ │ └── main │ │ │ ├── java │ │ │ └── org │ │ │ │ └── xeustechnologies │ │ │ │ └── jcl │ │ │ │ ├── ResourceType.java │ │ │ │ ├── proxy │ │ │ │ ├── ProxyProvider.java │ │ │ │ ├── ProxyProviderFactory.java │ │ │ │ ├── JdkProxyProvider.java │ │ │ │ └── CglibProxyProvider.java │ │ │ │ ├── context │ │ │ │ ├── JclContextLoader.java │ │ │ │ ├── DefaultContextLoader.java │ │ │ │ └── JclContext.java │ │ │ │ ├── JclJarEntry.java │ │ │ │ ├── utils │ │ │ │ ├── PathResolver.java │ │ │ │ └── Utils.java │ │ │ │ ├── exception │ │ │ │ ├── JclContextException.java │ │ │ │ ├── JclException.java │ │ │ │ └── ResourceNotFoundException.java │ │ │ │ ├── ProxyClassLoader.java │ │ │ │ ├── DelegateProxyClassLoader.java │ │ │ │ ├── CompositeProxyClassLoader.java │ │ │ │ ├── Configuration.java │ │ │ │ ├── JclUtils.java │ │ │ │ ├── JclObjectFactory.java │ │ │ │ └── JarResources.java │ │ │ └── resources │ │ │ └── org │ │ │ └── xeustechnologies │ │ │ └── jcl │ │ │ └── context │ │ │ └── jcl-context.xsd │ └── pom.xml ├── spring │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── META-INF │ │ │ │ │ ├── spring.handlers │ │ │ │ │ └── spring.schemas │ │ │ │ └── org │ │ │ │ │ └── xeustechnologies │ │ │ │ │ └── jcl │ │ │ │ │ └── spring │ │ │ │ │ └── jcl-schema.xsd │ │ │ └── java │ │ │ │ └── org │ │ │ │ └── xeustechnologies │ │ │ │ └── jcl │ │ │ │ └── spring │ │ │ │ ├── JclNamespaceHandler.java │ │ │ │ ├── JclBeanDefinitionParser.java │ │ │ │ └── JclBeanDefinitionDecorator.java │ │ └── test │ │ │ ├── java │ │ │ └── org │ │ │ │ └── xeustechnologies │ │ │ │ └── jcl │ │ │ │ ├── test │ │ │ │ └── TestInterface.java │ │ │ │ └── spring │ │ │ │ └── SpringTest.java │ │ │ └── resources │ │ │ ├── log4j.xml │ │ │ └── spring-test.xml │ └── pom.xml ├── test-jcl │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── org │ │ │ └── xeustechnologies │ │ │ └── jcl │ │ │ └── test │ │ │ ├── TestInterface.java │ │ │ └── Test.java │ └── pom.xml ├── test-web │ ├── src │ │ └── main │ │ │ ├── webapp │ │ │ ├── WEB-INF │ │ │ │ └── web.xml │ │ │ └── index.jsp │ │ │ └── resources │ │ │ └── jcl.xml │ └── pom.xml ├── web │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── org │ │ └── xeustechnologies │ │ └── jcl │ │ └── web │ │ ├── JclContextLoaderListener.java │ │ └── WebAppPathResolver.java └── pom.xml ├── .travis.yml ├── .gitattributes └── .gitignore /JCL/jcl.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kamranzafar/JCL/HEAD/JCL/jcl.jar -------------------------------------------------------------------------------- /JCL/test-jcl.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kamranzafar/JCL/HEAD/JCL/test-jcl.jar -------------------------------------------------------------------------------- /JCL/lib/spring.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kamranzafar/JCL/HEAD/JCL/lib/spring.jar -------------------------------------------------------------------------------- /JCL/lib/junit-4.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kamranzafar/JCL/HEAD/JCL/lib/junit-4.4.jar -------------------------------------------------------------------------------- /JCL2/core/src/test/resources/test/test.properties: -------------------------------------------------------------------------------- 1 | # Test properties resource 2 | testkey=testval -------------------------------------------------------------------------------- /JCL/lib/log4j-1.2.14.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kamranzafar/JCL/HEAD/JCL/lib/log4j-1.2.14.jar -------------------------------------------------------------------------------- /JCL/docs/web/js/clipboard.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kamranzafar/JCL/HEAD/JCL/docs/web/js/clipboard.swf -------------------------------------------------------------------------------- /JCL/lib/commons-logging-1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kamranzafar/JCL/HEAD/JCL/lib/commons-logging-1.1.jar -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: oraclejdk7 3 | 4 | before_script: cd JCL2 5 | script: mvn clean install -DskipTest=true 6 | -------------------------------------------------------------------------------- /JCL2/core/src/test/resources/test.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kamranzafar/JCL/HEAD/JCL2/core/src/test/resources/test.jar -------------------------------------------------------------------------------- /JCL2/core/src/test/java/org/xeustechnologies/jcl/sample/Test1.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl.sample; 2 | 3 | public class Test1 { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /JCL2/core/src/test/resources/lucene-core-5.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kamranzafar/JCL/HEAD/JCL2/core/src/test/resources/lucene-core-5.3.1.jar -------------------------------------------------------------------------------- /JCL2/spring/src/main/resources/META-INF/spring.handlers: -------------------------------------------------------------------------------- 1 | http\://www.xeustechnologies.org/schema/jcl=org.xeustechnologies.jcl.spring.JclNamespaceHandler -------------------------------------------------------------------------------- /JCL2/spring/src/main/resources/META-INF/spring.schemas: -------------------------------------------------------------------------------- 1 | http\://www.xeustechnologies.org/schema/jcl/jcl.xsd=org/xeustechnologies/jcl/spring/jcl-schema.xsd -------------------------------------------------------------------------------- /JCL2/core/src/test/java/org/xeustechnologies/jcl/test/TestInterface.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl.test; 2 | 3 | public interface TestInterface { 4 | public String sayHello(); 5 | } 6 | -------------------------------------------------------------------------------- /JCL2/spring/src/test/java/org/xeustechnologies/jcl/test/TestInterface.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl.test; 2 | 3 | public interface TestInterface { 4 | public String sayHello(); 5 | } 6 | -------------------------------------------------------------------------------- /JCL2/test-jcl/src/main/java/org/xeustechnologies/jcl/test/TestInterface.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl.test; 2 | 3 | public interface TestInterface { 4 | public String sayHello(); 5 | } 6 | -------------------------------------------------------------------------------- /JCL/src/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=WARN, stdout 2 | log4j.logger.xeus.jcl=TRACE 3 | log4j.logger.xeus.test.jcl=TRACE, stdout 4 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n 7 | -------------------------------------------------------------------------------- /JCL/test-src/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=WARN, stdout 2 | log4j.logger.xeus.jcl=TRACE 3 | log4j.logger.xeus.test.jcl=TRACE, stdout 4 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n 7 | -------------------------------------------------------------------------------- /JCL2/test-web/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | jcl-context 4 | 5 | classpath:jcl.xml 6 | 7 | 8 | 9 | 10 | org.xeustechnologies.jcl.web.JclContextLoaderListener 11 | 12 | 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /JCL2/core/src/test/java/org/xeustechnologies/jcl/test/TestLoader.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl.test; 2 | 3 | import java.io.InputStream; 4 | import java.net.URL; 5 | 6 | public class TestLoader extends org.xeustechnologies.jcl.ProxyClassLoader { 7 | 8 | @Override 9 | public Class loadClass(String className, boolean resolveIt) { 10 | return null; 11 | } 12 | 13 | @Override 14 | public InputStream loadResource(String name) { 15 | return null; 16 | } 17 | 18 | @Override 19 | public URL findResource(String name) { 20 | return null; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /JCL2/web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | jcl 5 | org.xeustechnologies 6 | 2.9-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | jcl-web 11 | JCL - Web 12 | 13 | 14 | 15 | org.xeustechnologies 16 | jcl-core 17 | ${project.version} 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /JCL2/test-web/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page import="org.xeustechnologies.jcl.*" %> 2 | <%@ page import="org.xeustechnologies.jcl.context.*" %> 3 | <% 4 | JarClassLoader jcl=JclContext.get("jcl1"); 5 | JclObjectFactory factory = JclObjectFactory.getInstance(); 6 | 7 | Object obj = factory.create(jcl, "org.xeustechnologies.jcl.test.Test"); 8 | String msg = (String) obj.getClass().getMethod("sayHello").invoke(obj, null); 9 | 10 | out.write("

"+msg+"


"); 11 | 12 | out.write("

Object Details:

"); 13 | out.write("

Class: "+obj.getClass().getName()+"

"); 14 | out.write("

Classloader: "+obj.getClass().getClassLoader().getClass().getName()+"

"); 15 | 16 | out.write("

Jcl Context:

"); 17 | out.write("

JCL instances: "+JclContext.getAll()+"

"); 18 | out.write("

Context classloader: "+jcl.getClass().getClassLoader()+"

"); 19 | %> -------------------------------------------------------------------------------- /JCL/test-src/xeus/jcl/test/Test.java: -------------------------------------------------------------------------------- 1 | package xeus.jcl.test; 2 | 3 | import java.io.Serializable; 4 | 5 | import org.apache.log4j.Logger; 6 | 7 | public class Test implements Serializable { 8 | 9 | private String firstName; 10 | private String lastName; 11 | 12 | private static Logger logger = Logger.getLogger(Test.class); 13 | 14 | public Test() { 15 | firstName = "World"; 16 | lastName = ""; 17 | } 18 | 19 | public Test(String firstName) { 20 | this.firstName = firstName; 21 | } 22 | 23 | public void sayHello() { 24 | logger.debug("Hello " + firstName + " " + lastName); 25 | } 26 | 27 | public String getFirstName() { 28 | return firstName; 29 | } 30 | 31 | public void setFirstName(String firstName) { 32 | this.firstName = firstName; 33 | } 34 | 35 | public String getLastName() { 36 | return lastName; 37 | } 38 | 39 | public void setLastName(String lastName) { 40 | this.lastName = lastName; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/ResourceType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl; 19 | 20 | /** 21 | * Enumeration for different resource types 22 | * 23 | * @author Kamran 24 | * 25 | */ 26 | public enum ResourceType { 27 | CLASS, XML, PROPERTIES, UNKNOWN; 28 | } 29 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/proxy/ProxyProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.proxy; 19 | 20 | /** 21 | * @author Kamran Zafar 22 | * 23 | */ 24 | public interface ProxyProvider { 25 | public Object createProxy(Object object, Class superClass, Class[] interfaces, ClassLoader cl); 26 | } 27 | -------------------------------------------------------------------------------- /JCL2/core/src/test/java/org/xeustechnologies/jcl/SpiTest.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.JUnit4; 7 | 8 | import java.util.Iterator; 9 | import java.util.ServiceLoader; 10 | 11 | /** 12 | * Created by kamran on 23/11/15. 13 | */ 14 | @RunWith(JUnit4.class) 15 | public class SpiTest { 16 | @Test 17 | public void spiTest() throws Exception { 18 | JarClassLoader jcl = new JarClassLoader(); 19 | jcl.add("./target/test-classes/lucene-core-5.3.1.jar"); 20 | 21 | Class codecClass = jcl.loadClass("org.apache.lucene.codecs.Codec"); 22 | 23 | ServiceLoader serviceLoader = ServiceLoader.load(codecClass, jcl); 24 | 25 | Iterator itr = serviceLoader.iterator(); 26 | 27 | Assert.assertTrue(itr.hasNext()); 28 | 29 | // while (itr.hasNext()) { 30 | // System.out.println(itr.next()); 31 | // } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /JCL2/spring/src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /JCL2/test-jcl/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.xeustechnologies 5 | jcl 6 | 2.9-SNAPSHOT 7 | 8 | test-jcl 9 | JCL - Test JAR 10 | A Test JAR file containg a Test bean. This file is used in unit tests 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-jar-plugin 16 | 17 | 18 | org/xeustechnologies/jcl/test/Test.class 19 | 20 | 21 | 22 | 23 | maven-deploy-plugin 24 | 25 | true 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/context/JclContextLoader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.context; 19 | 20 | /** 21 | * Loads context from different sources 22 | * 23 | * @author Kamran 24 | * 25 | */ 26 | public interface JclContextLoader { 27 | /** 28 | * Loads the context 29 | */ 30 | public void loadContext(); 31 | 32 | /** 33 | * Clears the context 34 | */ 35 | public void unloadContext(); 36 | } 37 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/ResourceType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with JCL. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl; 28 | 29 | public enum ResourceType { 30 | CLASS, XML, PROPERTIES, UNKNOWN; 31 | } 32 | -------------------------------------------------------------------------------- /JCL2/spring/src/main/resources/org/xeustechnologies/jcl/spring/jcl-schema.xsd: -------------------------------------------------------------------------------- 1 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/JclJarEntry.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl; 19 | 20 | public class JclJarEntry { 21 | 22 | private String baseUrl; 23 | private byte[] resourceBytes; 24 | 25 | public String getBaseUrl() { 26 | return baseUrl; 27 | } 28 | 29 | public void setBaseUrl(String argBaseUrl) { 30 | baseUrl = argBaseUrl; 31 | } 32 | 33 | public byte[] getResourceBytes() { 34 | return resourceBytes; 35 | } 36 | 37 | public void setResourceBytes(byte[] argResourceBytes) { 38 | resourceBytes = argResourceBytes; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/utils/PathResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.utils; 19 | 20 | /** 21 | * Resolves the path to jar sources. Used by XmlContextLoader. 22 | * 23 | * @author Kamran 24 | * 25 | */ 26 | public interface PathResolver { 27 | 28 | /** 29 | * Resolves the path to class/jar source. Could return multiple file 30 | * paths/streams/urls. 31 | * 32 | * Returns null if path could not be resolved. 33 | * 34 | * @param path 35 | * @return Object[] 36 | */ 37 | public Object[] resolvePath(String path); 38 | } 39 | -------------------------------------------------------------------------------- /JCL2/spring/src/test/java/org/xeustechnologies/jcl/spring/SpringTest.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl.spring; 2 | 3 | import junit.framework.TestCase; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.JUnit4; 7 | import org.springframework.context.support.ClassPathXmlApplicationContext; 8 | 9 | import java.lang.reflect.InvocationTargetException; 10 | 11 | @RunWith(JUnit4.class) 12 | public class SpringTest extends TestCase { 13 | @Test 14 | public void testWithSpring() throws ClassNotFoundException, IllegalArgumentException, SecurityException, 15 | IllegalAccessException, InvocationTargetException, NoSuchMethodException { 16 | ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( "classpath:spring-test.xml" ); 17 | 18 | // Bean loaded with JCL 19 | Object test1 = appContext.getBean( "test1" ); 20 | 21 | // Bean loaded with parent CL 22 | Object test2 = appContext.getBean( "test2" ); 23 | 24 | assertEquals( "org.xeustechnologies.jcl.JarClassLoader", test1.getClass().getClassLoader().getClass().getName() ); 25 | assertEquals( "sun.misc.Launcher$AppClassLoader", test2.getClass().getClassLoader().getClass().getName() ); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /JCL/docs/web/js/shBrushJava.js: -------------------------------------------------------------------------------- 1 | /* 2 | * JsMin 3 | * Javascript Compressor 4 | * http://www.crockford.com/ 5 | * http://www.smallsharptools.com/ 6 | */ 7 | 8 | dp.sh.Brushes.Java=function() 9 | {var keywords='abstract assert boolean break byte case catch char class const '+'continue default do double else enum extends '+'false final finally float for goto if implements import '+'instanceof int interface long native new null '+'package private protected public return '+'short static strictfp super switch synchronized this throw throws true '+'transient try void volatile while';this.regexList=[{regex:dp.sh.RegexLib.SingleLineCComments,css:'comment'},{regex:dp.sh.RegexLib.MultiLineCComments,css:'comment'},{regex:dp.sh.RegexLib.DoubleQuotedString,css:'string'},{regex:dp.sh.RegexLib.SingleQuotedString,css:'string'},{regex:new RegExp('\\b([\\d]+(\\.[\\d]+)?|0x[a-f0-9]+)\\b','gi'),css:'number'},{regex:new RegExp('(?!\\@interface\\b)\\@[\\$\\w]+\\b','g'),css:'annotation'},{regex:new RegExp('\\@interface\\b','g'),css:'keyword'},{regex:new RegExp(this.GetKeywords(keywords),'gm'),css:'keyword'}];this.CssClass='dp-j';this.Style='.dp-j .annotation { color: #646464; }'+'.dp-j .number { color: #C00000; }';} 10 | dp.sh.Brushes.Java.prototype=new dp.sh.Highlighter();dp.sh.Brushes.Java.Aliases=['java']; 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### JetBrains template 3 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm 4 | 5 | *.iml 6 | 7 | ## Directory-based project format: 8 | .idea/ 9 | # if you remove the above rule, at least ignore the following: 10 | 11 | # User-specific stuff: 12 | # .idea/workspace.xml 13 | # .idea/tasks.xml 14 | # .idea/dictionaries 15 | 16 | # Sensitive or high-churn files: 17 | # .idea/dataSources.ids 18 | # .idea/dataSources.xml 19 | # .idea/sqlDataSources.xml 20 | # .idea/dynamic.xml 21 | # .idea/uiDesigner.xml 22 | 23 | # Gradle: 24 | # .idea/gradle.xml 25 | # .idea/libraries 26 | 27 | # Mongo Explorer plugin: 28 | # .idea/mongoSettings.xml 29 | 30 | ## File-based project format: 31 | *.ipr 32 | *.iws 33 | 34 | ## Plugin-specific files: 35 | 36 | # IntelliJ 37 | out/ 38 | 39 | # mpeltonen/sbt-idea plugin 40 | .idea_modules/ 41 | 42 | # JIRA plugin 43 | atlassian-ide-plugin.xml 44 | 45 | # Crashlytics plugin (for Android Studio and IntelliJ) 46 | com_crashlytics_export_strings.xml 47 | crashlytics.properties 48 | crashlytics-build.properties 49 | 50 | 51 | ### Maven template 52 | target/ 53 | pom.xml.tag 54 | pom.xml.releaseBackup 55 | pom.xml.versionsBackup 56 | pom.xml.next 57 | release.properties 58 | 59 | 60 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/proxy/ProxyProviderFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.proxy; 19 | 20 | /** 21 | * @author Kamran Zafar 22 | * 23 | */ 24 | public class ProxyProviderFactory { 25 | private static ProxyProvider proxyProvider = new JdkProxyProvider(); 26 | 27 | public static void setDefaultProxyProvider(ProxyProvider proxyProvider) { 28 | ProxyProviderFactory.proxyProvider = proxyProvider; 29 | } 30 | 31 | /** 32 | * Returns the default instance of ProxyProvider implementation 33 | * 34 | * @return 35 | */ 36 | public static ProxyProvider create() { 37 | return proxyProvider; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /JCL2/spring/src/main/java/org/xeustechnologies/jcl/spring/JclNamespaceHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.spring; 19 | 20 | import org.springframework.beans.factory.xml.NamespaceHandlerSupport; 21 | 22 | /** 23 | * @author Kamran Zafar 24 | * 25 | */ 26 | public class JclNamespaceHandler extends NamespaceHandlerSupport { 27 | private static final String JCL_ELEMENT = "jcl"; 28 | private static final String JCL_REF_ELEMENT = "jcl-ref"; 29 | 30 | public void init() { 31 | registerBeanDefinitionParser( JCL_ELEMENT, new JclBeanDefinitionParser() ); 32 | registerBeanDefinitionDecorator( JCL_REF_ELEMENT, new JclBeanDefinitionDecorator() ); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /JCL/spring-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | test-jcl.jar 15 | 16 | 17 | 18 | 19 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | xeus.jcl.test.Test 34 | 35 | 36 | 37 | 38 | Kamran 39 | 40 | 41 | Zafar 42 | 43 | 44 | -------------------------------------------------------------------------------- /JCL2/test-jcl/src/main/java/org/xeustechnologies/jcl/test/Test.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl.test; 2 | 3 | import java.io.Serializable; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.xeustechnologies.jcl.test.TestInterface; 8 | 9 | public class Test implements Serializable, TestInterface { 10 | 11 | /** 12 | * serialVersionUID:long 13 | */ 14 | private static final long serialVersionUID = 7683330206220877077L; 15 | private String firstName; 16 | private String lastName; 17 | 18 | private final transient Logger logger = LoggerFactory.getLogger(Test.class); 19 | 20 | public Test() { 21 | firstName = "World"; 22 | lastName = ""; 23 | } 24 | 25 | public Test(String firstName) { 26 | this.firstName = firstName; 27 | } 28 | 29 | public String sayHello() { 30 | String hello = "Hello " + firstName + " " + lastName; 31 | 32 | logger.debug( "Hello {} {}", firstName, lastName ); 33 | 34 | return hello; 35 | } 36 | 37 | public String getFirstName() { 38 | return firstName; 39 | } 40 | 41 | public void setFirstName(String firstName) { 42 | this.firstName = firstName; 43 | } 44 | 45 | public String getLastName() { 46 | return lastName; 47 | } 48 | 49 | public void setLastName(String lastName) { 50 | this.lastName = lastName; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/exception/JclContextException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.exception; 19 | 20 | /** 21 | * 22 | * @author Kamran 23 | * 24 | */ 25 | public class JclContextException extends JclException { 26 | 27 | /** 28 | * serialVersionUID:long 29 | */ 30 | private static final long serialVersionUID = -799657685317877954L; 31 | 32 | public JclContextException() { 33 | super(); 34 | } 35 | 36 | public JclContextException(String message, Throwable cause) { 37 | super( message, cause ); 38 | } 39 | 40 | public JclContextException(String message) { 41 | super( message ); 42 | } 43 | 44 | public JclContextException(Throwable cause) { 45 | super( cause ); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /JCL/docs/web/js/shBrushXml.js: -------------------------------------------------------------------------------- 1 | /* 2 | * JsMin 3 | * Javascript Compressor 4 | * http://www.crockford.com/ 5 | * http://www.smallsharptools.com/ 6 | */ 7 | 8 | dp.sh.Brushes.Xml=function() 9 | {this.CssClass='dp-xml';this.Style='.dp-xml .cdata { color: #ff1493; }'+'.dp-xml .tag, .dp-xml .tag-name { color: #069; font-weight: bold; }'+'.dp-xml .attribute { color: red; }'+'.dp-xml .attribute-value { color: blue; }';} 10 | dp.sh.Brushes.Xml.prototype=new dp.sh.Highlighter();dp.sh.Brushes.Xml.Aliases=['xml','xhtml','xslt','html','xhtml'];dp.sh.Brushes.Xml.prototype.ProcessRegexList=function() 11 | {function push(array,value) 12 | {array[array.length]=value;} 13 | var index=0;var match=null;var regex=null;this.GetMatches(new RegExp('(\<|<)\\!\\[[\\w\\s]*?\\[(.|\\s)*?\\]\\](\>|>)','gm'),'cdata');this.GetMatches(new RegExp('(\<|<)!--\\s*.*?\\s*--(\>|>)','gm'),'comments');regex=new RegExp('([:\\w-\.]+)\\s*=\\s*(".*?"|\'.*?\'|\\w+)*|(\\w+)','gm');while((match=regex.exec(this.code))!=null) 14 | {if(match[1]==null) 15 | {continue;} 16 | push(this.matches,new dp.sh.Match(match[1],match.index,'attribute'));if(match[2]!=undefined) 17 | {push(this.matches,new dp.sh.Match(match[2],match.index+match[0].indexOf(match[2]),'attribute-value'));}} 18 | this.GetMatches(new RegExp('(\<|<)/*\\?*(?!\\!)|/*\\?*(\>|>)','gm'),'tag');regex=new RegExp('(?:\<|<)/*\\?*\\s*([:\\w-\.]+)','gm');while((match=regex.exec(this.code))!=null) 19 | {push(this.matches,new dp.sh.Match(match[1],match.index+match[0].indexOf(match[1]),'tag-name'));}} 20 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/exception/JclException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.exception; 19 | 20 | /** 21 | * General custom exception 22 | * 23 | * @author Kamran Zafar 24 | * 25 | */ 26 | public class JclException extends RuntimeException { 27 | /** 28 | * Default serial id 29 | */ 30 | private static final long serialVersionUID = 1L; 31 | 32 | /** 33 | * Default constructor 34 | */ 35 | public JclException() { 36 | super(); 37 | } 38 | 39 | /** 40 | * @param message 41 | */ 42 | public JclException(String message) { 43 | super( message ); 44 | } 45 | 46 | /** 47 | * @param cause 48 | */ 49 | public JclException(Throwable cause) { 50 | super( cause ); 51 | } 52 | 53 | /** 54 | * @param message 55 | * @param cause 56 | */ 57 | public JclException(String message, Throwable cause) { 58 | super( message, cause ); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /JCL2/test-web/src/main/resources/jcl.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 3 7 | true 8 | 9 | 10 | 1 11 | true 12 | 13 | 14 | 2 15 | true 16 | 17 | 18 | 4 19 | true 20 | 21 | 22 | 5 23 | true 24 | 25 | 26 | 27 | webapp:WEB-INF/mylib/ 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | true 36 | true 37 | org.xeustechnologies.jcl.test.* 38 | 39 | 40 | 41 | webapp:WEB-INF/mylib/test-jcl.jar 42 | 43 | 44 | 45 | 46 | 47 | webapp:WEB-INF/mylib/test-jcl.jar 48 | 49 | 50 | -------------------------------------------------------------------------------- /JCL2/core/src/test/java/org/xeustechnologies/jcl/DelegateProxyClassLoaderTest.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl; 2 | 3 | import org.junit.Test; 4 | import org.xeustechnologies.jcl.sample.Test1; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | /** 9 | * 10 | * DelegateProxyClassLoaderTest test with DelegateProxyClassLoader. 11 | * 12 | * @author Jerome Guibert 13 | * 14 | */ 15 | public class DelegateProxyClassLoaderTest { 16 | 17 | @Test 18 | public void checkDelegateProxyClassLoader() throws ClassNotFoundException { 19 | /** 20 | * First classLoader without Test1 21 | */ 22 | JarClassLoader classLoader = new JarClassLoader(); 23 | doIsolated(classLoader); 24 | assertTrue(classLoader.getLocalLoader().isEnabled()); 25 | try { 26 | classLoader.loadClass(Test1.class.getName()); 27 | fail("Should obtain java.lang.ClassNotFoundException: org.xeustechnologies.jcl.sample.Test1"); 28 | } catch (ClassNotFoundException e) { 29 | } 30 | /** 31 | * target classLoader with Test1 32 | */ 33 | JarClassLoader target = new JarClassLoader(); 34 | doIsolated(classLoader); 35 | assertTrue(classLoader.getLocalLoader().isEnabled()); 36 | target.add(Test1.class.getName()); 37 | target.loadClass(Test1.class.getName()); 38 | /** 39 | * Add delegate 40 | */ 41 | classLoader.addLoader(new DelegateProxyClassLoader(target)); 42 | classLoader.loadClass(Test1.class.getName()); 43 | } 44 | 45 | /** 46 | * Only local loader. 47 | * 48 | * @param classLoader 49 | */ 50 | protected void doIsolated(JarClassLoader classLoader) { 51 | classLoader.getCurrentLoader().setEnabled(false); 52 | classLoader.getParentLoader().setEnabled(false); 53 | classLoader.getThreadLoader().setEnabled(false); 54 | classLoader.getSystemLoader().setEnabled(false); 55 | classLoader.getOsgiBootLoader().setEnabled(false); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/context/DefaultContextLoader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.context; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | import org.xeustechnologies.jcl.JarClassLoader; 22 | 23 | /** 24 | * This class is builds the context from a single JCL instance. This should be 25 | * used if a single JarClassLoader is instantiated programmatically. 26 | * 27 | * @author Kamran 28 | * 29 | */ 30 | public class DefaultContextLoader implements JclContextLoader { 31 | private final JclContext jclContext; 32 | private final JarClassLoader jcl; 33 | 34 | private final transient Logger logger = LoggerFactory.getLogger(DefaultContextLoader.class); 35 | 36 | public DefaultContextLoader(JarClassLoader jcl) { 37 | jclContext = new JclContext(); 38 | this.jcl = jcl; 39 | } 40 | 41 | /** 42 | * Loads a single JCL instance in context 43 | * 44 | * @see org.xeustechnologies.jcl.context.JclContextLoader#loadContext() 45 | */ 46 | public void loadContext() { 47 | jclContext.addJcl( JclContext.DEFAULT_NAME, jcl ); 48 | 49 | logger.debug( "Default JarClassLoader loaded into context." ); 50 | } 51 | 52 | public void unloadContext() { 53 | JclContext.destroy(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/exception/JclException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Foobar. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl.exception; 28 | 29 | /** 30 | * General custom exception 31 | * 32 | * @author Kamran Zafar 33 | * 34 | */ 35 | public class JclException extends RuntimeException { 36 | /** 37 | * Default serial id 38 | */ 39 | private static final long serialVersionUID = 1L; 40 | 41 | /** 42 | * Default constructor 43 | */ 44 | public JclException() { 45 | super(); 46 | } 47 | 48 | /** 49 | * @param message 50 | */ 51 | public JclException(String message) { 52 | super( message ); 53 | } 54 | 55 | /** 56 | * @param cause 57 | */ 58 | public JclException(Throwable cause) { 59 | super( cause ); 60 | } 61 | 62 | /** 63 | * @param message 64 | * @param cause 65 | */ 66 | public JclException(String message, Throwable cause) { 67 | super( message, cause ); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /JCL2/core/src/test/resources/jcl.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 6 7 | true 8 | 9 | 10 | 11 | target/test-jcl.jar 12 | 13 | 14 | 15 | 16 | 17 | 3 18 | false 19 | 20 | 21 | 1 22 | false 23 | 24 | 25 | 2 26 | false 27 | 28 | 29 | 4 30 | false 31 | 32 | 33 | 5 34 | false 35 | 36 | 37 | 38 | target/test-jcl.jar 39 | 40 | 41 | 42 | 43 | 44 | true 45 | true 46 | org.xeustechnologies.jcl.test.* 47 | 48 | 49 | 50 | target/test-jcl.jar 51 | 52 | 53 | 54 | 55 | target/test-jcl.jar 56 | 57 | 58 | -------------------------------------------------------------------------------- /JCL2/spring/src/main/java/org/xeustechnologies/jcl/spring/JclBeanDefinitionParser.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.spring; 19 | 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | import org.springframework.beans.factory.config.BeanDefinition; 23 | import org.springframework.beans.factory.config.BeanDefinitionHolder; 24 | import org.springframework.beans.factory.xml.BeanDefinitionParser; 25 | import org.springframework.beans.factory.xml.ParserContext; 26 | import org.w3c.dom.Element; 27 | import org.xeustechnologies.jcl.JarClassLoader; 28 | 29 | /** 30 | * @author Kamran Zafar 31 | * 32 | */ 33 | public class JclBeanDefinitionParser implements BeanDefinitionParser { 34 | 35 | private final transient Logger logger = LoggerFactory.getLogger( JclBeanDefinitionParser.class ); 36 | 37 | public BeanDefinition parse(Element element, ParserContext parserContext) { 38 | BeanDefinitionHolder holder = parserContext.getDelegate().parseBeanDefinitionElement( element ); 39 | 40 | String beanName = holder.getBeanName(); 41 | 42 | BeanDefinition bd = holder.getBeanDefinition(); 43 | bd.setBeanClassName( JarClassLoader.class.getName() ); 44 | 45 | logger.info( "Registering JarClassLoader bean: {}", beanName ); 46 | 47 | parserContext.getRegistry().registerBeanDefinition( beanName, bd ); 48 | 49 | return bd; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /JCL/docs/web/css/style.css: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Christopher Robinson 3 | Email : christopher@edg3.co.uk 4 | Website : http://www.edg3.co.uk/ 5 | */ 6 | * { 7 | border:0; 8 | margin:0; 9 | padding:0; 10 | } 11 | 12 | /* general */ 13 | a { 14 | color:#777; 15 | text-decoration:none; 16 | } 17 | 18 | a:hover { 19 | color:#333; 20 | text-decoration:none; 21 | } 22 | 23 | /* body */ 24 | body { 25 | background:#fafafa; 26 | color:#555; 27 | font:0.8em Arial, Helvetica, "bitstream vera sans", sans-serif; 28 | } 29 | 30 | /* header */ 31 | #header { 32 | border-bottom:1px solid #999; 33 | height:80px; 34 | margin:0 auto; 35 | width:751px; 36 | } 37 | #header h1 { 38 | color:#888; 39 | font-size:300%; 40 | letter-spacing:-3px; 41 | text-align:right; 42 | padding:5px; 43 | margin-bottom:-20px; 44 | } 45 | #header h2 { 46 | color:#CCC; 47 | font-size:200%; 48 | letter-spacing:-2px; 49 | text-align:right; 50 | } 51 | 52 | /* navigation */ 53 | #navigation { 54 | background:#fafafa; 55 | border-right:1px solid #999; 56 | margin:0 auto; 57 | width:750px; 58 | height:50px; 59 | list-style:none; 60 | } 61 | #navigation li { 62 | border-left:1px solid #999; 63 | float:left; 64 | width:124px; 65 | list-style:none; 66 | } 67 | #navigation a { 68 | color:#555; 69 | display:block; 70 | line-height:50px; 71 | text-align:center; 72 | } 73 | #navigation a:hover { 74 | background:#e3e3e3; 75 | color:#555; 76 | } 77 | #navigation .active { 78 | background:#e3e3e3; 79 | color:#777; 80 | } 81 | 82 | /* content */ 83 | #content { 84 | height:auto; 85 | margin:0 auto; 86 | padding:0 0 20px; 87 | width:751px; 88 | } 89 | #content h1 { 90 | border-bottom:1px dashed #999; 91 | font-size:1.8em; 92 | padding:20px 0 0; 93 | } 94 | #content p { 95 | padding:20px 20px 0; 96 | } 97 | 98 | /* footer */ 99 | #footer { 100 | border-top:1px solid #999; 101 | height:50px; 102 | margin:0 auto; 103 | padding:10px; 104 | text-align:center; 105 | width:751px; 106 | } -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/utils/Utils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.utils; 19 | 20 | /** 21 | * Class that contains utility methods 22 | * 23 | * @author Kamran Zafar 24 | * 25 | */ 26 | public class Utils { 27 | /** 28 | * Converts wildcard to regular expression 29 | * 30 | * @param wildcard 31 | * @return regex 32 | */ 33 | public static String wildcardToRegex(String wildcard) { 34 | StringBuffer s = new StringBuffer( wildcard.length() ); 35 | s.append( '^' ); 36 | for( int i = 0, is = wildcard.length(); i < is; i++ ) { 37 | char c = wildcard.charAt( i ); 38 | switch (c) { 39 | case '*': 40 | s.append( ".*" ); 41 | break; 42 | case '?': 43 | s.append( "." ); 44 | break; 45 | case '(': 46 | case ')': 47 | case '[': 48 | case ']': 49 | case '$': 50 | case '^': 51 | case '.': 52 | case '{': 53 | case '}': 54 | case '|': 55 | case '\\': 56 | s.append( "\\" ); 57 | s.append( c ); 58 | break; 59 | default: 60 | s.append( c ); 61 | break; 62 | } 63 | } 64 | s.append( '$' ); 65 | return ( s.toString() ); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/proxy/JdkProxyProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.proxy; 19 | 20 | import java.lang.reflect.InvocationHandler; 21 | import java.lang.reflect.Method; 22 | import java.lang.reflect.Proxy; 23 | 24 | import org.xeustechnologies.jcl.JclUtils; 25 | 26 | /** 27 | * Creates JDK proxies 28 | * 29 | * @author Kamran Zafar 30 | * 31 | */ 32 | public class JdkProxyProvider implements ProxyProvider { 33 | private class JdkProxyHandler implements InvocationHandler { 34 | private final Object delegate; 35 | 36 | public JdkProxyHandler(Object delegate) { 37 | this.delegate = delegate; 38 | } 39 | 40 | /** 41 | * 42 | * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, 43 | * java.lang.reflect.Method, java.lang.Object[]) 44 | */ 45 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 46 | Method delegateMethod = delegate.getClass().getMethod( method.getName(), method.getParameterTypes() ); 47 | return delegateMethod.invoke( delegate, args ); 48 | } 49 | } 50 | 51 | public Object createProxy(Object object, Class superClass, Class[] interfaces, ClassLoader cl) { 52 | JdkProxyHandler handler = new JdkProxyHandler( object ); 53 | return Proxy.newProxyInstance( cl == null ? JclUtils.class.getClassLoader() : cl, interfaces, handler ); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /JCL2/core/src/main/resources/org/xeustechnologies/jcl/context/jcl-context.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /JCL2/web/src/main/java/org/xeustechnologies/jcl/web/JclContextLoaderListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.web; 18 | 19 | import javax.servlet.ServletContextEvent; 20 | import javax.servlet.ServletContextListener; 21 | 22 | import org.xeustechnologies.jcl.context.XmlContextLoader; 23 | 24 | /** 25 | * This class is used in web applications to load the JCL context from XML file. 26 | * 27 | * @author Kamran 28 | * 29 | */ 30 | public class JclContextLoaderListener implements ServletContextListener { 31 | private static final String JCL_CONTEXT = "jcl-context"; 32 | protected XmlContextLoader contextLoader; 33 | 34 | /** 35 | * Destroys the context 36 | * 37 | * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) 38 | */ 39 | public void contextDestroyed(ServletContextEvent sce) { 40 | contextLoader.unloadContext(); 41 | } 42 | 43 | /** 44 | * The context is initialised from xml on web application's deploy-time 45 | * 46 | * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) 47 | */ 48 | public void contextInitialized(ServletContextEvent sce) { 49 | String jclConfig = sce.getServletContext().getInitParameter( JCL_CONTEXT ); 50 | 51 | contextLoader = new XmlContextLoader( jclConfig ); 52 | contextLoader.addPathResolver( new WebAppPathResolver( sce.getServletContext() ) ); 53 | contextLoader.loadContext(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /JCL2/core/src/test/java/org/xeustechnologies/jcl/ClasspathResourcesTest.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl; 2 | 3 | import org.junit.Test; 4 | 5 | import java.io.File; 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.util.Properties; 9 | 10 | import static org.junit.Assert.assertEquals; 11 | import static org.junit.Assert.assertNotNull; 12 | import static org.junit.Assert.assertTrue; 13 | 14 | /** 15 | * Test handling resources inside and outside jars 16 | */ 17 | public class ClasspathResourcesTest { 18 | 19 | @Test 20 | public void testLoadResource() throws Exception { 21 | final String name = "test"; 22 | ClasspathResources jarResources = getClasspathResources(name); 23 | 24 | URL resourceURL = jarResources.getResourceURL("test.properties"); 25 | Properties props = getProperties(resourceURL); 26 | assertEquals("testval", props.getProperty("testkey")); 27 | } 28 | 29 | @Test 30 | public void testLoadResourcesFromJar() throws Exception { 31 | final String name = "test.jar"; 32 | ClasspathResources jarResources = getClasspathResources(name); 33 | 34 | URL resourceURL = jarResources.getResourceURL("test.properties"); 35 | Properties props = getProperties(resourceURL); 36 | assertEquals("testval in jar", props.getProperty("testkey")); 37 | 38 | resourceURL = jarResources.getResourceURL("test/subdir.properties"); 39 | props = getProperties(resourceURL); 40 | assertEquals("testval in jar in subdirectory", props.getProperty("testkey")); 41 | } 42 | 43 | private ClasspathResources getClasspathResources(String name) { 44 | final URL testJar = ClassLoader.getSystemClassLoader().getResource(name); 45 | assertNotNull("Could not find file or directory named '" + name + "'. It should be in the test resources directory", testJar); 46 | ClasspathResources jarResources = new ClasspathResources(); 47 | jarResources.loadResource(testJar); 48 | return jarResources; 49 | } 50 | 51 | private Properties getProperties(URL resourceURL) throws IOException { 52 | assertNotNull(resourceURL); 53 | Properties props = new Properties(); 54 | props.load(resourceURL.openStream()); 55 | return props; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /JCL2/core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | org.xeustechnologies 6 | jcl 7 | 2.9-SNAPSHOT 8 | 9 | jcl-core 10 | JCL - Core 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-dependency-plugin 16 | 17 | 18 | copy 19 | process-test-resources 20 | 21 | copy 22 | 23 | 24 | 25 | 26 | org.xeustechnologies 27 | test-jcl 28 | ${pom.version} 29 | 30 | 31 | ./target 32 | true 33 | false 34 | true 35 | 36 | 37 | 38 | copy-dependencies 39 | install 40 | 41 | copy-dependencies 42 | 43 | 44 | ./target/dependencies 45 | false 46 | true 47 | junit,test-jcl 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.xeustechnologies 57 | test-jcl 58 | ${project.version} 59 | test 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/ProxyClassLoader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl; 19 | 20 | import java.io.InputStream; 21 | import java.net.URL; 22 | 23 | /** 24 | * @author Kamran Zafar 25 | * 26 | */ 27 | public abstract class ProxyClassLoader implements Comparable { 28 | // Default order 29 | protected int order = 5; 30 | // Enabled by default 31 | protected boolean enabled = true; 32 | 33 | public int getOrder() { 34 | return order; 35 | } 36 | 37 | /** 38 | * Set loading order 39 | * 40 | * @param order 41 | */ 42 | public void setOrder(int order) { 43 | this.order = order; 44 | } 45 | 46 | /** 47 | * Loads the class 48 | * 49 | * @param className 50 | * @param resolveIt 51 | * @return class 52 | */ 53 | public abstract Class loadClass(String className, boolean resolveIt); 54 | 55 | /** 56 | * Loads the resource 57 | * 58 | * @param name 59 | * @return InputStream 60 | */ 61 | public abstract InputStream loadResource(String name); 62 | 63 | /** 64 | * Finds the resource 65 | * 66 | * @param name 67 | * @return InputStream 68 | */ 69 | public abstract URL findResource(String name); 70 | 71 | public boolean isEnabled() { 72 | return enabled; 73 | } 74 | 75 | public void setEnabled(boolean enabled) { 76 | this.enabled = enabled; 77 | } 78 | 79 | public int compareTo(ProxyClassLoader o) { 80 | return order - o.getOrder(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /JCL2/core/src/test/java/org/xeustechnologies/jcl/AbstractClassLoaderTest.java: -------------------------------------------------------------------------------- 1 | package org.xeustechnologies.jcl; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import org.junit.Test; 7 | 8 | /** 9 | * AbstractClassLoaderTest test case on AbstractClassLoader class. 10 | * 11 | * @author Jerome Guibert 12 | * 13 | */ 14 | public class AbstractClassLoaderTest { 15 | 16 | @Test 17 | public void checkInitializationOfDefaultProxyClassLoader() { 18 | AbstractClassLoader classLoader = new AbstractClassLoader() { 19 | }; 20 | 21 | assertNotNull("SystemLoader should not be null", classLoader.getSystemLoader()); 22 | assertNotNull("ThreadLoader should not be null", classLoader.getThreadLoader()); 23 | assertNotNull("ParentLoader should not be null", classLoader.getParentLoader()); 24 | assertNotNull("CurrentLoader should not be null", classLoader.getCurrentLoader()); 25 | assertNotNull("OsgiBootLoader should not be null", classLoader.getOsgiBootLoader()); 26 | 27 | assertEquals("SystemLoader order should be 50", 50, classLoader.getSystemLoader().getOrder()); 28 | assertEquals("ThreadLoader order should be 40", 40, classLoader.getThreadLoader().getOrder()); 29 | assertEquals("ParentLoader order should be 30", 30, classLoader.getParentLoader().getOrder()); 30 | assertEquals("CurrentLoader order should be 20", 20, classLoader.getCurrentLoader().getOrder()); 31 | assertEquals("OsgiBootLoader order should be 0", 0, classLoader.getOsgiBootLoader().getOrder()); 32 | 33 | } 34 | 35 | @Test 36 | public void checkDefaultEnabledProxy() { 37 | AbstractClassLoader classLoader = new AbstractClassLoader() { 38 | }; 39 | 40 | assertEquals(Configuration.isCurrentLoaderEnabled(), classLoader.getCurrentLoader().isEnabled()); 41 | assertEquals(Configuration.isParentLoaderEnabled(), classLoader.getParentLoader().isEnabled()); 42 | assertEquals(Configuration.isThreadContextLoaderEnabled(), classLoader.getThreadLoader().isEnabled()); 43 | assertEquals(Configuration.isSystemLoaderEnabled(), classLoader.getSystemLoader().isEnabled()); 44 | assertEquals(Configuration.isOsgiBootDelegationEnabled(), classLoader.getOsgiBootLoader().isEnabled()); 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /JCL2/spring/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | org.xeustechnologies 6 | jcl 7 | 2.9-SNAPSHOT 8 | 9 | 10 | jcl-spring 11 | JCL - Spring 12 | 13 | 14 | 15 | org.apache.maven.plugins 16 | maven-dependency-plugin 17 | 18 | 19 | copy 20 | process-test-resources 21 | 22 | copy 23 | 24 | 25 | 26 | 27 | org.xeustechnologies 28 | test-jcl 29 | ${pom.version} 30 | 31 | 32 | ./target 33 | true 34 | false 35 | true 36 | 37 | 38 | 39 | copy-dependencies 40 | install 41 | 42 | copy-dependencies 43 | 44 | 45 | ./target/dependencies 46 | false 47 | true 48 | junit,test-jcl 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.xeustechnologies 58 | jcl-core 59 | ${project.version} 60 | 61 | 62 | org.xeustechnologies 63 | test-jcl 64 | ${project.version} 65 | test 66 | 67 | 68 | org.springframework 69 | spring-context 70 | 3.2.5.RELEASE 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/loader/Loader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with JCL. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl.loader; 28 | 29 | import java.io.InputStream; 30 | 31 | /** 32 | * @author Kamran Zafar 33 | * 34 | */ 35 | public abstract class Loader implements Comparable { 36 | // Default order 37 | protected int order = 5; 38 | // Enabled by default 39 | protected boolean enabled = true; 40 | 41 | public int getOrder() { 42 | return order; 43 | } 44 | 45 | /** 46 | * Set loading order 47 | * 48 | * @param order 49 | */ 50 | public void setOrder(int order) { 51 | this.order = order; 52 | } 53 | 54 | /** 55 | * Loads the class 56 | * 57 | * @param className 58 | * @param resolveIt 59 | * @return class 60 | */ 61 | public abstract Class load(String className, boolean resolveIt); 62 | 63 | /** 64 | * Loads the resource 65 | * 66 | * @param name 67 | * @return java.io.InputStream 68 | */ 69 | public abstract InputStream loadResource(String name); 70 | 71 | /** 72 | * Checks if Loader is Enabled/Disabled 73 | * 74 | * @return boolean 75 | */ 76 | public boolean isEnabled() { 77 | return enabled; 78 | } 79 | 80 | /** 81 | * Enable/Disable Laoder 82 | * 83 | * @param enabled 84 | */ 85 | public void setEnabled(boolean enabled) { 86 | this.enabled = enabled; 87 | } 88 | 89 | /* 90 | * (non-Javadoc) 91 | * 92 | * @see java.lang.Comparable#compareTo(java.lang.Object) 93 | */ 94 | public int compareTo(Loader o) { 95 | return order - o.getOrder(); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/DelegateProxyClassLoader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. 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, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | */ 20 | package org.xeustechnologies.jcl; 21 | 22 | import java.io.InputStream; 23 | import java.net.URL; 24 | 25 | /** 26 | * 27 | * DelegateProxyClassLoader implements a ProxyClassLoader which delegate loading 28 | * to a specific AbstractClassLoader loader instance.. 29 | * 30 | * @author Jerome Guibert 31 | * 32 | */ 33 | public class DelegateProxyClassLoader extends ProxyClassLoader { 34 | 35 | private final AbstractClassLoader delegate; 36 | 37 | /** 38 | * Build a new instance of DelegateProxyClassLoader.java. 39 | * 40 | * @param delegate 41 | * instance of AbstractClassLoader where to delegate 42 | * @throws NullPointerException 43 | * if delegate is null 44 | */ 45 | public DelegateProxyClassLoader(AbstractClassLoader delegate) throws NullPointerException { 46 | super(); 47 | if (delegate == null) 48 | throw new NullPointerException("delegate can't be null"); 49 | this.delegate = delegate; 50 | this.order = 15; 51 | } 52 | 53 | @SuppressWarnings("rawtypes") 54 | public Class loadClass(String className, boolean resolveIt) { 55 | Class result; 56 | try { 57 | result = delegate.loadClass(className, resolveIt); 58 | } catch (ClassNotFoundException e) { 59 | return null; 60 | } 61 | return result; 62 | } 63 | 64 | public InputStream loadResource(String name) { 65 | return delegate.getResourceAsStream(name); 66 | } 67 | 68 | @Override 69 | public URL findResource(String name) { 70 | return delegate.getResource(name); 71 | } 72 | 73 | public AbstractClassLoader getDelegate() { 74 | return delegate; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/Configuration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with JCL. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl; 28 | 29 | /** 30 | * General configuration 31 | * 32 | * @author Kamran Zafar 33 | * 34 | */ 35 | public class Configuration { 36 | 37 | private static final String JCL_SUPPRESS_COLLISION_EXCEPTION = "jcl.suppressCollisionException"; 38 | 39 | public static boolean supressCollisionException() { 40 | if( System.getProperty( JCL_SUPPRESS_COLLISION_EXCEPTION ) == null ) 41 | return true; 42 | 43 | return Boolean.parseBoolean( System.getProperty( JCL_SUPPRESS_COLLISION_EXCEPTION ) ); 44 | } 45 | 46 | /** 47 | * Check to see if Loader is enabled/disabled The Loader can be 48 | * enabled/disabled programmatically or by passing the class names as 49 | * argument e.g.
50 | * -Dxeus.jcl.AbstractClassLoader.ParentLoader=false 51 | * 52 | * @param clazz 53 | * @return 54 | */ 55 | public static boolean isLoaderEnabled(Class clazz) { 56 | if( System.getProperty( clazz.getName() ) == null ) 57 | return true; 58 | 59 | return Boolean.parseBoolean( System.getProperty( clazz.getName() ) ); 60 | } 61 | 62 | public static boolean isLocalLoaderEnabled() { 63 | return isLoaderEnabled( JarClassLoader.LocalLoader.class ); 64 | } 65 | 66 | public static boolean isCurrentLoaderEnabled() { 67 | return isLoaderEnabled( AbstractClassLoader.CurrentLoader.class ); 68 | } 69 | 70 | public static boolean isParentLoaderEnabled() { 71 | return isLoaderEnabled( AbstractClassLoader.ParentLoader.class ); 72 | } 73 | 74 | public static boolean isSystemLoaderEnabled() { 75 | return isLoaderEnabled( AbstractClassLoader.SystemLoader.class ); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/context/JclContext.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.context; 19 | 20 | import java.util.Collections; 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | import org.xeustechnologies.jcl.JarClassLoader; 25 | import org.xeustechnologies.jcl.exception.JclContextException; 26 | 27 | /** 28 | * JclContext holds all the JarClassLoader instances so that they can be 29 | * accessed from anywhere in the application. 30 | * 31 | * @author Kamran 32 | * 33 | */ 34 | public class JclContext { 35 | private static final Map loaders = Collections 36 | .synchronizedMap( new HashMap() ); 37 | public static final String DEFAULT_NAME = "jcl"; 38 | 39 | public JclContext() { 40 | validate(); 41 | } 42 | 43 | private void validate() { 44 | if( isLoaded() ) { 45 | throw new JclContextException( "Context already loaded. Destroy the existing context to create a new one." ); 46 | } 47 | } 48 | 49 | public static boolean isLoaded() { 50 | return !loaders.isEmpty(); 51 | } 52 | 53 | /** 54 | * Populates the context with JarClassLoader instances 55 | * 56 | * @param name 57 | * @param jcl 58 | */ 59 | public void addJcl(String name, JarClassLoader jcl) { 60 | if( loaders.containsKey( name ) ) 61 | throw new JclContextException( "JarClassLoader[" + name + "] already exist. Name must be unique" ); 62 | 63 | loaders.put( name, jcl ); 64 | } 65 | 66 | /** 67 | * Clears the context 68 | */ 69 | public static void destroy() { 70 | if( isLoaded() ) { 71 | loaders.clear(); 72 | } 73 | } 74 | 75 | public static JarClassLoader get() { 76 | return loaders.get( DEFAULT_NAME ); 77 | } 78 | 79 | public static JarClassLoader get(String name) { 80 | return loaders.get( name ); 81 | } 82 | 83 | public static Map getAll() { 84 | return Collections.unmodifiableMap( loaders ); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /JCL/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | Jar Class Loader 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /JCL2/spring/src/test/resources/spring-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | target/test-jcl.jar 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | target/test-jcl.jar 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | Zafar 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | Zafar 42 | 43 | 44 | 45 | 46 | 47 | 48 | Kamran 49 | 50 | 51 | 52 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.xeustechnologies.jcl.test.Test 71 | 72 | 73 | 74 | 75 | Kamran 76 | 77 | 78 | Zafar 79 | 80 | 81 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/proxy/CglibProxyProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.proxy; 19 | 20 | import java.lang.reflect.Method; 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | import net.sf.cglib.proxy.Enhancer; 25 | import net.sf.cglib.proxy.MethodInterceptor; 26 | import net.sf.cglib.proxy.MethodProxy; 27 | 28 | import org.xeustechnologies.jcl.JclUtils; 29 | 30 | /** 31 | * Creates cglib proxies 32 | * 33 | * @author Kamran Zafar 34 | * 35 | */ 36 | public class CglibProxyProvider implements ProxyProvider { 37 | 38 | private class CglibProxyHandler implements MethodInterceptor { 39 | private final Object delegate; 40 | 41 | public CglibProxyHandler(Object delegate) { 42 | this.delegate = delegate; 43 | } 44 | 45 | /** 46 | * 47 | * @see net.sf.cglib.proxy.MethodInterceptor#intercept(java.lang.Object, 48 | * java.lang.reflect.Method, java.lang.Object[], 49 | * net.sf.cglib.proxy.MethodProxy) 50 | */ 51 | public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { 52 | Method delegateMethod = delegate.getClass().getMethod( method.getName(), method.getParameterTypes() ); 53 | return delegateMethod.invoke( delegate, args ); 54 | } 55 | } 56 | 57 | public Object createProxy(Object object, Class superClass, Class[] interfaces, ClassLoader cl) { 58 | CglibProxyHandler handler = new CglibProxyHandler( object ); 59 | 60 | Enhancer enhancer = new Enhancer(); 61 | 62 | if( superClass != null ) { 63 | enhancer.setSuperclass( superClass ); 64 | } 65 | 66 | enhancer.setCallback( handler ); 67 | 68 | if( interfaces != null ) { 69 | List il = new ArrayList(); 70 | 71 | for( Class i : interfaces ) { 72 | if( i.isInterface() ) { 73 | il.add( i ); 74 | } 75 | } 76 | 77 | enhancer.setInterfaces( il.toArray( new Class[il.size()] ) ); 78 | } 79 | 80 | enhancer.setClassLoader( cl == null ? JclUtils.class.getClassLoader() : cl ); 81 | 82 | return enhancer.create(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /JCL2/web/src/main/java/org/xeustechnologies/jcl/web/WebAppPathResolver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.web; 19 | 20 | import java.io.InputStream; 21 | import java.util.ArrayList; 22 | import java.util.Iterator; 23 | import java.util.List; 24 | import java.util.Set; 25 | 26 | import javax.servlet.ServletContext; 27 | 28 | import org.slf4j.Logger; 29 | import org.slf4j.LoggerFactory; 30 | import org.xeustechnologies.jcl.utils.PathResolver; 31 | 32 | /** 33 | * Resolves path to jar files and folder in a web application. The path must 34 | * starts with webapp: 35 | * 36 | * @author Kamran 37 | * 38 | */ 39 | public class WebAppPathResolver implements PathResolver { 40 | 41 | private final transient Logger logger = LoggerFactory.getLogger(WebAppPathResolver.class); 42 | 43 | private static final String JAR = ".jar"; 44 | private static final String WEB_APP = "webapp:"; 45 | private final ServletContext servletContext; 46 | 47 | public WebAppPathResolver(ServletContext servletContext) { 48 | this.servletContext = servletContext; 49 | } 50 | 51 | /** 52 | * Resolves path to jar files and folder in a web application 53 | * 54 | * @see org.xeustechnologies.jcl.utils.PathResolver#resolvePath(java.lang.String) 55 | */ 56 | @SuppressWarnings("unchecked") 57 | public Object[] resolvePath(String path) { 58 | if (path.startsWith( WEB_APP )) { 59 | String webpath = "/" + path.split( ":" )[1]; 60 | 61 | if (isJar( webpath )) { 62 | logger.debug( "Found jar: {}", webpath ); 63 | 64 | return new InputStream[] { servletContext.getResourceAsStream( webpath ) }; 65 | } 66 | 67 | Set paths = servletContext.getResourcePaths( webpath ); 68 | 69 | if (paths.size() > 0) { 70 | Iterator itr = paths.iterator(); 71 | List streams = new ArrayList(); 72 | 73 | while (itr.hasNext()) { 74 | String source = itr.next(); 75 | 76 | if (isJar( source )) { 77 | InputStream stream = servletContext.getResourceAsStream( source ); 78 | 79 | if (stream != null) { 80 | logger.debug( "Found jar: {}", source ); 81 | 82 | streams.add( stream ); 83 | } 84 | } 85 | } 86 | 87 | return streams.toArray( new InputStream[streams.size()] ); 88 | } 89 | 90 | } 91 | 92 | return null; 93 | } 94 | 95 | private boolean isJar(String path) { 96 | return path.toLowerCase().endsWith( JAR ); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/exception/ResourceNotFoundException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.exception; 19 | 20 | import org.xeustechnologies.jcl.ResourceType; 21 | 22 | /** 23 | * @author Kamran Zafar 24 | * 25 | */ 26 | public class ResourceNotFoundException extends JclException { 27 | /** 28 | * Default serial id 29 | */ 30 | private static final long serialVersionUID = 1L; 31 | 32 | private String resourceName; 33 | private ResourceType resourceType; 34 | 35 | /** 36 | * Default constructor 37 | */ 38 | public ResourceNotFoundException() { 39 | super(); 40 | } 41 | 42 | /** 43 | * @param message 44 | */ 45 | public ResourceNotFoundException(String message) { 46 | super( message ); 47 | } 48 | 49 | /** 50 | * @param resource 51 | * @param message 52 | */ 53 | public ResourceNotFoundException(String resource, String message) { 54 | super( message ); 55 | resourceName = resource; 56 | determineResourceType( resource ); 57 | } 58 | 59 | /** 60 | * @param e 61 | * @param resource 62 | * @param message 63 | */ 64 | public ResourceNotFoundException(Throwable e, String resource, String message) { 65 | super( message, e ); 66 | resourceName = resource; 67 | determineResourceType( resource ); 68 | } 69 | 70 | /** 71 | * @param resourceName 72 | */ 73 | private void determineResourceType(String resourceName) { 74 | if( resourceName.toLowerCase().endsWith( "." + ResourceType.CLASS.name().toLowerCase() ) ) 75 | resourceType = ResourceType.CLASS; 76 | else if( resourceName.toLowerCase().endsWith( "." + ResourceType.PROPERTIES.name().toLowerCase() ) ) 77 | resourceType = ResourceType.PROPERTIES; 78 | else if( resourceName.toLowerCase().endsWith( "." + ResourceType.XML.name().toLowerCase() ) ) 79 | resourceType = ResourceType.XML; 80 | else 81 | resourceType = ResourceType.UNKNOWN; 82 | } 83 | 84 | /** 85 | * @return {@link ResourceType} 86 | */ 87 | public String getResourceName() { 88 | return resourceName; 89 | } 90 | 91 | /** 92 | * @param resourceName 93 | */ 94 | public void setResourceName(String resourceName) { 95 | this.resourceName = resourceName; 96 | } 97 | 98 | /** 99 | * @return {@link ResourceType} 100 | */ 101 | public ResourceType getResourceType() { 102 | return resourceType; 103 | } 104 | 105 | /** 106 | * @param resourceType 107 | */ 108 | public void setResourceType(ResourceType resourceType) { 109 | this.resourceType = resourceType; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/CompositeProxyClassLoader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. 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, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | */ 20 | package org.xeustechnologies.jcl; 21 | 22 | import java.io.InputStream; 23 | import java.net.URL; 24 | import java.util.ArrayList; 25 | import java.util.Collection; 26 | import java.util.Iterator; 27 | import java.util.List; 28 | 29 | /** 30 | * 31 | * CompositeProxyClassLoader implement a composite of delegate class loader. 32 | * 33 | * @author Jerome Guibert 34 | * 35 | */ 36 | public class CompositeProxyClassLoader extends ProxyClassLoader { 37 | private final List proxyClassLoaders = new ArrayList(); 38 | 39 | /** 40 | * Build a new instance of CompositeProxyClassLoader.java. 41 | */ 42 | public CompositeProxyClassLoader() { 43 | super(); 44 | } 45 | 46 | @SuppressWarnings("rawtypes") 47 | public Class loadClass(String className, boolean resolveIt) { 48 | Class result = null; 49 | Iterator iterator = proxyClassLoaders.iterator(); 50 | while (result == null && iterator.hasNext()) { 51 | result = iterator.next().loadClass(className, resolveIt); 52 | } 53 | return result; 54 | } 55 | 56 | public InputStream loadResource(String name) { 57 | InputStream result = null; 58 | Iterator iterator = proxyClassLoaders.iterator(); 59 | while (result == null && iterator.hasNext()) { 60 | result = iterator.next().loadResource(name); 61 | } 62 | return result; 63 | } 64 | 65 | @Override 66 | public URL findResource(String name) { 67 | URL result = null; 68 | Iterator iterator = proxyClassLoaders.iterator(); 69 | while (result == null && iterator.hasNext()) { 70 | result = iterator.next().findResource(name); 71 | } 72 | return result; 73 | } 74 | 75 | /** 76 | * @return 77 | * @see java.util.List#isEmpty() 78 | */ 79 | public boolean isEmpty() { 80 | return proxyClassLoaders.isEmpty(); 81 | } 82 | 83 | public boolean contains(Object o) { 84 | return proxyClassLoaders.contains(o); 85 | } 86 | 87 | public boolean add(ProxyClassLoader e) { 88 | return proxyClassLoaders.add(e); 89 | } 90 | 91 | public boolean remove(ProxyClassLoader o) { 92 | return proxyClassLoaders.remove(o); 93 | } 94 | 95 | public boolean addAll(Collection c) { 96 | return proxyClassLoaders.addAll(c); 97 | } 98 | 99 | public List getProxyClassLoaders() { 100 | return proxyClassLoaders; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/exception/ResourceNotFoundException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with Foobar. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl.exception; 28 | 29 | import xeus.jcl.ResourceType; 30 | 31 | /** 32 | * @author Kamran Zafar 33 | * 34 | */ 35 | public class ResourceNotFoundException extends JclException { 36 | /** 37 | * Default serial id 38 | */ 39 | private static final long serialVersionUID = 1L; 40 | 41 | private String resourceName; 42 | private ResourceType resourceType; 43 | 44 | /** 45 | * Default constructor 46 | */ 47 | public ResourceNotFoundException() { 48 | super(); 49 | } 50 | 51 | /** 52 | * @param message 53 | */ 54 | public ResourceNotFoundException(String message) { 55 | super( message ); 56 | } 57 | 58 | /** 59 | * @param resource 60 | * @param message 61 | */ 62 | public ResourceNotFoundException(String resource, String message) { 63 | super( message ); 64 | resourceName = resource; 65 | determineResourceType( resource ); 66 | } 67 | 68 | /** 69 | * @param e 70 | * @param resource 71 | * @param message 72 | */ 73 | public ResourceNotFoundException(Throwable e, String resource, String message) { 74 | super( message, e ); 75 | resourceName = resource; 76 | determineResourceType( resource ); 77 | } 78 | 79 | /** 80 | * @param resourceName 81 | */ 82 | private void determineResourceType(String resourceName) { 83 | if( resourceName.toLowerCase().endsWith( ".class" ) ) 84 | resourceType = ResourceType.CLASS; 85 | else if( resourceName.toLowerCase().endsWith( ".properties" ) ) 86 | resourceType = ResourceType.PROPERTIES; 87 | else if( resourceName.toLowerCase().endsWith( ".xml" ) ) 88 | resourceType = ResourceType.XML; 89 | else 90 | resourceType = ResourceType.UNKNOWN; 91 | } 92 | 93 | /** 94 | * @return {@link ResourceType} 95 | */ 96 | public String getResourceName() { 97 | return resourceName; 98 | } 99 | 100 | /** 101 | * @param resourceName 102 | */ 103 | public void setResourceName(String resourceName) { 104 | this.resourceName = resourceName; 105 | } 106 | 107 | /** 108 | * @return {@link ResourceType} 109 | */ 110 | public ResourceType getResourceType() { 111 | return resourceType; 112 | } 113 | 114 | /** 115 | * @param resourceType 116 | */ 117 | public void setResourceType(ResourceType resourceType) { 118 | this.resourceType = resourceType; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /JCL2/test-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | org.xeustechnologies 6 | jcl 7 | 2.9-SNAPSHOT 8 | 9 | jcltest 10 | war 11 | JCL - Test Web App 12 | 13 | 14 | 15 | org.apache.maven.plugins 16 | maven-dependency-plugin 17 | 18 | 19 | copy 20 | process-resources 21 | 22 | copy 23 | 24 | 25 | 26 | 27 | org.xeustechnologies 28 | test-jcl 29 | ${pom.version} 30 | 31 | 32 | ${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/mylib 33 | true 34 | false 35 | true 36 | 37 | 38 | 39 | 40 | 41 | maven-deploy-plugin 42 | 43 | true 44 | 45 | 46 | 47 | org.mortbay.jetty 48 | maven-jetty-plugin 49 | 6.1.25 50 | 51 | 52 | 53 | 8080 54 | 60000 55 | 56 | 57 | 58 | 59 | java.awt.headless 60 | false 61 | 62 | 63 | file.encoding 64 | UTF-8 65 | 66 | 67 | 30 68 | jcltest 69 | ${project.build.directory}/${project.build.finalName}.war 70 | ${project.build.directory}/${project.build.finalName} 71 | ${project.build.directory}/${project.build.finalName}/WEB-INF/web.xml 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.xeustechnologies 79 | jcl-core 80 | ${project.version} 81 | 82 | 83 | org.xeustechnologies 84 | jcl-spring 85 | ${project.version} 86 | 87 | 88 | org.xeustechnologies 89 | jcl-web 90 | ${project.version} 91 | 92 | 93 | org.xeustechnologies 94 | test-jcl 95 | ${project.version} 96 | test 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/Configuration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl; 19 | 20 | /** 21 | * General configuration using System properties 22 | * 23 | * @author Kamran Zafar 24 | * 25 | */ 26 | public class Configuration { 27 | 28 | private static final String JCL_SUPPRESS_COLLISION_EXCEPTION = "jcl.suppressCollisionException"; 29 | private static final String JCL_SUPPRESS_MISSING_RESOURCE_EXCEPTION = "jcl.suppressMissingResourceException"; 30 | private static final String AUTO_PROXY = "jcl.autoProxy"; 31 | 32 | /** 33 | * OSGi boot delegation 34 | */ 35 | private static final String OSGI_BOOT_DELEGATION = "osgi.bootdelegation"; 36 | private static final String OSGI_BOOT_DELEGATION_STRICT = "osgi.bootdelegation.strict"; 37 | private static final String OSGI_BOOT_DELEGATION_CLASSES = "org.osgi.framework.bootdelegation"; 38 | 39 | public static boolean suppressCollisionException() { 40 | if (System.getProperty( JCL_SUPPRESS_COLLISION_EXCEPTION ) == null) 41 | return true; 42 | 43 | return Boolean.parseBoolean( System.getProperty( JCL_SUPPRESS_COLLISION_EXCEPTION ) ); 44 | } 45 | 46 | public static boolean suppressMissingResourceException() { 47 | if (System.getProperty( JCL_SUPPRESS_MISSING_RESOURCE_EXCEPTION ) == null) 48 | return true; 49 | 50 | return Boolean.parseBoolean( System.getProperty( JCL_SUPPRESS_MISSING_RESOURCE_EXCEPTION ) ); 51 | } 52 | 53 | public static boolean autoProxy() { 54 | if (System.getProperty( AUTO_PROXY ) == null) { 55 | return false; 56 | } 57 | 58 | return Boolean.parseBoolean( System.getProperty( AUTO_PROXY ) ); 59 | } 60 | 61 | @SuppressWarnings("unchecked") 62 | public static boolean isLoaderEnabled(Class cls) { 63 | if (System.getProperty( cls.getName() ) == null) 64 | return true; 65 | 66 | return Boolean.parseBoolean( System.getProperty( cls.getName() ) ); 67 | } 68 | 69 | public static boolean isSystemLoaderEnabled() { 70 | return isLoaderEnabled( AbstractClassLoader.SystemLoader.class ); 71 | } 72 | 73 | public static boolean isParentLoaderEnabled() { 74 | return isLoaderEnabled( AbstractClassLoader.ParentLoader.class ); 75 | } 76 | 77 | public static boolean isCurrentLoaderEnabled() { 78 | return isLoaderEnabled( AbstractClassLoader.CurrentLoader.class ); 79 | } 80 | 81 | public static boolean isLocalLoaderEnabled() { 82 | return isLoaderEnabled( JarClassLoader.LocalLoader.class ); 83 | } 84 | 85 | public static boolean isThreadContextLoaderEnabled() { 86 | if (System.getProperty( AbstractClassLoader.ThreadContextLoader.class.getName() ) == null) 87 | return false; 88 | 89 | return isLoaderEnabled( AbstractClassLoader.ThreadContextLoader.class ); 90 | } 91 | 92 | public static boolean isOsgiBootDelegationEnabled() { 93 | if (System.getProperty( OSGI_BOOT_DELEGATION ) == null) 94 | return false; 95 | 96 | return Boolean.parseBoolean( System.getProperty( OSGI_BOOT_DELEGATION ) ); 97 | } 98 | 99 | public static boolean isOsgiBootDelegationStrict() { 100 | if (System.getProperty( OSGI_BOOT_DELEGATION_STRICT ) == null) 101 | return true; 102 | 103 | return Boolean.parseBoolean( System.getProperty( OSGI_BOOT_DELEGATION_STRICT ) ); 104 | } 105 | 106 | public static String[] getOsgiBootDelegation() { 107 | if (System.getProperty( OSGI_BOOT_DELEGATION_CLASSES ) == null) 108 | return null; 109 | 110 | return System.getProperty( OSGI_BOOT_DELEGATION_CLASSES ).split( "," ); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /JCL2/spring/src/main/java/org/xeustechnologies/jcl/spring/JclBeanDefinitionDecorator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl.spring; 19 | 20 | import java.util.ArrayList; 21 | import java.util.Arrays; 22 | import java.util.List; 23 | import java.util.UUID; 24 | 25 | import org.springframework.beans.factory.config.BeanDefinitionHolder; 26 | import org.springframework.beans.factory.config.ConstructorArgumentValues; 27 | import org.springframework.beans.factory.support.AbstractBeanDefinition; 28 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; 29 | import org.springframework.beans.factory.support.GenericBeanDefinition; 30 | import org.springframework.beans.factory.xml.BeanDefinitionDecorator; 31 | import org.springframework.beans.factory.xml.ParserContext; 32 | import org.w3c.dom.Node; 33 | import org.xeustechnologies.jcl.JclObjectFactory; 34 | import org.xeustechnologies.jcl.exception.JclException; 35 | 36 | /** 37 | * @author Kamran Zafar 38 | * 39 | */ 40 | public class JclBeanDefinitionDecorator implements BeanDefinitionDecorator { 41 | 42 | private static final String JCL_REF = "ref"; 43 | private static final String JCL_FACTORY = "jcl-factory-" + UUID.randomUUID(); 44 | private static final String JCL_FACTORY_METHOD = "create"; 45 | private static final String JCL_FACTORY_CONSTRUCTOR = "getInstance"; 46 | 47 | public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder holder, ParserContext parserContext) { 48 | String jclRef = node.getAttributes().getNamedItem( JCL_REF ).getNodeValue(); 49 | 50 | GenericBeanDefinition bd = new GenericBeanDefinition(); 51 | bd.setFactoryBeanName( JCL_FACTORY ); 52 | bd.setFactoryMethodName( JCL_FACTORY_METHOD ); 53 | bd.setConstructorArgumentValues( holder.getBeanDefinition().getConstructorArgumentValues() ); 54 | bd.setPropertyValues( holder.getBeanDefinition().getPropertyValues() ); 55 | bd.getConstructorArgumentValues().addIndexedArgumentValue( 0, 56 | new ConstructorArgumentValues.ValueHolder( parserContext.getRegistry().getBeanDefinition( jclRef ) ) ); 57 | bd.getConstructorArgumentValues().addIndexedArgumentValue( 1, 58 | new ConstructorArgumentValues.ValueHolder( holder.getBeanDefinition().getBeanClassName() ) ); 59 | 60 | BeanDefinitionHolder newHolder = new BeanDefinitionHolder( bd, holder.getBeanName() ); 61 | 62 | createDependencyOnJcl( node, newHolder, parserContext ); 63 | 64 | return newHolder; 65 | } 66 | 67 | @SuppressWarnings("unchecked") 68 | private void createDependencyOnJcl(Node node, BeanDefinitionHolder holder, ParserContext parserContext) { 69 | AbstractBeanDefinition definition = ( (AbstractBeanDefinition) holder.getBeanDefinition() ); 70 | String jclRef = node.getAttributes().getNamedItem( JCL_REF ).getNodeValue(); 71 | 72 | if( !parserContext.getRegistry().containsBeanDefinition( JCL_FACTORY ) ) { 73 | BeanDefinitionBuilder initializer = BeanDefinitionBuilder.rootBeanDefinition( JclObjectFactory.class, 74 | JCL_FACTORY_CONSTRUCTOR ); 75 | parserContext.getRegistry().registerBeanDefinition( JCL_FACTORY, initializer.getBeanDefinition() ); 76 | } 77 | 78 | if( parserContext.getRegistry().containsBeanDefinition( jclRef ) ) { 79 | String[] dependsOn = definition.getDependsOn(); 80 | if( dependsOn == null ) { 81 | dependsOn = new String[] { jclRef, JCL_FACTORY }; 82 | } else { 83 | List dependencies = new ArrayList( Arrays.asList( dependsOn ) ); 84 | dependencies.add( jclRef ); 85 | dependencies.add( JCL_FACTORY ); 86 | dependsOn = (String[]) dependencies.toArray( new String[0] ); 87 | } 88 | definition.setDependsOn( dependsOn ); 89 | } else 90 | throw new JclException( "JCL Bean definition " + jclRef + "not found" ); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/JclUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with JCL. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl; 28 | 29 | import java.io.ByteArrayInputStream; 30 | import java.io.ByteArrayOutputStream; 31 | import java.io.IOException; 32 | import java.io.ObjectInputStream; 33 | import java.io.ObjectOutputStream; 34 | import java.lang.reflect.InvocationHandler; 35 | import java.lang.reflect.Method; 36 | import java.lang.reflect.Proxy; 37 | 38 | import xeus.jcl.exception.JclException; 39 | 40 | @SuppressWarnings("unchecked") 41 | public class JclUtils { 42 | 43 | /** 44 | * Casts the object ref to the passed interface class ref. It actually 45 | * returns a dynamic proxy for the passed object 46 | * 47 | * @param object 48 | * @param classes 49 | * @return castable 50 | * @return casted 51 | */ 52 | public static Object toCastable(Object object, Class[] classes) { 53 | JclProxyHandler handler = new JclProxyHandler( object ); 54 | return Proxy.newProxyInstance( JclUtils.class.getClassLoader(), classes, handler ); 55 | } 56 | 57 | /** 58 | * Casts the object ref to the passed interface class ref 59 | * 60 | * @param object 61 | * @param clazz 62 | * @return castable 63 | * @return casted 64 | */ 65 | public static Object toCastable(Object object, Class clazz) { 66 | return toCastable( object, new Class[] { clazz } ); 67 | } 68 | 69 | /** 70 | * Casts the object ref to the passed interface class ref and returns it 71 | * 72 | * @param object 73 | * @param clazz 74 | * @return T reference 75 | * @return casted 76 | */ 77 | public static T cast(Object object, Class clazz) { 78 | return (T) toCastable( object, clazz ); 79 | } 80 | 81 | /** 82 | * Deep clones the Serializable objects in the current classloader 83 | * 84 | * @param original 85 | * @return clone 86 | */ 87 | public static Object clone(Object original) { 88 | Object clone = null; 89 | try { 90 | // Increased buffer size to speed up writing 91 | ByteArrayOutputStream bos = new ByteArrayOutputStream( 5120 ); 92 | ObjectOutputStream out = new ObjectOutputStream( bos ); 93 | out.writeObject( original ); 94 | out.flush(); 95 | out.close(); 96 | 97 | ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( bos.toByteArray() ) ); 98 | clone = in.readObject(); 99 | 100 | in.close(); 101 | bos.close(); 102 | 103 | return clone; 104 | } catch (IOException e) { 105 | throw new JclException( e ); 106 | } catch (ClassNotFoundException cnfe) { 107 | throw new JclException( cnfe ); 108 | } 109 | } 110 | 111 | /** 112 | * proxy method invocation handler 113 | * 114 | */ 115 | private static class JclProxyHandler implements InvocationHandler { 116 | private final Object delegate; 117 | 118 | public JclProxyHandler(Object delegate) { 119 | this.delegate = delegate; 120 | } 121 | 122 | /* 123 | * (non-Javadoc) 124 | * 125 | * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, 126 | * java.lang.reflect.Method, java.lang.Object[]) 127 | */ 128 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 129 | Method delegateMethod = delegate.getClass().getMethod( method.getName(), method.getParameterTypes() ); 130 | return delegateMethod.invoke( delegate, args ); 131 | } 132 | } 133 | 134 | } -------------------------------------------------------------------------------- /JCL/docs/web/css/SyntaxHighlighter.css: -------------------------------------------------------------------------------- 1 | .dp-highlighter 2 | { 3 | font-family: "Consolas", "Courier New", Courier, mono, serif; 4 | font-size: 12px; 5 | background-color: #E7E5DC; 6 | width: 99%; 7 | overflow: auto; 8 | margin: 18px 0 18px 0 !important; 9 | padding-top: 1px; /* adds a little border on top when controls are hidden */ 10 | } 11 | 12 | /* clear styles */ 13 | .dp-highlighter ol, 14 | .dp-highlighter ol li, 15 | .dp-highlighter ol li span 16 | { 17 | margin: 0; 18 | padding: 0; 19 | border: none; 20 | } 21 | 22 | .dp-highlighter a, 23 | .dp-highlighter a:hover 24 | { 25 | background: none; 26 | border: none; 27 | padding: 0; 28 | margin: 0; 29 | } 30 | 31 | .dp-highlighter .bar 32 | { 33 | padding-left: 45px; 34 | } 35 | 36 | .dp-highlighter.collapsed .bar, 37 | .dp-highlighter.nogutter .bar 38 | { 39 | padding-left: 0px; 40 | } 41 | 42 | .dp-highlighter ol 43 | { 44 | list-style: decimal; /* for ie */ 45 | background-color: #fff; 46 | margin: 0px 0px 1px 45px !important; /* 1px bottom margin seems to fix occasional Firefox scrolling */ 47 | padding: 0px; 48 | color: #5C5C5C; 49 | } 50 | 51 | .dp-highlighter.nogutter ol, 52 | .dp-highlighter.nogutter ol li 53 | { 54 | list-style: none !important; 55 | margin-left: 0px !important; 56 | } 57 | 58 | .dp-highlighter ol li, 59 | .dp-highlighter .columns div 60 | { 61 | list-style: decimal-leading-zero; /* better look for others, override cascade from OL */ 62 | list-style-position: outside !important; 63 | border-left: 3px solid #6CE26C; 64 | background-color: #F8F8F8; 65 | color: #5C5C5C; 66 | padding: 0 3px 0 10px !important; 67 | margin: 0 !important; 68 | line-height: 14px; 69 | } 70 | 71 | .dp-highlighter.nogutter ol li, 72 | .dp-highlighter.nogutter .columns div 73 | { 74 | border: 0; 75 | } 76 | 77 | .dp-highlighter .columns 78 | { 79 | background-color: #F8F8F8; 80 | color: gray; 81 | overflow: hidden; 82 | width: 100%; 83 | } 84 | 85 | .dp-highlighter .columns div 86 | { 87 | padding-bottom: 5px; 88 | } 89 | 90 | .dp-highlighter ol li.alt 91 | { 92 | background-color: #FFF; 93 | color: inherit; 94 | } 95 | 96 | .dp-highlighter ol li span 97 | { 98 | color: black; 99 | background-color: inherit; 100 | } 101 | 102 | /* Adjust some properties when collapsed */ 103 | 104 | .dp-highlighter.collapsed ol 105 | { 106 | margin: 0px; 107 | } 108 | 109 | .dp-highlighter.collapsed ol li 110 | { 111 | display: none; 112 | } 113 | 114 | /* Additional modifications when in print-view */ 115 | 116 | .dp-highlighter.printing 117 | { 118 | border: none; 119 | } 120 | 121 | .dp-highlighter.printing .tools 122 | { 123 | display: none !important; 124 | } 125 | 126 | .dp-highlighter.printing li 127 | { 128 | display: list-item !important; 129 | } 130 | 131 | /* Styles for the tools */ 132 | 133 | .dp-highlighter .tools 134 | { 135 | padding: 3px 8px 3px 10px; 136 | font: 9px Verdana, Geneva, Arial, Helvetica, sans-serif; 137 | color: silver; 138 | background-color: #f8f8f8; 139 | padding-bottom: 10px; 140 | border-left: 3px solid #6CE26C; 141 | } 142 | 143 | .dp-highlighter.nogutter .tools 144 | { 145 | border-left: 0; 146 | } 147 | 148 | .dp-highlighter.collapsed .tools 149 | { 150 | border-bottom: 0; 151 | } 152 | 153 | .dp-highlighter .tools a 154 | { 155 | font-size: 9px; 156 | color: #a0a0a0; 157 | background-color: inherit; 158 | text-decoration: none; 159 | margin-right: 10px; 160 | } 161 | 162 | .dp-highlighter .tools a:hover 163 | { 164 | color: red; 165 | background-color: inherit; 166 | text-decoration: underline; 167 | } 168 | 169 | /* About dialog styles */ 170 | 171 | .dp-about { background-color: #fff; color: #333; margin: 0px; padding: 0px; } 172 | .dp-about table { width: 100%; height: 100%; font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; } 173 | .dp-about td { padding: 10px; vertical-align: top; } 174 | .dp-about .copy { border-bottom: 1px solid #ACA899; height: 95%; } 175 | .dp-about .title { color: red; background-color: inherit; font-weight: bold; } 176 | .dp-about .para { margin: 0 0 4px 0; } 177 | .dp-about .footer { background-color: #ECEADB; color: #333; border-top: 1px solid #fff; text-align: right; } 178 | .dp-about .close { font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; background-color: #ECEADB; color: #333; width: 60px; height: 22px; } 179 | 180 | /* Language specific styles */ 181 | 182 | .dp-highlighter .comment, .dp-highlighter .comments { color: #008200; background-color: inherit; } 183 | .dp-highlighter .string { color: blue; background-color: inherit; } 184 | .dp-highlighter .keyword { color: #069; font-weight: bold; background-color: inherit; } 185 | .dp-highlighter .preprocessor { color: gray; background-color: inherit; } 186 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/JclObjectFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with JCL. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl; 28 | 29 | import java.io.IOException; 30 | import java.lang.reflect.InvocationTargetException; 31 | 32 | /** 33 | * A factory class that loads classes from specified JarClassLoader and tries to 34 | * instantiate their objects 35 | * 36 | * @author Kamran Zafar 37 | * 38 | */ 39 | @SuppressWarnings("unchecked") 40 | public class JclObjectFactory { 41 | private static JclObjectFactory jclObjectFactory = new JclObjectFactory(); 42 | 43 | /** 44 | * private constructor 45 | */ 46 | private JclObjectFactory() { 47 | } 48 | 49 | /** 50 | * Returns the instance of the singleton factory 51 | * 52 | * @return JclObjectFactory 53 | */ 54 | public static JclObjectFactory getInstance() { 55 | return jclObjectFactory; 56 | } 57 | 58 | /** 59 | * Creates the object of the specified class from the specified class loader 60 | * by invoking the default constructor 61 | * 62 | * @param jcl 63 | * @param className 64 | * @return Object 65 | * @throws IllegalArgumentException 66 | * @throws SecurityException 67 | * @throws IOException 68 | * @throws ClassNotFoundException 69 | * @throws InstantiationException 70 | * @throws IllegalAccessException 71 | * @throws InvocationTargetException 72 | * @throws NoSuchMethodException 73 | */ 74 | public Object create(JarClassLoader jcl, String className) throws IllegalArgumentException, SecurityException, 75 | IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, 76 | InvocationTargetException, NoSuchMethodException { 77 | return create( jcl, className, null ); 78 | } 79 | 80 | /** 81 | * Creates the object of the specified class from the specified class loader 82 | * by invoking the right arguments-constructor 83 | * 84 | * @param jcl 85 | * @param className 86 | * @param args 87 | * @return Object 88 | * @throws IOException 89 | * @throws ClassNotFoundException 90 | * @throws InstantiationException 91 | * @throws IllegalAccessException 92 | * @throws IllegalArgumentException 93 | * @throws SecurityException 94 | * @throws InvocationTargetException 95 | * @throws NoSuchMethodException 96 | */ 97 | public Object create(JarClassLoader jcl, String className, Object[] args) throws IOException, 98 | ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, 99 | SecurityException, InvocationTargetException, NoSuchMethodException { 100 | if( args == null || args.length == 0 ) 101 | return jcl.loadClass( className ).newInstance(); 102 | 103 | Class[] types = new Class[args.length]; 104 | 105 | for( int i = 0; i < args.length; i++ ) 106 | types[i] = args[i].getClass(); 107 | 108 | return jcl.loadClass( className ).getConstructor( types ).newInstance( args ); 109 | } 110 | 111 | /** 112 | * Creates the object of the specified class from the specified class loader 113 | * by invoking the right static factory method 114 | * 115 | * @param jcl 116 | * @param className 117 | * @param methodName 118 | * @param args 119 | * @return Object 120 | * @throws IOException 121 | * @throws ClassNotFoundException 122 | * @throws InstantiationException 123 | * @throws IllegalAccessException 124 | * @throws IllegalArgumentException 125 | * @throws SecurityException 126 | * @throws InvocationTargetException 127 | * @throws NoSuchMethodException 128 | */ 129 | public Object create(JarClassLoader jcl, String className, String methodName, Object[] args) throws IOException, 130 | ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, 131 | SecurityException, InvocationTargetException, NoSuchMethodException { 132 | if( args == null || args.length == 0 ) 133 | return jcl.loadClass( className ).getMethod( methodName ).invoke( null ); 134 | 135 | Class[] types = new Class[args.length]; 136 | 137 | for( int i = 0; i < args.length; i++ ) 138 | types[i] = args[i].getClass(); 139 | 140 | return jcl.loadClass( className ).getMethod( methodName, types ).invoke( null, args ); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/JclUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.io.ByteArrayOutputStream; 22 | import java.io.ObjectInputStream; 23 | import java.io.ObjectOutputStream; 24 | 25 | import org.xeustechnologies.jcl.exception.JclException; 26 | import org.xeustechnologies.jcl.proxy.ProxyProviderFactory; 27 | import org.kamranzafar.commons.cloner.ObjectCloner; 28 | 29 | /** 30 | * This class has some important utility methods commonly required when using 31 | * JCL 32 | * 33 | * @author Kamran 34 | * 35 | */ 36 | @SuppressWarnings("unchecked") 37 | public class JclUtils { 38 | 39 | public static Object createProxy(Object object, Class superClass, Class[] interfaces, ClassLoader cl) { 40 | return ProxyProviderFactory.create().createProxy( object, superClass, interfaces, cl ); 41 | } 42 | 43 | /** 44 | * Casts the object ref to the passed interface class ref. It actually 45 | * returns a dynamic proxy for the passed object 46 | * 47 | * @param object 48 | * @param clazz 49 | * @return castable 50 | */ 51 | public static Object toCastable(Object object, Class clazz) { 52 | return createProxy( object, clazz, new Class[] { clazz }, null ); 53 | } 54 | 55 | /** 56 | * Casts the object ref to the passed interface class ref. It actually 57 | * returns a dynamic proxy for the passed object 58 | * 59 | * @param object 60 | * @param clazz 61 | * [] 62 | * @return castable 63 | */ 64 | public static Object toCastable(Object object, Class[] clazz) { 65 | return createProxy( object, clazz[0], clazz, null ); 66 | } 67 | 68 | /** 69 | * Casts the object ref to the passed interface class ref 70 | * 71 | * @param object 72 | * @param clazz 73 | * @param cl 74 | * @return castable 75 | */ 76 | public static Object toCastable(Object object, Class clazz, ClassLoader cl) { 77 | return createProxy( object, clazz, new Class[] { clazz }, cl ); 78 | } 79 | 80 | /** 81 | * Casts the object ref to the passed interface class ref 82 | * 83 | * @param object 84 | * @param clazz 85 | * [] 86 | * @param cl 87 | * @return castable 88 | */ 89 | public static Object toCastable(Object object, Class[] clazz, ClassLoader cl) { 90 | return createProxy( object, clazz[0], clazz, cl ); 91 | } 92 | 93 | /** 94 | * Casts the object ref to the passed interface class ref and returns it 95 | * 96 | * @param object 97 | * @param clazz 98 | * @return T reference 99 | * @return casted 100 | */ 101 | public static T cast(Object object, Class clazz) { 102 | return (T) toCastable( object, clazz, null ); 103 | } 104 | 105 | /** 106 | * Casts the object ref to the passed interface class ref and returns it 107 | * 108 | * @param object 109 | * @param clazz 110 | * @param cl 111 | * @return T reference 112 | * @return casted 113 | */ 114 | public static T cast(Object object, Class clazz, ClassLoader cl) { 115 | return (T) toCastable( object, clazz, cl ); 116 | } 117 | 118 | /** 119 | * Deep clones the Serializable objects in the current classloader. This 120 | * method is slow and uses Object streams to clone Serializable objects. 121 | * 122 | * This method is now deprecated because of its inefficiency and the 123 | * limitation to clone Serializable objects only. The use of deepClone or 124 | * shallowClone is now recommended 125 | * 126 | * @param original 127 | * @return clone 128 | * @deprecated As of release 2.0, replaced by 129 | * {@link #deepClone(Object original)} 130 | */ 131 | @Deprecated 132 | public static Object clone(Object original) { 133 | Object clone = null; 134 | 135 | try { 136 | // Increased buffer size 137 | ByteArrayOutputStream bos = new ByteArrayOutputStream( 5120 ); 138 | ObjectOutputStream out = new ObjectOutputStream( bos ); 139 | out.writeObject( original ); 140 | out.flush(); 141 | out.close(); 142 | 143 | ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( bos.toByteArray() ) ); 144 | clone = in.readObject(); 145 | 146 | in.close(); 147 | bos.close(); 148 | } catch (Exception e) { 149 | throw new JclException( e ); 150 | } 151 | 152 | return clone; 153 | } 154 | 155 | /** 156 | * Deep clones any object 157 | * 158 | * @param original 159 | * @return clone 160 | */ 161 | public static Object deepClone(Object original) { 162 | ObjectCloner cloner = new ObjectCloner(); 163 | 164 | return cloner.deepClone( original ); 165 | } 166 | 167 | /** 168 | * Shallow clones any object 169 | * 170 | * @param original 171 | * @return clone 172 | */ 173 | public static Object shallowClone(Object original) { 174 | ObjectCloner cloner = new ObjectCloner(); 175 | 176 | return cloner.shallowClone( original ); 177 | } 178 | } -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/JarResources.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with JCL. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl; 28 | 29 | import java.io.BufferedInputStream; 30 | import java.io.ByteArrayOutputStream; 31 | import java.io.FileInputStream; 32 | import java.io.IOException; 33 | import java.io.InputStream; 34 | import java.net.URL; 35 | import java.util.Collections; 36 | import java.util.HashMap; 37 | import java.util.Map; 38 | import java.util.jar.JarEntry; 39 | import java.util.jar.JarInputStream; 40 | 41 | import org.apache.log4j.Logger; 42 | 43 | import xeus.jcl.exception.JclException; 44 | 45 | /** 46 | * JarResources reads jar files and loads the class content/bytes in a HashMap 47 | * 48 | * @author Kamran Zafar 49 | * 50 | */ 51 | public class JarResources { 52 | 53 | protected Map jarEntryContents; 54 | 55 | private static Logger logger = Logger.getLogger( JarResources.class ); 56 | 57 | /** 58 | * @throws IOException 59 | */ 60 | public JarResources() { 61 | jarEntryContents = new HashMap(); 62 | } 63 | 64 | /** 65 | * @param name 66 | * @return byte[] 67 | */ 68 | public byte[] getResource(String name) { 69 | return jarEntryContents.get( name ); 70 | } 71 | 72 | /** 73 | * Returns an immutable Map of all jar resources 74 | * 75 | * @return Map 76 | */ 77 | public Map getResources() { 78 | return Collections.unmodifiableMap( jarEntryContents ); 79 | } 80 | 81 | /** 82 | * Reads the specified jar file 83 | * 84 | * @param jarFile 85 | * @throws IOException 86 | */ 87 | public void loadJar(String jarFile) throws IOException { 88 | if( logger.isTraceEnabled() ) 89 | logger.trace( "Loading jar: " + jarFile ); 90 | FileInputStream fis = new FileInputStream( jarFile ); 91 | loadJar( fis ); 92 | fis.close(); 93 | } 94 | 95 | /** 96 | * Reads the jar file from a specified URL 97 | * 98 | * @param url 99 | * @throws IOException 100 | */ 101 | public void loadJar(URL url) throws IOException { 102 | if( logger.isTraceEnabled() ) 103 | logger.trace( "Loading jar: " + url.toString() ); 104 | InputStream in = url.openStream(); 105 | loadJar( in ); 106 | in.close(); 107 | } 108 | 109 | /** 110 | * Load the jar contents from InputStream 111 | * 112 | * @throws IOException 113 | */ 114 | public void loadJar(InputStream jarStream) throws IOException { 115 | 116 | BufferedInputStream bis = null; 117 | JarInputStream jis = null; 118 | 119 | try { 120 | bis = new BufferedInputStream( jarStream ); 121 | jis = new JarInputStream( bis ); 122 | 123 | JarEntry jarEntry = null; 124 | while(( jarEntry = jis.getNextJarEntry() ) != null) { 125 | if( logger.isTraceEnabled() ) 126 | logger.trace( dump( jarEntry ) ); 127 | 128 | if( jarEntry.isDirectory() ) { 129 | continue; 130 | } 131 | 132 | if( jarEntryContents.containsKey( jarEntry.getName() ) ) { 133 | if( !Configuration.supressCollisionException() ) 134 | throw new JclException( "Class/Resource " + jarEntry.getName() + " already loaded" ); 135 | else { 136 | if( logger.isTraceEnabled() ) 137 | logger 138 | .trace( "Class/Resource " + jarEntry.getName() 139 | + " already loaded; ignoring entry..." ); 140 | continue; 141 | } 142 | } 143 | 144 | if( logger.isTraceEnabled() ) 145 | logger.trace( "Entry Name: " + jarEntry.getName() + ", " + "Entry Size: " + jarEntry.getSize() ); 146 | 147 | byte[] b = new byte[2048]; 148 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 149 | 150 | int len = 0; 151 | while(( len = jis.read( b ) ) > 0) { 152 | out.write( b, 0, len ); 153 | } 154 | 155 | // add to internal resource HashMap 156 | jarEntryContents.put( jarEntry.getName(), out.toByteArray() ); 157 | 158 | if( logger.isTraceEnabled() ) 159 | logger.trace( jarEntry.getName() + ": size=" + out.size() + " ,csize=" 160 | + jarEntry.getCompressedSize() ); 161 | 162 | out.close(); 163 | } 164 | } catch (NullPointerException e) { 165 | if( logger.isTraceEnabled() ) 166 | logger.trace( "Done loading." ); 167 | } finally { 168 | jis.close(); 169 | bis.close(); 170 | } 171 | } 172 | 173 | /** 174 | * For debugging 175 | * 176 | * @param je 177 | * @return String 178 | */ 179 | private String dump(JarEntry je) { 180 | StringBuffer sb = new StringBuffer(); 181 | if( je.isDirectory() ) { 182 | sb.append( "d " ); 183 | } else { 184 | sb.append( "f " ); 185 | } 186 | 187 | if( je.getMethod() == JarEntry.STORED ) { 188 | sb.append( "stored " ); 189 | } else { 190 | sb.append( "defalted " ); 191 | } 192 | 193 | sb.append( je.getName() ); 194 | sb.append( "\t" ); 195 | sb.append( "" + je.getSize() ); 196 | if( je.getMethod() == JarEntry.DEFLATED ) { 197 | sb.append( "/" + je.getCompressedSize() ); 198 | } 199 | 200 | return ( sb.toString() ); 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /JCL/LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/JclObjectFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | import org.xeustechnologies.jcl.exception.JclException; 26 | 27 | /** 28 | * A factory class that loads classes from specified JarClassLoader and tries to 29 | * instantiate their objects 30 | * 31 | * @author Kamran Zafar 32 | * 33 | */ 34 | @SuppressWarnings("unchecked") 35 | public class JclObjectFactory { 36 | private static JclObjectFactory jclObjectFactory = new JclObjectFactory(); 37 | private static boolean autoProxy; 38 | private final transient Logger logger = LoggerFactory.getLogger(JclObjectFactory.class); 39 | 40 | /** 41 | * private constructor 42 | */ 43 | private JclObjectFactory() { 44 | autoProxy = Configuration.autoProxy(); 45 | } 46 | 47 | /** 48 | * Returns the instance of the singleton factory 49 | * 50 | * @return JclObjectFactory 51 | */ 52 | public static JclObjectFactory getInstance() { 53 | return jclObjectFactory; 54 | } 55 | 56 | /** 57 | * Returns the instance of the singleton factory that can be used to create 58 | * auto proxies for jcl-created objects 59 | * 60 | * @return JclObjectFactory 61 | */ 62 | public static JclObjectFactory getInstance(boolean autoProxy) { 63 | JclObjectFactory.autoProxy = autoProxy; 64 | return jclObjectFactory; 65 | } 66 | 67 | /** 68 | * Creates the object of the specified class from the specified class loader 69 | * by invoking the default constructor 70 | * 71 | * @param jcl 72 | * @param className 73 | * @return Object 74 | */ 75 | public Object create(JarClassLoader jcl, String className) { 76 | return create( jcl, className, (Object[]) null ); 77 | } 78 | 79 | /** 80 | * Creates the object of the specified class from the specified class loader 81 | * by invoking the right arguments-constructor 82 | * 83 | * @param jcl 84 | * @param className 85 | * @param args 86 | * @return Object 87 | */ 88 | public Object create(JarClassLoader jcl, String className, Object... args) { 89 | if (args == null || args.length == 0) { 90 | try { 91 | return newInstance( jcl.loadClass( className ).newInstance() ); 92 | } catch (Throwable e) { 93 | throw new JclException( e ); 94 | } 95 | } 96 | 97 | Class[] types = new Class[args.length]; 98 | 99 | for (int i = 0; i < args.length; i++) 100 | types[i] = args[i].getClass(); 101 | 102 | return create( jcl, className, args, types ); 103 | } 104 | 105 | /** 106 | * Creates the object of the specified class from the specified class loader 107 | * by invoking the right arguments-constructor based on the passed types 108 | * parameter 109 | * 110 | * @param jcl 111 | * @param className 112 | * @param args 113 | * @param types 114 | * @return Object 115 | */ 116 | public Object create(JarClassLoader jcl, String className, Object[] args, Class[] types) { 117 | Object obj = null; 118 | 119 | if (args == null || args.length == 0) { 120 | try { 121 | obj = jcl.loadClass( className ).newInstance(); 122 | } catch (Throwable e) { 123 | throw new JclException( e ); 124 | } 125 | } else { 126 | try { 127 | obj = jcl.loadClass( className ).getConstructor( types ).newInstance( args ); 128 | } catch (Exception e) { 129 | throw new JclException( e ); 130 | } 131 | } 132 | 133 | return newInstance( obj ); 134 | } 135 | 136 | /** 137 | * Creates the object of the specified class from the specified class loader 138 | * by invoking the right static factory method 139 | * 140 | * @param jcl 141 | * @param className 142 | * @param methodName 143 | * @param args 144 | * @return Object 145 | */ 146 | public Object create(JarClassLoader jcl, String className, String methodName, Object... args) { 147 | if (args == null || args.length == 0) { 148 | try { 149 | return newInstance( jcl.loadClass( className ).getMethod( methodName ).invoke( null ) ); 150 | } catch (Exception e) { 151 | throw new JclException( e ); 152 | } 153 | } 154 | Class[] types = new Class[args.length]; 155 | 156 | for (int i = 0; i < args.length; i++) 157 | types[i] = args[i].getClass(); 158 | 159 | return create( jcl, className, methodName, args, types ); 160 | } 161 | 162 | /** 163 | * Creates the object of the specified class from the specified class loader 164 | * by invoking the right static factory method based on the types parameter 165 | * 166 | * @param jcl 167 | * @param className 168 | * @param methodName 169 | * @param args 170 | * @param types 171 | * @return Object 172 | */ 173 | public Object create(JarClassLoader jcl, String className, String methodName, Object[] args, Class[] types) { 174 | Object obj = null; 175 | if (args == null || args.length == 0) { 176 | try { 177 | obj = jcl.loadClass( className ).getMethod( methodName ).invoke( null ); 178 | } catch (Exception e) { 179 | throw new JclException( e ); 180 | } 181 | } else { 182 | try { 183 | obj = jcl.loadClass( className ).getMethod( methodName, types ).invoke( null, args ); 184 | } catch (Exception e) { 185 | throw new JclException( e ); 186 | } 187 | } 188 | 189 | return newInstance( obj ); 190 | } 191 | 192 | /** 193 | * Creates a proxy 194 | * 195 | * @param object 196 | * @return 197 | */ 198 | private Object newInstance(Object object) { 199 | if (autoProxy) { 200 | 201 | Class superClass = null; 202 | 203 | // Check class 204 | try { 205 | Class.forName( object.getClass().getSuperclass().getName() ); 206 | superClass = object.getClass().getSuperclass(); 207 | } catch (ClassNotFoundException e) { 208 | } 209 | 210 | Class[] interfaces = object.getClass().getInterfaces(); 211 | 212 | List il = new ArrayList(); 213 | 214 | // Check available interfaces 215 | for (Class i : interfaces) { 216 | try { 217 | Class.forName( i.getClass().getName() ); 218 | il.add( i ); 219 | } catch (ClassNotFoundException e) { 220 | } 221 | } 222 | 223 | logger.debug( "Class: {}", superClass ); 224 | logger.debug( "Class Interfaces: {}", il ); 225 | 226 | if (superClass == null && il.size() == 0) { 227 | throw new JclException( "Neither the class [" + object.getClass().getSuperclass().getName() 228 | + "] nor all the implemented interfaces found in the current classloader" ); 229 | } 230 | 231 | return JclUtils.createProxy( object, superClass, il.toArray( new Class[il.size()] ), null ); 232 | } 233 | 234 | return object; 235 | } 236 | } 237 | -------------------------------------------------------------------------------- /JCL2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | org.xeustechnologies 5 | jcl 6 | Jar Class Loader 7 | http://jcloader.sourceforge.net 8 | 9 | 2.9-SNAPSHOT 10 | pom 11 | Jar Class Loader 12 | 13 | 14 | org.sonatype.oss 15 | oss-parent 16 | 5 17 | 18 | 19 | 20 | test-jcl 21 | core 22 | spring 23 | test-web 24 | web 25 | 26 | 27 | 28 | test-jcl.jar 29 | false 30 | UTF-8 31 | 32 | 33 | 34 | 35 | kzafar 36 | Kamran Zafar 37 | kamran@kamranzafar.org 38 | 39 | Kamran Zafar 40 | 41 | developer 42 | 43 | 44 | 45 | 46 | 47 | 48 | The Apache Software License, Version 2.0 49 | http://www.apache.org/licenses/LICENSE-2.0.txt 50 | repo 51 | 52 | 53 | 54 | 55 | 56 | scm:git:git@github.com:kamranzafar/JCL.git 57 | 58 | 59 | scm:git:git@github.com:kamranzafar/JCL.git 60 | 61 | git@github.com:kamranzafar/JCL.git 62 | 63 | 64 | 65 | 66 | 67 | ossrh 68 | https://oss.sonatype.org/content/repositories/snapshots 69 | 70 | 71 | ossrh 72 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 73 | 74 | 75 | 76 | 77 | Kamran Zafar 78 | http://kamranzafar.org 79 | 80 | 81 | 82 | 83 | 84 | org.apache.maven.plugins 85 | maven-resources-plugin 86 | 87 | 88 | UTF-8 89 | 90 | 91 | 92 | maven-compiler-plugin 93 | 94 | 1.7 95 | 1.7 96 | UTF-8 97 | 98 | 99 | 100 | maven-surefire-plugin 101 | 102 | ${skipTest} 103 | 104 | 105 | 106 | org.apache.maven.plugins 107 | maven-release-plugin 108 | 109 | true 110 | release 111 | deploy 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | junit 120 | junit 121 | 4.12 122 | test 123 | 124 | 125 | org.objenesis 126 | objenesis 127 | 2.1 128 | 129 | 130 | javax.servlet 131 | servlet-api 132 | 2.5 133 | provided 134 | 135 | 136 | cglib 137 | cglib-nodep 138 | 2.2 139 | 140 | 141 | org.slf4j 142 | slf4j-api 143 | 1.7.21 144 | 145 | 146 | org.kamranzafar.commons 147 | object-cloner 148 | 0.1 149 | 150 | 151 | 152 | 153 | 154 | release 155 | 156 | 157 | 158 | org.sonatype.plugins 159 | nexus-staging-maven-plugin 160 | true 161 | 162 | ossrh 163 | https://oss.sonatype.org/ 164 | true 165 | 166 | 167 | 168 | org.apache.maven.plugins 169 | maven-source-plugin 170 | 171 | 172 | attach-sources 173 | 174 | jar-no-fork 175 | 176 | 177 | 178 | 179 | 180 | org.apache.maven.plugins 181 | maven-javadoc-plugin 182 | 183 | 184 | attach-javadocs 185 | 186 | jar 187 | 188 | 189 | 190 | 191 | true 192 | 193 | 194 | 195 | org.apache.maven.plugins 196 | maven-gpg-plugin 197 | 198 | 199 | sign-artifacts 200 | verify 201 | 202 | sign 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/junit/LoadTest.java: -------------------------------------------------------------------------------- 1 | package xeus.jcl.junit; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.FileNotFoundException; 6 | import java.io.IOException; 7 | import java.io.Serializable; 8 | import java.lang.reflect.InvocationTargetException; 9 | import java.net.URISyntaxException; 10 | import java.net.URL; 11 | 12 | import junit.framework.TestCase; 13 | 14 | import org.apache.log4j.Logger; 15 | import org.springframework.beans.factory.xml.XmlBeanFactory; 16 | import org.springframework.core.io.FileSystemResource; 17 | 18 | import xeus.jcl.JarClassLoader; 19 | import xeus.jcl.JclObjectFactory; 20 | import xeus.jcl.JclUtils; 21 | import xeus.jcl.exception.JclException; 22 | 23 | @SuppressWarnings("all") 24 | public class LoadTest extends TestCase { 25 | 26 | private static Logger logger = Logger.getLogger( LoadTest.class ); 27 | 28 | public void testWithResourceName() throws IOException, InstantiationException, IllegalAccessException, 29 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 30 | NoSuchMethodException, JclException { 31 | JarClassLoader jc = new JarClassLoader( new String[] { "test-jcl.jar", "./test-classes" } ); 32 | 33 | // New class 34 | Object testObj = jc.loadClass( "xeus.jcl.test.Test" ).newInstance(); 35 | assertNotNull( testObj ); 36 | 37 | // Locally loaded 38 | testObj = jc.loadClass( "xeus.jcl.test.Test" ).newInstance(); 39 | assertNotNull( testObj ); 40 | 41 | testObj.getClass().getDeclaredMethod( "sayHello", null ).invoke( testObj, null ); 42 | } 43 | 44 | public void testWithClassFolder() throws IOException, InstantiationException, IllegalAccessException, 45 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 46 | NoSuchMethodException, JclException { 47 | JarClassLoader jc = new JarClassLoader( new String[] { "./test-classes" } ); 48 | 49 | Object testObj = jc.loadClass( "xeus.jcl.test.Test" ).newInstance(); 50 | assertNotNull( testObj ); 51 | 52 | testObj.getClass().getDeclaredMethod( "sayHello", null ).invoke( testObj, null ); 53 | } 54 | 55 | public void testWithUrl() throws IOException, InstantiationException, IllegalAccessException, 56 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 57 | NoSuchMethodException, JclException, URISyntaxException { 58 | // URL url = new URL("http://localhost:8080/blank/test-jcl.jar"); 59 | File f = new File( "test-jcl.jar" ); 60 | 61 | JarClassLoader jc = new JarClassLoader( new URL[] { f.toURI().toURL() } ); 62 | Object testObj = jc.loadClass( "xeus.jcl.test.Test" ).newInstance(); 63 | assertNotNull( testObj ); 64 | 65 | testObj.getClass().getDeclaredMethod( "sayHello", null ).invoke( testObj, null ); 66 | } 67 | 68 | public void testWithInputStream() throws IOException, InstantiationException, IllegalAccessException, 69 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 70 | NoSuchMethodException, JclException { 71 | FileInputStream fis = new FileInputStream( "test-jcl.jar" ); 72 | JarClassLoader jc = new JarClassLoader( new FileInputStream[] { fis } ); 73 | Object testObj = jc.loadClass( "xeus.jcl.test.Test" ).newInstance(); 74 | assertNotNull( testObj ); 75 | 76 | testObj.getClass().getDeclaredMethod( "sayHello", null ).invoke( testObj, null ); 77 | fis.close(); 78 | } 79 | 80 | public void testWithSpring() throws FileNotFoundException, InstantiationException, IllegalAccessException, 81 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 82 | NoSuchMethodException { 83 | XmlBeanFactory bf = new XmlBeanFactory( new FileSystemResource( "spring-test.xml" ) ); 84 | Object testObj = bf.getBean( "test" ); 85 | assertNotNull( testObj ); 86 | 87 | testObj.getClass().getDeclaredMethod( "sayHello", null ).invoke( testObj, null ); 88 | } 89 | 90 | public void testAddingMoreResources() throws IOException, InstantiationException, IllegalAccessException, 91 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 92 | NoSuchMethodException, JclException { 93 | JarClassLoader jc = new JarClassLoader(); 94 | jc.add( "./test-classes" ); 95 | Object testObj = jc.loadClass( "xeus.jcl.test.Test" ).newInstance(); 96 | assertNotNull( testObj ); 97 | 98 | testObj.getClass().getDeclaredMethod( "sayHello", null ).invoke( testObj, null ); 99 | } 100 | 101 | public void testChangeClassLoadingOrder() throws IOException, InstantiationException, IllegalAccessException, 102 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 103 | NoSuchMethodException, JclException { 104 | JarClassLoader jc = new JarClassLoader(); 105 | jc.getSystemLoader().setOrder( 1 ); 106 | jc.getParentLoader().setOrder( 3 ); 107 | jc.getLocalLoader().setOrder( 2 ); 108 | 109 | jc.add( "./test-classes" ); 110 | 111 | // Should be loaded from system 112 | Object testObj = jc.loadClass( "xeus.jcl.test.Test" ).newInstance(); 113 | assertNotNull( testObj ); 114 | 115 | testObj.getClass().getDeclaredMethod( "sayHello", null ).invoke( testObj, null ); 116 | } 117 | 118 | public void testInterfaceCast() throws IOException, InstantiationException, IllegalAccessException, 119 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 120 | NoSuchMethodException, JclException { 121 | JarClassLoader jc = new JarClassLoader(); 122 | jc.add( "test-jcl.jar" ); 123 | 124 | JclObjectFactory factory = JclObjectFactory.getInstance(); 125 | Object serializable = factory.create( jc, "xeus.jcl.test.Test" ); 126 | 127 | Serializable s = JclUtils.cast( serializable, Serializable.class ); 128 | 129 | assertNotNull( s ); 130 | 131 | s = (Serializable) JclUtils.toCastable( serializable, Serializable.class ); 132 | 133 | assertNotNull( s ); 134 | 135 | s = (Serializable) JclUtils.clone( serializable ); 136 | 137 | assertNotNull( s ); 138 | } 139 | 140 | public void testUnloading() throws IOException, InstantiationException, IllegalAccessException, 141 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 142 | NoSuchMethodException, JclException { 143 | JarClassLoader jc = new JarClassLoader( new String[] { "./test-classes" } ); 144 | 145 | Object testObj = null; 146 | jc.loadClass( "xeus.jcl.test.Test" ); 147 | jc.unloadClass( "xeus.jcl.test.Test" ); 148 | 149 | try { 150 | // Should get loaded from system 151 | testObj = jc.loadClass( "xeus.jcl.test.Test" ).newInstance(); 152 | assertNotNull( testObj ); 153 | return; 154 | } catch (ClassNotFoundException cnfe) { 155 | if( logger.isTraceEnabled() ) 156 | logger.trace( cnfe ); 157 | testObj = null; 158 | } 159 | 160 | assertNull( testObj ); 161 | } 162 | 163 | public void testEnabledFlag() throws IOException, InstantiationException, IllegalAccessException, 164 | ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, 165 | NoSuchMethodException, JclException { 166 | JarClassLoader jc = new JarClassLoader( new String[] { "test-jcl.jar" } ); 167 | jc.getLocalLoader().setEnabled( false ); 168 | jc.getCurrentLoader().setEnabled( false ); 169 | jc.getParentLoader().setEnabled( false ); 170 | jc.getSystemLoader().setEnabled( false ); 171 | 172 | try { 173 | jc.loadClass( "xeus.jcl.test.Test" ); 174 | } catch (ClassNotFoundException e) { 175 | // expected 176 | return; 177 | } 178 | 179 | throw new AssertionError( "Expected: ClassNotFoundException" ); 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/AbstractClassLoader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with JCL. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl; 28 | 29 | import java.io.InputStream; 30 | import java.util.ArrayList; 31 | import java.util.Collections; 32 | import java.util.List; 33 | 34 | import org.apache.log4j.Logger; 35 | 36 | import xeus.jcl.exception.ResourceNotFoundException; 37 | import xeus.jcl.loader.Loader; 38 | 39 | /** 40 | * Abstract class loader that can load classes from different resources 41 | * 42 | * @author Kamran Zafar 43 | * 44 | */ 45 | @SuppressWarnings("unchecked") 46 | public abstract class AbstractClassLoader extends ClassLoader { 47 | 48 | protected final List loaders = new ArrayList(); 49 | 50 | private final Loader systemLoader = new SystemLoader(); 51 | private final Loader parentLoader = new ParentLoader(); 52 | private final Loader currentLoader = new CurrentLoader(); 53 | 54 | /** 55 | * No arguments constructor 56 | */ 57 | public AbstractClassLoader() { 58 | loaders.add( systemLoader ); 59 | loaders.add( parentLoader ); 60 | loaders.add( currentLoader ); 61 | } 62 | 63 | public void addLoader(Loader loader) { 64 | loaders.add( loader ); 65 | } 66 | 67 | /** 68 | * Override loadClass 69 | * 70 | * @see java.lang.ClassLoader#loadClass(java.lang.String) 71 | */ 72 | @Override 73 | public Class loadClass(String className) throws ClassNotFoundException { 74 | return ( loadClass( className, true ) ); 75 | } 76 | 77 | /** 78 | * Overrides the loadClass method to load classes from other resources, 79 | * JarClassLoader is the only subclass in this project that loads classes 80 | * from jar files 81 | * 82 | * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean) 83 | */ 84 | @Override 85 | public Class loadClass(String className, boolean resolveIt) throws ClassNotFoundException { 86 | Collections.sort( loaders ); 87 | Class clazz = null; 88 | for( Loader l : loaders ) { 89 | if( l.isEnabled() ) { 90 | clazz = l.load( className, resolveIt ); 91 | if( clazz != null ) 92 | break; 93 | } 94 | } 95 | 96 | if( clazz == null ) 97 | throw new ClassNotFoundException( className ); 98 | 99 | return clazz; 100 | } 101 | 102 | /** 103 | * Overrides the getResourceAsStream method to load non-class resources from 104 | * other sources, JarClassLoader is the only subclass in this project that 105 | * loads non-class resources from jar files 106 | * 107 | * @see java.lang.ClassLoader#getResourceAsStream(java.lang.String) 108 | */ 109 | @Override 110 | public InputStream getResourceAsStream(String name) { 111 | Collections.sort( loaders ); 112 | InputStream is = null; 113 | for( Loader l : loaders ) { 114 | if( l.isEnabled() ) { 115 | is = l.loadResource( name ); 116 | if( is != null ) 117 | break; 118 | } 119 | } 120 | 121 | if( is == null ) 122 | throw new ResourceNotFoundException( "Resource " + name + " not found." ); 123 | 124 | return is; 125 | 126 | } 127 | 128 | /** 129 | * System class loader 130 | * 131 | */ 132 | class SystemLoader extends Loader { 133 | 134 | private final Logger logger = Logger.getLogger( SystemLoader.class ); 135 | 136 | public SystemLoader() { 137 | order = 4; 138 | enabled = Configuration.isSystemLoaderEnabled(); 139 | } 140 | 141 | @Override 142 | public Class load(String className, boolean resolveIt) { 143 | Class result; 144 | try { 145 | result = findSystemClass( className ); 146 | } catch (ClassNotFoundException e) { 147 | return null; 148 | } 149 | 150 | if( logger.isTraceEnabled() ) 151 | logger.trace( "Returning system class " + className ); 152 | 153 | return result; 154 | } 155 | 156 | @Override 157 | public InputStream loadResource(String name) { 158 | InputStream is = getSystemResourceAsStream( name ); 159 | 160 | if( is != null ) { 161 | if( logger.isTraceEnabled() ) 162 | logger.trace( "Returning system resource " + name ); 163 | 164 | return is; 165 | } 166 | 167 | return null; 168 | } 169 | } 170 | 171 | /** 172 | * Parent class loader 173 | * 174 | */ 175 | class ParentLoader extends Loader { 176 | private final Logger logger = Logger.getLogger( ParentLoader.class ); 177 | 178 | public ParentLoader() { 179 | order = 3; 180 | enabled = Configuration.isParentLoaderEnabled(); 181 | } 182 | 183 | @Override 184 | public Class load(String className, boolean resolveIt) { 185 | Class result; 186 | try { 187 | result = getParent().loadClass( className ); 188 | } catch (ClassNotFoundException e) { 189 | return null; 190 | } 191 | 192 | if( logger.isTraceEnabled() ) 193 | logger.trace( "Returning class " + className + " loaded with parent classloader" ); 194 | 195 | return result; 196 | } 197 | 198 | @Override 199 | public InputStream loadResource(String name) { 200 | InputStream is = getParent().getResourceAsStream( name ); 201 | 202 | if( is != null ) { 203 | if( logger.isTraceEnabled() ) 204 | logger.trace( "Returning resource " + name + " loaded with parent classloader" ); 205 | 206 | return is; 207 | } 208 | return null; 209 | } 210 | 211 | } 212 | 213 | /** 214 | * Current class loader 215 | * 216 | */ 217 | class CurrentLoader extends Loader { 218 | private final Logger logger = Logger.getLogger( CurrentLoader.class ); 219 | 220 | public CurrentLoader() { 221 | order = 2; 222 | enabled = Configuration.isCurrentLoaderEnabled(); 223 | } 224 | 225 | @Override 226 | public Class load(String className, boolean resolveIt) { 227 | Class result; 228 | try { 229 | result = getClass().getClassLoader().loadClass( className ); 230 | } catch (ClassNotFoundException e) { 231 | return null; 232 | } 233 | 234 | if( logger.isTraceEnabled() ) 235 | logger.trace( "Returning class " + className + " loaded with current classloader" ); 236 | 237 | return result; 238 | } 239 | 240 | @Override 241 | public InputStream loadResource(String name) { 242 | InputStream is = getClass().getClassLoader().getResourceAsStream( name ); 243 | 244 | if( is != null ) { 245 | if( logger.isTraceEnabled() ) 246 | logger.trace( "Returning resource " + name + " loaded with current classloader" ); 247 | 248 | return is; 249 | } 250 | 251 | return null; 252 | } 253 | 254 | } 255 | 256 | public Loader getSystemLoader() { 257 | return systemLoader; 258 | } 259 | 260 | public Loader getParentLoader() { 261 | return parentLoader; 262 | } 263 | 264 | public Loader getCurrentLoader() { 265 | return currentLoader; 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/ClasspathResources.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with JCL. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl; 28 | 29 | import java.io.ByteArrayOutputStream; 30 | import java.io.File; 31 | import java.io.FileInputStream; 32 | import java.io.IOException; 33 | import java.io.InputStream; 34 | import java.net.URISyntaxException; 35 | import java.net.URL; 36 | 37 | import org.apache.log4j.Logger; 38 | 39 | import xeus.jcl.exception.JclException; 40 | import xeus.jcl.exception.ResourceNotFoundException; 41 | 42 | /** 43 | * Class that builds a local classpath by loading resources from different 44 | * files/paths 45 | * 46 | * @author Kamran Zafar 47 | * 48 | */ 49 | public class ClasspathResources extends JarResources { 50 | 51 | private static Logger logger = Logger.getLogger( ClasspathResources.class ); 52 | 53 | /** 54 | * Reads the resource content 55 | * 56 | * @param resource 57 | * @throws IOException 58 | */ 59 | private void loadResourceContent(String resource) throws IOException { 60 | File resourceFile = new File( resource ); 61 | 62 | FileInputStream fis = new FileInputStream( resourceFile ); 63 | 64 | byte[] content = new byte[(int) resourceFile.length()]; 65 | fis.read( content ); 66 | 67 | if( jarEntryContents.containsKey( resourceFile.getName() ) ) { 68 | if( !Configuration.supressCollisionException() ) 69 | throw new JclException( "Resource " + resourceFile.getName() + " already loaded" ); 70 | else { 71 | if( logger.isTraceEnabled() ) 72 | logger.trace( "Resource " + resourceFile.getName() + " already loaded; ignoring entry..." ); 73 | return; 74 | } 75 | } 76 | 77 | fis.close(); 78 | 79 | if( logger.isTraceEnabled() ) 80 | logger.trace( "Loading resource: " + resourceFile.getName() ); 81 | jarEntryContents.put( resourceFile.getName(), content ); 82 | } 83 | 84 | /** 85 | * Attempts to load a remote resource (jars, properties files, etc) 86 | * 87 | * @param url 88 | * @throws IOException 89 | */ 90 | private void loadRemoteResource(URL url) throws IOException { 91 | if( logger.isTraceEnabled() ) 92 | logger.trace( "Attempting to load a remote resource." ); 93 | 94 | if( url.toString().toLowerCase().endsWith( ".jar" ) ) { 95 | loadJar( url ); 96 | return; 97 | } 98 | 99 | InputStream stream = url.openStream(); 100 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 101 | 102 | int byt; 103 | while(( ( byt = stream.read() ) != -1 )) { 104 | out.write( byt ); 105 | } 106 | 107 | byte[] content = out.toByteArray(); 108 | 109 | if( jarEntryContents.containsKey( url.toString() ) ) { 110 | if( !Configuration.supressCollisionException() ) 111 | throw new JclException( "Resource " + url.toString() + " already loaded" ); 112 | else { 113 | if( logger.isTraceEnabled() ) 114 | logger.trace( "Resource " + url.toString() + " already loaded; ignoring entry..." ); 115 | return; 116 | } 117 | } 118 | 119 | if( logger.isTraceEnabled() ) 120 | logger.trace( "Loading remote resource." ); 121 | jarEntryContents.put( url.toString(), content ); 122 | 123 | out.close(); 124 | stream.close(); 125 | } 126 | 127 | /** 128 | * Reads the class content 129 | * 130 | * @param clazz 131 | * @param pack 132 | * @throws IOException 133 | */ 134 | private void loadClassContent(String clazz, String pack) throws IOException { 135 | File cf = new File( clazz ); 136 | FileInputStream fis = new FileInputStream( cf ); 137 | 138 | byte[] content = new byte[(int) cf.length()]; 139 | fis.read( content ); 140 | 141 | String entryName = pack + "/" + cf.getName(); 142 | 143 | if( jarEntryContents.containsKey( entryName ) ) { 144 | if( !Configuration.supressCollisionException() ) 145 | throw new JclException( "Class " + entryName + " already loaded" ); 146 | else { 147 | if( logger.isTraceEnabled() ) 148 | logger.trace( "Class " + entryName + " already loaded; ignoring entry..." ); 149 | return; 150 | } 151 | } 152 | 153 | fis.close(); 154 | 155 | if( logger.isTraceEnabled() ) 156 | logger.trace( "Loading class: " + entryName ); 157 | jarEntryContents.put( entryName, content ); 158 | } 159 | 160 | /** 161 | * Reads local and remote resources 162 | * 163 | * @param url 164 | * @throws IOException 165 | */ 166 | public void loadResource(URL url) throws IOException { 167 | try { 168 | // Is Local 169 | loadResource( new File( url.toURI() ), "" ); 170 | } catch (IllegalArgumentException iae) { 171 | // Is Remote 172 | loadRemoteResource( url ); 173 | } catch (URISyntaxException e) { 174 | throw new JclException( "URISyntaxException", e ); 175 | } 176 | } 177 | 178 | /** 179 | * Reads local resources from - Jar files - Class folders - Jar Library 180 | * folders 181 | * 182 | * @param path 183 | * @throws IOException 184 | */ 185 | public void loadResource(String path) throws IOException { 186 | if( logger.isTraceEnabled() ) 187 | logger.trace( "Resource: " + path ); 188 | loadResource( new File( path ), "" ); 189 | } 190 | 191 | /** 192 | * Reads local resources from - Jar files - Class folders - Jar Library 193 | * folders 194 | * 195 | * @param fol 196 | * @param packName 197 | * @throws IOException 198 | */ 199 | private void loadResource(File fol, String packName) throws IOException { 200 | if( fol.isFile() ) { 201 | if( fol.getName().toLowerCase().endsWith( ".class" ) ) { 202 | loadClassContent( fol.getAbsolutePath(), packName ); 203 | } else { 204 | if( fol.getName().toLowerCase().endsWith( ".jar" ) ) { 205 | loadJar( fol.getAbsolutePath() ); 206 | } else { 207 | loadResourceContent( fol.getAbsolutePath() ); 208 | } 209 | } 210 | 211 | return; 212 | } 213 | 214 | if( fol.list() != null ) { 215 | for( String f : fol.list() ) { 216 | File fl = new File( fol.getAbsolutePath() + "/" + f ); 217 | 218 | String pn = packName; 219 | 220 | if( fl.isDirectory() ) { 221 | 222 | if( !pn.equals( "" ) ) 223 | pn = pn + "/"; 224 | 225 | pn = pn + fl.getName(); 226 | } 227 | 228 | loadResource( fl, pn ); 229 | } 230 | } 231 | } 232 | 233 | /** 234 | * Removes the loaded resource 235 | * 236 | * @param resource 237 | * @throws ResourceNotFoundException 238 | */ 239 | public void unload(String resource) throws ResourceNotFoundException { 240 | if( jarEntryContents.containsKey( resource ) ) { 241 | if( logger.isTraceEnabled() ) 242 | logger.trace( "Removing resource " + resource ); 243 | jarEntryContents.remove( resource ); 244 | } else { 245 | throw new ResourceNotFoundException( resource, "Resource not found in local ClasspathResources" ); 246 | } 247 | } 248 | } 249 | -------------------------------------------------------------------------------- /JCL/src/xeus/jcl/JarClassLoader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * JCL (Jar Class Loader) 3 | * 4 | * Copyright (C) 2009 Xeus Technologies 5 | * 6 | * This file is part of Jar Class Loader (JCL). 7 | * Jar Class Loader (JCL) is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * JarClassLoader is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with JCL. If not, see . 19 | * 20 | * @author Kamran Zafar 21 | * 22 | * Contact Info: 23 | * Email: xeus.man@gmail.com 24 | * Web: http://xeustech.blogspot.com 25 | */ 26 | 27 | package xeus.jcl; 28 | 29 | import java.io.ByteArrayInputStream; 30 | import java.io.IOException; 31 | import java.io.InputStream; 32 | import java.net.URL; 33 | import java.util.Collections; 34 | import java.util.HashMap; 35 | import java.util.Map; 36 | 37 | import org.apache.log4j.Logger; 38 | 39 | import xeus.jcl.exception.JclException; 40 | import xeus.jcl.exception.ResourceNotFoundException; 41 | import xeus.jcl.loader.Loader; 42 | 43 | /** 44 | * Reads the class bytes from jar files and other resources using 45 | * ClasspathResources 46 | * 47 | * @author Kamran Zafar 48 | * 49 | */ 50 | @SuppressWarnings("unchecked") 51 | public class JarClassLoader extends AbstractClassLoader { 52 | protected final Map classes; 53 | protected final ClasspathResources classpathResources; 54 | protected char classNameReplacementChar; 55 | private static Logger logger = Logger.getLogger( JarClassLoader.class ); 56 | private final Loader localLoader = new LocalLoader(); 57 | 58 | public JarClassLoader() { 59 | classpathResources = new ClasspathResources(); 60 | classes = Collections.synchronizedMap( new HashMap() ); 61 | loaders.add( localLoader ); 62 | } 63 | 64 | /** 65 | * Loads classes from different sources 66 | * 67 | * @param resources 68 | * @throws IOException 69 | */ 70 | public JarClassLoader(Object[] resources) throws IOException { 71 | this(); 72 | 73 | for( Object resource : resources ) { 74 | if( resource instanceof InputStream ) 75 | add( (InputStream) resource ); 76 | else if( resource instanceof URL ) 77 | add( (URL) resource ); 78 | else if( resource instanceof String ) 79 | add( (String) resource ); 80 | else 81 | throw new JclException( "Unknown Resource type" ); 82 | } 83 | } 84 | 85 | /** 86 | * Loads local/remote resource 87 | * 88 | * @param resourceName 89 | * @throws IOException 90 | */ 91 | public void add(String resourceName) throws IOException { 92 | classpathResources.loadResource( resourceName ); 93 | } 94 | 95 | /** 96 | * Loads resource from InputStream 97 | * 98 | * @param jarStream 99 | * @throws IOException 100 | */ 101 | public void add(InputStream jarStream) throws IOException { 102 | classpathResources.loadJar( jarStream ); 103 | } 104 | 105 | /** 106 | * Loads local/remote resource 107 | * 108 | * @param url 109 | * @throws IOException 110 | */ 111 | public void add(URL url) throws IOException { 112 | classpathResources.loadResource( url ); 113 | } 114 | 115 | /** 116 | * 117 | * Reads the class bytes from different local and remote resources using 118 | * ClasspathResources 119 | * 120 | * @param className 121 | * @return class bytes 122 | */ 123 | protected byte[] loadClassBytes(String className) { 124 | className = formatClassName( className ); 125 | 126 | return classpathResources.getResource( className ); 127 | } 128 | 129 | /** 130 | * @param className 131 | * @return String 132 | */ 133 | protected String formatClassName(String className) { 134 | if( classNameReplacementChar == '\u0000' ) { 135 | // '/' is used to map the package to the path 136 | return className.replace( '.', '/' ) + ".class"; 137 | } else { 138 | // Replace '.' with custom char, such as '_' 139 | return className.replace( '.', classNameReplacementChar ) + ".class"; 140 | } 141 | } 142 | 143 | /** 144 | * Attempts to unload class, it only unloads the locally loaded classes by 145 | * JCL 146 | * 147 | * @param className 148 | */ 149 | public void unloadClass(String className) { 150 | if( logger.isTraceEnabled() ) 151 | logger.trace( "Unloading class " + className ); 152 | 153 | if( classes.containsKey( className ) ) { 154 | if( logger.isTraceEnabled() ) 155 | logger.trace( "Removing loaded class " + className ); 156 | classes.remove( className ); 157 | try { 158 | classpathResources.unload( formatClassName( className ) ); 159 | } catch (ResourceNotFoundException e) { 160 | throw new JclException( "Something is very wrong!!!" 161 | + "The locally loaded classes must be in synch with ClasspathResources", e ); 162 | } 163 | } else { 164 | try { 165 | classpathResources.unload( formatClassName( className ) ); 166 | } catch (ResourceNotFoundException e) { 167 | throw new JclException( "Class could not be unloaded " 168 | + "[Possible reason: Class belongs to the system]", e ); 169 | } 170 | } 171 | } 172 | 173 | /** 174 | * @param replacement 175 | */ 176 | public void setClassNameReplacementChar(char replacement) { 177 | classNameReplacementChar = replacement; 178 | } 179 | 180 | /** 181 | * @return char 182 | */ 183 | public char getClassNameReplacementChar() { 184 | return classNameReplacementChar; 185 | } 186 | 187 | /** 188 | * Local class loader 189 | * 190 | */ 191 | class LocalLoader extends Loader { 192 | 193 | private final Logger logger = Logger.getLogger( LocalLoader.class ); 194 | 195 | public LocalLoader() { 196 | order = 1; 197 | enabled = Configuration.isLocalLoaderEnabled(); 198 | } 199 | 200 | @Override 201 | public Class load(String className, boolean resolveIt) { 202 | Class result = null; 203 | byte[] classBytes; 204 | // if( logger.isTraceEnabled() ) 205 | // logger.trace( "Loading class: " + className + ", " + resolveIt + 206 | // "" ); 207 | 208 | result = classes.get( className ); 209 | if( result != null ) { 210 | if( logger.isTraceEnabled() ) 211 | logger.trace( "Returning local loaded class " + className ); 212 | return result; 213 | } 214 | 215 | classBytes = loadClassBytes( className ); 216 | if( classBytes == null ) { 217 | return null; 218 | } 219 | 220 | result = defineClass( className, classBytes, 0, classBytes.length ); 221 | 222 | if( result == null ) { 223 | return null; 224 | } 225 | 226 | if( resolveIt ) 227 | resolveClass( result ); 228 | 229 | classes.put( className, result ); 230 | if( logger.isTraceEnabled() ) 231 | logger.trace( "Return newly loaded class " + className ); 232 | return result; 233 | } 234 | 235 | @Override 236 | public InputStream loadResource(String name) { 237 | byte[] arr = classpathResources.getResource( name ); 238 | if( arr != null ) { 239 | if( logger.isTraceEnabled() ) 240 | logger.trace( "Returning newly loaded resource " + name ); 241 | 242 | return new ByteArrayInputStream( arr ); 243 | } 244 | 245 | return null; 246 | } 247 | } 248 | 249 | /** 250 | * @return Local JCL Loader 251 | */ 252 | public Loader getLocalLoader() { 253 | return localLoader; 254 | } 255 | 256 | /** 257 | * Returns all JCL-loaded-classes as an immutable Map 258 | * 259 | * @return Map 260 | */ 261 | public Map getLoadedClasses() { 262 | return Collections.unmodifiableMap( classes ); 263 | } 264 | } 265 | -------------------------------------------------------------------------------- /JCL2/core/src/main/java/org/xeustechnologies/jcl/JarResources.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * Copyright 2015 Kamran Zafar 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 | package org.xeustechnologies.jcl; 19 | 20 | import java.io.BufferedInputStream; 21 | import java.io.ByteArrayOutputStream; 22 | import java.io.File; 23 | import java.io.FileInputStream; 24 | import java.io.IOException; 25 | import java.io.InputStream; 26 | import java.net.MalformedURLException; 27 | import java.net.URL; 28 | import java.util.Collections; 29 | import java.util.HashMap; 30 | import java.util.Map; 31 | import java.util.jar.JarEntry; 32 | import java.util.jar.JarFile; 33 | import java.util.jar.JarInputStream; 34 | 35 | import org.slf4j.Logger; 36 | import org.slf4j.LoggerFactory; 37 | import org.xeustechnologies.jcl.exception.JclException; 38 | 39 | /** 40 | * JarResources reads jar files and loads the class content/bytes in a HashMap 41 | * 42 | * @author Kamran Zafar 43 | * 44 | */ 45 | public class JarResources { 46 | 47 | protected Map jarEntryContents; 48 | protected boolean collisionAllowed; 49 | 50 | private final transient Logger logger = LoggerFactory.getLogger( JarResources.class ); 51 | 52 | /** 53 | * Default constructor 54 | */ 55 | public JarResources() { 56 | jarEntryContents = new HashMap(); 57 | collisionAllowed = Configuration.suppressCollisionException(); 58 | } 59 | 60 | /** 61 | * @param name 62 | * @return URL 63 | */ 64 | public URL getResourceURL(String name) { 65 | 66 | JclJarEntry entry = jarEntryContents.get(name); 67 | if (entry != null) { 68 | if (entry.getBaseUrl() == null) { 69 | throw new JclException( "non-URL accessible resource" ); 70 | } 71 | try { 72 | return new URL( entry.getBaseUrl().toString() + name ); 73 | } catch (MalformedURLException e) { 74 | throw new JclException( e ); 75 | } 76 | } 77 | 78 | return null; 79 | } 80 | 81 | /** 82 | * @param name 83 | * @return byte[] 84 | */ 85 | public byte[] getResource(String name) { 86 | JclJarEntry entry = jarEntryContents.get(name); 87 | if (entry != null) { 88 | return entry.getResourceBytes(); 89 | } 90 | else { 91 | return null; 92 | } 93 | } 94 | 95 | /** 96 | * Returns an immutable Map of all jar resources 97 | * 98 | * @return Map 99 | */ 100 | public Map getResources() { 101 | 102 | Map resourcesAsBytes = new HashMap(jarEntryContents.size()); 103 | 104 | for (Map.Entry entry : jarEntryContents.entrySet()) { 105 | resourcesAsBytes.put(entry.getKey(), entry.getValue().getResourceBytes()); 106 | } 107 | 108 | return resourcesAsBytes; 109 | } 110 | 111 | /** 112 | * Reads the specified jar file 113 | * 114 | * @param jarFile 115 | */ 116 | public void loadJar(String jarFile) { 117 | logger.debug( "Loading jar: {}", jarFile ); 118 | 119 | FileInputStream fis = null; 120 | try { 121 | File file = new File( jarFile ); 122 | String baseUrl = "jar:" + file.toURI().toString() + "!/"; 123 | fis = new FileInputStream( file ); 124 | loadJar(baseUrl, fis); 125 | } catch (IOException e) { 126 | throw new JclException( e ); 127 | } finally { 128 | if (fis != null) 129 | try { 130 | fis.close(); 131 | } catch (IOException e) { 132 | throw new JclException( e ); 133 | } 134 | } 135 | } 136 | 137 | /** 138 | * Reads the jar file from a specified URL 139 | * 140 | * @param url 141 | */ 142 | public void loadJar(URL url) { 143 | logger.debug( "Loading jar: {}", url.toString() ); 144 | 145 | InputStream in = null; 146 | try { 147 | String baseUrl = "jar:" + url.toString() + "!/"; 148 | in = url.openStream(); 149 | loadJar( baseUrl, in ); 150 | } catch (IOException e) { 151 | throw new JclException( e ); 152 | } finally { 153 | if (in != null) 154 | try { 155 | in.close(); 156 | } catch (IOException e) { 157 | throw new JclException( e ); 158 | } 159 | } 160 | } 161 | 162 | public void loadJar(String argBaseUrl, InputStream jarStream) { 163 | loadJar(argBaseUrl, jarStream, true); 164 | } 165 | 166 | /** 167 | * Load the jar contents from InputStream 168 | * @param argBaseUrl 169 | * 170 | */ 171 | public void loadJar(String argBaseUrl, InputStream jarStream, boolean closeStream) { 172 | 173 | BufferedInputStream bis = null; 174 | JarInputStream jis = null; 175 | 176 | try { 177 | bis = new BufferedInputStream( jarStream ); 178 | jis = new JarInputStream( bis ); 179 | 180 | JarEntry jarEntry = null; 181 | while (( jarEntry = jis.getNextJarEntry() ) != null) { 182 | logger.debug( dump( jarEntry ) ); 183 | 184 | if (jarEntry.isDirectory()) { 185 | continue; 186 | } 187 | 188 | if (jarEntryContents.containsKey( jarEntry.getName() )) { 189 | if (!collisionAllowed) 190 | throw new JclException( "Class/Resource " + jarEntry.getName() + " already loaded" ); 191 | else { 192 | logger.debug( "Class/Resource {} already loaded; ignoring entry...", jarEntry.getName() ); 193 | continue; 194 | } 195 | } 196 | 197 | logger.debug( "Entry Name: {}, Entry Size: {}", jarEntry.getName(), jarEntry.getSize() ); 198 | 199 | byte[] b = new byte[2048]; 200 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 201 | 202 | int len = 0; 203 | while (( len = jis.read( b ) ) > 0) { 204 | out.write( b, 0, len ); 205 | } 206 | 207 | // add to internal resource HashMap 208 | JclJarEntry entry = new JclJarEntry(); 209 | entry.setBaseUrl(argBaseUrl); 210 | entry.setResourceBytes(out.toByteArray()); 211 | jarEntryContents.put( jarEntry.getName(), entry ); 212 | 213 | logger.debug("{}: size={}, csize={}", jarEntry.getName(), out.size(), jarEntry.getCompressedSize()); 214 | 215 | out.close(); 216 | } 217 | } catch (IOException e) { 218 | throw new JclException( e ); 219 | } catch (NullPointerException e) { 220 | logger.debug( "Done loading." ); 221 | } finally { 222 | if(closeStream) { 223 | if (jis != null) 224 | try { 225 | jis.close(); 226 | } catch (IOException e) { 227 | throw new JclException(e); 228 | } 229 | 230 | if (bis != null) 231 | try { 232 | bis.close(); 233 | } catch (IOException e) { 234 | throw new JclException(e); 235 | } 236 | } 237 | } 238 | } 239 | 240 | /** 241 | * For debugging 242 | * 243 | * @param je 244 | * @return String 245 | */ 246 | private String dump(JarEntry je) { 247 | StringBuffer sb = new StringBuffer(); 248 | if (je.isDirectory()) { 249 | sb.append( "d " ); 250 | } else { 251 | sb.append( "f " ); 252 | } 253 | 254 | if (je.getMethod() == JarEntry.STORED) { 255 | sb.append( "stored " ); 256 | } else { 257 | sb.append( "defalted " ); 258 | } 259 | 260 | sb.append( je.getName() ); 261 | sb.append( "\t" ); 262 | sb.append( "" + je.getSize() ); 263 | if (je.getMethod() == JarEntry.DEFLATED) { 264 | sb.append( "/" + je.getCompressedSize() ); 265 | } 266 | 267 | return ( sb.toString() ); 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /JCL/docs/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Jar Class Loader 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 30 | 31 | 32 | 33 | 34 |

38 | 48 |
49 |

Overview

50 |

JCL is a simple Java API that allows loading classes from Jar files. It uses 51 | a custom class loader to load classes from JARs, which helps in isolating class 52 | loaders and it even makes it possible to load multiple versions of the same class 53 | file from different jars. JCL also integrates with Spring; the integration is 54 | tested with Spring 2.5.

55 |

Installation

56 |

To use JCL, download 57 | and build the JCL project and put jcl.jar and dependencies in the application's 58 | classpath.

59 |

Usage

60 |

JCL is a light weight API and has only a few but useful classes. Here is a 61 | simple example on how to programmatically use JCL. JarClassLoader has an arguments-constructor and add methods that 62 | take jar-file/class-folder paths, URLs and InputStreams.

63 |

64 |
			JarClassLoader jcl = new JarClassLoader();
 65 | 			jcl.add("myjar.jar"); //Load jar file
 66 | 			jcl.add(new URL("http://myserver.com/myjar.jar")); //Load jar from a URL
 67 | 			jcl.add(new FileInputStream("myotherjar.jar")); //Load jar file from stream
 68 | 			jcl.add("myclassfolder/"); //Load class folder
 69 | 			jcl.add("myjarlib/"); //Recursively load all jar files in the folder/sub-folder(s)
 70 | 			
 71 | 			JclObjectFactory factory = JclObjectFactory.getInstance();
 72 | 			
 73 | 			//Create object of loaded class
 74 | 			Object obj = factory.create(jcl,"mypackage.MyClass");
 75 | 			
76 | 77 |

Now in order to invoke the methods of the object one has to use reflection.

78 |

Similarly JCL can be used with spring framework.

79 |
			
 80 | <!-- Singleton object factory -->
 81 | <bean id="jclFactory" class="xeus.jcl.JclObjectFactory"></bean>
 82 | <!-- Test jar class loader -->
 83 | <bean id="jcl" class="xeus.jcl.JarClassLoader">
 84 | 	<constructor-arg>
 85 | 		<list>
 86 | 			<value>myjar.jar</value>
 87 | 			<ref bean="someJarInputStream"></ref>
 88 | 			<ref bean="someJarURL" /><ref></ref>
 89 | 		</list>
 90 | 	</constructor-arg>
 91 | </bean>
 92 | 
 93 | <!-- 
 94 | 	The Bean arguments: 
 95 | 	1- The jar class loader
 96 | 	2- Name of the class to be loaded
 97 | 	The rest are optional arguments used in constructor/setter injection for the bean to be loaded 
 98 | -->
 99 | <bean id="test" factory-bean="jclFactory" factory-method="create">
100 | 	<!-- Factory-method arguments -->
101 | 	<constructor-arg>
102 | 		<ref bean="jcl"></ref>
103 | 	</constructor-arg>
104 | 	<constructor-arg>
105 | 		<value>mypackage.MyClass</value>
106 | 	</constructor-arg>
107 | 
108 | 	<!-- Bean arguments -->
109 | 	<constructor-arg>
110 | 		<value>Hello</value>
111 | 	</constructor-arg>
112 | 	<property name="name">
113 | 		<value>Kamran Zafar</value>
114 | 	</property>
115 | </bean>
116 | 			
117 |
118 |

Casting objects loaded in JCL

119 |

Java disallows casting objects loaded in a different classloader; such casting attempt results in a ClassCastException. But sometimes it is necessary to cast objects to obtain interface references 120 | loaded in the current classloader. E.g. suppose that an API implementation is loaded using JCL and the API itself is loaded in the current classloader, now it is easy to use the interface reference to invoke methods than using reflection. JCL provides a few ways to obtain interface references and coverting the JCL-loaded objects into castable objects.

121 |
			JarClassLoader jcl = new JarClassLoader();
122 | 			jcl.add("myapi-impl.jar"); //Load jar file
123 | 			
124 | 			JclObjectFactory factory = JclObjectFactory.getInstance();
125 | 			
126 | 			//Create object of loaded class
127 | 			Object obj = factory.create(jcl,"myapi.impl.MyInterfaceImpl");
128 | 
129 | 			//Obtain interface reference in the current classloader
130 | 			MyInterface mi = JclUtils.cast(obj, MyInterface.class);
131 | 
132 | 			//Convert the object into a castable object in the current classloader
133 | 			MyInterface mi1 = (MyInterface) JclUtils.toCastable(obj, MyInterface.class);
134 | 
135 | 			//Clone "Serializable" object into a castable object in the current classloader
136 | 			//The JCL-loaded object must implement Serializable
137 | 			MyInterface mi2 = (MyInterface) JclUtils.clone(obj);
138 | 			
139 |
140 |

Manipulating class loading order & adding custom classloaders

141 |

JCL as of version 1.3 by default looks for classes in four places in order, local JCL class loader (order=1), 142 | current class loader (order=2), parent class loader (order=3) and then in system class loader (order=4). More 143 | class loaders can be added by extending the xeus.jcl.loader.Loader class. 144 | Each loader has a loading order, the default loading orders can be changed. Below 145 | is an example that shows how this loading order can be changed.

146 |
JarClassLoader jcl=new JarClassLoader();
147 | jcl.add("myjar.jar"); // Add some class source
148 | 
149 | jcl.getSystemLoader().setOrder(1); // Look for classes in system class loader first
150 | jcl.getLocalLoader().setOrder(2); // if not found look for classes in local class loader
151 | jcl.getParentLoader().setOrder(3); // if not found look for classes in parent class loader
152 | jcl.getCurrentLoader().setOrder(4); // if not found look for classes in current class loader
153 | 
154 | // A custom class loader that extends xeus.jcl.loader.Loader
155 | MyLoader loader=new MyLoader();
156 | loader.setOrder(5);
157 | 
158 | jcl.addLoader(loader); //Add custom loader
159 |

Resources

160 |

161 | 162 | - A nice tutorial with examples by Nestor
163 | - Javadoc API for more details. 164 |

165 |

Downloads

166 |

JCL is open source under GPL 167 | and is available for download 168 | on sourceforge. The source can be anonymously checked-out from subversion using 169 | the following command:
170 |
171 | svn co https://jcloader.svn.sourceforge.net/svnroot/jcloader/trunk/JCL JCL

172 |
173 |
174 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | --------------------------------------------------------------------------------