Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration classes modifications for Dynamic API branch alignment #446

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.Comparator;
import java.util.List;

import javax.servlet.ServletContext;

import net.sf.jga.fn.UnaryFunctor;
import net.sf.jga.fn.adaptor.ChainUnary;
import net.sf.jga.fn.property.GetProperty;
Expand All @@ -16,12 +14,12 @@
/**
* Static methods to help create commonly used filters.
*/
public class VitroFilterUtils {
public class VitroFilterUtils {
/**
* Gets a filter that hides any property or resource that is restricted from
* public view.
*/
public static VitroFilters getPublicFilter(ServletContext ctx) {
public static VitroFilters getPublicFilter() {
return new FilterByDisplayPermission();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,7 @@ public class VClassGroupCache implements SearchIndexer.Listener {

private final RebuildGroupCacheThread _cacheRebuildThread;

/**
* Need a pointer to the context to get DAOs and models.
*/
private final ServletContext context;


private VClassGroupCache(ServletContext context) {
this.context = context;
this._groupList = null;

if (StartupStatus.getBean(context).isStartupAborted()) {
Expand Down Expand Up @@ -199,7 +192,7 @@ protected void requestStop() {
}

protected VClassGroupDao getVCGDao() {
return ModelAccess.on(context).getWebappDaoFactory().getVClassGroupDao();
return ModelAccess.getInstance().getWebappDaoFactory().getVClassGroupDao();
}

public void doSynchronousRebuild(){
Expand Down Expand Up @@ -263,11 +256,11 @@ public static VClassGroupsForRequest getVClassGroups(HttpServletRequest req) {
*/
protected static void rebuildCacheUsingSearch( VClassGroupCache cache ) throws SearchEngineException{
long start = System.currentTimeMillis();
WebappDaoFactory wdFactory = ModelAccess.on(cache.context).getWebappDaoFactory();
WebappDaoFactory wdFactory = ModelAccess.getInstance().getWebappDaoFactory();

SearchEngine searchEngine = ApplicationUtils.instance().getSearchEngine();

VitroFilters vFilters = VitroFilterUtils.getPublicFilter(cache.context);
VitroFilters vFilters = VitroFilterUtils.getPublicFilter();
VClassGroupDao vcgDao = new WebappDaoFactoryFiltering(wdFactory, vFilters).getVClassGroupDao();

List<VClassGroup> groups = vcgDao.getPublicGroupsWithVClasses(ORDER_BY_DISPLAYRANK,
Expand Down Expand Up @@ -488,7 +481,7 @@ protected void checkAndDoUpdate(Statement stmt) {
} else if(VitroVocabulary.DISPLAY_RANK.equals(stmt.getPredicate().getURI())){
requestCacheUpdate();
} else if (RDFS.label.equals(stmt.getPredicate())){
OntModel jenaOntModel = ModelAccess.on(context).getOntModelSelector().getTBoxModel();
OntModel jenaOntModel = ModelAccess.getInstance().getOntModelSelector().getTBoxModel();
if( isClassNameChange(stmt, jenaOntModel) ) {
requestCacheUpdate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -93,7 +92,6 @@ public class SearchIndexerImpl implements SearchIndexer {
private Integer threadPoolSize;
private WorkerThreadPool pool;

private ServletContext ctx;
private List<SearchIndexExcluder> excluders;
private List<DocumentModifier> modifiers;
private Set<IndexingUriFinder> uriFinders;
Expand Down Expand Up @@ -134,7 +132,6 @@ public void startup(Application application, ComponentStartupStatus ss) {
"startup() called after shutdown().");
}
try {
this.ctx = application.getServletContext();
this.wadf = getFilteredWebappDaoFactory();
loadConfiguration();

Expand All @@ -150,14 +147,14 @@ public void startup(Application application, ComponentStartupStatus ss) {

/** With a filtered factory, only public data goes into the search index. */
private WebappDaoFactory getFilteredWebappDaoFactory() {
WebappDaoFactory rawWadf = ModelAccess.on(ctx).getWebappDaoFactory();
VitroFilters vf = VitroFilterUtils.getPublicFilter(ctx);
WebappDaoFactory rawWadf = ModelAccess.getInstance().getWebappDaoFactory();
VitroFilters vf = VitroFilterUtils.getPublicFilter();
return new WebappDaoFactoryFiltering(rawWadf, vf);
}

private void loadConfiguration() throws ConfigurationBeanLoaderException {
ConfigurationBeanLoader beanLoader = new ConfigurationBeanLoader(
ModelAccess.on(ctx).getOntModel(DISPLAY), ctx);
ModelAccess.getInstance().getOntModel(DISPLAY));
uriFinders = beanLoader.loadAll(IndexingUriFinder.class);

excluders = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,58 @@

package edu.cornell.mannlib.vitro.webapp.utils.configuration;

import static org.apache.jena.rdf.model.ResourceFactory.createResource;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;

import edu.cornell.mannlib.vitro.webapp.utils.jena.criticalsection.LockableModel;
import edu.cornell.mannlib.vitro.webapp.utils.jena.criticalsection.LockedModel;

/**
* Load one or more Configuration beans from a specified model.
*/
public class ConfigurationBeanLoader {

private static final String FIND_URI_QUERY = "" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"PREFIX dynapi: <https://vivoweb.org/ontology/vitro-dynamic-api#>\n" +
"SELECT ?uri\n" +
"WHERE {\n" +
" {\n" +
" ?uri a ?type .\n" +
" ?type dynapi:implementedBy ?java .\n" +
" }\n" +
" UNION\n" +
" {\n" +
" ?uri a ?java .\n" +
" }\n" +
"} ";


private static final Log log = LogFactory.getLog(ConfigurationBeanLoader.class);


private static final String JAVA_URI_PREFIX = "java:";

Map<String, Object> instancesMap = new HashMap<String,Object>();


// ----------------------------------------------------------------------
// utility methods
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -80,72 +107,63 @@ public static boolean isMatchingJavaUri(String uri1, String uri2) {
/** Must not be null. */
private final LockableModel locking;

/**
* May be null, but the loader will be unable to satisfy instances of
* ContextModelUser.
*/
private final ServletContext ctx;

/**
* May be null, but the loader will be unable to satisfy instances of
* RequestModelUser.
*/
private final HttpServletRequest req;

public ConfigurationBeanLoader(Model model) {
this(new LockableModel(model), null, null);
this(new LockableModel(model), null);
}

public ConfigurationBeanLoader(LockableModel locking) {
this(locking, null, null);
}

public ConfigurationBeanLoader(Model model, ServletContext ctx) {
this(new LockableModel(model), ctx, null);
}

public ConfigurationBeanLoader(LockableModel locking, ServletContext ctx) {
this(locking, ctx, null);
this(locking, null);
}

public ConfigurationBeanLoader(Model model, HttpServletRequest req) {
this(new LockableModel(model), req);
}

public ConfigurationBeanLoader(LockableModel locking,
HttpServletRequest req) {
this(locking,
(req == null) ? null : req.getSession().getServletContext(),
req);
}

private ConfigurationBeanLoader(LockableModel locking, ServletContext ctx,
HttpServletRequest req) {
this.locking = Objects.requireNonNull(locking,
"locking may not be null.");
this.req = req;
this.ctx = ctx;
}
public ConfigurationBeanLoader(LockableModel locking, HttpServletRequest req) {
this.locking = Objects.requireNonNull(locking, "locking may not be null.");
this.req = req;
}

/**
* Load the instance with this URI, if it is assignable to this class.
*/
public <T> T loadInstance(String uri, Class<T> resultClass)
public <T> T loadInstance(String uri, Class<T> resultClass) throws ConfigurationBeanLoaderException {
instancesMap.clear();
T result = loadSubordinateInstance(uri, resultClass);
instancesMap.clear();
return result;
}

protected <T> T loadSubordinateInstance(String uri, Class<T> resultClass)
throws ConfigurationBeanLoaderException {
if (uri == null) {
throw new NullPointerException("uri may not be null.");
}
if (resultClass == null) {
throw new NullPointerException("resultClass may not be null.");
}

if (instancesMap.containsKey(uri)) {
try {
T t = (T) instancesMap.get(uri);
return t;
} catch (ClassCastException e) {
throw new ConfigurationBeanLoaderException(uri, e);
}
}
try {
ConfigurationRdf<T> parsedRdf = ConfigurationRdfParser
.parse(locking, uri, resultClass);
WrappedInstance<T> wrapper = InstanceWrapper
.wrap(parsedRdf.getConcreteClass());
wrapper.satisfyInterfaces(ctx, req);
wrapper.satisfyInterfaces(req);
wrapper.checkCardinality(parsedRdf.getPropertyStatements());
instancesMap.put(uri, wrapper.getInstance());
wrapper.setProperties(this, parsedRdf.getPropertyStatements());
wrapper.validate();
return wrapper.getInstance();
Expand All @@ -161,22 +179,48 @@ public <T> T loadInstance(String uri, Class<T> resultClass)
public <T> Set<T> loadAll(Class<T> resultClass)
throws ConfigurationBeanLoaderException {
Set<String> uris = new HashSet<>();
try (LockedModel m = locking.read()) {
for (String typeUri : toPossibleJavaUris(resultClass)) {
List<Resource> resources = m.listResourcesWithProperty(RDF.type,
createResource(typeUri)).toList();
for (Resource r : resources) {
if (r.isURIResource()) {
uris.add(r.getURI());
}
}
}
}

findUris(resultClass, uris);
Set<T> instances = new HashSet<>();
for (String uri : uris) {
instances.add(loadInstance(uri, resultClass));
}
return instances;
}

/**
* Find all of the resources with the specified class, and instantiate them.
*/
public <T> Map<String, T> loadEach(Class<T> resultClass){
Set<String> uris = new HashSet<>();
findUris(resultClass, uris);
HashMap<String,T> instances = new HashMap<>();
for (String uri : uris) {
try {
instances.put(uri, loadInstance(uri, resultClass));
} catch (ConfigurationBeanLoaderException e) {
log.error(e, e);
}
}
return instances;
}

private <T> void findUris(Class<T> resultClass, Set<String> uris) {
try (LockedModel m = locking.read()) {
for (String typeUri : toPossibleJavaUris(resultClass)) {
ParameterizedSparqlString pss = new ParameterizedSparqlString(FIND_URI_QUERY);
pss.setIri("java", typeUri);
Query query = QueryFactory.create(pss.toString());
QueryExecution qexec = QueryExecutionFactory.create(query, m);
try {
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
QuerySolution solution = results.nextSolution();
uris.add(solution.getResource("uri").getURI());
}
} finally {
qexec.close();
}
}
}
}
}
Loading
Loading