Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up duplication in Session/ApplicationBeanMap & AbstractBeanMapA…
…daptor git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@566 1c488680-804c-0410-94cd-c6b725194a0e
- Loading branch information
Showing
3 changed files
with
143 additions
and
239 deletions.
There are no files selected for viewing
210 changes: 100 additions & 110 deletions
210
webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ApplicationBeanMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,100 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* | ||
* Distributable under LGPL license. | ||
* See terms of license at gnu.org. | ||
*/ | ||
package org.jboss.webbeans.servlet; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
|
||
import javax.servlet.ServletContext; | ||
import javax.webbeans.manager.Contextual; | ||
|
||
import org.jboss.webbeans.CurrentManager; | ||
import org.jboss.webbeans.contexts.AbstractBeanMapAdaptor; | ||
import org.jboss.webbeans.contexts.ApplicationContext; | ||
import org.jboss.webbeans.log.LogProvider; | ||
import org.jboss.webbeans.log.Logging; | ||
import org.jboss.webbeans.util.EnumerationIterable; | ||
|
||
/** | ||
* Abstracts the servlet API specific application context | ||
* as a Map. | ||
* | ||
* @author Gavin King | ||
*/ | ||
public class ApplicationBeanMap extends AbstractBeanMapAdaptor | ||
{ | ||
private LogProvider log = Logging.getLogProvider(ApplicationBeanMap.class); | ||
|
||
// The current servlet context | ||
private ServletContext servletContext; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param servletContext The servlet context | ||
*/ | ||
public ApplicationBeanMap(ServletContext servletContext) | ||
{ | ||
this.servletContext = servletContext; | ||
} | ||
|
||
|
||
@SuppressWarnings("unchecked") | ||
public void clear() | ||
{ | ||
for (String name : new EnumerationIterable<String>(servletContext.getAttributeNames())) | ||
{ | ||
if (name.startsWith(getKeyPrefix())) | ||
{ | ||
servletContext.removeAttribute(name); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> T get(Contextual<? extends T> bean) | ||
{ | ||
String key = getBeanKey(bean); | ||
T instance = (T) servletContext.getAttribute(key); | ||
log.trace("Searched application for key " + key + " and got " + instance); | ||
return instance; | ||
} | ||
|
||
public <T> void put(Contextual<? extends T> bean, T instance) | ||
{ | ||
String key = getBeanKey(bean); | ||
servletContext.setAttribute(key, instance); | ||
log.trace("Stored instance " + instance + " for bean " + bean + " under key " + key + " in session"); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> T remove(Contextual<? extends T> bean) | ||
{ | ||
String key = getBeanKey(bean); | ||
T result = (T) servletContext.getAttribute(key); | ||
servletContext.removeAttribute(key); | ||
return result; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Iterable<Contextual<? extends Object>> keySet() | ||
{ | ||
List<Contextual<?>> beans = new ArrayList<Contextual<?>>(); | ||
|
||
Enumeration names = servletContext.getAttributeNames(); | ||
while (names.hasMoreElements()) | ||
{ | ||
String name = (String) names.nextElement(); | ||
if (name.startsWith(getKeyPrefix())) | ||
{ | ||
String id = name.substring(getKeyPrefix().length() + 1); | ||
Contextual<?> bean = CurrentManager.rootManager().getBeans().get(Integer.parseInt(id)); | ||
beans.add(bean); | ||
} | ||
} | ||
|
||
return beans; | ||
} | ||
|
||
@Override | ||
protected String getKeyPrefix() | ||
{ | ||
return ApplicationContext.class.getName(); | ||
} | ||
|
||
} | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2008, Red Hat Middleware LLC, and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jboss.webbeans.servlet; | ||
|
||
import java.util.Enumeration; | ||
|
||
import javax.servlet.ServletContext; | ||
|
||
import org.jboss.webbeans.contexts.AbstractBeanMapAdaptor; | ||
import org.jboss.webbeans.contexts.ApplicationContext; | ||
import org.jboss.webbeans.log.LogProvider; | ||
import org.jboss.webbeans.log.Logging; | ||
|
||
/** | ||
* A BeanMap that uses a servlet context as backing map | ||
* | ||
* @author Nicklas Karlsson | ||
* | ||
* @see org.jboss.webbeans.contexts.ApplicationContext | ||
*/ | ||
public class ApplicationBeanMap extends AbstractBeanMapAdaptor | ||
{ | ||
// The log provider | ||
private static LogProvider log = Logging.getLogProvider(ApplicationBeanMap.class); | ||
// The servlet context to use as backing map | ||
private ServletContext context; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param context The servlet context instance | ||
*/ | ||
public ApplicationBeanMap(ServletContext context) | ||
{ | ||
super(); | ||
this.context = context; | ||
} | ||
|
||
/** | ||
* @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#getKeyPrefix() | ||
*/ | ||
@Override | ||
protected String getKeyPrefix() | ||
{ | ||
return ApplicationContext.class.getName(); | ||
} | ||
|
||
/** | ||
* @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#getAttribute() | ||
*/ | ||
@Override | ||
protected Object getAttribute(String key) | ||
{ | ||
return context.getAttribute(key); | ||
} | ||
|
||
/** | ||
* @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#getAttributeNames() | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
protected Enumeration<String> getAttributeNames() | ||
{ | ||
return context.getAttributeNames(); | ||
} | ||
|
||
/** | ||
* @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#removeAttributes() | ||
*/ | ||
@Override | ||
protected void removeAttribute(String key) | ||
{ | ||
context.removeAttribute(key); | ||
} | ||
|
||
/** | ||
* @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#setAttribute() | ||
*/ | ||
@Override | ||
protected void setAttribute(String key, Object instance) | ||
{ | ||
context.setAttribute(key, instance); | ||
} | ||
|
||
} |
Oops, something went wrong.