001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model.impl;
023
024import java.text.ParseException;
025import java.util.Arrays;
026import java.util.HashSet;
027
028import org.nuxeo.ecm.webengine.WebEngine;
029import org.nuxeo.ecm.webengine.WebException;
030import org.nuxeo.ecm.webengine.loader.ClassProxy;
031import org.nuxeo.ecm.webengine.model.AdapterType;
032import org.nuxeo.ecm.webengine.model.Resource;
033import org.nuxeo.ecm.webengine.model.ResourceType;
034import org.nuxeo.ecm.webengine.model.WebAdapter;
035import org.nuxeo.ecm.webengine.security.PermissionService;
036import org.nuxeo.runtime.annotations.AnnotationManager;
037
038/**
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class AdapterTypeImpl extends AbstractResourceType implements AdapterType {
042
043    // we are using arrays and not sets since the targetTypes and targetFacets have usually very small sizes
044    protected String targetType;
045
046    protected String[] targetFacets;
047
048    protected final String adapterName;
049
050    public AdapterTypeImpl(WebEngine engine, ModuleImpl module, ResourceTypeImpl superType, String name,
051            String adapterName, ClassProxy clazz, int visibility) {
052        super(engine, module, superType, name, clazz, visibility);
053        this.adapterName = adapterName;
054    }
055
056    public String getAdapterName() {
057        return adapterName;
058    }
059
060    public String getTargetType() {
061        return targetType;
062    }
063
064    public String[] getTargetFacets() {
065        return targetFacets;
066    }
067
068    public boolean acceptResource(Resource resource) {
069        if (acceptType(resource.getType())) {
070            if (targetFacets != null && targetFacets.length > 0) {
071                String[] facets = targetFacets; // make a local copy to avoid parallel type definition updates
072                for (String f : facets) {
073                    if (!resource.hasFacet(f)) {
074                        return false;
075                    }
076                }
077            }
078        }
079        return true;
080    }
081
082    public boolean acceptType(ResourceType type) {
083        if (targetType == null || targetType == ROOT_TYPE_NAME) {
084            return true;
085        }
086        return type.isDerivedFrom(targetType);
087    }
088
089    @Override
090    protected void loadAnnotations(AnnotationManager annoMgr) {
091        Class<?> c = clazz.get();
092        WebAdapter ws = c.getAnnotation(WebAdapter.class);
093        if (ws == null) {
094            return;
095        }
096        String g = ws.guard();
097        if (g != null && g.length() > 0) {
098            try {
099                guard = PermissionService.parse(g);
100            } catch (ParseException e) {
101                throw WebException.wrap("Failed to parse guard: " + g + " on WebObject " + c.getName(), e);
102            }
103        } else {
104            loadGuardFromAnnoation(c);
105        }
106        String[] facets = ws.facets();
107        if (facets != null && facets.length > 0) {
108            this.facets = new HashSet<String>(Arrays.asList(facets));
109        }
110        targetType = ws.targetType();
111        String[] targetFacets = ws.targetFacets();
112        if (targetFacets != null && targetFacets.length > 0) {
113            this.targetFacets = targetFacets;
114        }
115    }
116
117}