001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.webengine.model.impl; 021 022import java.text.ParseException; 023import java.util.Arrays; 024import java.util.HashSet; 025 026import org.nuxeo.ecm.webengine.WebEngine; 027import org.nuxeo.ecm.webengine.WebException; 028import org.nuxeo.ecm.webengine.loader.ClassProxy; 029import org.nuxeo.ecm.webengine.model.AdapterType; 030import org.nuxeo.ecm.webengine.model.Resource; 031import org.nuxeo.ecm.webengine.model.ResourceType; 032import org.nuxeo.ecm.webengine.model.WebAdapter; 033import org.nuxeo.ecm.webengine.security.PermissionService; 034import org.nuxeo.runtime.annotations.AnnotationManager; 035 036/** 037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 038 */ 039public class AdapterTypeImpl extends AbstractResourceType implements AdapterType { 040 041 // we are using arrays and not sets since the targetTypes and targetFacets have usually very small sizes 042 protected String targetType; 043 044 protected String[] targetFacets; 045 046 protected final String adapterName; 047 048 public AdapterTypeImpl(WebEngine engine, ModuleImpl module, ResourceTypeImpl superType, String name, 049 String adapterName, ClassProxy clazz, int visibility) { 050 super(engine, module, superType, name, clazz, visibility); 051 this.adapterName = adapterName; 052 } 053 054 public String getAdapterName() { 055 return adapterName; 056 } 057 058 public String getTargetType() { 059 return targetType; 060 } 061 062 public String[] getTargetFacets() { 063 return targetFacets; 064 } 065 066 public boolean acceptResource(Resource resource) { 067 if (acceptType(resource.getType())) { 068 if (targetFacets != null && targetFacets.length > 0) { 069 String[] facets = targetFacets; // make a local copy to avoid parallel type definition updates 070 for (String f : facets) { 071 if (!resource.hasFacet(f)) { 072 return false; 073 } 074 } 075 } 076 } 077 return true; 078 } 079 080 public boolean acceptType(ResourceType type) { 081 if (targetType == null || targetType == ROOT_TYPE_NAME) { 082 return true; 083 } 084 return type.isDerivedFrom(targetType); 085 } 086 087 @Override 088 protected void loadAnnotations(AnnotationManager annoMgr) { 089 Class<?> c = clazz.get(); 090 WebAdapter ws = c.getAnnotation(WebAdapter.class); 091 if (ws == null) { 092 return; 093 } 094 String g = ws.guard(); 095 if (g != null && g.length() > 0) { 096 try { 097 guard = PermissionService.parse(g); 098 } catch (ParseException e) { 099 throw WebException.wrap("Failed to parse guard: " + g + " on WebObject " + c.getName(), e); 100 } 101 } else { 102 loadGuardFromAnnoation(c); 103 } 104 String[] facets = ws.facets(); 105 if (facets != null && facets.length > 0) { 106 this.facets = new HashSet<String>(Arrays.asList(facets)); 107 } 108 targetType = ws.targetType(); 109 String[] targetFacets = ws.targetFacets(); 110 if (targetFacets != null && targetFacets.length > 0) { 111 this.targetFacets = targetFacets; 112 } 113 } 114 115}