View Javadoc

1   package org.openrdf.elmo.rolemapper;
2   
3   import java.io.IOException;
4   import java.net.URL;
5   import java.util.Enumeration;
6   import java.util.HashSet;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.Properties;
10  import java.util.Set;
11  
12  import org.openrdf.elmo.RoleMapper;
13  import org.openrdf.elmo.exceptions.ElmoConversionException;
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  public class RoleClassLoader<URI> {
18  	private final Logger logger = LoggerFactory
19  			.getLogger(DirectMapper.class);
20  
21  	private RoleMapper<URI> roleMapper;
22  
23  	private ClassLoader cl;
24  
25  	public void setRoleMapper(RoleMapper<URI> roleMapper) {
26  		this.roleMapper = roleMapper;
27  	}
28  
29  	public void setClassLoader(ClassLoader cl) {
30  		this.cl = cl;
31  	}
32  
33  	/**
34  	 * Loads and registers roles from META-INF/org.openrdf.elmo.roles files.
35  	 * 
36  	 * @param cl
37  	 *            ClassLoader to use when loading files.
38  	 */
39  	public void loadClasses(String roles) {
40  		try {
41  			ClassLoader elmo = RoleClassLoader.class.getClassLoader();
42  			load(cl, roles, load(elmo, roles, new HashSet<URL>()));
43  		} catch (Exception e) {
44  			throw new ElmoConversionException(e);
45  		}
46  	}
47  
48  	private Set<URL> load(ClassLoader cl, String roles, Set<URL> exclude) throws IOException,
49  			ClassNotFoundException {
50  		if (cl == null)
51  			return exclude;
52  		Scanner scanner = new Scanner(cl, roles);
53  		Enumeration<URL> resources = cl.getResources(roles);
54  		while (resources.hasMoreElements()) {
55  			URL url = resources.nextElement();
56  			if (!exclude.contains(url)) {
57  				exclude.add(url);
58  				logger.debug("Reading roles from {}", url);
59  				try {
60  					Properties p = new Properties();
61  					p.load(url.openStream());
62  					if (p.isEmpty()) {
63  						load(scanner.scan(url), cl);
64  					} else {
65  						load(p, cl);
66  					}
67  				} catch (IOException e) {
68  					String msg = e.getMessage() + " in: " + url;
69  					IOException ioe = new IOException(msg);
70  					ioe.initCause(e);
71  					throw ioe;
72  				}
73  			}
74  		}
75  		return exclude;
76  	}
77  
78  	private void load(List<String> roles, ClassLoader cl)
79  			throws ClassNotFoundException, IOException {
80  		for (String role : roles) {
81  			Class<?> clazz = Class.forName(role, true, cl);
82  			recordRole(clazz, null);
83  		}
84  	}
85  
86  	private void load(Properties p, ClassLoader cl)
87  			throws ClassNotFoundException, IOException {
88  		for (Map.Entry<Object, Object> e : p.entrySet()) {
89  			String role = (String) e.getKey();
90  			String types = (String) e.getValue();
91  			Class<?> clazz = Class.forName(role, true, cl);
92  			for (String rdf : types.split("\\s+")) {
93  				recordRole(clazz, rdf);
94  			}
95  		}
96  	}
97  
98  	private void recordRole(Class<?> clazz, String uri) {
99  		if (uri == null || uri.length() == 0) {
100 			roleMapper.recordRole(clazz);
101 		} else {
102 			roleMapper.recordRole(clazz, uri);
103 		}
104 	}
105 }