This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GTNPORTAL-2896 Introduce @PortletLifecycleScoped and @PortletRedispla…
…yScoped to support portlet lifecycle in CDI portlets
- Loading branch information
1 parent
b23ad43
commit 44fdced
Showing
24 changed files
with
1,397 additions
and
206 deletions.
There are no files selected for viewing
192 changes: 192 additions & 0 deletions
192
cdi/contexts/src/main/java/org/gatein/cdi/contexts/AbstractCDIPortletContext.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 |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2013, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.gatein.cdi.contexts; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.enterprise.context.ContextNotActiveException; | ||
import javax.enterprise.context.spi.Contextual; | ||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.inject.spi.PassivationCapable; | ||
|
||
import org.exoplatform.portal.pc.aspects.PortletLifecyclePhaseInterceptor; | ||
import org.gatein.cdi.contexts.beanstore.BeanStore; | ||
import org.gatein.cdi.contexts.beanstore.BeanStoreInstance; | ||
import org.gatein.cdi.contexts.beanstore.LockedBean; | ||
import org.gatein.cdi.contexts.beanstore.serial.SerializableBeanStoreInstance; | ||
|
||
/** | ||
* @author <a href="mailto:nscavell@redhat.com">Nick Scavelli</a> | ||
*/ | ||
public abstract class AbstractCDIPortletContext implements CDIPortletContext { | ||
|
||
private final ThreadLocal<BeanStore> beanStore; | ||
private final ThreadLocal<Map<String, PortletRequestLifecycle>> lifecycles; | ||
private final ThreadLocal<PortletRequestLifecycle> currentLifecycle; | ||
private final boolean multithreaded; | ||
|
||
protected AbstractCDIPortletContext(boolean multithreaded) { | ||
this.multithreaded = multithreaded; | ||
this.beanStore = new ThreadLocal<BeanStore>(); | ||
this.lifecycles = new ThreadLocal<Map<String, PortletRequestLifecycle>>(); | ||
this.currentLifecycle = new ThreadLocal<PortletRequestLifecycle>(); | ||
} | ||
|
||
@Override | ||
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) { | ||
if (!isActive()) { | ||
throw new ContextNotActiveException(); | ||
} | ||
|
||
BeanStore beanStore = getBeanStore(); | ||
if (beanStore == null) { | ||
return null; | ||
} | ||
|
||
String id = getId(contextual); | ||
BeanStoreInstance<T> beanInstance = beanStore.getBean(id); | ||
if (beanInstance != null) { | ||
return beanInstance.getInstance(); | ||
} else if (creationalContext != null) { | ||
LockedBean lock = null; | ||
try { | ||
if (multithreaded) { | ||
lock = beanStore.lock(id); | ||
beanInstance = beanStore.getBean(id); | ||
if (beanInstance != null) { | ||
return beanInstance.getInstance(); | ||
} | ||
} | ||
|
||
T instance = contextual.create(creationalContext); | ||
if (instance != null) { | ||
beanInstance = new SerializableBeanStoreInstance<T>(contextual, instance, creationalContext); | ||
beanStore.put(id, beanInstance); | ||
} | ||
return instance; | ||
} finally { | ||
if (lock != null) { | ||
lock.unlock(); | ||
} | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T get(Contextual<T> contextual) { | ||
return get(contextual, null); | ||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return currentLifecycle.get() != null; | ||
} | ||
|
||
protected void setCurrentLifecycle(String windowId, PortletRequestLifecycle lifecycle) { | ||
if (lifecycle == null) { | ||
currentLifecycle.remove(); | ||
} else { | ||
currentLifecycle.set(lifecycle); | ||
Map<String, PortletRequestLifecycle> map = lifecycles.get(); | ||
if (map == null) { | ||
map = new HashMap<String, PortletRequestLifecycle>(); | ||
lifecycles.set(map); | ||
} | ||
map.put(windowId, lifecycle); | ||
} | ||
} | ||
|
||
protected PortletRequestLifecycle getLifecycle(String windowId) { | ||
Map<String, PortletRequestLifecycle> map = lifecycles.get(); | ||
if (map == null) { | ||
map = new HashMap<String, PortletRequestLifecycle>(); | ||
lifecycles.set(map); | ||
} | ||
|
||
return map.get(windowId); | ||
} | ||
|
||
protected BeanStore getBeanStore() { | ||
return beanStore.get(); | ||
} | ||
|
||
protected void setBeanStore(BeanStore beanStore) { | ||
if (beanStore == null) { | ||
this.beanStore.remove(); | ||
} else { | ||
this.beanStore.set(beanStore); | ||
} | ||
} | ||
|
||
protected void destroy(String windowId) { | ||
try { | ||
BeanStore store = getBeanStore(); | ||
if (store != null) { | ||
store.destroy(windowId); | ||
} | ||
} finally { | ||
Map<String, PortletRequestLifecycle> map = lifecycles.get(); | ||
if (map != null) { | ||
map.remove(windowId); | ||
if (map.isEmpty()) { | ||
lifecycles.remove(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected void destroy() { | ||
try { | ||
BeanStore store = getBeanStore(); | ||
if (store != null) { | ||
store.destroy(); | ||
} | ||
} finally { | ||
cleanup(); | ||
} | ||
} | ||
|
||
protected void cleanup() { | ||
lifecycles.remove(); | ||
beanStore.remove(); | ||
} | ||
|
||
//TODO: Need to revisit this implementation | ||
private static String getId(Contextual contextual) { | ||
String id; | ||
if (contextual instanceof PassivationCapable) { | ||
id = ((PassivationCapable) contextual).getId(); | ||
} else { | ||
id = "" + contextual.hashCode(); | ||
} | ||
|
||
return windowId() + "#" + id; | ||
} | ||
|
||
private static String windowId() { | ||
return PortletLifecyclePhaseInterceptor.currentWindowId(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
cdi/contexts/src/main/java/org/gatein/cdi/contexts/CDIPortletContext.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2013, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.gatein.cdi.contexts; | ||
|
||
import javax.enterprise.context.spi.Context; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* @author <a href="mailto:nscavell@redhat.com">Nick Scavelli</a> | ||
*/ | ||
public interface CDIPortletContext extends Context { | ||
|
||
void transition(HttpServletRequest request, String windowId, PortletRequestLifecycle.State state); | ||
} |
73 changes: 73 additions & 0 deletions
73
cdi/contexts/src/main/java/org/gatein/cdi/contexts/CDIPortletContextExtension.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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2013, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.gatein.cdi.contexts; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.enterprise.inject.spi.AfterBeanDiscovery; | ||
import javax.enterprise.inject.spi.Extension; | ||
|
||
/** | ||
* @author <a href="mailto:nscavell@redhat.com">Nick Scavelli</a> | ||
*/ | ||
public class CDIPortletContextExtension implements Extension { | ||
|
||
private Collection<CDIPortletContext> contexts; | ||
|
||
@SuppressWarnings("unused") | ||
public void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery) { | ||
List<CDIPortletContext> contextList = new ArrayList<CDIPortletContext>(2); | ||
contextList.add(new PortletLifecycleContextImpl()); | ||
contextList.add(new PortletRedisplayedContextImpl()); | ||
|
||
for (CDIPortletContext context : contextList) { | ||
afterBeanDiscovery.addContext(context); | ||
} | ||
contexts = Collections.unmodifiableCollection(contextList); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends CDIPortletContext> T getContext(Class<T> type) { | ||
if (type == null) return null; | ||
|
||
for (CDIPortletContext context : contexts) { | ||
if (type.isAssignableFrom(context.getClass())) { | ||
return (T) context; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public Collection<CDIPortletContext> getContexts() { | ||
Collection<CDIPortletContext> collection = contexts; | ||
if (collection == null) { | ||
return Collections.emptyList(); | ||
} | ||
return collection; | ||
} | ||
} |
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
Oops, something went wrong.