001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.webengine.jaxrs.servlet.config;
013
014import java.util.ArrayList;
015import java.util.HashMap;
016import java.util.List;
017
018import javax.servlet.http.HttpServlet;
019
020import org.nuxeo.common.xmap.annotation.XNode;
021import org.nuxeo.common.xmap.annotation.XNodeList;
022import org.nuxeo.common.xmap.annotation.XNodeMap;
023import org.nuxeo.common.xmap.annotation.XObject;
024import org.nuxeo.ecm.webengine.jaxrs.BundleNotFoundException;
025import org.nuxeo.ecm.webengine.jaxrs.Utils;
026import org.nuxeo.ecm.webengine.jaxrs.Utils.ClassRef;
027import org.nuxeo.ecm.webengine.jaxrs.servlet.FilterSet;
028import org.osgi.framework.Bundle;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033@XObject("servlet")
034public class ServletDescriptor {
035
036    @XNode("@name")
037    protected String name;
038
039    @XNode("@class")
040    protected String classRef;
041
042    /**
043     * The absolute path of the servlet (including the context path)
044     */
045    @XNode("@path")
046    protected String path;
047
048    @XNode("resources")
049    protected String resources;
050
051    /**
052     * Must use hashtable since it extends Dictionary
053     */
054    @XNodeMap(value = "properties/property", key = "@name", type = HashMap.class, componentType = String.class, trim = true, nullByDefault = false)
055    protected HashMap<String, String> initParams;
056
057    @XNodeList(value = "filters", type = ArrayList.class, componentType = FilterSetDescriptor.class, nullByDefault = false)
058    protected ArrayList<FilterSetDescriptor> filters;
059
060    @XNode("listeners")
061    protected ListenerSetDescriptor listeners;
062
063    /**
064     * The bundle that deployed this extension
065     */
066    protected Bundle bundle;
067
068    private ClassRef ref;
069
070    public ServletDescriptor() {
071    }
072
073    public void setBundle(Bundle bundle) {
074        this.bundle = bundle;
075    }
076
077    public Bundle getBundle() {
078        return bundle;
079    }
080
081    public String getPath() {
082        return path;
083    }
084
085    public HashMap<String, String> getInitParams() {
086        return initParams;
087    }
088
089    public ClassRef getClassRef() throws ClassNotFoundException, BundleNotFoundException {
090        if (ref == null) {
091            ref = Utils.getClassRef(classRef, bundle);
092        }
093        return ref;
094    }
095
096    public HttpServlet getServlet() throws ReflectiveOperationException, BundleNotFoundException {
097        return (HttpServlet) getClassRef().get().newInstance();
098    }
099
100    public String getName() {
101        return name;
102    }
103
104    public ListenerSetDescriptor getListenerSet() {
105        return listeners;
106    }
107
108    public String getResources() {
109        return resources;
110    }
111
112    public FilterSet[] getFilters() {
113        List<FilterSetDescriptor> list = ServletRegistry.getInstance().getFiltersFor(name);
114        int len1 = list.size();
115        int len2 = filters.size();
116        FilterSet[] filterSets = new FilterSet[len1 + len2];
117        for (int i = 0; i < len1; i++) {
118            filterSets[i] = list.get(i).getFilterSet();
119        }
120        for (int i = 0; i < len2; i++) {
121            filterSets[i + len1] = filters.get(i).getFilterSet();
122        }
123        return filterSets;
124    }
125
126    @Override
127    public String toString() {
128        StringBuilder buf = new StringBuilder();
129        buf.append(name).append(" { ").append(path).append(" [").append(classRef).append("]");
130        buf.append("\n  Properties: ").append(initParams);
131        if (!filters.isEmpty()) {
132            buf.append("\n  Filters:\n    ");
133            for (FilterSetDescriptor fsd : filters) {
134                buf.append(fsd.toString());
135                buf.append("\n    ");
136            }
137        }
138        if (listeners != null) {
139            buf.append("\n  Listeners: ").append(listeners.toString());
140        }
141        buf.append("\n}\n");
142        return buf.toString();
143    }
144
145}