KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > resource > ResourceResolver


1 package org.exoplatform.services.xml.querying.impl.xtas.resource;
2
3 import java.net.MalformedURLException JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import org.exoplatform.services.xml.querying.ConfigException;
7 import org.exoplatform.services.xml.querying.impl.xtas.XMLConfig;
8 import org.exoplatform.services.xml.querying.impl.xtas.resource.plugins.LocalFile;
9
10
11 /**
12  * Singleton for finding and instantiate
13  * XML resource by its ID (fileName, URL etc)
14  * @version $Id: ResourceResolver.java 566 2005-01-25 12:50:49Z kravchuk $
15  */

16 public class ResourceResolver {
17
18     private static ResourceResolver resolver = null;
19     private Collection JavaDoc descriptors;
20
21     /** @link dependency
22      * @stereotype instantiate*/

23     /*# Resource lnkResource; */
24
25     protected ResourceResolver() throws ConfigException
26     {
27        descriptors = XMLConfig.getInstance().getResources();
28     }
29
30     public static ResourceResolver getInstance() throws ConfigException
31     {
32         if (resolver == null) {
33             resolver = new ResourceResolver();
34         }
35         return resolver;
36     }
37
38     /**
39      * Resolves resource by ID
40      */

41     public Resource getResource(String JavaDoc resourceId) throws MalformedURLException JavaDoc, ConfigException
42     {
43
44         Iterator JavaDoc iter = descriptors.iterator();
45         Resource res = null;
46
47         while(iter.hasNext()) {
48             ResourceDescriptor descr = (ResourceDescriptor) iter.next();
49             String JavaDoc prefix = descr.getPrefix();
50
51             if(prefix != null && prefix.length() <= resourceId.length()){
52
53               if ( prefix.equals( resourceId.substring( 0, prefix.length() ) ) ) {
54                   try {
55                      res = (Resource)Class.forName(descr.getClassname()).newInstance();
56                   } catch (Exception JavaDoc e) {
57                      throw new ConfigException("XTAS Resource Config Exception - can not instantiate: "+e.getMessage());
58                   }
59                   res.init( resourceId );
60                   return res;
61               }
62             }
63         }
64 /*
65         res = new LocalFile();
66         res.init(resourceId);
67         return res;
68 */

69         return new LocalFile( resourceId );
70     }
71 }
72
Popular Tags