All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.microprofile.config.ServletContextConfigSourceImpl Maven / Gradle / Ivy

There is a newer version: 4.0.0.Beta5
Show newest version
package org.jboss.resteasy.microprofile.config;

import java.io.Serializable;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

public class ServletContextConfigSourceImpl implements ConfigSource, Serializable {

   private static final long serialVersionUID = 3280445687403079031L;
   private volatile String name;

   @Override
   public Map getProperties() {
      ServletContext context = ResteasyProviderFactory.getContextData(ServletContext.class);
      if (context == null) {
         return Collections.emptyMap();
      }
      Map map = new HashMap();
      Enumeration keys = context.getInitParameterNames();
      if (keys != null) {
         while (keys.hasMoreElements())
         {
            String key = keys.nextElement();
            map.put(key, context.getInitParameter(key));
         }
      }
      return map;
   }

   @Override
   public String getValue(String propertyName) {
      ServletContext context = ResteasyProviderFactory.getContextData(ServletContext.class);
      if (context == null) {
         return null;
      }
      return context.getInitParameter(propertyName);
   }

   @Override
   public String getName() {
      if (name == null) {
         synchronized(this) {
            if (name == null) {
               ServletContext servletContext = ResteasyProviderFactory.getContextData(ServletContext.class);
               StringBuilder sb = new StringBuilder();
               name = sb.append(servletContext != null ? servletContext.getServletContextName() : null)
                     .append(":ServletContextConfigSource").toString();
            }
         }
      }
      return name;
   }

   @Override
   public int getOrdinal() {
      return 40;
   }

   @Override
   public Set getPropertyNames() {
      return getProperties().keySet();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy