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