[EJB 3.0] - ScrollableResults possible in JBoss ?
by marcelvanvelzen
Hi there,
I am retrieving information from a Postgres database via the EntityManager, createQuery and its function getResultList(). It is easy to use especially with the EJB entity annotations. A disadvantage is that it stores all the results in memory. I know about the pagination possibilities with setFirstResult() and setMaxResults().
I want to access data like a cursor and only having one record at a time in memory (not totaly true, since the dbms will store some additional records in memory, but ok). It is meant for a server side process triggered by a timer, so pagination using the getResultList() makes no sence.
Has anybody some experience with ScrollableResults and the scroll() and evict() functions in JBoss ? And if possible share some code how to do this in JBoss.
I have spent several days now on this topic, but I have no idea how to proceed.
Thanks in advance,
Marcel
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4036118#4036118
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4036118
19 years
[JBossCache] - Re: PojoCacheListener: modify
by supi
"jason.greene(a)jboss.com" wrote :
| Also, there is a known design flaw, where notifications are only dispatched locally.
| http://jira.jboss.com/jira/browse/JBCACHE-774
|
Yes, I realized this too. Unfortunately, I need to have it now, so I'm trying to do everything from the outside by converting (tree) cache notifications to pojo cache notifications.
anonymous wrote :
| Is PojoCache.findAll(String id) what you are looking for?
|
No, exactly the opposite. For example, if I get notified of address object changes, how can the person the address belongs to be determined? I think that I should be able to look up all (cycle-free) paths.
Could look something like this:
| public List<String> findPojoIds(Object inPojo) {
| List<String> result = new ArrayList<String>();
|
| if (inPojo instanceof Advised) {
| PojoInstance instance = null;
|
| // retrieve instance object
| BaseInterceptor interceptor = (BaseInterceptor)AopUtil.findCacheInterceptor(((Advised)inPojo)._getInstanceAdvisor());
| if (interceptor != null) {
| NodeSPI node = cache_.getRoot().getChildDirect(interceptor.getFqn());
| if (node != null) {
| instance = (PojoInstance)node.getDirect(PojoInstance.KEY);
| }
| }
|
| // find ids recursively
| Set<Fqn> visited = new HashSet<Fqn>();
| visited.add(interceptor.getFqn());
| result = getVirtualIds((CacheImpl)_cacheL.getCache(), instance, new Fqn(), visited);
| }
|
| return result;
| }
|
| private static List<String> getVirtualIds(CacheSPI inCache, PojoInstance inInstance, Fqn inVirtualTail, Set<Fqn> inVisited) {
| List<String> result = new ArrayList<String>();
|
| if (inInstance != null) {
| List<Fqn> refs = inInstance.getReferencedBy(); // not currently accessible
| for(Fqn ref : refs) {
| if (isInternal(ref)) {
| Object element = ref.getLastElement();
| Fqn oneLevelUp = ref.getSubFqn(0, ref.size() - 1);
| if (!inVisited.contains(oneLevelUp)) {
| // read instance, recurse
| NodeSPI node = (NodeSPI)inCache.getRoot().getChildDirect(oneLevelUp);
| PojoInstance instance = (PojoInstance)node.getDirect(PojoInstance.KEY);
| Set<Fqn> visited = new HashSet<Fqn>(inVisited); // we want a complete list of cycle-free paths
| visited.add(oneLevelUp);
| result.addAll(getVirtualIds(inCache, instance, new Fqn(new Fqn(element), inVirtualTail), visited));
| }
| } else {
| // found something
| result.add(new Fqn(ref, inVirtualTail).toString());
| }
| }
| }
| return result;
| }
|
For symmetry reasons, find() would also have to be changed to take virtual paths into account: (should be anyway, IMO)
| public Object find(Fqn inFqn) {
| Object result = null;
| Fqn fqn = inFqn;
| LinkedList<Object> fqn2 = new LinkedList<Object>();
| while (!_cache.getCache().getRoot().hasChild(fqn) && fqn.size() > 0) {
| fqn2.addFirst(fqn.getLastElement());
| fqn = fqn.getSubFqn(0, fqn.size() - 1);
| }
| if (fqn.size() > 0) {
| while (!fqn2.isEmpty()) {
| NodeSPI node = cache_.getRoot().getChildDirect(fqn);
| fqn = new Fqn(((PojoReference)node.get(PojoReference.KEY)).getFqn(), fqn2.removeFirst());
| }
| // now that we have the internal fqn, retrieve the object as usual
| result = getObject(fqn);
| }
| return result;
| }
|
|
So, this is more or less what I am looking for :) I guess that in many cases the object itself contains some kind of identifier, or even a reference back to its owner. However, I think people shouldn't be forced to include this information in their pojos. findPojoId() can't be used because the internal fqn isn't exactly useful. Neither is a randomly chosen external one.
Basil
Achermann
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4036109#4036109
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4036109
19 years
[JBoss Seam] - Seam Security: isUserInRole() expression
by jeffconstantin
What is required to use isUserInRole() to control component rendering as follows:
rendered="#{isUserInRole['admin']}"
Any help is very much appreciated.
This check is always returning false even though I am sure the user has the "admin" role. At least I can see the role being added to the Identity bean in my Authenticator bean.
System.out.println("User: " + Identity.instance().getUsername() + " Adding Role: " + userRole.getRole());
| Identity.instance().addRole(userRole.getRole());
|
Output From my Authenticator bean:
15:26:32,301 INFO [STDOUT] User: constant Adding Role: admin
| 15:26:32,301 INFO [STDOUT] User: constant Adding Role: user
|
I think the authenticator.authenticate is working properly because I can use the rendered="#{identity.loggedIn}" successfully. However, I cannot get the isUserInRole() function to work.
My Environment:
1. jboss-seam-1.2.1.GA
2. jboss-4.0.5.GA AS with EJB3 profile
3. Seam Security "simplified mode" - this mode supports authentication services and simple role-based security checks.
Components.xml
| <?xml version="1.0" encoding="utf-8"?>
| <components xmlns="http://jboss.com/products/seam/components"
| xmlns:core="http://jboss.com/products/seam/core"
| xmlns:security="http://jboss.com/products/seam/security"
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| xmlns:framework="http://jboss.com/products/seam/framework"
| xsi:schemaLocation=
| "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-1.2.xsd
| http://jboss.com/products/seam/components http://jboss.com/products/seam/components-1.2.xsd
| http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-1.2.xsd
| http://jboss.com/products/seam/security http://jboss.com/products/seam/security-1.2.xsd">
|
| <security:identity authenticate-method="#{authenticator.authenticate}"/>
|
| <component name="org.jboss.seam.core.init">
| <property name="myFacesLifecycleBug">true</property>
| <property name="jndiPattern">seamapp/#{ejbName}/local</property>
| </component>
| <component name="entityManager" class="org.jboss.seam.core.ManagedPersistenceContext">
| <property name="persistenceUnitJndiName">java:/seamappEntityManagerFactory</property>
| </component>
|
| <!-- this will create the ejb objects for the selectItems -->
| <framework:entity-query name="colors" ejbql="select d from EdmColors d" />
| <framework:entity-query name="cars" ejbql="select d from EdmCars d" />
| <framework:entity-query name="yesnos" ejbql="select d from EdmBoolean d" />
|
| </components>
|
Authenticator Bean
package com.cox.edm;
|
| import java.util.List;
|
| import javax.ejb.Stateless;
| import javax.persistence.EntityManager;
| import javax.persistence.NoResultException;
| import javax.persistence.Query;
|
| import org.jboss.seam.annotations.In;
| import org.jboss.seam.annotations.Name;
| import org.jboss.seam.core.FacesMessages;
| import org.jboss.seam.security.Identity;
|
| @Name("authenticator")
| public class Authenticator {
|
| @In(create = true)
| private EntityManager entityManager;
|
| public boolean authenticate() {
| try
| {
| Query query = entityManager.createQuery("from EdmUsers where user_id = :userid and password = :password");
| query.setParameter("userid", Identity.instance().getUsername());
| query.setParameter("password", Identity.instance().getPassword());
| EdmUsers user = (EdmUsers) query.getSingleResult();
|
| query = entityManager.createQuery( "from EdmUserRoles where user_id = :userid ");
| query.setParameter("userid", Identity.instance().getUsername() );
| List<EdmUserRoles> list = (List<EdmUserRoles>)query.getResultList();
|
| if (list != null)
| {
| for (EdmUserRoles userRole : list){
| System.out.println("User: " + Identity.instance().getUsername() + " Adding Role: " + userRole.getRole());
| Identity.instance().addRole(userRole.getRole());
| }
| }
|
| return true;
| }
| catch (NoResultException ex)
| {
| FacesMessages.instance().add("Invalid username/password");
| return false;
| }
|
| }
|
| }
My Logon.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
| <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
| <%@ taglib uri="http://jboss.com/products/seam/taglib" prefix="s" %>
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
| <f:view>
| <f:loadBundle basename="messages" var="msg"/>
| <head>
| <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
| <title><h:outputText value="#{msg.Application} #{msg.LoginTitle}"/></title>
| <style type="text/css" media="all">
| @import "style/default/screen.css";
| </style>
| </head>
| <body>
|
| <h1><h:outputText value="#{msg.Application} #{msg.Authentication}"/></h1>
|
| <h:form>
|
| <!-- ADD THE MENU SWITCHER -->
| <%@ include file="/menu.jsp" %>
|
| <div class="rvgFind" >
| <fieldset class="rvgFieldSet" >
| <legend><h:outputText value="#{msg.Authentication} Form"/></legend>
|
| <span class="rvgInputs">
| <h:outputLabel value="#{msg.Username}" for="username">
| <h:inputText value="#{identity.username}" id="username"/>
| </h:outputLabel>
| <h:outputLabel value="#{msg.Password}" for="password">
| <h:inputSecret redisplay="false" value="#{identity.password}" id="password"/>
| </h:outputLabel>
| </span>
|
| <span class="rvgActions">
| <h:commandButton type="submit" value="#{msg.Login}" action="#{identity.login}" rendered="#{not identity.loggedIn}"/>
| <h:commandButton type="submit" value="#{msg.Logout}" action="#{identity.logout}" rendered="#{identity.loggedIn}"/>
| </span>
|
| </fieldset>
| </div>
|
| </h:form>
|
|
| </body>
| </f:view>
| </html>
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4036102#4036102
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4036102
19 years
[JBoss Seam] - Re: Hot deploy error
by gmoraes
I was trying to use hotdeploy in tomcat 5.5 with components registered in /WEB-INF/dev/META-INF/components.xml and got some problems so I made some little corrections in Initialization.java.
Please verify the changes e test if I created some type of problem.
| /*
| * JBoss, Home of Professional Open Source
| *
| * Distributable under LGPL license. See terms of license at gnu.org.
| */
| package org.jboss.seam.init;
|
| import java.io.File;
| import java.io.IOException;
| import java.io.InputStream;
| import java.net.MalformedURLException;
| import java.net.URL;
| import java.net.URLClassLoader;
| import java.util.ArrayList;
| import java.util.Enumeration;
| import java.util.HashMap;
| import java.util.HashSet;
| import java.util.List;
| import java.util.Map;
| import java.util.Properties;
| import java.util.Set;
| import java.util.StringTokenizer;
| import java.util.TreeSet;
|
| import javax.servlet.ServletContext;
| import javax.servlet.http.HttpSession;
|
| import org.dom4j.Attribute;
| import org.dom4j.DocumentException;
| import org.dom4j.Element;
| import org.jboss.seam.Component;
| import org.jboss.seam.ScopeType;
| import org.jboss.seam.Seam;
| import org.jboss.seam.annotations.Install;
| import org.jboss.seam.annotations.Name;
| import org.jboss.seam.annotations.Namespace;
| import org.jboss.seam.annotations.Role;
| import org.jboss.seam.annotations.Roles;
| import org.jboss.seam.contexts.Context;
| import org.jboss.seam.contexts.Contexts;
| import org.jboss.seam.contexts.Lifecycle;
| import org.jboss.seam.core.Expressions;
| import org.jboss.seam.core.Init;
| import org.jboss.seam.core.Jbpm;
| import org.jboss.seam.core.PojoCache;
| import org.jboss.seam.deployment.ComponentScanner;
| import org.jboss.seam.deployment.NamespaceScanner;
| import org.jboss.seam.log.LogProvider;
| import org.jboss.seam.log.Logging;
| import org.jboss.seam.util.Conversions;
| import org.jboss.seam.util.Naming;
| import org.jboss.seam.util.Reflections;
| import org.jboss.seam.util.Resources;
| import org.jboss.seam.util.Strings;
| import org.jboss.seam.util.XML;
|
| /**
| * @author Gavin King
| * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
| * @version $Revision: 1.166 $
| */
| public class Initialization
| {
| public static final String COMPONENT_SUFFIX = ".component";
| private static final LogProvider log = Logging.getLogProvider(Initialization.class);
|
| private ServletContext servletContext;
| private Map<String, Conversions.PropertyValue> properties = new HashMap<String, Conversions.PropertyValue>();
| private Map<String, Set<ComponentDescriptor>> componentDescriptors = new HashMap<String, Set<ComponentDescriptor>>();
| private List<FactoryDescriptor> factoryDescriptors = new ArrayList<FactoryDescriptor>();
| private Set<Class> installedComponentClasses = new HashSet<Class>();
| private Set<String> importedPackages = new HashSet<String>();
| private Map<String, NamespaceDescriptor> namespaceMap = new HashMap<String, NamespaceDescriptor>();
| private final Map<String, EventListenerDescriptor> eventListenerDescriptors = new HashMap<String, EventListenerDescriptor>();
|
| private File[] hotDeployPaths;
| private ClassLoader hotDeployClassLoader;
|
| public Initialization(ServletContext servletContext)
| {
| this.servletContext = servletContext;
| }
|
| public Initialization create()
| {
| addNamespaces();
| initComponentsFromXmlDocument("/WEB-INF/components.xml");
| initComponentsFromXmlDocument("/WEB-INF/events.xml"); //deprecated
| initComponentsFromXmlDocuments();
| initPropertiesFromServletContext();
| initPropertiesFromResource();
| initJndiProperties();
| return this;
| }
|
| private void initComponentsFromXmlDocuments()
| {
| ClassLoader cl = Thread.currentThread().getContextClassLoader();
| if (hotDeployClassLoader != null) {
| // Must create a new ClassLoader without parent resources to prevent
| // error of duplicate component registration
| URL[] urls = ((URLClassLoader)hotDeployClassLoader).getURLs();
| cl = new URLClassLoader(urls);
| }
| Enumeration<URL> resources;
| try
| {
| resources = cl.getResources("META-INF/components.xml");
| }
| catch (IOException ioe)
| {
| throw new RuntimeException("error scanning META-INF/components.xml files", ioe);
| }
|
| Properties replacements = getReplacements();
| while (resources.hasMoreElements())
| {
| URL url = resources.nextElement();
| try
| {
| log.info("reading " + url);
| installComponentsFromXmlElements( XML.getRootElement( url.openStream() ), replacements );
| }
| catch (Exception e)
| {
| throw new RuntimeException("error while reading " + url, e);
| }
| }
|
| }
|
| private void initComponentsFromXmlDocument(String resource)
| {
| InputStream stream = Resources.getResourceAsStream(resource, servletContext);
| if (stream != null)
| {
| log.info("reading " + resource);
| try
| {
| installComponentsFromXmlElements( XML.getRootElement(stream), getReplacements() );
| }
| catch (Exception e)
| {
| throw new RuntimeException("error while reading /WEB-INF/components.xml", e);
| }
| }
| }
|
| private Properties getReplacements()
| {
| try
| {
| Properties replacements = new Properties();
| InputStream replaceStream = Resources.getResourceAsStream("components.properties");
| if (replaceStream != null) replacements.load(replaceStream);
| return replacements;
| }
| catch (IOException ioe)
| {
| throw new RuntimeException("error reading components.properties", ioe);
| }
| }
|
| @SuppressWarnings("unchecked")
| private void installComponentsFromXmlElements(Element rootElement, Properties replacements)
| throws DocumentException, ClassNotFoundException
| {
| List<Element> importElements = rootElement.elements("import-java-package");
| for (Element importElement : importElements)
| {
| String pkgName = importElement.getTextTrim();
| importedPackages.add(pkgName);
| addNamespace(Package.getPackage(pkgName));
| }
|
| List<Element> componentElements = rootElement.elements("component");
| for (Element component : componentElements)
| {
| installComponentFromXmlElement(component, component.attributeValue("name"), component
| .attributeValue("class"), replacements);
| }
|
| List<Element> factoryElements = rootElement.elements("factory");
| for (Element factory : factoryElements)
| {
| installFactoryFromXmlElement(factory);
| }
|
| List<Element> elements = rootElement.elements("event");
| for (Element event: elements)
| {
| installEventListenerFromXmlElement(event);
| }
|
| for (Element elem : (List<Element>) rootElement.elements())
| {
| String ns = elem.getNamespace().getURI();
| NamespaceDescriptor nsInfo = namespaceMap.get(ns);
| if (nsInfo != null)
| {
| String name = elem.attributeValue("name");
| String elemName = toCamelCase( elem.getName(), true );
|
| String className = nsInfo.getPackage().getName() + '.' + elemName;
| try
| {
| //get the class implied by the namespaced XML element name
| Class<Object> clazz = Reflections.classForName(className);
| Name nameAnnotation = clazz.getAnnotation(Name.class);
|
| //if the name attribute is not explicitly specified in the XML,
| //imply the name from the @Name annotation on the class implied
| //by the XML element name
| if (name == null && nameAnnotation!=null)
| {
| name = nameAnnotation.value();
| }
|
| //if this class already has the @Name annotation, the XML element
| //is just adding configuration to the existing component, don't
| //add another ComponentDescriptor (this is super-important to
| //allow overriding!)
| if ( nameAnnotation!=null && nameAnnotation.value().equals(name) )
| {
| Install install = clazz.getAnnotation(Install.class);
| if ( install == null || install.value() )
| {
| className = null;
| }
| }
| }
| catch (ClassNotFoundException cnfe)
| {
| //there is no class implied by the XML element name so the
| //component must be defined some other way, assume that we are
| //just adding configuration, don't add a ComponentDescriptor
| className = null;
| }
|
| //finally, if we could not get the name from the XML name attribute,
| //or from an @Name annotation on the class, imply it
| if (name == null)
| {
| String prefix = nsInfo.getNamespace().prefix();
| name = Strings.isEmpty(prefix) ?
| elemName : prefix + '.' + elemName;
| }
|
| installComponentFromXmlElement(elem, name, className, replacements);
| }
| }
| }
|
| @SuppressWarnings("unchecked")
| private void installEventListenerFromXmlElement(Element event)
| {
| String type = event.attributeValue("type");
| if (type==null)
| {
| throw new IllegalArgumentException("must specify type for <event/> declaration");
| }
| EventListenerDescriptor eventListener = eventListenerDescriptors.get(type);
| if (eventListener==null)
| {
| eventListener = new EventListenerDescriptor(type);
| eventListenerDescriptors.put(type, eventListener);
| }
|
| List<Element> actions = event.elements("action");
| for (Element action: actions)
| {
| String actionExpression = action.attributeValue("expression");
| if (actionExpression==null)
| {
| throw new IllegalArgumentException("must specify expression for <action/> declaration");
| }
| eventListener.getListenerMethodBindings().add(actionExpression);
| }
| }
|
| private void installFactoryFromXmlElement(Element factory)
| {
| String scopeName = factory.attributeValue("scope");
| String name = factory.attributeValue("name");
| if (name == null)
| {
| throw new IllegalArgumentException("must specify name in <factory/> declaration");
| }
| String method = factory.attributeValue("method");
| String value = factory.attributeValue("value");
| if (method == null && value == null)
| {
| throw new IllegalArgumentException(
| "must specify either method or value in <factory/> declaration for variable: "
| + name);
| }
| ScopeType scope = scopeName == null ? ScopeType.UNSPECIFIED : ScopeType.valueOf(scopeName
| .toUpperCase());
| boolean autoCreate = "true".equals(factory.attributeValue("auto-create"));
| factoryDescriptors.add(new FactoryDescriptor(name, scope, method, value, autoCreate));
| }
|
| private String replace(String value, Properties replacements)
| {
| if (value.startsWith("@"))
| {
| value = replacements.getProperty(value.substring(1, value.length() - 1));
| }
| return value;
| }
|
| @SuppressWarnings("unchecked")
| private void installComponentFromXmlElement(Element component, String name, String className,
| Properties replacements) throws ClassNotFoundException
| {
| String installText = component.attributeValue("installed");
| boolean installed = false;
| if (installText == null || "true".equals(replace(installText, replacements)))
| {
| installed = true;
| }
|
| String scopeName = component.attributeValue("scope");
| String jndiName = component.attributeValue("jndi-name");
| String precedenceString = component.attributeValue("precedence");
| int precedence = precedenceString==null ? Install.APPLICATION : Integer.valueOf(precedenceString);
| ScopeType scope = scopeName == null ? null : ScopeType.valueOf(scopeName.toUpperCase());
| boolean autoCreate = "true".equals(component.attributeValue("auto-create"));
| if (className != null)
| {
| Class<?> clazz = null;
| try
| {
| if (hotDeployClassLoader != null) {
| clazz = hotDeployClassLoader.loadClass(className);
| } else {
| clazz = Reflections.classForName(className);
| }
| }
| catch (ClassNotFoundException cnfe)
| {
| for (String pkg : importedPackages)
| {
| try
| {
| if (hotDeployClassLoader != null) {
| clazz = hotDeployClassLoader.loadClass(pkg + '.' + className);
| } else {
| clazz = Reflections.classForName(pkg + '.' + className);
| }
| break;
| }
| catch (Exception e)
| {
| }
| }
| if (clazz == null) throw cnfe;
| }
|
| if (name == null)
| {
| if ( !clazz.isAnnotationPresent(Name.class) )
| {
| throw new IllegalArgumentException(
| "Component class must have @Name annotation or name must be specified in components.xml: " +
| clazz.getName());
| }
|
| name = clazz.getAnnotation(Name.class).value();
| }
|
| ComponentDescriptor descriptor = new ComponentDescriptor(name, clazz, scope, autoCreate, jndiName, installed, precedence);
| addComponentDescriptor(descriptor);
| installedComponentClasses.add(clazz);
| }
| else if (name == null)
| {
| throw new IllegalArgumentException("must specify either class or name in <component/> declaration");
| }
|
| for (Element prop : (List<Element>) component.elements())
| {
| String propName = prop.attributeValue("name");
| if (propName == null)
| {
| propName = prop.getQName().getName();
| }
| String qualifiedPropName = name + '.' + toCamelCase(propName, false);
| properties.put(qualifiedPropName, getPropertyValue(prop, qualifiedPropName, replacements));
| }
|
| for (Attribute prop: (List<Attribute>) component.attributes())
| {
| String attributeName = prop.getName();
| boolean isProperty = !"name".equals(attributeName) &&
| !"installed".equals(attributeName) &&
| !"scope".equals(attributeName) &&
| !"class".equals(attributeName) &&
| !"jndi-name".equals(attributeName) &&
| !"precedence".equals(attributeName) &&
| !"auto-create".equals(attributeName);
| if (isProperty)
| {
| String qualifiedPropName = name + '.' + toCamelCase( prop.getQName().getName(), false );
| properties.put(qualifiedPropName, getPropertyValue(prop, replacements));
| }
| }
| }
|
| private void addComponentDescriptor(ComponentDescriptor descriptor)
| {
| String name = descriptor.getName();
| Set<ComponentDescriptor> set = componentDescriptors.get(name);
| if (set==null)
| {
| set = new TreeSet<ComponentDescriptor>(new ComponentDescriptor.PrecedenceComparator());
| componentDescriptors.put(name, set);
| }
| if ( !set.isEmpty() )
| {
| log.info("two components with same name, higher precedence wins: " + name);
| }
| if ( !set.add(descriptor) )
| {
| throw new IllegalStateException("Two components with the same name and precedence: " + name);
| }
| }
|
| private Conversions.PropertyValue getPropertyValue(Attribute prop, Properties replacements)
| {
| return new Conversions.FlatPropertyValue( trimmedText(prop, replacements) );
| }
|
| @SuppressWarnings("unchecked")
| private Conversions.PropertyValue getPropertyValue(Element prop, String propName,
| Properties replacements)
| {
| List<Element> keyElements = prop.elements("key");
| List<Element> valueElements = prop.elements("value");
|
| if (valueElements.isEmpty() && keyElements.isEmpty())
| {
| return new Conversions.FlatPropertyValue(
| trimmedText(prop, propName, replacements));
| }
| else if (keyElements.isEmpty())
| {
| // a list-like structure
| int len = valueElements.size();
| String[] values = new String[len];
| for (int i = 0; i < len; i++)
| {
| values = trimmedText(valueElements.get(i), propName, replacements);
| }
| return new Conversions.MultiPropertyValue(values);
| }
| else
| {
| // a map-like structure
| if (valueElements.size() != keyElements.size())
| {
| throw new IllegalArgumentException("value elements must match key elements: "
| + propName);
| }
| Map<String, String> keyedValues = new HashMap<String, String>();
| for (int i = 0; i < keyElements.size(); i++)
| {
| String key = trimmedText(keyElements.get(i), propName, replacements);
| String value = trimmedText(valueElements.get(i), propName, replacements);
| keyedValues.put(key, value);
| }
| return new Conversions.AssociativePropertyValue(keyedValues);
| }
| }
|
| private String trimmedText(Element element, String propName, Properties replacements)
| {
| String text = element.getTextTrim();
| if (text == null)
| {
| throw new IllegalArgumentException("property value must be specified in element body: "
| + propName);
| }
| return replace(text, replacements);
| }
|
| private String trimmedText(Attribute attribute, Properties replacements)
| {
| return replace( attribute.getText(), replacements );
| }
|
| public Initialization setProperty(String name, Conversions.PropertyValue value)
| {
| properties.put(name, value);
| return this;
| }
|
| public Initialization init()
| {
| log.info("initializing Seam");
| Lifecycle.beginInitialization(servletContext);
| Contexts.getApplicationContext().set(Component.PROPERTIES, properties);
| initHotDeployClassLoader();
| initComponentsFromXmlDocuments();
| scanForHotDeployableComponents();
| scanForComponents();
|
| addComponent( new ComponentDescriptor(Init.class), Contexts.getApplicationContext() );
| Init init = (Init) Component.getInstance(Init.class, ScopeType.APPLICATION);
| ComponentDescriptor desc = findDescriptor(Jbpm.class);
| if (desc != null && desc.isInstalled())
| {
| init.setJbpmInstalled(true);
| }
| init.setTimestamp( System.currentTimeMillis() );
| init.setHotDeployPaths(hotDeployPaths);
|
| addSpecialComponents(init);
| installComponents(init);
| Lifecycle.endInitialization();
| log.info("done initializing Seam");
| return this;
| }
|
| public Initialization redeploy(HttpSession session)
| {
| log.info("redeploying");
| Lifecycle.beginReinitialization(servletContext, session);
| Init init = Init.instance();
| for ( String name: init.getHotDeployableComponents() )
| {
| Component component = Component.forName(name);
| ScopeType scope = component.getScope();
| if ( scope!=ScopeType.STATELESS && scope.isContextActive() )
| {
| scope.getContext().remove(name);
| }
| Contexts.getApplicationContext().remove(name + ".component");
| }
| init.getHotDeployableComponents().clear();
| Contexts.getApplicationContext().set(Component.PROPERTIES, properties);
| initHotDeployClassLoader();
| initComponentsFromXmlDocuments();
| scanForHotDeployableComponents();
| init.setTimestamp( System.currentTimeMillis() );
| init.setHotDeployPaths(hotDeployPaths);
| installComponents(init);
| Lifecycle.endInitialization();
| log.info("done redeploying");
| return this;
| }
|
| private void initHotDeployClassLoader()
| {
| ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
| try
| {
| URL resource = contextClassLoader.getResource("META-INF/debug.xhtml");
| if (resource!=null)
| {
| String path = resource.toExternalForm();
| String hotDeployDirectory = path.substring( 9, path.length()-46 ) + "dev";
| File directory = new File(hotDeployDirectory);
| if ( directory.exists() )
| {
| URL url = directory.toURI().toURL();
| /*File[] jars = directory.listFiles( new FilenameFilter() {
| public boolean accept(File file, String name) { return name.endsWith(".jar"); }
| } );
| URL[] urls = new URL[jars.length];
| for (int i=0; i<jars.length; i++)
| {
| urls = jars.toURL();
| }*/
| URL[] urls = {url};
| hotDeployClassLoader = new URLClassLoader(urls, contextClassLoader);
| hotDeployPaths = new File[] {directory};
| }
| }
| }
| catch (MalformedURLException mue)
| {
| throw new RuntimeException(mue);
| }
| }
|
| private void scanForHotDeployableComponents()
| {
| if ( hotDeployClassLoader!=null )
| {
| Set<Class<Object>> scannedClasses = new HashSet<Class<Object>>();
| scannedClasses.addAll( new ComponentScanner(null, hotDeployClassLoader).getClasses() );
| Set<Package> scannedPackages = new HashSet<Package>();
| for (Class<Object> scannedClass: scannedClasses)
| {
| installScannedClass(scannedPackages, scannedClass);
| }
| }
| }
|
| private void scanForComponents()
| {
| Set<Class<Object>> scannedClasses = new HashSet<Class<Object>>();
| scannedClasses.addAll( new ComponentScanner("seam.properties").getClasses() );
| scannedClasses.addAll( new ComponentScanner("META-INF/seam.properties").getClasses() );
| scannedClasses.addAll( new ComponentScanner("META-INF/components.xml").getClasses() );
|
| Set<Package> scannedPackages = new HashSet<Package>();
| for (Class<Object> scannedClass: scannedClasses)
| {
| installScannedClass(scannedPackages, scannedClass);
| }
| }
|
| private void installScannedClass(Set<Package> scannedPackages, Class<Object> scannedClass)
| {
| installScannedComponentAndRoles(scannedClass);
| installComponentsFromDescriptor( classDescriptorFilename(scannedClass), scannedClass );
| Package pkg = scannedClass.getPackage();
| if (pkg != null && scannedPackages.add(pkg) )
| {
| installComponentsFromDescriptor( packageDescriptorFilename(pkg), scannedClass );
| }
| }
|
| private static String classDescriptorFilename(Class<Object> scannedClass)
| {
| return scannedClass.getName().replace('.', '/') + ".component.xml";
| }
|
| private static String packageDescriptorFilename(Package pkg)
| {
| return pkg.getName().replace('.', '/') + "/components.xml";
| }
|
| private void installComponentsFromDescriptor(String fileName, Class clazz)
| {
| //note: this is correct, we do not need to scan other classloaders!
| InputStream stream = clazz.getClassLoader().getResourceAsStream(fileName);
| if (stream != null)
| {
| try
| {
| Properties replacements = getReplacements();
| Element root = XML.getRootElement(stream);
| if ( root.getName().equals("components") )
| {
| installComponentsFromXmlElements(root, replacements);
| }
| else
| {
| //TODO: namespaced components!!!
| installComponentFromXmlElement(
| root,
| root.attributeValue("name"),
| clazz.getName(), replacements
| );
| }
| }
| catch (Exception e)
| {
| throw new RuntimeException("error while reading " + fileName, e);
| }
| }
| }
|
| private void installScannedComponentAndRoles(Class<Object> scannedClass)
| {
| if (scannedClass.isAnnotationPresent(Name.class))
| {
| addComponentDescriptor(new ComponentDescriptor(scannedClass));
| }
| if (scannedClass.isAnnotationPresent(Role.class))
| {
| installRole(scannedClass, scannedClass.getAnnotation(Role.class));
| }
| if (scannedClass.isAnnotationPresent(Roles.class))
| {
| Role[] roles = scannedClass.getAnnotation(Roles.class).value();
| for (Role role : roles)
| {
| installRole(scannedClass, role);
| }
| }
| }
|
| private void installRole(Class<Object> scannedClass, Role role)
| {
| ScopeType scope = Seam.getComponentRoleScope(scannedClass, role);
| addComponentDescriptor( new ComponentDescriptor( role.name(), scannedClass, scope ) );
| }
|
| private void addNamespace(Package pkg)
| {
| if (pkg != null)
| {
| Namespace ns = pkg.getAnnotation(Namespace.class);
| if (ns != null)
| {
| log.info("Namespace: " + ns.value() + ", package: " + pkg.getName() + ", prefix: " + ns.prefix());
| namespaceMap.put(ns.value(), new NamespaceDescriptor(ns, pkg));
| }
| }
| }
|
| private void addNamespaces()
| {
| for ( Package pkg : new NamespaceScanner("META-INF/components.xml").getPackages() )
| {
| addNamespace(pkg);
| }
| for ( Package pkg : new NamespaceScanner("seam.properties").getPackages() )
| {
| addNamespace(pkg);
| }
| for ( Package pkg : new NamespaceScanner("META-INF/seam.properties").getPackages() )
| {
| addNamespace(pkg);
| }
| }
|
| private void initPropertiesFromServletContext()
| {
| Enumeration params = servletContext.getInitParameterNames();
| while (params.hasMoreElements())
| {
| String name = (String) params.nextElement();
| properties.put(name, new Conversions.FlatPropertyValue(servletContext
| .getInitParameter(name)));
| }
| }
|
| private void initPropertiesFromResource()
| {
| Properties props = loadFromResource("/seam.properties");
| for (Map.Entry me : props.entrySet())
| {
| properties.put((String) me.getKey(), new Conversions.FlatPropertyValue((String) me
| .getValue()));
| }
| }
|
| private void initJndiProperties()
| {
| Properties jndiProperties = new Properties();
| jndiProperties.putAll(loadFromResource("/jndi.properties"));
| jndiProperties.putAll(loadFromResource("/seam-jndi.properties"));
| Naming.setInitialContextProperties(jndiProperties);
| }
|
| private Properties loadFromResource(String resource)
| {
| Properties props = new Properties();
| InputStream stream = Resources.getResourceAsStream(resource, servletContext);
| if (stream != null)
| {
| log.info("reading properties from: " + resource);
| try
| {
| props.load(stream);
| }
| catch (IOException ioe)
| {
| log.error("could not read " + resource, ioe);
| }
| }
| else
| {
| log.debug("not found: " + resource);
| }
| return props;
| }
|
| protected ComponentDescriptor findDescriptor(Class<?> componentClass)
| {
| for (Set<ComponentDescriptor> components : componentDescriptors.values())
| {
| for (ComponentDescriptor component: components)
| {
| if ( component.getComponentClass().equals(componentClass) )
| {
| return component;
| }
| }
| }
| return null;
| }
|
| private void addSpecialComponents(Init init)
| {
| try
| {
| Reflections.classForName("org.jboss.cache.aop.PojoCache");
| addComponentDescriptor( new ComponentDescriptor(PojoCache.class, true) );
| }
| catch (ClassNotFoundException e) {}
| catch (NoClassDefFoundError e) {
| //temp solution due to broken JEMS installer portal profile!
| log.warn("Did not install PojoCache due to NoClassDefFoundError: " + e.getMessage());
| }
| }
|
| private void installComponents(Init init)
| {
| log.info("Installing components...");
| Context context = Contexts.getApplicationContext();
|
| DependencyManager manager = new DependencyManager(componentDescriptors);
|
| Set<ComponentDescriptor> installable = manager.installedSet();
| for (ComponentDescriptor componentDescriptor: installable) {
| String compName = componentDescriptor.getName() + COMPONENT_SUFFIX;
|
| if (!context.isSet(compName)) {
| addComponent(componentDescriptor, context);
|
| if (componentDescriptor.isAutoCreate()) {
| init.addAutocreateVariable( componentDescriptor.getName() );
| }
|
| if (componentDescriptor.isFilter()) {
| init.addInstalledFilter( componentDescriptor.getName() );
| }
|
| if (componentDescriptor.isResourceProvider()) {
| init.addResourceProvider( componentDescriptor.getName() );
| }
| }
| }
|
|
| for (FactoryDescriptor factoryDescriptor : factoryDescriptors)
| {
| if (factoryDescriptor.isValueBinding())
| {
| init.addFactoryValueBinding(factoryDescriptor.getName(), factoryDescriptor.getValue(),
| factoryDescriptor.getScope());
| }
| else
| {
| init.addFactoryMethodBinding(factoryDescriptor.getName(),
| factoryDescriptor.getMethod(), factoryDescriptor.getScope());
| }
| if (factoryDescriptor.isAutoCreate())
| {
| init.addAutocreateVariable(factoryDescriptor.getName());
| }
| }
|
| for (EventListenerDescriptor listenerDescriptor: eventListenerDescriptors.values())
| {
| for (String expression: listenerDescriptor.getListenerMethodBindings())
| {
| init.addObserverMethodBinding( listenerDescriptor.getType(), Expressions.instance().createMethodBinding(expression) );
| }
| }
| }
|
|
| /**
| * This actually creates a real Component and should only be called when
| * we want to install a component
| */
| protected void addComponent(ComponentDescriptor descriptor, Context context)
| {
| String name = descriptor.getName();
| String componentName = name + COMPONENT_SUFFIX;
| try
| {
| Component component = new Component(
| descriptor.getComponentClass(),
| name,
| descriptor.getScope(),
| descriptor.getJndiName()
| );
| context.set(componentName, component);
| if ( descriptor.getComponentClass().getClassLoader()==hotDeployClassLoader )
| {
| Init.instance().addHotDeployableComponent( component.getName() );
| }
| }
| catch (Throwable e)
| {
| throw new RuntimeException("Could not create Component: " + name, e);
| }
| }
|
| private static String toCamelCase(String hyphenated, boolean initialUpper)
| {
| StringTokenizer tokens = new StringTokenizer(hyphenated, "-");
| StringBuilder result = new StringBuilder( hyphenated.length() );
| String firstToken = tokens.nextToken();
| if (initialUpper)
| {
| result.append( Character.toUpperCase( firstToken.charAt(0) ) )
| .append( firstToken.substring(1) );
| }
| else
| {
| result.append(firstToken);
| }
| while ( tokens.hasMoreTokens() )
| {
| String token = tokens.nextToken();
| result.append( Character.toUpperCase( token.charAt(0) ) )
| .append( token.substring(1) );
| }
| return result.toString();
| }
|
|
|
| private static class EventListenerDescriptor
| {
| private String type;
| private List<String> listenerMethodBindings = new ArrayList<String>();
|
| EventListenerDescriptor(String type)
| {
| this.type = type;
| }
|
| public String getType()
| {
| return type;
| }
|
| public List<String> getListenerMethodBindings()
| {
| return listenerMethodBindings;
| }
|
| @Override
| public String toString()
| {
| return "EventListenerDescriptor(" + type + ')';
| }
| }
|
|
| }
|
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4036101#4036101
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4036101
19 years