View Javadoc

1   /*
2   Copyright (c) 2006, University of Tromsø
3   All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without 
6   modification, are permitted provided that the following conditions are met:
7   
8    * Redistributions of source code must retain the above copyright notice, this list 
9      of conditions and the following disclaimer.
10  
11   * Redistributions in binary form must reproduce the above copyright notice, this 
12     list of conditions and the following disclaimer in the documentation and/or other 
13     materials provided with the distribution.
14  
15   * Neither the name of the University of Tromsø nor the names of its contributors may 
16     be used to endorse or promote products derived from this software without specific 
17     prior written permission.
18  
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
20  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
21  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
22  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
23  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
24  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
25  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
26  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
27  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
28  DAMAGE.
29  */
30  
31  package argos.deploy;
32  
33  import java.io.File;
34  import java.net.MalformedURLException;
35  import java.net.URL;
36  import java.net.URLClassLoader;
37  import java.util.ArrayList;
38  import java.util.List;
39  import java.util.logging.Level;
40  import java.util.logging.Logger;
41  
42  import argos.metadata.ServiceMetaInfo;
43  
44  public final class JarClassloader extends URLClassLoader implements JarClassloaderMBean {
45  	private static final Logger logger = Logger.getLogger(JarClassloader.class.getName());
46  	public static final String DYNAMIC_PROXY = "DynamicProxy.jar";
47  	public static final String DYNAMIC_PROXY_CLASS_NAME = "argos.proxy.DynamicProxy";
48  	
49  	private URLClassLoader componentClassLoader;
50  	
51  	public JarClassloader(URL[] files) {
52  		this(files, ClassLoader.getSystemClassLoader());
53  	}
54  	
55  	private JarClassloader(URL[] files, ClassLoader loader) {
56  		super(new URL[]{});
57  		try {
58  			addURL(new URL("file:" + (new File(DYNAMIC_PROXY)).getAbsolutePath()));
59  		}
60  		catch(MalformedURLException e) {
61  			logger.log(Level.SEVERE, "Unable to load " + DYNAMIC_PROXY + " into classpath", e);
62  		}
63  		componentClassLoader = new URLClassLoader(files, loader);
64  	}
65  	
66  	public static JarClassloader getInstance(ServiceMetaInfo service) {
67  		return getInstance(service, null);
68  	}
69  	
70  	@Override
71  	public Class<?> findClass(String className) throws ClassNotFoundException {
72  		if(!className.equals(DYNAMIC_PROXY_CLASS_NAME)) {
73  			return componentClassLoader.loadClass(className);
74  		}
75  		else {
76  			return super.findClass(className);
77  		}
78  	}
79  	
80  	@Override
81  	public Class<?> loadClass(String className) throws ClassNotFoundException {
82  		if(!className.equals(DYNAMIC_PROXY_CLASS_NAME)) {
83  			return componentClassLoader.loadClass(className);
84  		}
85  		else {
86  			return super.loadClass(className);
87  		}
88  	}
89  	
90  	@Override
91  	public URL findResource(String name) {
92  		if(!name.equals(DYNAMIC_PROXY_CLASS_NAME)) {
93  			return componentClassLoader.findResource(name);
94  		}
95  		else {
96  			return super.findResource(name);
97  		}
98  	}
99  	
100 	@Override
101 	public URL getResource(String name) {
102 		if(!name.equals(DYNAMIC_PROXY_CLASS_NAME)) {
103 			return componentClassLoader.getResource(name);
104 		}
105 		else {
106 			return super.getResource(name);
107 		}
108 	}
109 	
110 	public static JarClassloader getInstance(ServiceMetaInfo service, ClassLoader classLoader) {
111 		//Find files
112 		List<File> files = new ArrayList<File>();
113 		files.add(service.getTempFile());
114 		for(File newFile : service.getLibraries()) {
115 			files.add(newFile);
116 		}
117 		
118 		//Add files
119 		URL[] urls = new URL[files.size()];
120 		for(int i = 0; i < files.size(); i++) {
121 			try {
122 				urls[i] = new URL("file:" + files.get(i).getAbsolutePath());
123 			}
124 			catch (MalformedURLException e) {
125 				logger.log(Level.SEVERE, "Unable to load jar into classpath: " + 
126 						files.get(i).getAbsolutePath(), e);
127 			}
128 		}
129 		
130 		//Create class loader
131 		if(classLoader == null) {
132 			return new JarClassloader(urls);
133 		}
134 		else {
135 			return new JarClassloader(urls, classLoader);
136 		}
137 	}
138 }