KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > loading > RepositoryClassLoader


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.loading;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.security.CodeSource JavaDoc;
31 import java.security.PermissionCollection JavaDoc;
32 import java.security.Policy JavaDoc;
33 import java.security.ProtectionDomain JavaDoc;
34 import java.security.cert.Certificate JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.Vector JavaDoc;
38 import java.util.Collections JavaDoc;
39 import java.util.Set JavaDoc;
40
41 import javax.management.MalformedObjectNameException JavaDoc;
42 import javax.management.ObjectName JavaDoc;
43
44 import org.jboss.logging.Logger;
45 import org.jboss.util.loading.Translator;
46 import org.jboss.util.collection.SoftSet;
47
48 import EDU.oswego.cs.dl.util.concurrent.ReentrantLock;
49 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
50
51 /**
52  * A RepositoryClassLoader.
53  *
54  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
55  * @version $Revision: 44794 $
56  */

57 public abstract class RepositoryClassLoader extends URLClassLoader JavaDoc
58 {
59    // Constants -----------------------------------------------------
60

61    /** The log */
62    private static final Logger log = Logger.getLogger(RepositoryClassLoader.class);
63
64    /** The value returned by {@link #getURLs}. */
65    private static final URL JavaDoc[] EMPTY_URL_ARRAY = {};
66
67    // Attributes -----------------------------------------------------
68

69    /** Reference to the repository. */
70    protected LoaderRepository repository = null;
71    /** The location where unregister is called from */
72    protected Exception JavaDoc unregisterTrace;
73
74    /** The relative order in which this class loader was added to the respository */
75    private int addedOrder;
76    
77    /** The parent classloader */
78    protected ClassLoader JavaDoc parent = null;
79    
80    /** Names of classes which have resulted in CNFEs in loadClassLocally */
81    private Set JavaDoc classBlackList = Collections.synchronizedSet(new SoftSet());
82    /** Names of resources that were not found in loadResourceLocally */
83    private Set JavaDoc resourceBlackList = Collections.synchronizedSet(new HashSet JavaDoc());
84    /** A HashMap<String, URL> for resource found in loadResourceLocally */
85    private ConcurrentReaderHashMap resourceCache = new ConcurrentReaderHashMap();
86    
87    /** Lock */
88    protected ReentrantLock loadLock = new ReentrantLock();
89
90    /** A debugging variable used to track the recursive depth of loadClass() */
91    protected int loadClassDepth;
92
93    // Static --------------------------------------------------------
94

95    // Constructors --------------------------------------------------
96

97    /**
98     * Create a new LoaderRepositoryClassLoader
99     *
100     * @param urls the urls
101     * @param parent the parent classloader
102     */

103    protected RepositoryClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parent)
104    {
105       super(urls, parent);
106       this.parent = parent;
107       // Check the blacklist mode
108
String JavaDoc mode = ClassToStringAction.getProperty("org.jboss.mx.loading.blacklistMode", null);
109       if( mode == null || mode.equalsIgnoreCase("HashSet") )
110       {
111          classBlackList = Collections.synchronizedSet(new HashSet JavaDoc());
112          resourceBlackList = Collections.synchronizedSet(new HashSet JavaDoc());
113       }
114       else if( mode.equalsIgnoreCase("SoftSet") )
115       {
116          classBlackList = Collections.synchronizedSet(new SoftSet());
117          resourceBlackList = Collections.synchronizedSet(new SoftSet());
118       }
119    }
120    
121    // Public --------------------------------------------------------
122

123    /**
124     * Get the ObjectName
125     *
126     * @return the object name
127     */

128    public abstract ObjectName JavaDoc getObjectName() throws MalformedObjectNameException JavaDoc;
129    
130    /**
131     * Get the loader repository for this classloader
132     */

133    public LoaderRepository getLoaderRepository()
134    {
135       return repository;
136    }
137
138    /**
139     * Set the loader repository
140     *
141     * @param repository the repository
142     */

143    public void setRepository(LoaderRepository repository)
144    {
145       log.debug("setRepository, repository="+repository+", cl=" + this);
146       this.repository = repository;
147    }
148
149    /**
150     * Get the order this classloader was added to the repository
151     *
152     * @return the order
153     */

154    public int getAddedOrder()
155    {
156       return addedOrder;
157    }
158
159    /**
160     * Set the order this classloader was added to the repository
161     *
162     * @param addedOrder the added order
163     */

164    public void setAddedOrder(int addedOrder)
165    {
166       this.addedOrder = addedOrder;
167    }
168
169    /**
170     * Called to attempt to load a class from the set of URLs associated with this classloader.
171     */

172    public Class JavaDoc loadClassLocally(String JavaDoc name, boolean resolve)
173       throws ClassNotFoundException JavaDoc
174    {
175       boolean trace = log.isTraceEnabled();
176       if( trace )
177          log.trace("loadClassLocally, " + this + " name=" + name);
178       Class JavaDoc result = null;
179       try
180       {
181          if (isClassBlackListed(name))
182          {
183             if( trace )
184                log.trace("Class in blacklist, name="+name);
185             throw new ClassNotFoundException JavaDoc("Class Not Found(blacklist): " + name);
186          }
187
188          try
189          {
190             result = super.loadClass(name, resolve);
191             return result;
192          }
193          catch (ClassNotFoundException JavaDoc cnfe)
194          {
195             addToClassBlackList(name);
196             // If this is an array class, use Class.forName to resolve it
197
if( name.charAt(0) == '[' )
198             {
199                result = Class.forName(name, true, this);
200                removeFromClassBlackList(name);
201                return result;
202             }
203             if( trace )
204                log.trace("CFNE: Adding to blacklist: "+name);
205             throw cnfe;
206          }
207       }
208       finally
209       {
210          if (trace)
211          {
212             if (result != null)
213                log.trace("loadClassLocally, " + this + " name=" + name + " class=" + result + " cl=" + result.getClassLoader());
214             else
215                log.trace("loadClassLocally, " + this + " name=" + name + " not found");
216          }
217       }
218    }
219
220    /**
221    * Provides the same functionality as {@link java.net.URLClassLoader#getResource}.
222    */

223    public URL JavaDoc getResourceLocally(String JavaDoc name)
224    {
225       URL JavaDoc resURL = (URL JavaDoc) resourceCache.get(name);
226       if (resURL != null)
227          return resURL;
228       if (isResourceBlackListed(name))
229          return null;
230       resURL = super.getResource(name);
231       if( log.isTraceEnabled() == true )
232          log.trace("getResourceLocally("+this+"), name="+name+", resURL:"+resURL);
233       if (resURL == null)
234          addToResourceBlackList(name);
235       else
236          resourceCache.put(name, resURL);
237       return resURL;
238    }
239
240    /**
241     * Get the URL associated with the UCL.
242     *
243     * @return the url
244     */

245    public URL JavaDoc getURL()
246    {
247       URL JavaDoc[] urls = super.getURLs();
248       if (urls.length > 0)
249          return urls[0];
250       else
251          return null;
252    }
253    
254    public void unregister()
255    {
256       log.debug("Unregistering cl=" + this);
257       if (repository != null)
258          repository.removeClassLoader(this);
259       clearBlacklists();
260       resourceCache.clear();
261       repository = null;
262       this.unregisterTrace = new Exception JavaDoc();
263    }
264
265    /**
266     * This method simply invokes the super.getURLs() method to access the
267     * list of URLs that make up the RepositoryClassLoader classpath.
268     *
269     * @return the urls that make up the classpath
270     */

271    public URL JavaDoc[] getClasspath()
272    {
273       return super.getURLs();
274    }
275
276    /**
277     * Return all library URLs associated with this RepositoryClassLoader
278     *
279     * <p>Do not remove this method without running the WebIntegrationTestSuite
280     */

281    public URL JavaDoc[] getAllURLs()
282    {
283       return repository.getURLs();
284    }
285
286    /**
287     * Black list a class
288     *
289     * @param name the name of the class
290     */

291    public void addToClassBlackList(String JavaDoc name)
292    {
293       classBlackList.add(name);
294    }
295
296    /**
297     * Remove class from black list
298     *
299     * @param name the name of the class
300     */

301    public void removeFromClassBlackList(String JavaDoc name)
302    {
303       classBlackList.remove(name);
304    }
305    
306    /**
307     * Is the class black listed?
308     *
309     * @param name the name of the class
310     * @return true when the class is black listed, false otherwise
311     */

