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.util.ArrayList; 022import java.util.HashMap; 023import java.util.List; 024 025import javax.servlet.http.HttpServlet; 026 027import org.nuxeo.common.xmap.annotation.XNode; 028import org.nuxeo.common.xmap.annotation.XNodeList; 029import org.nuxeo.common.xmap.annotation.XNodeMap; 030import org.nuxeo.common.xmap.annotation.XObject; 031import org.nuxeo.ecm.webengine.jaxrs.BundleNotFoundException; 032import org.nuxeo.ecm.webengine.jaxrs.Utils; 033import org.nuxeo.ecm.webengine.jaxrs.Utils.ClassRef; 034import org.nuxeo.ecm.webengine.jaxrs.servlet.FilterSet; 035import org.osgi.framework.Bundle; 036 037/** 038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 039 */ 040@XObject("servlet") 041public class ServletDescriptor { 042 043 @XNode("@name") 044 protected String name; 045 046 @XNode("@class") 047 protected String classRef; 048 049 /** 050 * The absolute path of the servlet (including the context path) 051 */ 052 @XNode("@path") 053 protected String path; 054 055 @XNode("resources") 056 protected String resources; 057 058 /** 059 * Must use hashtable since it extends Dictionary 060 */ 061 @XNodeMap(value = "properties/property", key = "@name", type = HashMap.class, componentType = String.class, trim = true, nullByDefault = false) 062 protected HashMap<String, String> initParams; 063 064 @XNodeList(value = "filters", type = ArrayList.class, componentType = FilterSetDescriptor.class, nullByDefault = false) 065 protected ArrayList<FilterSetDescriptor> filters; 066 067 @XNode("listeners") 068 protected ListenerSetDescriptor listeners; 069 070 /** 071 * The bundle that deployed this extension 072 */ 073 protected Bundle bundle; 074 075 private ClassRef ref; 076 077 public ServletDescriptor() { 078 } 079 080 public void setBundle(Bundle bundle) { 081 this.bundle = bundle; 082 } 083 084 public Bundle getBundle() { 085 return bundle; 086 } 087 088 public String getPath() { 089 return path; 090 } 091 092 public HashMap<String, String> getInitParams() { 093 return initParams; 094 } 095 096 public ClassRef getClassRef() throws ClassNotFoundException, BundleNotFoundException { 097 if (ref == null) { 098 ref = Utils.getClassRef(classRef, bundle); 099 } 100 return ref; 101 } 102 103 public HttpServlet getServlet() throws ReflectiveOperationException, BundleNotFoundException { 104 return (HttpServlet) getClassRef().get().newInstance(); 105 } 106 107 public String getName() { 108 return name; 109 } 110 111 public ListenerSetDescriptor getListenerSet() { 112 return listeners; 113 } 114 115 public String getResources() { 116 return resources; 117 } 118 119 public FilterSet[] getFilters() { 120 List<FilterSetDescriptor> list = ServletRegistry.getInstance().getFiltersFor(name); 121 int len1 = list.size(); 122 int len2 = filters.size(); 123 FilterSet[] filterSets = new FilterSet[len1 + len2]; 124 for (int i = 0; i < len1; i++) { 125 filterSets[i] = list.get(i).getFilterSet(); 126 } 127 for (int i = 0; i < len2; i++) { 128 filterSets[i + len1] = filters.get(i).getFilterSet(); 129 } 130 return filterSets; 131 } 132 133 @Override 134 public String toString() { 135 StringBuilder buf = new StringBuilder(); 136 buf.append(name).append(" { ").append(path).append(" [").append(classRef).append("]"); 137 buf.append("\n Properties: ").append(initParams); 138 if (!filters.isEmpty()) { 139 buf.append("\n Filters:\n "); 140 for (FilterSetDescriptor fsd : filters) { 141 buf.append(fsd.toString()); 142 buf.append("\n "); 143 } 144 } 145 if (listeners != null) { 146 buf.append("\n Listeners: ").append(listeners.toString()); 147 } 148 buf.append("\n}\n"); 149 return buf.toString(); 150 } 151 152}