private boolean isRegistered(Class<? extends ResourceHandler> handlerClass) { return this.templatesByHandler.containsKey(HandlerId.createId(handlerClass)); }
static HandlerId createId(Class<? extends ResourceHandler> handlerClass) { checkNotNull(handlerClass,"Resource handler class cannot be null"); return new HandlerId( handlerClass.getCanonicalName(), System.identityHashCode(handlerClass), ContainerHandler.class.isAssignableFrom(handlerClass)); }
@Test public void testHasCustomString() throws Exception { HandlerId id = resourceHandlerId(); assertThat(id.toString(),not(equalTo(HandlerId.class.getName()+"@"+System.identityHashCode(id)))); }
@Test public void testEqualsForNullObjects() throws Exception { HandlerId id1 = resourceHandlerId(); assertThat(id1.equals(null),not(equalTo(true))); }
private ResourceTemplate retrieve(Class<? extends ResourceHandler> handlerClass) { return this.templatesByHandler.get(HandlerId.createId(handlerClass)); }
private HandlerId containerHandlerId() { return HandlerId.createId(PersonContainerHandler.class); }
private HandlerId resourceHandlerId() { return HandlerId.createId(PersonHandler.class); }
<T extends ResourceHandler> T getHandler(Class<? extends T> handlerClass, ResourceTemplate template) { checkNotNull(handlerClass,"Handler class cannot be null"); checkNotNull(template,"Template cannot be null"); checkArgument(this.library.contains(template),"Unknown template '"+template+"'"); checkArgument(handlerClass.isAssignableFrom(template.handlerClass())); return handlerClass.cast(this.handlers.get(HandlerId.createId(template.handlerClass()))); }
@Override public void visitResourceTemplate(ResourceTemplate template) { Class<? extends ResourceHandler> handlerClass = template.handlerClass(); ResourceHandler handler=this.handlers.getInstance(handlerClass); if(handler==null) { try { handler=handlerClass.newInstance(); } catch (Exception e) { String message=String.format("Could not instantiate resource handler from template '%s' (%s)",template.id(),handlerClass.getCanonicalName()); LOGGER.warn(message); throw new ResourceHandlerInstantiationException(message,handlerClass,e); } } this.builder.put(HandlerId.createId(handlerClass), handler); }
@Override public void register(Class<? extends ResourceHandler> handlerClass, ResourceTemplate template) { ResourceTemplate previousTemplate = retrieve(handlerClass); if(previousTemplate!=null) { if(template==previousTemplate) { return; } throw new TemplateCreationException(template.id(), String.format("Cannot register two templates with the same handler (new: %s, registered: %s)",template,previousTemplate)); } previousTemplate=this.templatesById.get(template.id()); if(previousTemplate!=null) { throw new TemplateCreationException(template.id(), String.format("Cannot register two templates with the same identifier (new: %s, registered: %s)",template,previousTemplate)); } this.templatesByHandler.put(HandlerId.createId(handlerClass), template); this.templatesById.put(template.id(), template); }