You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2015/05/18 20:43:04 UTC
svn commit: r1680067 - in /openwebbeans/trunk/webbeans-impl/src:
main/java/org/apache/webbeans/context/
test/java/org/apache/webbeans/test/contexts/
Author: struberg
Date: Mon May 18 18:43:03 2015
New Revision: 1680067
URL: http://svn.apache.org/r1680067
Log:
improve passivation logic by removing unnecessary map gets
Added:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/PassivatingContext.java (with props)
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ConversationContext.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/SessionContext.java
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ConversationContext.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ConversationContext.java?rev=1680067&r1=1680066&r2=1680067&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ConversationContext.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/ConversationContext.java Mon May 18 18:43:03 2015
@@ -18,15 +18,7 @@
*/
package org.apache.webbeans.context;
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.NotSerializableException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import java.lang.annotation.Annotation;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import javax.enterprise.context.ConversationScoped;
@@ -35,7 +27,6 @@ import javax.enterprise.context.spi.Cont
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.context.creational.BeanInstanceBag;
import org.apache.webbeans.conversation.ConversationImpl;
-import org.apache.webbeans.util.WebBeansUtil;
/**
* Conversation context implementation.
@@ -43,9 +34,9 @@ import org.apache.webbeans.util.WebBeans
* It should not be confused with the Map of conversationId -> Conversation
* which we internally store in the SessionContext.
*/
-public class ConversationContext extends AbstractContext implements Externalizable
+public class ConversationContext extends PassivatingContext implements Serializable
{
- private static final long serialVersionUID = -576054696008715282L;
+ private static final long serialVersionUID = 2L;
private ConversationImpl conversation;
@@ -75,46 +66,4 @@ public class ConversationContext extends
return conversation;
}
- @Override
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException
- {
- scopeType = (Class<? extends Annotation>) in.readObject();
- Map<String, BeanInstanceBag<?>> map = (Map<String, BeanInstanceBag<?>>)in.readObject();
- setComponentInstanceMap();
- Iterator<String> it = map.keySet().iterator();
- Contextual<?> contextual = null;
- while(it.hasNext())
- {
- String id = it.next();
- if (id != null)
- {
- contextual = org.apache.webbeans.config.WebBeansContext.currentInstance().getBeanManagerImpl().getPassivationCapableBean(id);
- }
- if (contextual != null)
- {
- componentInstanceMap.put(contextual, map.get(id));
- }
- }
- }
-
- @Override
- public void writeExternal(ObjectOutput out) throws IOException
- {
- out.writeObject(scopeType);
- Iterator<Contextual<?>> it = componentInstanceMap.keySet().iterator();
- Map<String, BeanInstanceBag<?>> map = new HashMap<String, BeanInstanceBag<?>>();
- while(it.hasNext())
- {
- Contextual<?>contextual = it.next();
- String id = WebBeansUtil.getPassivationId(contextual);
- if (id == null)
- {
- throw new NotSerializableException("cannot serialize " + contextual.toString());
- }
- map.put(id, componentInstanceMap.get(contextual));
- }
- out.writeObject(map);
- }
-
}
Added: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/PassivatingContext.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/PassivatingContext.java?rev=1680067&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/PassivatingContext.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/PassivatingContext.java Mon May 18 18:43:03 2015
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.webbeans.context;
+
+import javax.enterprise.context.spi.Contextual;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.NotSerializableException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.context.creational.BeanInstanceBag;
+import org.apache.webbeans.util.WebBeansUtil;
+
+/**
+ * Base class for passivating contexts.
+ * It basically provides serialisation support
+ */
+public abstract class PassivatingContext extends AbstractContext implements Serializable, Externalizable
+{
+
+ public PassivatingContext(Class<? extends Annotation> scopeType)
+ {
+ super(scopeType);
+ }
+
+ @Override
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
+ {
+ WebBeansContext webBeansContext = WebBeansContext.currentInstance();
+
+ scopeType = (Class<? extends Annotation>) in.readObject();
+ Map<String, BeanInstanceBag<?>> map = (Map<String, BeanInstanceBag<?>>)in.readObject();
+ setComponentInstanceMap();
+ for (Map.Entry<String, BeanInstanceBag<?>> beanBagEntry : map.entrySet())
+ {
+ String id = beanBagEntry.getKey();
+ if (id != null)
+ {
+ Contextual<?> contextual = webBeansContext.getBeanManagerImpl().getPassivationCapableBean(id);
+ if (contextual != null)
+ {
+ componentInstanceMap.put(contextual, beanBagEntry.getValue());
+ }
+ }
+ }
+ }
+
+ @Override
+ public void writeExternal(ObjectOutput out) throws IOException
+ {
+ out.writeObject(scopeType);
+ Map<String, BeanInstanceBag<?>> map = new HashMap<String, BeanInstanceBag<?>>(componentInstanceMap.size());
+
+ for (Map.Entry<Contextual<?>, BeanInstanceBag<?>> beanBagEntry : componentInstanceMap.entrySet())
+ {
+ Contextual<?> contextual = beanBagEntry.getKey();
+
+ String id = WebBeansUtil.getPassivationId(contextual);
+ if (id == null)
+ {
+ throw new NotSerializableException("cannot serialize " + contextual.toString());
+ }
+ map.put(id, beanBagEntry.getValue());
+ }
+
+ out.writeObject(map);
+ }
+
+}
Propchange: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/PassivatingContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/SessionContext.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/SessionContext.java?rev=1680067&r1=1680066&r2=1680067&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/SessionContext.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/context/SessionContext.java Mon May 18 18:43:03 2015
@@ -18,31 +18,20 @@
*/
package org.apache.webbeans.context;
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.NotSerializableException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
import java.io.Serializable;
-import java.lang.annotation.Annotation;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.context.spi.Contextual;
-import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.context.creational.BeanInstanceBag;
-import org.apache.webbeans.util.WebBeansUtil;
/**
* Session context implementation.
*/
-public class SessionContext extends AbstractContext implements Serializable, Externalizable
+public class SessionContext extends PassivatingContext implements Serializable
{
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 2L;
public SessionContext()
{
@@ -55,49 +44,4 @@ public class SessionContext extends Abst
componentInstanceMap = new ConcurrentHashMap<Contextual<?>, BeanInstanceBag<?>>();
}
-
- @Override
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException
- {
- WebBeansContext webBeansContext = WebBeansContext.currentInstance();
-
- scopeType = (Class<? extends Annotation>) in.readObject();
- Map<String, BeanInstanceBag<?>> map = (Map<String, BeanInstanceBag<?>>)in.readObject();
- setComponentInstanceMap();
- Iterator<String> it = map.keySet().iterator();
- Contextual<?> contextual = null;
- while(it.hasNext())
- {
- String id = it.next();
- if (id != null)
- {
- contextual = webBeansContext.getBeanManagerImpl().getPassivationCapableBean(id);
- }
- if (contextual != null)
- {
- componentInstanceMap.put(contextual, map.get(id));
- }
- }
- }
-
- @Override
- public void writeExternal(ObjectOutput out) throws IOException
- {
- out.writeObject(scopeType);
- Iterator<Contextual<?>> it = componentInstanceMap.keySet().iterator();
- Map<String, BeanInstanceBag<?>> map = new HashMap<String, BeanInstanceBag<?>>();
- while(it.hasNext())
- {
- Contextual<?>contextual = it.next();
- String id = WebBeansUtil.getPassivationId(contextual);
- if (id == null)
- {
- throw new NotSerializableException("cannot serialize " + contextual.toString());
- }
- map.put(id, componentInstanceMap.get(contextual));
- }
- out.writeObject(map);
- }
-
}
Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java?rev=1680067&r1=1680066&r2=1680067&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java Mon May 18 18:43:03 2015
@@ -21,6 +21,7 @@ package org.apache.webbeans.test.context
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.container.SerializableBean;
+import org.apache.webbeans.context.SessionContext;
import org.apache.webbeans.test.AbstractUnitTest;
import org.apache.webbeans.test.contexts.serialize.AppScopedBean;
import org.apache.webbeans.test.contexts.serialize.SessScopedBean;
@@ -138,15 +139,18 @@ public class SerializationTest extends A
// and now we are keen and try to serialize the whole passivatable Contexts!
PersonalDataBean pdb = getInstance(PersonalDataBean.class);
pdb.business();
-
- // first we need to actually create a few instances
+
+ Bean<PersonalDataBean> pdbBean = getBean(PersonalDataBean.class);
Context sessionContext = webBeansContext.getBeanManagerImpl().getContext(SessionScoped.class);
Assert.assertNotNull(sessionContext);
+ Assert.assertNotNull(sessionContext.get(pdbBean));
byte[] ba = serializeObject(sessionContext);
Assert.assertNotNull(ba);
Context sessContext2 = (Context) deSerializeObject(ba);
Assert.assertNotNull(sessContext2);
+ ((SessionContext) sessContext2).setActive(true);
+ Assert.assertNotNull(sessContext2.get(pdbBean));
}
@Test