Author: nickarls
Date: 2008-12-26 18:26:01 -0500 (Fri, 26 Dec 2008)
New Revision: 741
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractBeanMapContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ApplicationContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/BasicContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ContextMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ConversationContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/DependentContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/RequestContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/SessionContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/AbstractBeanMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/ApplicationBeanMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/BeanMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/SessionBeanMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/SimpleBeanMap.java
Log:
ngh n+1. some renames/removes/re-appearances and local svn won't even revert...
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractBeanMapContext.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractBeanMapContext.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractBeanMapContext.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,111 @@
+/*
+ * 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.context;
+
+import java.lang.annotation.Annotation;
+
+import javax.webbeans.ContextNotActiveException;
+import javax.webbeans.manager.Bean;
+import javax.webbeans.manager.Contextual;
+
+import org.jboss.webbeans.context.beanmap.BeanMap;
+
+/**
+ * Base for the Context implementations. Delegates calls to the abstract
+ * getBeanMap and getActive to allow for different implementations (storage
+ * types and ThreadLocal vs. shared)
+ *
+ * @author Nicklas Karlsson
+ * @author Pete Muir
+ *
+ * @see org.jboss.webbeans.contexts.SharedContext
+ * @see org.jboss.webbeans.context.BasicContext
+ */
+public abstract class AbstractBeanMapContext extends AbstractContext
+{
+ /**
+ * Constructor
+ *
+ * @param scopeType The scope type
+ */
+ public AbstractBeanMapContext(Class<? extends Annotation> scopeType)
+ {
+ super(scopeType);
+ }
+
+ /**
+ * Get the bean if it exists in the contexts.
+ *
+ * @param create If true, a new instance of the bean will be created if none
+ * exists
+ * @return An instance of the bean
+ * @throws ContextNotActiveException if the context is not active
+ *
+ * @see javax.webbeans.manager.Context#get(Bean, boolean)
+ */
+ public <T> T get(Contextual<T> bean, boolean create)
+ {
+ if (!isActive())
+ {
+ throw new ContextNotActiveException();
+ }
+ T instance = getBeanMap().get(bean);
+ if (instance != null)
+ {
+ return instance;
+ }
+ if (!create)
+ {
+ return null;
+ }
+ instance = bean.create();
+ getBeanMap().put(bean, instance);
+ return instance;
+ }
+
+ /**
+ * Destroys a bean
+ *
+ * @param <T> The type of the bean
+ * @param bean The bean to destroy
+ */
+ private <T> void destroy(Contextual<T> bean)
+ {
+ bean.destroy(getBeanMap().get(bean));
+ }
+
+ /**
+ * Destroys the context
+ */
+ public void destroy()
+ {
+ for (Contextual<? extends Object> bean : getBeanMap().keySet())
+ {
+ destroy(bean);
+ }
+ getBeanMap().clear();
+ }
+
+ /**
+ * A method that should return the actual bean map implementation
+ *
+ * @return The actual bean map
+ */
+ protected abstract BeanMap getBeanMap();
+
+}
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractContext.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractContext.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,91 @@
+/*
+ * 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.context;
+
+import java.lang.annotation.Annotation;
+
+import javax.webbeans.manager.Context;
+
+public abstract class AbstractContext implements Context
+{
+ // The scope type
+ private Class<? extends Annotation> scopeType;
+ // The active state of the context
+ private ThreadLocal<Boolean> active;
+
+ /**
+ * Constructor
+ *
+ * @param scopeType The scope type
+ */
+ public AbstractContext(Class<? extends Annotation> scopeType)
+ {
+ this.scopeType = scopeType;
+ this.active = new ThreadLocal<Boolean>()
+ {
+ @Override
+ protected Boolean initialValue()
+ {
+ return Boolean.TRUE;
+ }
+ };
+ }
+
+ /**
+ * Get the scope the context is for
+ *
+ * @return The scope type
+ *
+ * @see javax.webbeans.manager.Context#getScopeType()
+ */
+ public Class<? extends Annotation> getScopeType()
+ {
+ return scopeType;
+ }
+
+ /**
+ * Return true if the context is active
+ *
+ * @return The active state
+ *
+ * @see javax.webbeans.manager.Context#isActive()
+ */
+ public boolean isActive()
+ {
+ return active.get().booleanValue();
+ }
+
+ /**
+ * Set the context active, internal API for WBRI
+ *
+ * @param active The new state
+ */
+ public void setActive(boolean active)
+ {
+ this.active.set(Boolean.valueOf(active));
+ }
+
+ /**
+ * Delegates to a ThreadLocal instance
+ */
+ protected Boolean getActive()
+ {
+ return active.get();
+ }
+
+}
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ApplicationContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ApplicationContext.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ApplicationContext.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,103 @@
+/*
+ * 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.context;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.webbeans.ApplicationScoped;
+
+import org.jboss.webbeans.context.beanmap.BeanMap;
+
+/**
+ * The Application context
+ *
+ * @author Nicklas Karlsson
+ *
+ * @see org.jboss.webbeans.context.ApplicationContext
+ */
+public class ApplicationContext extends AbstractBeanMapContext
+{
+
+ public static ApplicationContext INSTANCE = new ApplicationContext();
+
+ // The beans
+ private BeanMap beanMap;
+ // Is the context active?
+ private AtomicBoolean active;
+
+ /**
+ * Constructor
+ */
+ protected ApplicationContext()
+ {
+ super(ApplicationScoped.class);
+ this.active = new AtomicBoolean(true);
+ }
+
+ /**
+ * Gets the bean map
+ *
+ * @return The bean map
+ */
+ @Override
+ public BeanMap getBeanMap()
+ {
+ return this.beanMap;
+ }
+
+ /**
+ * Sets the bean map
+ *
+ * @param applicationBeanMap The bean map
+ */
+ public void setBeanMap(BeanMap applicationBeanMap)
+ {
+ this.beanMap = applicationBeanMap;
+ }
+
+ /**
+ * Indicates if the context is active
+ *
+ * @return True if active, false otherwise
+ */
+ @Override
+ public boolean isActive()
+ {
+ return active.get();
+ }
+
+ /**
+ * Sets the active state of the context
+ *
+ * @param active The new state
+ */
+ @Override
+ public void setActive(boolean active)
+ {
+ this.active.set(active);
+ }
+
+ @Override
+ public String toString()
+ {
+ String active = isActive() ? "Active " : "Inactive ";
+ String beanMapInfo = getBeanMap() == null ? "" :
getBeanMap().toString();
+ return active + "application context " + beanMapInfo;
+ }
+
+}
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/BasicContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/BasicContext.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/BasicContext.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,72 @@
+/*
+ * 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.context;
+
+import java.lang.annotation.Annotation;
+
+import org.jboss.webbeans.context.beanmap.BeanMap;
+import org.jboss.webbeans.context.beanmap.SimpleBeanMap;
+
+/**
+ * The abstraction of a private context, on that operates on a ThreadLocal
+ * BeanMap and ThreadLocal active state
+ *
+ * A private context doesn't rely on some external context to hold it's state
+ *
+ * @author Nicklas Karlsson
+ *
+ * @see org.jboss.webbeans.context.DependentContext
+ * @see org.jboss.webbeans.context.RequestContext
+ * @see org.jboss.webbeans.context.ConversationContext
+ * @see org.jboss.webbeans.context.SessionContext
+ */
+public abstract class BasicContext extends AbstractBeanMapContext
+{
+ // The beans
+ protected ThreadLocal<BeanMap> beans;
+
+ /**
+ * Constructor
+ *
+ * @param scopeType The scope types
+ */
+ public BasicContext(Class<? extends Annotation> scopeType)
+ {
+ super(scopeType);
+ beans = new ThreadLocal<BeanMap>()
+ {
+
+ @Override
+ protected BeanMap initialValue()
+ {
+ return new SimpleBeanMap();
+ }
+
+ };
+ }
+
+ /**
+ * Delegates to a ThreadLocal instance
+ */
+ @Override
+ protected BeanMap getBeanMap()
+ {
+ return beans.get();
+ }
+
+}
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ContextMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ContextMap.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ContextMap.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,155 @@
+/*
+ * 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.context;
+
+import java.lang.annotation.Annotation;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Future;
+
+import javax.webbeans.ExecutionException;
+import javax.webbeans.manager.Context;
+
+import org.jboss.webbeans.util.ConcurrentCache;
+import org.jboss.webbeans.util.Strings;
+
+/**
+ * A map from a scope to a list of contexts
+ *
+ * @author Nicklas Karlsson
+ * @author Pete Muir
+ *
+ */
+public class ContextMap extends ConcurrentCache<Class<? extends Annotation>,
List<Context>>
+{
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Gets the dependent context
+ *
+ * @param scopeType The scope type to get
+ * @return The dependent context
+ */
+ public AbstractContext getBuiltInContext(Class<? extends Annotation> scopeType)
+ {
+ boolean interrupted = false;
+ try
+ {
+ while (true)
+ {
+ try
+ {
+ Future<List<Context>> future = getFuture(scopeType);
+ if (future == null)
+ return null;
+ return (AbstractContext) future.get().iterator().next();
+ }
+ catch (InterruptedException e)
+ {
+ interrupted = true;
+ }
+ catch (java.util.concurrent.ExecutionException e)
+ {
+ rethrow(e);
+ }
+ }
+ }
+ finally
+ {
+ if (interrupted)
+ {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
+ /**
+ * Gets the list of context with the given scope type
+ *
+ * @param scopeType The scope type to match
+ * @return A list of matching contexts. An empty list is returned if there
+ * are no matches
+ */
+ public List<Context> getContext(Class<? extends Annotation> scopeType)
+ {
+ boolean interrupted = false;
+ try
+ {
+ while (true)
+ {
+ try
+ {
+ if (getFuture(scopeType) == null)
+ {
+ throw new ExecutionException("No scope registered for " +
scopeType);
+ }
+ return getFuture(scopeType).get();
+ }
+ catch (InterruptedException e)
+ {
+ interrupted = true;
+ }
+ catch (java.util.concurrent.ExecutionException e)
+ {
+ rethrow(e);
+ }
+ }
+ }
+ finally
+ {
+ if (interrupted)
+ {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
+ @Override
+ public String toString()
+ {
+ return "ContextMap holding " + delegate().size() + " contexts:
" + delegate().keySet();
+ }
+
+ public String toDetailedString()
+ {
+ return Strings.mapToString("ContextMap (scope type -> context list):
", delegate());
+ }
+
+ /**
+ * Adds a context under a scope type
+ *
+ * Creates the list of contexts if it doesn't exist
+ *
+ * @param context The new context
+ */
+ public void add(Context context)
+ {
+ List<Context> contexts = putIfAbsent(context.getScopeType(), new
Callable<List<Context>>()
+ {
+
+ public List<Context> call() throws Exception
+ {
+ return new CopyOnWriteArrayList<Context>();
+ }
+
+ });
+ contexts.add(context);
+ }
+
+}
\ No newline at end of file
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ConversationContext.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ConversationContext.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ConversationContext.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,46 @@
+/*
+ * 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.context;
+
+import javax.webbeans.ConversationScoped;
+
+/**
+ * The conversation context
+ *
+ * @author Nicklas Karlsson
+ */
+public class ConversationContext extends BasicContext
+{
+
+ /**
+ * Constructor
+ */
+ public ConversationContext()
+ {
+ super(ConversationScoped.class);
+ }
+
+ @Override
+ public String toString()
+ {
+ String active = isActive() ? "Active " : "Inactive ";
+ String beanMapInfo = getBeanMap() == null ? "" :
getBeanMap().toString();
+ return active + "conversation context " + beanMapInfo;
+ }
+
+}
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/DependentContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/DependentContext.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/DependentContext.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,98 @@
+/*
+ * 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.context;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.webbeans.ContextNotActiveException;
+import javax.webbeans.Dependent;
+import javax.webbeans.manager.Contextual;
+
+/**
+ * The dependent context
+ *
+ * @author Nicklas Karlsson
+ */
+public class DependentContext extends AbstractContext
+{
+
+ public static final DependentContext INSTANCE = new DependentContext();
+
+ private ThreadLocal<AtomicInteger> reentrantActiveCount;
+
+ /**
+ * Constructor
+ */
+ public DependentContext()
+ {
+ super(Dependent.class);
+ super.setActive(false);
+ this.reentrantActiveCount = new ThreadLocal<AtomicInteger>()
+ {
+ @Override
+ protected AtomicInteger initialValue()
+ {
+ return new AtomicInteger(0);
+ }
+ };
+ }
+
+ /**
+ * Overridden method always creating a new instance
+ *
+ * @param bean The bean to create
+ * @param create Should a new one be created
+ */
+ @Override
+ public <T> T get(Contextual<T> bean, boolean create)
+ {
+ if (!isActive())
+ {
+ throw new ContextNotActiveException();
+ }
+ // Dependent contexts don't really use any BeanMap storage
+ return create == false ? null : bean.create();
+ }
+
+ @Override
+ public String toString()
+ {
+ String active = isActive() ? "Active " : "Inactive ";
+ return active + "dependent context";
+ }
+
+ @Override
+ public void setActive(boolean active)
+ {
+ if (active)
+ {
+ if (reentrantActiveCount.get().incrementAndGet() == 1)
+ {
+ super.setActive(true);
+ }
+ }
+ else
+ {
+ if (reentrantActiveCount.get().decrementAndGet() == 0)
+ {
+ super.setActive(false);
+ }
+ }
+ }
+
+}
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/RequestContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/RequestContext.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/RequestContext.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,48 @@
+/*
+ * 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.context;
+
+import javax.webbeans.RequestScoped;
+
+/**
+ * The request context
+ *
+ * @author Nicklas Karlsson
+ */
+public class RequestContext extends BasicContext
+{
+
+ public static RequestContext INSTANCE = new RequestContext();
+
+ /**
+ * Constructor
+ */
+ protected RequestContext()
+ {
+ super(RequestScoped.class);
+ }
+
+ @Override
+ public String toString()
+ {
+ String active = isActive() ? "Active " : "Inactive ";
+ String beanMapInfo = getBeanMap() == null ? "" :
getBeanMap().toString();
+ return active + "request context " + beanMapInfo;
+ }
+
+}
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/SessionContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/SessionContext.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/SessionContext.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,80 @@
+/*
+ * 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.context;
+
+import javax.webbeans.SessionScoped;
+
+import org.jboss.webbeans.context.beanmap.BeanMap;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+
+/**
+ * The session context
+ *
+ * @author Nicklas Karlsson
+ */
+public class SessionContext extends AbstractBeanMapContext
+{
+ private static LogProvider log = Logging.getLogProvider(SessionContext.class);
+
+ public static SessionContext INSTANCE = new SessionContext();
+
+ // The beans
+ private ThreadLocal<BeanMap> beanMap;
+
+ /**
+ * Constructor
+ */
+ protected SessionContext()
+ {
+ super(SessionScoped.class);
+ log.trace("Created session context");
+ this.beanMap = new ThreadLocal<BeanMap>();
+ }
+
+ /**
+ * Gets the bean map
+ *
+ * @returns The bean map
+ * @see org.jboss.webbeans.context.AbstractContext#getBeanMap()
+ */
+ @Override
+ public BeanMap getBeanMap()
+ {
+ return beanMap.get();
+ }
+
+ /**
+ * Sets the bean map
+ *
+ * @param beanMap The bean map
+ */
+ public void setBeanMap(BeanMap beanMap)
+ {
+ this.beanMap.set(beanMap);
+ }
+
+ @Override
+ public String toString()
+ {
+ String active = isActive() ? "Active " : "Inactive ";
+ String beanMapInfo = getBeanMap() == null ? "" :
getBeanMap().toString();
+ return active + "session context " + beanMapInfo;
+ }
+
+}
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/AbstractBeanMap.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/AbstractBeanMap.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/AbstractBeanMap.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,173 @@
+/*
+ * 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.context.beanmap;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import javax.webbeans.manager.Contextual;
+
+import org.jboss.webbeans.CurrentManager;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+import org.jboss.webbeans.util.Names;
+
+public abstract class AbstractBeanMap implements BeanMap
+{
+ // The log provider
+ private static LogProvider log = Logging.getLogProvider(ApplicationBeanMap.class);
+
+ /**
+ * Gets a bean from the map
+ *
+ * @param bean The bean to get
+ * @return The instance
+ */
+ @SuppressWarnings("unchecked")
+ public <T> T get(Contextual<? extends T> bean)
+ {
+ String key = getBeanKey(bean);
+ T instance = (T) getAttribute(key);
+ log.trace("Looked for " + key + " and got " + instance);
+ return instance;
+ }
+
+ /**
+ * Removes an instance from the map
+ *
+ * @param bean The bean of the instance to remove
+ * @return The removed instance
+ */
+ public <T> T remove(Contextual<? extends T> bean)
+ {
+ T instance = get(bean);
+ String key = getBeanKey(bean);
+ removeAttribute(key);
+ log.trace("Removed bean under key " + key);
+ return instance;
+ }
+
+ /**
+ * Clears the bean map
+ */
+ @SuppressWarnings("unchecked")
+ public void clear()
+ {
+ Enumeration names = getAttributeNames();
+ while (names.hasMoreElements())
+ {
+ String name = (String) names.nextElement();
+ removeAttribute(name);
+ log.trace("Cleared " + name);
+ }
+ log.trace("Bean Map cleared");
+ }
+
+ /**
+ * Returns the beans present in the map
+ *
+ * @return The beans
+ */
+ @SuppressWarnings("unchecked")
+ public Iterable<Contextual<? extends Object>> keySet()
+ {
+ List<Contextual<?>> beans = new
ArrayList<Contextual<?>>();
+ Enumeration names = 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;
+ }
+
+ /**
+ * Puts an instance of a bean in the map
+ *
+ * @param bean The key bean
+ * @param instance The instance
+ * @return The instance added
+ */
+ public <T> void put(Contextual<? extends T> bean, T instance)
+ {
+ String key = getBeanKey(bean);
+ setAttribute(key, instance);
+ log.trace("Added bean " + bean + " under key " + key);
+ }
+
+ /**
+ * Gets an attribute from the underlying storage
+ *
+ * @param key The key of the attribute
+ * @return The data
+ */
+ protected abstract Object getAttribute(String key);
+
+ /**
+ * Removes an attribute from the underlying storage
+ *
+ * @param key The attribute to remove
+ */
+ protected abstract void removeAttribute(String key);
+
+ /**
+ * Gets an enumeration of the beans present in the underlying storage
+ *
+ * @return The current beans
+ */
+ protected abstract Enumeration<String> getAttributeNames();
+
+ /**
+ * Sets an instance under a key in the underlying storage
+ *
+ * @param key The key
+ * @param instance The instance
+ */
+ protected abstract void setAttribute(String key, Object instance);
+
+ /**
+ * Gets a key prefix that should be prefixed to names
+ *
+ * @return The prefix
+ */
+ protected abstract String getKeyPrefix();
+
+ /**
+ * Returns a map key to a bean. Uses a known prefix and appends the index of
+ * the Bean in the Manager bean list.
+ *
+ * @param bean The bean to generate a key for.
+ * @return A unique key;
+ */
+ protected String getBeanKey(Contextual<?> bean)
+ {
+ return getKeyPrefix() + "#" +
CurrentManager.rootManager().getBeans().indexOf(bean);
+ }
+
+ @Override
+ public String toString()
+ {
+ return "holding " + Names.count(keySet()) + " instances under the
key prefix " + getKeyPrefix();
+ }
+}
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/ApplicationBeanMap.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/ApplicationBeanMap.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/ApplicationBeanMap.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,95 @@
+/*
+ * 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.context.beanmap;
+
+import java.util.Enumeration;
+
+import javax.servlet.ServletContext;
+
+import org.jboss.webbeans.context.ApplicationContext;
+
+/**
+ * A BeanMap that uses a servlet context as backing map
+ *
+ * @author Nicklas Karlsson
+ *
+ * @see org.jboss.webbeans.context.ApplicationContext
+ */
+public class ApplicationBeanMap extends AbstractBeanMap
+{
+ // 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.context.beanmap.AbstractBeanMap#getKeyPrefix()
+ */
+ @Override
+ protected String getKeyPrefix()
+ {
+ return ApplicationContext.class.getName();
+ }
+
+ /**
+ * @see org.jboss.webbeans.context.beanmap.AbstractBeanMap#getAttribute()
+ */
+ @Override
+ protected Object getAttribute(String key)
+ {
+ return context.getAttribute(key);
+ }
+
+ /**
+ * @see org.jboss.webbeans.context.beanmap.AbstractBeanMap#getAttributeNames()
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ protected Enumeration<String> getAttributeNames()
+ {
+ return context.getAttributeNames();
+ }
+
+ /**
+ * @see org.jboss.webbeans.context.beanmap.AbstractBeanMap#removeAttributes()
+ */
+ @Override
+ protected void removeAttribute(String key)
+ {
+ context.removeAttribute(key);
+ }
+
+ /**
+ * @see org.jboss.webbeans.context.beanmap.AbstractBeanMap#setAttribute()
+ */
+ @Override
+ protected void setAttribute(String key, Object instance)
+ {
+ context.setAttribute(key, instance);
+ }
+
+}
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/BeanMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/BeanMap.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/BeanMap.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,70 @@
+/*
+ * 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.context.beanmap;
+
+import javax.webbeans.manager.Contextual;
+
+
+/**
+ * Interface for different implementations of Bean to Bean instance storage.
+ * Used primarily by the contexts.
+ *
+ * @author Nicklas Karlsson
+ *
+ * @see org.jboss.webbeans.context.beanmap.SimpleBeanMap
+ * @see org.jboss.webbeans.context.beanmap.SessionBeanMap
+ */
+public interface BeanMap
+{
+ /**
+ * Gets an instance of a bean from the storage.
+ *
+ * @param bean The bean whose instance to return
+ * @return The instance. Null if not found
+ */
+ 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(Contextual<? extends T>
bean);
+
+ /**
+ * Clears the storage of any bean instances
+ */
+ public abstract void clear();
+
+ /**
+ * Returns an Iterable over the current keys in the storage
+ *
+ * @return An Iterable over the keys in the storage
+ */
+ public abstract Iterable<Contextual<? extends Object>> keySet();
+
+ /**
+ * Adds a bean instance to the storage
+ *
+ * @param bean The bean type. Used as key
+ * @param instance The instance to add
+ * @return The instance added
+ */
+ public abstract <T> void put(Contextual<? extends T> bean, T instance);
+}
\ No newline at end of file
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/SessionBeanMap.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/SessionBeanMap.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/SessionBeanMap.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,95 @@
+/*
+ * 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.context.beanmap;
+
+import java.util.Enumeration;
+
+import javax.servlet.http.HttpSession;
+
+import org.jboss.webbeans.context.ApplicationContext;
+
+/**
+ * A BeanMap that uses a HTTP session as backing map
+ *
+ * @author Nicklas Karlsson
+ *
+ * @see org.jboss.webbeans.context.ApplicationContext
+ */
+public class SessionBeanMap extends AbstractBeanMap
+{
+ // The HTTP session context to use as backing map
+ private HttpSession session;
+
+ /**
+ * Constructor
+ *
+ * @param session The HTTP session
+ */
+ public SessionBeanMap(HttpSession session)
+ {
+ super();
+ this.session = session;
+ }
+
+ /**
+ * @see org.jboss.webbeans.context.beanmap.AbstractBeanMap#getKeyPrefix()
+ */
+ @Override
+ protected String getKeyPrefix()
+ {
+ return ApplicationContext.class.getName();
+ }
+
+ /**
+ * @see org.jboss.webbeans.context.beanmap.AbstractBeanMap#getAttribute()
+ */
+ @Override
+ protected Object getAttribute(String key)
+ {
+ return session.getAttribute(key);
+ }
+
+ /**
+ * @see org.jboss.webbeans.context.beanmap.AbstractBeanMap#getAttributeNames()
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ protected Enumeration<String> getAttributeNames()
+ {
+ return session.getAttributeNames();
+ }
+
+ /**
+ * @see org.jboss.webbeans.context.beanmap.AbstractBeanMap#removeAttributes()
+ */
+ @Override
+ protected void removeAttribute(String key)
+ {
+ session.removeAttribute(key);
+ }
+
+ /**
+ * @see org.jboss.webbeans.context.beanmap.AbstractBeanMap#setAttribute()
+ */
+ @Override
+ protected void setAttribute(String key, Object instance)
+ {
+ session.setAttribute(key, instance);
+ }
+
+}
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/SimpleBeanMap.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/SimpleBeanMap.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/SimpleBeanMap.java 2008-12-26
23:26:01 UTC (rev 741)
@@ -0,0 +1,144 @@
+/*
+ * 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.context.beanmap;
+
+import java.util.Map;
+import java.util.Set;
+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;
+import org.jboss.webbeans.util.Strings;
+
+import com.google.common.collect.ForwardingMap;
+
+/**
+ * A BeanMap that uses a simple forwarding HashMap as backing map
+ *
+ * @author Nicklas Karlsson
+ */
+public class SimpleBeanMap extends ForwardingMap<Contextual<? extends Object>,
Object> implements BeanMap
+{
+ private static LogProvider log = Logging.getLogProvider(SimpleBeanMap.class);
+
+ // The backing map
+ protected Map<Contextual<? extends Object>, Object> delegate;
+
+ /**
+ * Constructor
+ */
+ public SimpleBeanMap()
+ {
+ delegate = new ConcurrentHashMap<Contextual<? extends Object>,
Object>();
+ }
+
+ /**
+ * Gets an instance from the map
+ *
+ * @param The bean to look for
+ * @return An instance, if found
+ *
+ * @see org.jboss.webbeans.context.beanmap.BeanMap#get(Bean)
+ */
+ @SuppressWarnings("unchecked")
+ 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);
+ return instance;
+ }
+
+ /**
+ * Gets the delegate for the map
+ *
+ * @return The delegate
+ */
+ @Override
+ public Map<Contextual<? extends Object>, Object> delegate()
+ {
+ return delegate;
+ }
+
+ /**
+ * Removed a instance from the map
+ *
+ * @param bean the bean to remove
+ * @return The instance removed
+ *
+ * @see org.jboss.webbeans.context.beanmap.BeanMap#remove(Bean)
+ */
+ @SuppressWarnings("unchecked")
+ 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");
+ return instance;
+ }
+
+ /**
+ * Clears the map
+ *
+ * @see org.jboss.webbeans.context.beanmap.BeanMap#clear()
+ */
+ public void clear()
+ {
+ delegate.clear();
+ log.trace("Bean map cleared");
+ }
+
+ /**
+ * Returns the beans contained in the map
+ *
+ * @return The beans present
+ *
+ * @see org.jboss.webbeans.context.beanmap.BeanMap#keySet()
+ */
+ public Set<Contextual<? extends Object>> keySet()
+ {
+ return delegate.keySet();
+ }
+
+ /**
+ * Puts a bean instance under the bean key in the map
+ *
+ * @param bean The bean
+ * @param instance the instance
+ *
+ * @see org.jboss.webbeans.context.beanmap.BeanMap#put(Bean, Object)
+ */
+ 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");
+ }
+
+ @Override
+ public String toString()
+ {
+ return "holding " + delegate.size() + " instances";
+ }
+
+ public String toDetailedString()
+ {
+ return Strings.mapToString("SimpleBeanMap (bean -> instance): ",
delegate);
+ }
+
+}
\ No newline at end of file