org.openrdf.repository.object.managers.helpers.RoleClassLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alibaba-composition-object Show documentation
Show all versions of alibaba-composition-object Show documentation
The Object Composition library merges multiple Java objects into a single multi-subject object.
/*
* Copyright (c) 2009, James Leigh All rights reserved.
* Copyright (c) 2011 Talis Inc., Some rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the openrdf.org nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package org.openrdf.repository.object.managers.helpers;
import org.openrdf.annotations.Iri;
import org.openrdf.annotations.Matching;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.repository.object.exceptions.ObjectStoreConfigException;
import org.openrdf.repository.object.managers.RoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
/**
* Loads the annotations, concepts and behaviours into memory.
*
* @author James Leigh
*
*/
public class RoleClassLoader {
private static final String CONCEPTS = "META-INF/org.openrdf.concepts";
private static final String BEHAVIOURS = "META-INF/org.openrdf.behaviours";
private static final String ANNOTATIONS = "META-INF/org.openrdf.annotations";
private final Logger logger = LoggerFactory.getLogger(DirectMapper.class);
private RoleMapper roleMapper;
public RoleClassLoader(RoleMapper roleMapper) {
this.roleMapper = roleMapper;
}
/**
* Loads and registers roles listed in resource.
*
* @throws ObjectStoreConfigException
*/
public void loadRoles(ClassLoader cl) throws ObjectStoreConfigException {
try {
ClassLoader first = RoleClassLoader.class.getClassLoader();
Set loaded;
loaded = load(new CheckForAnnotation(first), first, "annotations", ANNOTATIONS, true, new HashSet());
loaded = load(new CheckForAnnotation(cl), cl, "annotations", ANNOTATIONS, true, loaded);
loaded = load(new CheckForConcept(first), first, "concepts", CONCEPTS, true, new HashSet());
loaded = load(new CheckForConcept(cl), cl, "concepts", CONCEPTS, true, loaded);
loaded = load(new CheckForBehaviour(first), first, "behaviours", BEHAVIOURS, false, new HashSet());
loaded = load(new CheckForBehaviour(cl), cl, "behaviours", BEHAVIOURS, false, loaded);
} catch (ObjectStoreConfigException e) {
throw e;
} catch (Exception e) {
throw new ObjectStoreConfigException(e);
}
}
public void scan(URL jar, ClassLoader cl) throws ObjectStoreConfigException {
scan(jar, new CheckForAnnotation(cl), ANNOTATIONS, cl);
scan(jar, new CheckForConcept(cl), CONCEPTS, cl);
scan(jar, new CheckForBehaviour(cl), BEHAVIOURS, cl);
}
private void scan(URL url, CheckForConcept checker, String role, ClassLoader cl)
throws ObjectStoreConfigException {
try {
Scanner scanner = new Scanner(checker);
load(scanner.scan(url, checker.getName(), role), cl, false);
} catch (Exception e) {
throw new ObjectStoreConfigException(e);
}
}
private Set load(CheckForConcept checker, ClassLoader cl, String forType, String roles, boolean concept, Set exclude)
throws IOException, ClassNotFoundException, ObjectStoreConfigException {
if (cl == null)
return exclude;
Scanner scanner = new Scanner(checker, roles);
Enumeration resources = cl.getResources(roles);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
if (!exclude.contains(url)) {
exclude.add(url);
logger.debug("Reading roles from {}", url);
try {
Properties p = new Properties();
p.load(url.openStream());
if (p.isEmpty()) {
load(scanner.scan(url, forType, null), cl, concept);
} else {
load(p, cl, concept);
}
} catch (IOException e) {
String msg = e.getMessage() + " in: " + url;
throw new ObjectStoreConfigException(msg, e);
} catch (IllegalArgumentException e) {
String msg = e.getMessage() + " in: " + url;
throw new ObjectStoreConfigException(msg, e);
}
}
}
if(CONCEPTS.equals(roles)) {
List urls = new ArrayList<>();
try {
// Try the root directoy of the classLoader
URL resource = cl.getResource("");
if(resource != null) {
// get resource root
urls.add(resource);
// get parent diretory
URL parent = resource.getPath().endsWith("/") ? resource.toURI().resolve("..").toURL() : resource.toURI().resolve(".").toURL();
urls.add(parent);
} else {
// if the classloader didn't find the root, try the root directory of the current class
resource = RoleClassLoader.class.getResource("/");
if(resource != null) {
urls.add(resource);
// get parent diretory
URL parent = resource.getPath().endsWith("/") ? resource.toURI().resolve("..").toURL() : resource.toURI().resolve(".").toURL();
urls.add(parent);
}
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
// get codesource root
URL urlCodeSource = RoleClassLoader.class.getProtectionDomain().getCodeSource().getLocation();
urls.add(urlCodeSource);
scanner = new Scanner(checker);
for(URL url : urls) {
logger.debug("url " + url);
if (!exclude.contains(url)) {
exclude.add(url);
logger.debug("Reading extra concepts from {}", url);
try {
load(scanner.scan(url, forType, null), cl, concept);
} catch (IOException e) {
String msg = e.getMessage() + " in: " + url;
throw new ObjectStoreConfigException(msg, e);
} catch (IllegalArgumentException e) {
String msg = e.getMessage() + " in: " + url;
throw new ObjectStoreConfigException(msg, e);
}
}
}
}
return exclude;
}
private void load(List roles, ClassLoader cl, boolean concept)
throws IOException, ObjectStoreConfigException {
for (String role : roles) {
try {
Class> clazz = forName(role, true, cl);
recordRole(clazz, null, concept);
} catch (ClassNotFoundException exc) {
logger.error(exc.toString());
}
}
}
private void load(Properties p, ClassLoader cl, boolean concept)
throws ClassNotFoundException, IOException, ObjectStoreConfigException {
for (Map.Entry