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@Deprecated
039@XObject(value = "resource", order = { "@path", "@class" })
040public class ResourceBinding {
041
042    @XNode("@path")
043    public String path;
044
045    @XNode("@singleton")
046    public boolean singleton = 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                Path p = clazz.getAnnotation(Path.class);
073                if (p == null) {
074                    throw new NuxeoException("Invalid resource binding. Path not defined");
075                }
076                path = p.value();
077            } else {
078            }
079        }
080    }
081
082    public void reload(WebEngine engine) throws ClassNotFoundException {
083        clazz = null;
084        resolve(engine);
085    }
086
087    @Override
088    public int hashCode() {
089        return path.hashCode();
090    }
091
092    @Override
093    public boolean equals(Object obj) {
094        if (obj == this) {
095            return true;
096        }
097        if (obj instanceof ResourceBinding) {
098            ResourceBinding binding = (ResourceBinding) obj;
099            return binding.path.equals(path) && binding.clazz == clazz;
100        }
101        return false;
102    }
103
104    public static ResourceBinding fromAnnotation(Class<?> clazz) {
105        Path path = clazz.getAnnotation(Path.class);
106        ResourceBinding binding = null;
107        if (path != null) {
108            binding = new ResourceBinding();
109            binding.path = path.value();
110            binding.clazz = clazz;
111        }
112        return binding;
113    }
114
115    public String getPath() {
116        return path;
117    }
118
119    public Class<?> getClazz() {
120        return clazz;
121    }
122
123    public boolean isSingleton() {
124        return singleton;
125    }
126
127    @Override
128    public String toString() {
129        return path + " -> " + clazz;
130    }
131
132}