312    public boolean isClassBlackListed(String JavaDoc name)
313    {
314       return classBlackList.contains(name);
315    }
316     
317    /**
318     * Clear any class black list.
319     */

320    public void clearClassBlackList()
321    {
322       classBlackList.clear();
323    }
324
325    /**
326     * Black list a resource
327     *
328     * @param name the name of the resource
329     */

330    public void addToResourceBlackList(String JavaDoc name)
331    {
332       resourceBlackList.add(name);
333    }
334
335    /**
336     * Remove resource from black list
337     *
338     * @param name the name of the resource
339     */

340    public void removeFromResourceBlackList(String JavaDoc name)
341    {
342       resourceBlackList.remove(name);
343    }
344    
345    /**
346     * Is the resource black listed?
347     *
348     * @param name the name of the resource
349     * @return true when the resource is black listed, false otherwise
350     */

351    public boolean isResourceBlackListed(String JavaDoc name)
352    {
353       return resourceBlackList.contains(name);
354    }
355    
356    /**
357     * Clear any resource blacklist.
358     */

359    public void clearResourceBlackList()
360    {
361       resourceBlackList.clear();
362    }
363
364    /**
365     * Clear all blacklists
366     */

367    public void clearBlacklists()
368    {
369       clearClassBlackList();
370       clearResourceBlackList();
371    }
372
373
374    // URLClassLoader overrides --------------------------------------
375

376    /** The only caller of this method should be the VM initiated
377    * loadClassInternal() method. This method attempts to acquire the
378    * UnifiedLoaderRepository2 lock and then asks the repository to
379    * load the class.
380    *
381    * <p>Forwards request to {@link LoaderRepository}.
382    */

383    public Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
384       throws ClassNotFoundException JavaDoc
385    {
386       boolean trace = log.isTraceEnabled();
387       if (trace)
388          log.trace("loadClass " + this + " name=" + name+", loadClassDepth="+loadClassDepth);
389       Class JavaDoc clazz = null;
390       try
391       {
392          if (repository != null)
393          {
394             clazz = repository.getCachedClass(name);
395             if (clazz != null)
396             {
397                if( log.isTraceEnabled() )
398                {
399                   StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("Loaded class from cache, ");
400                   ClassToStringAction.toString(clazz, buffer);
401                   log.trace(buffer.toString());
402                }
403                return clazz;
404             }
405          }
406          clazz = loadClassImpl(name, resolve, Integer.MAX_VALUE);
407          return clazz;
408       }
409       finally
410       {
411          if (trace)
412          {
413             if (clazz != null)
414                log.trace("loadClass " + this + " name=" + name + " class=" + clazz + " cl=" + clazz.getClassLoader());
415             else
416                log.trace("loadClass " + this + " name=" + name + " not found");
417          }
418       }
419    }
420
421    /** The only caller of this method should be the VM initiated
422    * loadClassInternal() method. This method attempts to acquire the
423    * UnifiedLoaderRepository2 lock and then asks the repository to
424    * load the class.
425    *
426    * <p>Forwards request to {@link LoaderRepository}.
427    */

428    public Class JavaDoc loadClassBefore(String JavaDoc name)
429       throws ClassNotFoundException JavaDoc
430    {
431       boolean trace = log.isTraceEnabled();
432       if (trace)
433          log.trace("loadClassBefore " + this + " name=" + name);
434       Class JavaDoc clazz = null;
435       try
436       {
437          clazz = loadClassImpl(name, false, addedOrder);
438          return clazz;
439       }
440       finally
441       {
442          if (trace)
443          {
444             if (clazz != null)
445                log.trace("loadClassBefore " + this + " name=" + name + " class=" + clazz + " cl=" + clazz.getClassLoader());
446             else
447                log.trace("loadClassBefore " + this + " name=" + name + " not found");
448          }
449       }
450    }
451
452    public abstract Class JavaDoc loadClassImpl(String JavaDoc name, boolean resolve, int stopAt)
453       throws ClassNotFoundException JavaDoc;
454
455    /**
456    * Attempts to load the resource from its URL and if not found
457    * forwards to the request to {@link LoaderRepository}.
458    */

459    public URL JavaDoc getResource(String JavaDoc name)
460    {
461       if (repository != null)
462          return repository.getResource(name, this);
463       return null;
464    }
465
466    /** Find all resource URLs for the given name. This overrides the
467     * URLClassLoader version to look for resources in the repository.
468     *
469     * @param name the name of the resource
470     * @return Enumeration<URL>
471     * @throws java.io.IOException
472     */

473    public Enumeration JavaDoc findResources(String JavaDoc name) throws IOException JavaDoc
474    {
475       Vector JavaDoc resURLs = new Vector JavaDoc();
476       if( repository == null )
477       {
478          String JavaDoc msg = "Invalid use of destroyed classloader, UCL destroyed at:";
479          IOException JavaDoc e = new IOException JavaDoc(msg);
480          e.initCause(this.unregisterTrace);
481          throw e;
482       }
483       repository.getResources(name, this, resURLs);
484       return resURLs.elements();
485    }
486
487    /**
488    * Provides the same functionality as {@link java.net.URLClassLoader#findResources}.
489    */

490    public Enumeration JavaDoc findResourcesLocally(String JavaDoc name) throws IOException JavaDoc
491    {
492       return super.findResources(name);
493    }
494
495     /** Called by loadClassLocally to find the requested class within this
496      * class loaders class path.
497      *
498      * @param name the name of the class
499      * @return the resulting class
500      * @exception ClassNotFoundException if the class could not be found
501      */

502    protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc
503    {
504       boolean trace = log.isTraceEnabled();
505       if( trace )
506          log.trace("findClass, name="+name);
507       if (isClassBlackListed(name))
508       {
509          if( trace )
510             log.trace("Class in blacklist, name="+name);
511          throw new ClassNotFoundException JavaDoc("Class Not Found(blacklist): " + name);
512       }
513
514       Translator translator = repository.getTranslator();
515       if (translator != null)
516       {
517          // Obtain the transformed class bytecode
518
try
519          {
520             // Obtain the raw bytecode from the classpath
521
URL JavaDoc classUrl = getClassURL(name);
522             byte[] rawcode = loadByteCode(classUrl);
523             URL JavaDoc codeSourceUrl = getCodeSourceURL(name, classUrl);
524             ProtectionDomain JavaDoc pd = getProtectionDomain(codeSourceUrl);
525             byte[] bytecode = translator.transform(this, name, null, pd, rawcode);
526             // If there was no transform use the raw bytecode
527
if( bytecode == null )
528                bytecode = rawcode;
529             // Define the class package and instance
530
definePackage(name);
531             return defineClass(name, bytecode, 0, bytecode.length, pd);
532          }
533          catch(ClassNotFoundException JavaDoc e)
534          {
535             throw e;
536          }
537          catch (Throwable JavaDoc ex)
538          {
539             throw new ClassNotFoundException JavaDoc(name, ex);
540          }
541       }
542
543       Class JavaDoc clazz = null;
544       try
545       {
546          clazz = findClassLocally(name);
547       }
548       catch(ClassNotFoundException JavaDoc e)
549       {
550          if( trace )
551             log.trace("CFNE: Adding to blacklist: "+name);
552          addToClassBlackList(name);
553          throw e;
554       }
555       return clazz;
556    }
557
558    /**
559     * Find the class
560     *
561     * @param name the name of the class
562     * @return the class
563     */

564    protected Class JavaDoc findClassLocally(String JavaDoc name) throws ClassNotFoundException JavaDoc
565    {
566       return super.findClass(name);
567    }
568    
569    /**
570     * Define the package for the class if not already done
571     *
572     * @todo this properly
573     * @param className the class name
574     */

575    protected void definePackage(String JavaDoc className)
576    {
577       int i = className.lastIndexOf('.');
578       if (i == -1)
579          return;
580
581       try
582       {
583          definePackage(className.substring(0, i), null, null, null, null, null, null, null);
584       }
585       catch (IllegalArgumentException JavaDoc alreadyDone)
586       {
587       }
588    }
589
590    /** Append the given url to the URLs used for class and resource loading
591     * @param url the URL to load from
592     */

593    public void addURL(URL JavaDoc url)
594    {
595       if( url == null )
596          throw new IllegalArgumentException JavaDoc("url cannot be null");
597
598       if( repository.addClassLoaderURL(this, url) == true )
599       {
600          log.debug("Added url: "+url+", to ucl: "+this);
601          // Strip any query parameters
602
String JavaDoc query = url.getQuery();
603          if( query != null )
604          {
605             String JavaDoc ext = url.toExternalForm();
606             String JavaDoc ext2 = ext.substring(0, ext.length() - query.length() - 1);
607             try
608             {
609                url = new URL JavaDoc (ext2);
610             }
611             catch(MalformedURLException JavaDoc e)
612             {
613                log.warn("Failed to strip query from: "+url, e);
614             }
615          }
616          super.addURL(url);
617          clearBlacklists();
618       }
619       else if( log.isTraceEnabled() )
620       {
621          log.trace("Ignoring duplicate url: "+url+", for ucl: "+this);
622       }
623    }
624
625    /**
626    * Return an empty URL array to force the RMI marshalling subsystem to
627    * use the <tt>java.server.codebase</tt> property as the annotated codebase.
628    *
629    * <p>Do not remove this method without discussing it on the dev list.
630    *
631    * @return Empty URL[]
632    */

633    public URL JavaDoc[] getURLs()
634    {
635       return EMPTY_URL_ARRAY;
636    }
637
638    public Package JavaDoc getPackage(String JavaDoc name)
639    {
640       return super.getPackage(name);
641    }
642    
643    public Package JavaDoc[] getPackages()
644    {
645       return super.getPackages();
646    }
647    
648    // Object overrides ----------------------------------------------
649

650    /**
651     * This is here to document that this must delegate to the
652     * super implementation to perform identity based equality. Using
653     * URL based equality caused conflicts with the Class.forName(String,
654     * boolean, ClassLoader).
655     */

656    public final boolean equals(Object JavaDoc other)
657    {
658       return super.equals(other);
659    }
660
661    /**
662     * This is here to document that this must delegate to the
663     * super implementation to perform identity based hashing. Using
664     * URL based hashing caused conflicts with the Class.forName(String,
665     * boolean, ClassLoader).
666     */

667    public final int hashCode()
668    {
669       return super.hashCode();
670    }
671
672    /**
673    * Returns a string representation.
674    */

675    public String JavaDoc toString()
676    {
677       return super.toString() + "{ url=" + getURL() + " }";
678    }
679    
680    // Protected -----------------------------------------------------
681

682    /** Attempt to acquire the class loading lock. This lock must be acquired
683     * before a thread enters the class loading task loop in loadClass. This
684     * method maintains any interrupted state of the calling thread.
685     *@see #loadClass(String, boolean)
686     */

687    protected boolean attempt(long waitMS)
688    {
689       boolean acquired = false;
690       boolean trace = log.isTraceEnabled();
691       // Save and clear the interrupted state of the incoming thread
692
boolean threadWasInterrupted = Thread.interrupted();
693       try
694       {
695          acquired = loadLock.attempt(waitMS);
696       }
697       catch(InterruptedException JavaDoc e)
698       {
699       }
700       finally
701       {
702          // Restore the interrupted state of the thread
703
if( threadWasInterrupted )
704             Thread.currentThread().interrupt();
705       }
706       if( trace )
707          log.trace("attempt("+loadLock.holds()+") was: "+acquired+" for :"+this);
708       return acquired;
709    }
710    
711    /** Acquire the class loading lock. This lock must be acquired
712     * before a thread enters the class loading task loop in loadClass.
713     *@see #loadClass(String, boolean)
714     */

715    protected void acquire()
716    {
717       // Save and clear the interrupted state of the incoming thread
718
boolean threadWasInterrupted = Thread.interrupted();
719       try
720       {
721          loadLock.acquire();
722       }
723       catch(InterruptedException JavaDoc e)
724       {
725       }
726       finally
727       {
728          // Restore the interrupted state of the thread
729
if( threadWasInterrupted )
730             Thread.currentThread().interrupt();
731       }
732       if( log.isTraceEnabled() )
733          log.trace("acquired("+loadLock.holds()+") for :"+this);
734    }
735    /** Release the class loading lock previous acquired through the acquire
736     * method.
737     */

