Skip to content

Commit

Permalink
Contextual interface
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@396 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Dec 3, 2008
1 parent 741204c commit 0cb3233
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 50 deletions.
5 changes: 2 additions & 3 deletions webbeans-api/src/main/java/javax/webbeans/manager/Bean.java
Expand Up @@ -26,10 +26,9 @@
*
* @author Gavin King
*
* @param <T>
* an API type of the Web Bean
* @param <T> an API type of the Web Bean
*/
public abstract class Bean<T>
public abstract class Bean<T> implements Contextual<T>
{

private final Manager manager;
Expand Down
39 changes: 19 additions & 20 deletions webbeans-api/src/main/java/javax/webbeans/manager/Context.java
@@ -1,25 +1,24 @@
/*
* 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.
*/
* 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 javax.webbeans.manager;

import java.lang.annotation.Annotation;


/**
* The contract between the Web Bean manager and a Web Beans context object.
* This interface should not be called directly by the application.
Expand All @@ -32,9 +31,9 @@ public interface Context
{

public Class<? extends Annotation> getScopeType();
public <T> T get(Bean<T> bean, boolean create);

public <T> T get(Contextual<T> bean, boolean create);

boolean isActive();

}
31 changes: 31 additions & 0 deletions webbeans-api/src/main/java/javax/webbeans/manager/Contextual.java
@@ -0,0 +1,31 @@
/*
* 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 javax.webbeans.manager;

/**
* The contract between a Context and an object that has its lifecycle managed
* by the context
*
* @author Nicklas Karlsson
*/
public interface Contextual<T>
{
public abstract T create();

public abstract void destroy(T instance);
}
Expand Up @@ -23,6 +23,7 @@
import javax.webbeans.ContextNotActiveException;
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Context;
import javax.webbeans.manager.Contextual;
import javax.webbeans.manager.Manager;

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public AbstractContext(Class<? extends Annotation> scopeType)
*
* @see javax.webbeans.manager.Context#get(Bean, boolean)
*/
public <T> T get(Bean<T> bean, boolean create)
public <T> T get(Contextual<T> bean, boolean create)
{
if (!isActive())
{
Expand Down Expand Up @@ -118,15 +119,15 @@ public void setActive(boolean active)
}

// TODO Do we need this
private <T> void destroy(Manager manager, Bean<T> bean)
private <T> void destroy(Manager manager, Contextual<T> bean)
{
bean.destroy(getBeanMap().get(bean));
}

// TODO Do we need this
public void destroy(Manager manager)
{
for (Bean<? extends Object> bean : getBeanMap().keySet())
for (Contextual<? extends Object> bean : getBeanMap().keySet())
{
destroy(manager, bean);
}
Expand Down
Expand Up @@ -17,7 +17,7 @@

package org.jboss.webbeans.contexts;

import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Contextual;

/**
* Interface for different implementations of Bean to Bean instance storage.
Expand All @@ -36,15 +36,15 @@ public interface BeanMap
* @param bean The bean whose instance to return
* @return The instance. Null if not found
*/
public abstract <T extends Object> T get(Bean<? extends T> bean);
public abstract <T extends Object> T get(Contextual<? extends T> bean);

/**
* Removes an instance of a bean from the storage
*
* @param bean The bean whose instance to remove
* @return The removed instance. Null if not found in storage.
*/
public abstract <T extends Object> T remove(Bean<? extends T> bean);
public abstract <T extends Object> T remove(Contextual<? extends T> bean);

/**
* Clears the storage of any bean instances
Expand All @@ -56,7 +56,7 @@ public interface BeanMap
*
* @return An Iterable over the keys in the storage
*/
public abstract Iterable<Bean<? extends Object>> keySet();
public abstract Iterable<Contextual<? extends Object>> keySet();

/**
* Adds a bean instance to the storage
Expand All @@ -65,5 +65,5 @@ public interface BeanMap
* @param instance The instance to add
* @return The instance added
*/
public abstract <T> void put(Bean<? extends T> bean, T instance);
public abstract <T> void put(Contextual<? extends T> bean, T instance);
}
Expand Up @@ -19,7 +19,7 @@

import javax.webbeans.ContextNotActiveException;
import javax.webbeans.Dependent;
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Contextual;

/**
* The dependent context
Expand All @@ -43,7 +43,7 @@ public DependentContext()
* @param create Should a new one be created
*/
@Override
public <T> T get(Bean<T> bean, boolean create)
public <T> T get(Contextual<T> bean, boolean create)
{
if (!isActive())
{
Expand Down
Expand Up @@ -23,6 +23,7 @@

import javax.servlet.http.HttpSession;
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Contextual;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.log.LogProvider;
Expand Down Expand Up @@ -91,7 +92,7 @@ private void checkSession()
* @param bean The bean to generate a key for.
* @return A unique key;
*/
private String getBeanKey(Bean<?> bean)
private String getBeanKey(Contextual<?> bean)
{
return keyPrefix + manager.getBeans().indexOf(bean);
}
Expand All @@ -109,7 +110,7 @@ private String getBeanKey(Bean<?> bean)
* @see org.jboss.webbeans.contexts.BeanMap#get(Bean)
*/
@SuppressWarnings("unchecked")
public <T> T get(Bean<? extends T> bean)
public <T> T get(Contextual<? extends T> bean)
{
checkSession();
String key = getBeanKey(bean);
Expand All @@ -130,7 +131,7 @@ public <T> T get(Bean<? extends T> bean)
*
* @see org.jboss.webbeans.contexts.BeanMap#remove(Bean)
*/
public <T> T remove(Bean<? extends T> bean)
public <T> T remove(Contextual<? extends T> bean)
{
checkSession();
T instance = get(bean);
Expand Down Expand Up @@ -175,11 +176,11 @@ public void clear()
* @see org.jboss.webbeans.contexts.BeanMap#keySet()
*/
@SuppressWarnings("unchecked")
public Iterable<Bean<? extends Object>> keySet()
public Iterable<Contextual<? extends Object>> keySet()
{
checkSession();

List<Bean<?>> beans = new ArrayList<Bean<?>>();
List<Contextual<?>> beans = new ArrayList<Contextual<?>>();

Enumeration names = session.getAttributeNames();
while (names.hasMoreElements())
Expand All @@ -188,7 +189,7 @@ public Iterable<Bean<? extends Object>> keySet()
if (name.startsWith(keyPrefix))
{
String id = name.substring(keyPrefix.length());
Bean<?> bean = manager.getBeans().get(Integer.parseInt(id));
Contextual<?> bean = manager.getBeans().get(Integer.parseInt(id));
beans.add(bean);
}
}
Expand All @@ -207,7 +208,7 @@ public Iterable<Bean<? extends Object>> keySet()
*
* @see org.jboss.webbeans.contexts.BeanMap#put(Bean, Object)
*/
public <T> void put(Bean<? extends T> bean, T instance)
public <T> void put(Contextual<? extends T> bean, T instance)
{
checkSession();
String key = getBeanKey(bean);
Expand All @@ -220,10 +221,10 @@ public <T> void put(Bean<? extends T> bean, T instance)
public String toString()
{
StringBuffer buffer = new StringBuffer();
List<Bean<?>> beans = (List) keySet();
List<Contextual<?>> beans = (List) keySet();
buffer.append("Bean -> bean instance mappings in HTTP session: " + beans.size() + "\n");
int i = 0;
for (Bean<?> bean : beans)
for (Contextual<?> bean : beans)
{
Object instance = get(bean);
buffer.append(++i + " - " + getBeanKey(bean) + ": " + instance + "\n");
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.ConcurrentHashMap;

import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Contextual;

import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
Expand All @@ -34,19 +35,19 @@
*
* @author Nicklas Karlsson
*/
public class SimpleBeanMap extends ForwardingMap<Bean<? extends Object>, Object> implements BeanMap
public class SimpleBeanMap extends ForwardingMap<Contextual<? extends Object>, Object> implements BeanMap
{
private static LogProvider log = Logging.getLogProvider(SimpleBeanMap.class);

// The backing map
protected Map<Bean<? extends Object>, Object> delegate;
protected Map<Contextual<? extends Object>, Object> delegate;

/**
* Constructor
*/
public SimpleBeanMap()
{
delegate = new ConcurrentHashMap<Bean<? extends Object>, Object>();
delegate = new ConcurrentHashMap<Contextual<? extends Object>, Object>();
}

/**
Expand All @@ -58,7 +59,7 @@ public SimpleBeanMap()
* @see org.jboss.webbeans.contexts.BeanMap#get(Bean)
*/
@SuppressWarnings("unchecked")
public <T extends Object> T get(Bean<? extends T> bean)
public <T extends Object> T get(Contextual<? extends T> bean)
{
T instance = (T) super.get(bean);
log.trace("Searched bean map for " + bean + " and got " + instance);
Expand All @@ -71,7 +72,7 @@ public <T extends Object> T get(Bean<? extends T> bean)
* @return The delegate
*/
@Override
public Map<Bean<? extends Object>, Object> delegate()
public Map<Contextual<? extends Object>, Object> delegate()
{
return delegate;
}
Expand All @@ -85,7 +86,7 @@ public Map<Bean<? extends Object>, Object> delegate()
* @see org.jboss.webbeans.contexts.BeanMap#remove(Bean)
*/
@SuppressWarnings("unchecked")
public <T extends Object> T remove(Bean<? extends T> bean)
public <T extends Object> T remove(Contextual<? extends T> bean)
{
T instance = (T) super.remove(bean);
log.trace("Removed instace " + instance + " for bean " + bean + " from the bean map");
Expand All @@ -110,7 +111,7 @@ public void clear()
*
* @see org.jboss.webbeans.contexts.BeanMap#keySet()
*/
public Set<Bean<? extends Object>> keySet()
public Set<Contextual<? extends Object>> keySet()
{
return delegate.keySet();
}
Expand All @@ -123,7 +124,7 @@ public Set<Bean<? extends Object>> keySet()
*
* @see org.jboss.webbeans.contexts.BeanMap#put(Bean, Object)
*/
public <T> void put(Bean<? extends T> bean, T instance)
public <T> void put(Contextual<? extends T> bean, T instance)
{
delegate.put(bean, instance);
log.trace("Stored instance " + instance + " for bean " + bean + " in bean map");
Expand Down

0 comments on commit 0cb3233

Please sign in to comment.