public static ResourceId createId(Name<?> name, ResourceTemplate template) { checkNotNull(name,"Resource name cannot be null"); checkNotNull(template,"Template cannot be null"); return createId(name,template.id()); }
private String toString(ResourceId resourceId) { Class<?> handler=resolveHandler(resourceId); if(handler!=null) { return String.format("named '%s' with handler class '%s'",resourceId.name(),handler.getName()); } else { return String.format("named '%s' of unknown template '%s'",resourceId.name(),resourceId.templateId()); } }
public static ResourceId createId(Name<?> name, String templateId) { checkNotNull(name,"Resource name cannot be null"); checkNotNull(templateId,"Template identifier cannot be null"); return new ResourceId(name,templateId); }
private void assemble() { if(this.cacheAvailable) { return; } this.cacheAvailable=true; if(this.nameValue==null || this.templateId==null) { return; } this.cachedId= ResourceId. createId( Key.fromBase64(this.nameValue), this.templateId); // Self healing... this.nameType=nameTypeOf(this.cachedId.name()); }
@Test public void testResolverSnapshotResolver$resolveSnapshot$engineFailure() throws Exception { final URI base = URI.create("http://www.examples.org/base/"); final ResourceId resourceId = ResourceId.createId(NamingScheme.getDefault().name("name"),"template"); new Expectations() {{ snapshot.name();result=resourceId.name(); snapshot.templateId();result=resourceId.templateId(); }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { throw new ApplicationEngineException("FAILURE"); } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); try { assertThat(resolver.resolve(snapshot),nullValue()); fail("Should not resolve snapshot if engine fails"); } catch (SnapshotResolutionException e) { assertThat(e.getMessage(),equalTo("Could not resolve endpoint for resource '"+resourceId+"'")); } }
<T extends DelegatedResourceSnapshot> T newTransient(Class<? extends T> snapshotClass, ResourceId resourceId, DelegatedResourceSnapshot parent) { checkNotNull(snapshotClass,"Snapshot class cannot be null"); DelegatedResourceSnapshot tmp=newTransient(resourceId,parent); checkState(!snapshotClass.isInstance(tmp),"Cannot create snapshot of type '"+snapshotClass.getCanonicalName()+"' from template '"+resourceId.templateId()+"'"); return snapshotClass.cast(tmp); }
@Override public final Name<?> name() { return resourceId().name(); }
private void addPublication(final BiMap<ResourceId, String> rootResourceMap, RootResource candidateResource) throws ApplicationConfigurationException { candidateResource.validate(); ResourceId resourceId = candidateResource.resourceId(); String path = candidateResource.path(); String prevPath=rootResourceMap.get(resourceId); if(prevPath!=null && !prevPath.equals(path)) { throw new ApplicationConfigurationException(String.format("Resource %s is already published (%s)",toString(resourceId),prevPath)); } ResourceId prevResource=rootResourceMap.inverse().get(path); if(prevResource!=null && !prevResource.equals(resourceId)) { throw new ApplicationConfigurationException(String.format("Path '%s' is already used by resource %s",path,toString(prevResource))); } rootResourceMap.put(resourceId, path); }
@Test public void testResolverSnapshotResolver$resolveSnapshot$resourceGone() throws Exception { final URI base = URI.create("http://www.examples.org/base/"); final ResourceId resourceId = ResourceId.createId(NamingScheme.getDefault().name("name"),"template"); new Expectations() {{ snapshot.name();result=resourceId.name(); snapshot.templateId();result=resourceId.templateId(); engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.activeContext();result=context; context.resolveResource(resourceId);result=endpoint; endpoint.deleted();result=new Date(); }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); assertThat(resolver.resolve(snapshot),nullValue()); }
@Test public void testResolverSnapshotResolver$resolveEndpoint$happyPath() throws Exception { final URI base = URI.create("http://www.examples.org/base/"); final String path="resource/path/"; final ResourceId resourceId = ResourceId.createId(NamingScheme.getDefault().name("name"),"template"); new Expectations() {{ engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.endpointManagementService();result=endpointManagementService; endpointManagementService.resolveEndpoint(path);result=endpoint; endpoint.deleted();result=null; engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.activeContext();result=context; context.resolveResource(endpoint);result=resource; resource.id();result=resourceId; engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.activeContext();result=context; context.resourceTemplate(resource);result=template; template.handlerClass();result=ResourceHandler.class; session.find(ResourceSnapshot.class,resourceId.name(),ResourceHandler.class);result=snapshot; }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); assertThat(resolver.resolve(base.resolve(path)),sameInstance(snapshot)); }
private <S extends ResourceSnapshot> S find( Class<? extends S> snapshotClass, ResourceId resourceId) { ResourceTemplate template=loadTemplate(resourceId.templateId()); checkArgument(template!=null,"Unknown template '%s' ",resourceId.templateId()); checkArgument(Snapshots.areCompatible(snapshotClass,template),"Cannot wrap an object managed by '%s' with an snapshot of type '%s'",resourceId.templateId(),snapshotClass.getCanonicalName()); DelegatedResourceSnapshot resource = resolveResource(resourceId, template); return snapshotClass.cast(resource); }
public ResourceSnapshot attach(final WriteSession writeSession, final Resource resource, final Class<? extends ResourceHandler> handlerClass) { return writeSession.find(ResourceSnapshot.class, resource.id().name(), handlerClass); }
private void publish(RootResource rootResource, Date creationDate) throws ApplicationConfigurationException { ResourceId resourceId = rootResource.resourceId(); String path = rootResource.path(); Resource prevResource = this.resourceRepository.resourceById(resourceId,Resource.class); Endpoint prevEndpoint = this.endpointRepository.endpointOfPath(path); if(prevEndpoint!=null && !prevEndpoint.resourceId().equals(resourceId)) { throw new ApplicationConfigurationException(String.format("Resource %s cannot be published at '%s' as that path is already in use by a resource %s",toString(resourceId),path,toString(prevEndpoint.resourceId()))); } if(prevEndpoint==null) { if(prevResource!=null) { throw new ApplicationConfigurationException(String.format("Resource %s cannot be published at '%s' as it is already published at '%s'",toString(resourceId),path,this.endpointRepository.endpointOfResource(resourceId).path())); } else { Resource resource=this.modelFactory.createResource(rootResource.template(),rootResource.name()); this.resourceRepository.add(resource); Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,creationDate,EntityTag.createStrong(path)); this.endpointRepository.add(endpoint); } } }
ResourceId resourceId() { return ResourceId.createId(this.resourceName, this.template.id()); }
@Test public void testResolverSnapshotResolver$resolveSnapshot$resourceNotfound() throws Exception { final URI base = URI.create("http://www.examples.org/base/"); final ResourceId resourceId = ResourceId.createId(NamingScheme.getDefault().name("name"),"template"); new Expectations() {{ snapshot.name();result=resourceId.name(); snapshot.templateId();result=resourceId.templateId(); engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.activeContext();result=context; context.resolveResource(resourceId);result=null; }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); assertThat(resolver.resolve(snapshot),nullValue()); }
public Value resourceSurrogate(PublicResource member) { ResourceId resourceId = ((DefaultPublicResource)member).id(); ManagedIndividualId surrogateId = ManagedIndividualId.createId(resourceId.name(), resourceId.templateId()); return this.dataSet.individualOfId(surrogateId); }
private ResourceTemplate getTemplate(ResourceId resourceId) { checkNotNull(resourceId,"Resource identifier cannot be null"); ResourceTemplate template = this.session.loadTemplate(resourceId.templateId()); checkArgument(template!=null,"Could not find template for resource '%s'",resourceId); return template; }
private void logAction(final Stage stage, final Action action, final org.ldp4j.application.kernel.resource.Resource resource) { LOGGER.debug(String.format(">> %s %s(%s)",stage.prefix,action,resource.id().name())); }
boolean removeMember(ResourceSnapshot member) { if(member==null) { return false; } boolean result = this.members.containsValue(member); if(result) { ResourceId memberId=ResourceId.createId(member.name(),member.templateId()); this.members.remove(memberId); this.newMembers.remove(member); } return result; }
@Test public void testResolverSnapshotResolver$resolveSnapshot$happyPath() throws Exception { final URI base = URI.create("http://www.examples.org/base/"); final String path="resource/path/"; final ResourceId resourceId = ResourceId.createId(NamingScheme.getDefault().name("name"),"template"); new Expectations() {{ snapshot.name();result=resourceId.name(); snapshot.templateId();result=resourceId.templateId(); engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.activeContext();result=context; context.resolveResource(resourceId);result=endpoint; endpoint.deleted();result=null; endpoint.path();result=path; }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); assertThat(resolver.resolve(snapshot),equalTo(base.resolve(path))); }