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
038import com.sun.jersey.server.spi.component.ResourceComponentConstructor;
039
040/**
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043public class AdapterTypeImpl extends AbstractResourceType implements AdapterType {
044
045    // we are using arrays and not sets since the targetTypes and targetFacets have usually very small sizes
046    protected String targetType;
047
048    protected String[] targetFacets;
049
050    protected final String adapterName;
051
052    public AdapterTypeImpl(WebEngine engine, ModuleImpl module, ResourceTypeImpl superType, String name,
053            String adapterName, ClassProxy clazz, ResourceComponentConstructor constructor, int visibility) {
054        super(engine, module, superType, name, clazz, constructor, visibility);
055        this.adapterName = adapterName;
056    }
057
058    @Override
059    public String getAdapterName() {
060        return adapterName;
061    }
062
063    @Override
064    public String getTargetType() {
065        return targetType;
066    }
067
068    @Override
069    public String[] getTargetFacets() {
070        return targetFacets;
071    }
072
073    @Override
074    public boolean acceptResource(Resource resource) {
075        if (acceptType(resource.getType())) {
076            if (targetFacets != null && targetFacets.length > 0) {
077                String[] facets = targetFacets; // make a local copy to avoid parallel type definition updates
078                for (String f : facets) {
079                    if (!resource.hasFacet(f)) {
080                        return false;
081                    }
082                }
083            }
084        }
085        return true;
086    }
087
088    public boolean acceptType(ResourceType type) {
089        if (targetType == null || targetType == ROOT_TYPE_NAME) {
090            return true;
091        }
092        return type.isDerivedFrom(targetType);
093    }
094
095    @Override
096    protected void loadAnnotations(AnnotationManager annoMgr) {
097        Class<?> c = clazz.get();
098        WebAdapter ws = c.getAnnotation(WebAdapter.class);
099        if (ws == null) {
100            return;
101        }
102        String g = ws.guard();
103        if (g != null && g.length() > 0) {
104            try {
105                guard = PermissionService.parse(g);
106            } catch (ParseException e) {
107                throw WebException.wrap("Failed to parse guard: " + g + " on WebObject " + c.getName(), e);
108            }
109        } else {
110            loadGuardFromAnnoation(c);
111        }
112        String[] facets = ws.facets();
113        if (facets != null && facets.length > 0) {
114            this.facets = new HashSet<String>(Arrays.asList(facets));
115        }
116        targetType = ws.targetType();
117        String[] targetFacets = ws.targetFacets();
118        if (targetFacets != null && targetFacets.length > 0) {
119            this.targetFacets = targetFacets;
120        }
121    }
122
123}