001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.jxpath.servlet; 018 019 import java.util.Enumeration; 020 import java.util.HashSet; 021 022 import javax.servlet.ServletContext; 023 024 import org.apache.commons.jxpath.DynamicPropertyHandler; 025 026 /** 027 * Implementation of the {@link DynamicPropertyHandler} interface that provides 028 * access to attributes of a {@link ServletContext}. 029 * 030 * @author Dmitri Plotnikov 031 * @version $Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $ 032 */ 033 public class ServletContextHandler implements DynamicPropertyHandler { 034 035 private static final int DEFAULT_PROPERTY_COUNT = 16; 036 037 public String[] getPropertyNames(Object context) { 038 HashSet list = new HashSet(DEFAULT_PROPERTY_COUNT); 039 collectPropertyNames(list, context); 040 return (String[]) list.toArray(new String[list.size()]); 041 } 042 043 /** 044 * Collect the property names from bean, storing in set. 045 * @param set destination 046 * @param bean to read 047 */ 048 protected void collectPropertyNames(HashSet set, Object bean) { 049 Enumeration e = ((ServletContext) bean).getAttributeNames(); 050 while (e.hasMoreElements()) { 051 set.add(e.nextElement()); 052 } 053 } 054 055 public Object getProperty(Object context, String property) { 056 return ((ServletContext) context).getAttribute(property); 057 } 058 059 public void setProperty(Object context, String property, Object value) { 060 ((ServletContext) context).setAttribute(property, value); 061 } 062 }