001/*
002 * (C) Copyright 2006-2011 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 */
019package org.nuxeo.ecm.webengine.jaxrs.servlet.config;
020
021import java.net.URL;
022
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XObject;
025import org.osgi.framework.Bundle;
026
027/**
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030@XObject("resources")
031public class ResourcesDescriptor {
032
033    @XNode("@servlet")
034    protected String servlet;
035
036    @XNode("@path")
037    protected String path;
038
039    private Bundle bundle;
040
041    private String id;
042
043    private ResourceResolver resolver;
044
045    void setBundle(Bundle bundle) {
046        this.bundle = bundle;
047        if (path.startsWith("file:")) {
048            resolver = new FileResourceResolver(path.substring("file:".length()));
049        } else {
050            resolver = new BundleResourceResolver(bundle, path);
051        }
052        id = bundle.getSymbolicName() + ":" + servlet + ":" + path;
053    }
054
055    public void setResolver(ResourceResolver resolver) {
056        this.resolver = resolver;
057    }
058
059    public Bundle getBundle() {
060        return bundle;
061    }
062
063    public String getPath() {
064        return path;
065    }
066
067    public String getServlet() {
068        return servlet;
069    }
070
071    public String getId() {
072        return id;
073    }
074
075    public URL getResource(String name) {
076        return resolver.getResource(name);
077    }
078
079    @Override
080    public boolean equals(Object obj) {
081        if (obj == this) {
082            return true;
083        }
084        if (obj instanceof ResourcesDescriptor) {
085            return id.equals(((ResourcesDescriptor) obj).id);
086        }
087        return false;
088    }
089
090    @Override
091    public String toString() {
092        return id;
093    }
094}