View Javadoc

1   package es.caib.signatura.impl;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.net.MalformedURLException;
6   import java.net.URL;
7   import java.net.URLClassLoader;
8   import java.util.Hashtable;
9   
10  public class ClassLoaderFactory {
11  	private static ClassLoaderFactory theFactory = null;
12  	private static ClassLoader masterClassLoader = null;
13  	private static Hashtable childClassLoader = new Hashtable();
14  	private static File libDir = null;
15  	
16  	private ClassLoaderFactory () throws FileNotFoundException
17  	{
18  		//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: ClassLoaderFactory:enter" );
19  		if (masterClassLoader == null)
20  		{
21  			try {
22  				libDir = locateSignaturaDir();
23  				masterClassLoader = createClassLoader (libDir, getClass().getClassLoader());
24  			} catch (Throwable e){			
25  			}
26  		}
27  		//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: ClassLoaderFactory: return" );
28  	}
29  
30  	/**
31  	 * 
32  	 */
33  	private File locateSignaturaDir() {
34  		File libDir = null;
35  		try {
36  			String signaturaDir = System.getProperty ("es.caib.signatura.library_path");
37  			if (signaturaDir != null)
38  			{
39  				File sigDir = new File(signaturaDir);
40  				if (sigDir.isAbsolute() && sigDir.isDirectory())
41  					libDir = sigDir;
42  				else 
43  				{
44  					//* DEBUG OFF */ System.out.println("Signaturadir no absolute");
45  					URL url = this.getClass().getResource("ClassLoaderFactory.class");
46  					if ("jar".equals(url.getProtocol()))
47  					{
48  						int i  = url.getFile().lastIndexOf('!');
49  						if (i > 0)
50  						{
51  							URL jarFileUrl = new URL(url.getFile().substring(0, i));
52  							if ("file".equals(jarFileUrl.getProtocol()))
53  							{
54  								File f = new File (jarFileUrl.getFile());
55  								File newLibDir = new File (f.getParentFile(), signaturaDir);
56  								//* DEBUG OFF */ System.out.println ("newlibdir="+newLibDir.getPath());
57  								if (newLibDir.isDirectory())
58  								{
59  									libDir = newLibDir;
60  								}
61  							}
62  						}
63  					}
64  				}
65  			}
66  		} catch (Throwable e) {
67  			e.printStackTrace();
68  		}
69  		if (libDir == null)
70  		{
71  			String java_home = System.getProperty("java.home");
72  			libDir = new File (new File (java_home), "lib/signaturacaib");
73  		}
74  		return libDir;
75  	}
76  	
77  	public static synchronized  ClassLoaderFactory getFactory () throws FileNotFoundException
78  	{
79  		//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: getFactory:enter" );
80  		if (theFactory == null) {
81  			//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: getFactory:new LoaderFactory" );
82  			theFactory = new ClassLoaderFactory ();
83  		}
84  		//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: getFactory:return" );
85  		return theFactory;
86  	}
87  
88  	public File getLibraryDir ()
89  	{
90  		return libDir;
91  	}
92  	
93  	
94  	private synchronized static URLClassLoader createClassLoader(File dir, ClassLoader parentClassLoader) throws FileNotFoundException {
95  		//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: createClassLoader:enter " + dir.toString() );
96  		File files [] = dir.listFiles();
97  		if (files == null) {
98  			throw new FileNotFoundException(dir.getPath());
99  		}
100 
101 		URL urls [] = new URL [ files.length ];
102 		for (int i = 0; i < files.length; i++)
103 		{
104 			try {
105 				urls [ i ] = files [ i ] .toURL();
106 				//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: createClassLoader: new url " + urls[i] );
107 			} catch (MalformedURLException e) {
108 				System.err.println(e.toString());
109 			}
110 		}
111 
112 		//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: createClassLoader: new URLClassLoader " + urls.toString() );
113 		return new ExclusiveURLClassLoader (urls, parentClassLoader);
114 	}
115 
116 	public synchronized  ClassLoader getMasterClassLoader ()
117 	{
118 		return masterClassLoader;
119 	}
120 	
121 	public synchronized ClassLoader getClassLoader(String name) throws FileNotFoundException
122 	{
123 		//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: getClassLoader: enter" );
124 		ClassLoader c = (ClassLoader) childClassLoader.get(name);
125 		if (c == null)
126 		{
127 			//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: getClassLoader: createClassLoader " + libDir + ", " + name  );
128 			c = createClassLoader (new File (libDir, name), getMasterClassLoader());
129 			childClassLoader.put(name, c);
130 		}
131 		//* DEBUG OFF */ System.out.println( "DEBUG ClassLoaderFactory: getClassLoader: return" );
132 		return c;
133 	}
134 }