1 package net.sf.jldapbeans.lang;
2
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.util.Enumeration;
8
9 import org.apache.commons.digester.Digester;
10 import org.apache.commons.digester.Rule;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.xml.sax.Attributes;
14 import org.xml.sax.SAXException;
15
16 /***
17 * Holds a singleton instance of the bean repositories loaded from the
18 * classpath.
19 * <p>
20 * <tt>BeanRepositoryFactory</tt> looks for any resource named <tt>jldapbeans-repository.xml</tt>
21 * inside the <tt>META-INF</tt> folder using the application thread's <tt>ClassLoader</tt>.
22 *
23 * @author <a href="mailto:alonsoft@users.sf.net">A. Alonso Domínguez</a>
24 * @version 1.0 $ Revision 12-nov-2005 :: alonso $
25 */
26 public final class BeanRepositoryFactory {
27 private static final Log log = LogFactory.getLog(BeanRepositoryFactory.class);
28
29 private static class LdapBeanRule extends Rule {
30
31 public void begin(String namespace, String name, Attributes attrs) {
32 BeanRepositoryImpl repository = (BeanRepositoryImpl) digester.peek();
33 String objectClass = attrs.getValue("objectClass");
34 String className = attrs.getValue("classname");
35 repository.setObjectClass(objectClass, className);
36 }
37
38 }
39
40 /*** The singleton <tt>BeanRepository</tt> instance. */
41 private static BeanRepositoryImpl m_singletonRepository;
42
43 private static final String REPOSITORY_FILE = "META-INF/jldapbeans-repository.xml";
44
45 private static BeanRepositoryImpl loadXMLResource(InputStream resource)
46 throws IOException, SAXException {
47 Digester digester = new Digester();
48 digester.addRule("jldapbeans-repository/ldap-bean", new LdapBeanRule());
49 digester.push(new BeanRepositoryImpl());
50 return (BeanRepositoryImpl) digester.parse(resource);
51 }
52
53 private static BeanRepositoryImpl createBeanRepository(ClassLoader cl) {
54 BeanRepositoryImpl result = new BeanRepositoryImpl();
55 log.debug("Starting loading LDAP Beans repositories");
56 try {
57 Enumeration<URL> e = cl.getResources(REPOSITORY_FILE);
58 if(!e.hasMoreElements())
59 log.warn("No LDAP Beans repositories found");
60 while(e.hasMoreElements()) {
61 URL url = e.nextElement();
62 String filename = url.toString().substring("file:".length());
63
64 log.trace("Loading repository file [" + filename + "]");
65 InputStream in = new FileInputStream(filename);
66 try {
67 BeanRepositoryImpl internal = loadXMLResource(in);
68 result.include(internal);
69 }
70 catch(SAXException se) {
71 log.error("Error parsing repository file [" + url + "]", se);
72 }
73 }
74 log.info("LDAP Beans repositories successfully loaded.");
75 }
76 catch(Exception e) {
77 log.fatal("Unexpected error loading LDAP Beans repositories", e);
78 }
79 return result;
80 }
81
82 /***
83 * Obtains the @link net.sf.jldapbeans.lang.BeanRepository for the application's
84 * main thread.
85 *
86 * @return The singleton <tt>BeanRepository</tt> instance or a new one if it has not been
87 * already loaded.
88 */
89 public static BeanRepository getRepository() {
90 ClassLoader cl = Thread.currentThread().getContextClassLoader();
91 if(cl == null) cl = ClassLoader.getSystemClassLoader();
92
93 if(m_singletonRepository == null)
94 m_singletonRepository = createBeanRepository(cl);
95 return m_singletonRepository;
96 }
97
98 /*** Private constructor for prevent instantiation */
99 private BeanRepositoryFactory() { }
100
101 }