738    protected void release()
739    {
740       if( log.isTraceEnabled() )
741          log.trace("release("+loadLock.holds()+") for :"+this);
742       loadLock.release();
743       if( log.isTraceEnabled() )
744          log.trace("released, holds: "+loadLock.holds());
745    }
746
747    /** Obtain the bytecode for the indicated class from this class loaders
748     * classpath.
749     *
750     * @param classname
751     * @return the bytecode array if found
752     * @exception ClassNotFoundException - if the class resource could not
753     * be found
754     */

755    protected byte[] loadByteCode(String JavaDoc classname)
756       throws ClassNotFoundException JavaDoc, IOException JavaDoc
757    {
758       byte[] bytecode = null;
759       URL JavaDoc classURL = getClassURL(classname);
760
761       // Load the class bytecode
762
InputStream JavaDoc is = null;
763       try
764       {
765          is = classURL.openStream();
766          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
767          byte[] tmp = new byte[1024];
768          int read = 0;
769          while( (read = is.read(tmp)) > 0 )
770          {
771             baos.write(tmp, 0, read);
772          }
773          bytecode = baos.toByteArray();
774       }
775       finally
776       {
777          if( is != null )
778             is.close();
779       }
780
781       return bytecode;
782    }
783
784    /** Obtain the bytecode for the indicated class from this class loaders
785     * classpath.
786     *
787     * @param classURL
788     * @return the bytecode array if found
789     * @exception ClassNotFoundException - if the class resource could not
790     * be found
791     */

792    protected byte[] loadByteCode(URL JavaDoc classURL)
793       throws ClassNotFoundException JavaDoc, IOException JavaDoc
794    {
795       byte[] bytecode = null;
796       // Load the class bytecode
797
InputStream JavaDoc is = null;
798       try
799       {
800          is = classURL.openStream();
801          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
802          byte[] tmp = new byte[1024];
803          int read = 0;
804          while( (read = is.read(tmp)) > 0 )
805          {
806             baos.write(tmp, 0, read);
807          }
808          bytecode = baos.toByteArray();
809       }
810       finally
811       {
812          if( is != null )
813             is.close();
814       }
815
816       return bytecode;
817    }
818    
819    /**
820     * Determine the protection domain. If we are a copy of the original
821     * deployment, use the original url as the codebase.
822     * @return the protection domain
823     * @todo certificates and principles?
824     */

825    protected ProtectionDomain JavaDoc getProtectionDomain(URL JavaDoc codesourceUrl)
826    {
827       Certificate JavaDoc certs[] = null;
828       CodeSource JavaDoc cs = new CodeSource JavaDoc(codesourceUrl, certs);
829       PermissionCollection JavaDoc permissions = Policy.getPolicy().getPermissions(cs);
830       if (log.isTraceEnabled())
831          log.trace("getProtectionDomain, url=" + codesourceUrl +
832                    " codeSource=" + cs + " permissions=" + permissions);
833       return new ProtectionDomain JavaDoc(cs, permissions);
834    }
835
836    // Package Private -----------------------------------------------
837

838    // Private -------------------------------------------------------
839

840    private URL JavaDoc getCodeSourceURL(String JavaDoc classname, URL JavaDoc classURL) throws java.net.MalformedURLException JavaDoc
841    {
842       String JavaDoc classRsrcName = classname.replace('.', '/') + ".class";
843       String JavaDoc urlAsString = classURL.toString();
844       int idx = urlAsString.indexOf(classRsrcName);
845       if (idx == -1) return classURL;
846       urlAsString = urlAsString.substring(0, idx);
847       return new URL JavaDoc(urlAsString);
848    }
849
850    private URL JavaDoc getClassURL(String JavaDoc classname) throws ClassNotFoundException JavaDoc
851    {
852       String JavaDoc classRsrcName = classname.replace('.', '/') + ".class";
853       URL JavaDoc classURL = this.getResourceLocally(classRsrcName);
854       if( classURL == null )
855       {
856          String JavaDoc msg = "Failed to find: "+classname+" as resource: "+classRsrcName;
857          throw new ClassNotFoundException JavaDoc(msg);
858       }
859       return classURL;
860    }
861
862    // Inner classes -------------------------------------------------
863
}
864
Popular Tags