summaryrefslogtreecommitdiffstats
path: root/sandbox/slaws/classloader
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/slaws/classloader')
-rw-r--r--sandbox/slaws/classloader/pom.xml52
-rw-r--r--sandbox/slaws/classloader/src/main/java/cl/CustomClassLoader.java83
-rw-r--r--sandbox/slaws/classloader/src/main/java/patha/SomeClassA.java15
-rw-r--r--sandbox/slaws/classloader/src/main/java/patha/SomeClassB.java21
-rw-r--r--sandbox/slaws/classloader/src/test/java/test/ClassloaderTestCase.java90
5 files changed, 261 insertions, 0 deletions
diff --git a/sandbox/slaws/classloader/pom.xml b/sandbox/slaws/classloader/pom.xml
new file mode 100644
index 0000000000..12490917be
--- /dev/null
+++ b/sandbox/slaws/classloader/pom.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+-->
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-sca</artifactId>
+ <version>1.0-incubating-SNAPSHOT</version>
+ <relativePath>../../pom.xml</relativePath>
+ </parent>
+ <artifactId>sample-classloader</artifactId>
+ <name>Apache Tuscany Classloader Sample</name>
+
+ <repositories>
+ <repository>
+ <id>apache.incubator</id>
+ <url>http://people.apache.org/repo/m2-incubating-repository</url>
+ </repository>
+ </repositories>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.2</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <finalName>${artifactId}</finalName>
+ </build>
+</project>
diff --git a/sandbox/slaws/classloader/src/main/java/cl/CustomClassLoader.java b/sandbox/slaws/classloader/src/main/java/cl/CustomClassLoader.java
new file mode 100644
index 0000000000..050b770eab
--- /dev/null
+++ b/sandbox/slaws/classloader/src/main/java/cl/CustomClassLoader.java
@@ -0,0 +1,83 @@
+package cl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+public class CustomClassLoader extends ClassLoader
+{
+ public CustomClassLoader(){
+ super(CustomClassLoader.class.getClassLoader());
+ }
+
+ public Class loadClass( String name, boolean resolve )
+ throws ClassNotFoundException {
+ // Our goal is to get a Class object
+ Class clazz = null;
+
+ // First, see if we've already dealt with this one
+ clazz = findLoadedClass(name);
+
+ String fileStub = name.replace( '.', '/' );
+ String classFilename = "target/classes/" + fileStub+".class";
+
+ try {
+ // read the bytes
+ byte raw[] = getBytes( classFilename );
+ // try to turn them into a class
+ clazz = defineClass( name, raw, 0, raw.length );
+ } catch( IOException ex ) {
+ // This is not a failure! If we reach here, it might
+ // mean that we are dealing with a class in a library,
+ // such as java.lang.Object
+ }
+
+ // Maybe the class is in a library -- try loading
+ // the normal way
+ if (clazz==null) {
+ clazz = findSystemClass( name );
+ }
+
+ // Resolve the class, if any, but only if the "resolve"
+ // flag is set to true
+ if (resolve && clazz != null) {
+ resolveClass( clazz );
+ }
+
+ // If we still don't have a class, it's an error
+ if (clazz == null) {
+ throw new ClassNotFoundException( name );
+ }
+
+ return clazz;
+ }
+
+ // Given a filename, read the entirety of that file from disk
+ // and return it as a byte array.
+ private byte[] getBytes( String filename ) throws IOException {
+ // Find out the length of the file
+ File file = new File( filename );
+ long len = file.length();
+
+ // Create an array that's just the right size for the file's
+ // contents
+ byte raw[] = new byte[(int)len];
+
+ // Open the file
+ FileInputStream fin = new FileInputStream( file );
+
+ // Read all of it into the array; if we don't get all,
+ // then it's an error.
+ int r = fin.read( raw );
+
+ if (r != len) {
+ throw new IOException( "Can't read all, "+r+" != "+len );
+ }
+
+ // Don't forget to close the file!
+ fin.close();
+
+ // And finally return the file contents as an array
+ return raw;
+ }
+}
diff --git a/sandbox/slaws/classloader/src/main/java/patha/SomeClassA.java b/sandbox/slaws/classloader/src/main/java/patha/SomeClassA.java
new file mode 100644
index 0000000000..bf9c8f012f
--- /dev/null
+++ b/sandbox/slaws/classloader/src/main/java/patha/SomeClassA.java
@@ -0,0 +1,15 @@
+package patha;
+
+
+
+public class SomeClassA {
+
+
+
+ public SomeClassA() {
+ }
+
+ public void doSomething(SomeClassB someClassB) {
+ System.out.println(someClassB.getSomeString());
+ }
+}
diff --git a/sandbox/slaws/classloader/src/main/java/patha/SomeClassB.java b/sandbox/slaws/classloader/src/main/java/patha/SomeClassB.java
new file mode 100644
index 0000000000..e4b9302055
--- /dev/null
+++ b/sandbox/slaws/classloader/src/main/java/patha/SomeClassB.java
@@ -0,0 +1,21 @@
+package patha;
+
+
+
+public class SomeClassB {
+
+ String someString;
+
+ public SomeClassB() {
+ this.someString = "Default String";
+ }
+
+ public void setSomeString(String someString) {
+ this.someString = someString;
+ System.out.println(someString);
+ }
+
+ public String getSomeString() {
+ return someString;
+ }
+}
diff --git a/sandbox/slaws/classloader/src/test/java/test/ClassloaderTestCase.java b/sandbox/slaws/classloader/src/test/java/test/ClassloaderTestCase.java
new file mode 100644
index 0000000000..65bfefb82a
--- /dev/null
+++ b/sandbox/slaws/classloader/src/test/java/test/ClassloaderTestCase.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package test;
+
+import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.util.logging.LogManager;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import cl.CustomClassLoader;
+
+import patha.SomeClassA;
+
+
+/**
+ * This shows how to test the Calculator service component.
+ */
+public class ClassloaderTestCase {
+
+
+
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ @After
+ public void tearDown() throws Exception {
+
+ }
+
+ @Test
+ public void testClassloader() throws Exception {
+
+ try {
+ CustomClassLoader cl1 = new CustomClassLoader();
+
+ // get the class
+ Class classA1 = cl1.loadClass("patha.SomeClassA");
+ Class classB1 = cl1.loadClass("patha.SomeClassB");
+
+ // Find the doSomething method in the class
+ Method doSomething = classA1.getMethod( "doSomething", classB1 );
+
+ // create an instance of the class
+ Object instanceA1 = classA1.newInstance();
+ Object instanceB1 = classB1.newInstance();
+
+ // Create a list containing the arguments
+ Object argsArray1[] = { instanceB1 };
+
+ // Call the method
+ doSomething.invoke( instanceA1, argsArray1 );
+
+ // Set up a B with a new class loader
+ CustomClassLoader cl2 = new CustomClassLoader();
+ Class classB2 = cl2.loadClass("patha.SomeClassB");
+ Method setSomeString = classB2.getMethod( "setSomeString", String.class );
+ Object instanceB2 = classB2.newInstance();
+ Object argsArray2[] = { "B2 String" };
+ setSomeString.invoke( instanceB2, argsArray2 );
+
+ // try to pass it into A from the first class loader
+ Object argsArray3[] = { instanceB2 };
+ doSomething.invoke( instanceA1, argsArray3 );
+
+
+ } catch (Exception ex) {
+ System.err.println(ex.toString());
+ }
+ }
+}