Skip to content
Merged
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
124 changes: 97 additions & 27 deletions trellis-app/src/main/java/org/trellisldp/app/TrellisApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,30 @@
import static org.trellisldp.app.TrellisUtils.getRDFConnection;
import static org.trellisldp.app.TrellisUtils.getWebacConfiguration;

import com.google.common.cache.Cache;

import io.dropwizard.Application;
import io.dropwizard.auth.chained.ChainedAuthFilter;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

import javax.jms.JMSException;

import org.apache.jena.rdfconnection.RDFConnection;
import org.trellisldp.agent.SimpleAgent;
import org.trellisldp.api.AuditService;
import org.trellisldp.api.BinaryService;
import org.trellisldp.api.CacheService;
import org.trellisldp.api.EventService;
import org.trellisldp.api.IOService;
import org.trellisldp.api.IdentifierService;
import org.trellisldp.api.MementoService;
import org.trellisldp.api.NamespaceService;
import org.trellisldp.api.ResourceService;
import org.trellisldp.api.RuntimeTrellisException;
import org.trellisldp.app.config.TrellisConfiguration;
import org.trellisldp.app.health.RDFConnectionHealthCheck;
import org.trellisldp.audit.DefaultAuditService;
import org.trellisldp.file.FileBinaryService;
import org.trellisldp.file.FileMementoService;
import org.trellisldp.http.AgentAuthorizationFilter;
Expand All @@ -56,6 +64,28 @@
*/
public class TrellisApplication extends Application<TrellisConfiguration> {

private Environment environment;

private TrellisConfiguration config;

private EventService notificationService;

private IdentifierService idService;

private MementoService mementoService;

private ResourceService resourceService;

private NamespaceService namespaceService;

private BinaryService binaryService;

private CacheService<String, String> profileCache;

private IOService ioService;

private AuditService auditService;

/**
* The main entry point.
* @param args the argument list
Expand All @@ -75,40 +105,29 @@ public void initialize(final Bootstrap<TrellisConfiguration> bootstrap) {
// Not currently used
}

protected void init(final TrellisConfiguration config, final Environment environment) {
this.environment = environment;
this.config = config;
this.notificationService = buildNotificationService(environment);
this.idService = buildIdService();
this.mementoService = buildMementoService();
this.resourceService = buildResourceService(idService, mementoService, notificationService);
this.namespaceService = buildNamespaceService();
this.binaryService = buildBinaryService(idService);
this.profileCache = buildCacheService();
this.ioService = buildIoService(namespaceService, profileCache);
this.auditService = buildAuditService();
}

@Override
public void run(final TrellisConfiguration config,
final Environment environment) throws Exception {

final EventService notificationService = getNotificationService(config.getNotifications(), environment);

final RDFConnection rdfConnection = getRDFConnection(config);

final IdentifierService idService = new UUIDGenerator();

final MementoService mementoService = new FileMementoService(config.getMementos());

final TriplestoreResourceService resourceService = new TriplestoreResourceService(rdfConnection, idService,
mementoService, notificationService);

final NamespaceService namespaceService = new NamespacesJsonContext(config.getNamespaces());

final BinaryService binaryService = new FileBinaryService(idService, config.getBinaries(),
config.getBinaryHierarchyLevels(), config.getBinaryHierarchyLength());

// IO Service
final CacheService<String, String> profileCache = new TrellisCache<>(newBuilder()
.maximumSize(config.getJsonld().getCacheSize())
.expireAfterAccess(config.getJsonld().getCacheExpireHours(), HOURS).build());
final IOService ioService = new JenaIOService(namespaceService, profileCache,
TrellisUtils.getAssetConfiguration(config));

// Health checks
environment.healthChecks().register("rdfconnection", new RDFConnectionHealthCheck(rdfConnection));
init(config, environment);

getAuthFilters(config).ifPresent(filters -> environment.jersey().register(new ChainedAuthFilter<>(filters)));

// Resource matchers
environment.jersey().register(new LdpResource(resourceService, ioService, binaryService, resourceService,
environment.jersey().register(new LdpResource(resourceService, ioService, binaryService, auditService,
config.getBaseUrl()));

// Filters
Expand All @@ -127,4 +146,55 @@ public void run(final TrellisConfiguration config,
new CrossOriginResourceSharingFilter(cors.getAllowOrigin(), cors.getAllowMethods(),
cors.getAllowHeaders(), cors.getExposeHeaders(), cors.getAllowCredentials(), cors.getMaxAge())));
}

protected <T extends ResourceService> T buildResourceService(final IdentifierService idService,
final MementoService mementoService, final EventService notificationService) {
final RDFConnection rdfConnection = getRDFConnection(config);
// Health checks
environment.healthChecks().register("rdfconnection", new RDFConnectionHealthCheck(rdfConnection));
return (T) new TriplestoreResourceService(rdfConnection, idService, mementoService, notificationService);
}

protected AuditService buildAuditService() {
return resourceService != null && resourceService instanceof AuditService
? (AuditService) resourceService
: new DefaultAuditService();
}

protected TrellisCache<String, String> buildCacheService() {
final Long cacheSize = config.getJsonld().getCacheSize();
final Long hours = config.getJsonld().getCacheExpireHours();
final Cache<String, String> cache = newBuilder().maximumSize(cacheSize).expireAfterAccess(hours, HOURS).build();
return new TrellisCache<>(cache);
}

protected IOService buildIoService(final NamespaceService namespaceService,
final CacheService<String, String> profileCache) {
return new JenaIOService(namespaceService, profileCache, TrellisUtils.getAssetConfiguration(config));
}

protected BinaryService buildBinaryService(final IdentifierService idService) {
return new FileBinaryService(idService, config.getBinaries(), config.getBinaryHierarchyLevels(),
config.getBinaryHierarchyLength());
}

protected NamespaceService buildNamespaceService() {
return new NamespacesJsonContext(config.getNamespaces());
}

protected MementoService buildMementoService() {
return new FileMementoService(config.getMementos());
}

protected IdentifierService buildIdService() {
return new UUIDGenerator();
}

protected EventService buildNotificationService(final Environment environment) {
try {
return getNotificationService(config.getNotifications(), environment);
} catch (JMSException e) {
throw new RuntimeTrellisException(e);
}
}
}