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;
023
024import javax.ws.rs.Path;
025
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.ecm.core.api.NuxeoException;
029
030/**
031 * Defines a JAX-RS root resource binding. This is an extension to JAX-RS to be able to declare root resource binding
032 * dynamically without using {@link Path} annotations on classes.
033 *
034 * @deprecated resources are deprecated - you should use a jax-rs application to declare more resources.
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 * @see Path
037 */
038@XObject(value = "resource", order = { "@path", "@class" })
039public class ResourceBinding {
040
041    @XNode("@path")
042    public String path;
043
044    @XNode("@singleton")
045    public boolean singleton = false;
046
047    private boolean hasUserPath = false;
048
049    /**
050     * Use this to specify the resource class.
051     */
052    @XNode("@class")
053    public String className;
054
055    public Class<?> clazz;
056
057    public ResourceBinding() {
058    }
059
060    public ResourceBinding(String path, Class<?> clazz, boolean singleton) {
061        this.path = path;
062        this.clazz = clazz;
063        this.singleton = singleton;
064    }
065
066    /**
067     * Must be called before using this binding.
068     */
069    public void resolve(WebEngine engine) throws ClassNotFoundException {
070        if (clazz == null) {
071            clazz = engine.loadClass(className);
072            if (path == null) {
073                hasUserPath = false;
074                Path p = clazz.getAnnotation(Path.class);
075                if (p == null) {
076                    throw new NuxeoException("Invalid resource binding. Path not defined");
077                }
078                path = p.value();
079            } else {
080                hasUserPath = true;
081            }
082        }
083    }
084
085    public void reload(WebEngine engine) throws ClassNotFoundException {
086        clazz = null;
087        resolve(engine);
088    }
089
090    @Override
091    public int hashCode() {
092        return path.hashCode();
093    }
094
095    @Override
096    public boolean equals(Object obj) {
097        if (obj == this) {
098            return true;
099        }
100        if (obj instanceof ResourceBinding) {
101            ResourceBinding binding = (ResourceBinding) obj;
102            return binding.path.equals(path) && binding.clazz == clazz;
103        }
104        return false;
105    }
106
107    public static ResourceBinding fromAnnotation(Class<?> clazz) {
108        Path path = clazz.getAnnotation(Path.class);
109        ResourceBinding binding = null;
110        if (path != null) {
111            binding = new ResourceBinding();
112            binding.path = path.value();
113            binding.clazz = clazz;
114        }
115        return binding;
116    }
117
118    public String getPath() {
119        return path;
120    }
121
122    public Class<?> getClazz() {
123        return clazz;
124    }
125
126    public boolean isSingleton() {
127        return singleton;
128    }
129
130    @Override
131    public String toString() {
132        return path + " -> " + clazz;
133    }
134
135}