[jboss-cvs] JBossAS SVN: r95918 - projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Nov 2 10:01:54 EST 2009
Author: alesj
Date: 2009-11-02 10:01:53 -0500 (Mon, 02 Nov 2009)
New Revision: 95918
Added:
projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi/JndiBinderDeployer.java
Removed:
projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi/JndiBinder.java
Log:
Add binding logic per BB per DU.
Deleted: projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi/JndiBinder.java
===================================================================
--- projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi/JndiBinder.java 2009-11-02 14:21:11 UTC (rev 95917)
+++ projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi/JndiBinder.java 2009-11-02 15:01:53 UTC (rev 95918)
@@ -1,154 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2008, Red Hat Middleware LLC, 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.jboss.weld.integration.deployer.jndi;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Hashtable;
-import java.util.Map;
-import java.util.Set;
-import java.util.Map.Entry;
-
-import javax.enterprise.inject.spi.BeanManager;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import org.jboss.util.naming.NonSerializableFactory;
-import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
-import org.jboss.weld.integration.deployer.env.helpers.BootstrapBean;
-import org.jboss.weld.manager.api.WeldManager;
-
-/**
- * Jndi name binder. This class binds Bean Managers into global JNDI
- * under a well-known subcontext.
- *
- * @author Pete Muir
- * @author <a href="mailto:stan.silvert at jboss.org">Stan Silvert</a>
- */
-public class JndiBinder
-{
-
- protected static class DeploymentVisitor
- {
-
- private final Map<String, BeanManager> beanManagers;
- private final BootstrapBean bootstrapBean;
-
- public DeploymentVisitor(BootstrapBean bootstrapBean)
- {
- this.beanManagers = new HashMap<String, BeanManager>();
- this.bootstrapBean = bootstrapBean;
- }
-
- public Map<String, BeanManager> getBeanManagers()
- {
- return Collections.unmodifiableMap(beanManagers);
- }
-
- public DeploymentVisitor visit()
- {
- for (BeanDeploymentArchive bda : bootstrapBean.getDeployment().getBeanDeploymentArchives())
- {
- visit(bda, new HashSet<BeanDeploymentArchive>());
- }
- return this;
- }
-
- private void visit(BeanDeploymentArchive bda, Set<BeanDeploymentArchive> seenBdas)
- {
- WeldManager beanManager = bootstrapBean.getBootstrap().getManager(bda);
- // BeanManager id is simply the BDA ID
- String key = beanManager.getId();
- beanManagers.put(key, beanManager);
- for (BeanDeploymentArchive child : bda.getBeanDeploymentArchives())
- {
- if (!seenBdas.contains(child))
- {
- visit(child, seenBdas);
- }
- }
- }
-
- }
-
- public static final String BEAN_MANAGER_JNDI_SUBCONTEXT = "BeanManagers";
-
- protected static Context createSubContextForFactories(Hashtable<String, String> jndiEnvironment, String prefix)
- {
- try
- {
- Context root = new InitialContext(jndiEnvironment);
- return root.createSubcontext(BEAN_MANAGER_JNDI_SUBCONTEXT).createSubcontext(prefix);
- }
- catch (NamingException e)
- {
- throw new IllegalStateException("Unable to create JNDI subcontext for Bean Managers", e);
- }
- }
-
- protected static void addNonSerializableFactory(Hashtable<String, String> environment)
- {
- String nonSerializableFactory = NonSerializableFactory.class.getName();
- String objFactory = environment.get(Context.OBJECT_FACTORIES);
- if (objFactory != null)
- {
- objFactory = nonSerializableFactory + ":" + objFactory;
- }
- else
- {
- objFactory = nonSerializableFactory;
- }
-
- environment.put(Context.OBJECT_FACTORIES, objFactory);
- }
-
- private final Context beanManagerContext;
- private final Map<String, BeanManager> beanManagers;
-
- public JndiBinder(BootstrapBean bootstrapBean, String deploymentUnitName)
- {
- Hashtable<String, String> jndiEnvironment = new Hashtable<String, String>();
- this.beanManagers = new DeploymentVisitor(bootstrapBean).visit().getBeanManagers();
- this.beanManagerContext = createSubContextForFactories(jndiEnvironment, deploymentUnitName);
- addNonSerializableFactory(jndiEnvironment);
- }
-
- public void bind() throws NamingException
- {
- for (Entry<String, BeanManager> entry : beanManagers.entrySet())
- {
- NonSerializableFactory.rebind(this.beanManagerContext, entry.getKey(), entry.getValue());
- }
- }
-
- public void unbind() throws NamingException
- {
- for (Entry<String, BeanManager> entry : beanManagers.entrySet())
- {
- this.beanManagerContext.unbind(entry.getKey());
- NonSerializableFactory.unbind(entry.getKey());
- }
- }
-
-}
Copied: projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi/JndiBinderDeployer.java (from rev 95915, projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi/JndiBinder.java)
===================================================================
--- projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi/JndiBinderDeployer.java (rev 0)
+++ projects/weld-int/trunk/deployer/src/main/java/org/jboss/weld/integration/deployer/jndi/JndiBinderDeployer.java 2009-11-02 15:01:53 UTC (rev 95918)
@@ -0,0 +1,212 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, 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.jboss.weld.integration.deployer.jndi;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Set;
+
+import javax.enterprise.inject.spi.BeanManager;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.beans.metadata.api.annotations.Inject;
+import org.jboss.beans.metadata.api.model.FromContext;
+import org.jboss.beans.metadata.plugins.ThisValueMetaData;
+import org.jboss.beans.metadata.spi.BeanMetaData;
+import org.jboss.beans.metadata.spi.builder.BeanMetaDataBuilder;
+import org.jboss.beans.metadata.spi.builder.ParameterMetaDataBuilder;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.deployers.spi.DeploymentException;
+import org.jboss.deployers.spi.deployer.DeploymentStages;
+import org.jboss.deployers.spi.deployer.helpers.AbstractDeployer;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.util.naming.NonSerializableFactory;
+import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
+import org.jboss.weld.integration.deployer.DeployersUtils;
+import org.jboss.weld.integration.deployer.env.helpers.BootstrapBean;
+import org.jboss.weld.manager.api.WeldManager;
+
+/**
+ * This deployer intercepts BootstrapBean metadata,
+ * and adds JndiBinder invocations to it.
+ *
+ * @author Pete Muir
+ * @author <a href="mailto:stan.silvert at jboss.org">Stan Silvert</a>
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class JndiBinderDeployer extends AbstractDeployer
+{
+ public static final String BEAN_MANAGER_JNDI_SUBCONTEXT = "BeanManagers";
+
+ private Object thisName;
+ private Hashtable<String, String> jndiEnvironment = new Hashtable<String, String>();
+ private Context beanManagerContext;
+
+ public JndiBinderDeployer()
+ {
+ setTopLevelOnly(true);
+ addInput(BeanMetaData.class);
+ setStage(DeploymentStages.PRE_REAL);
+ }
+
+ public void deploy(DeploymentUnit unit) throws DeploymentException
+ {
+ String bootstrapName = DeployersUtils.getBootstrapBeanName(unit);
+ String bbAttachmentName = bootstrapName + "_" + BeanMetaData.class.getSimpleName();
+
+ BeanMetaData bbBMD = unit.getAttachment(bbAttachmentName, BeanMetaData.class);
+ if (bbBMD != null)
+ {
+ BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder(bbBMD);
+
+ ParameterMetaDataBuilder pmdb = builder.addInstallWithParameters("bind", thisName.toString(), ControllerState.INSTALLED, ControllerState.INSTALLED);
+ pmdb.addParameterMetaData(BootstrapBean.class.getName(), new ThisValueMetaData());
+ pmdb.addParameterMetaData(String.class.getName(), unit.getSimpleName());
+
+ pmdb = builder.addUninstallWithParameters("unbind", thisName.toString(), ControllerState.INSTALLED, ControllerState.INSTALLED);
+ pmdb.addParameterMetaData(BootstrapBean.class.getName(), new ThisValueMetaData());
+ pmdb.addParameterMetaData(String.class.getName(), unit.getSimpleName());
+ }
+ }
+
+ @Inject(fromContext = FromContext.NAME)
+ public void setThisName(Object thisName)
+ {
+ this.thisName = thisName;
+ }
+
+ public void create() throws Exception
+ {
+ if (jndiEnvironment != null)
+ addNonSerializableFactory(jndiEnvironment); // is here OK?
+
+ Context context = new InitialContext(jndiEnvironment);
+ beanManagerContext = context.createSubcontext(BEAN_MANAGER_JNDI_SUBCONTEXT);
+ }
+
+ public void destroy()
+ {
+ try
+ {
+ Context context = new InitialContext(jndiEnvironment);
+ context.destroySubcontext(BEAN_MANAGER_JNDI_SUBCONTEXT);
+ }
+ catch (Exception ignore)
+ {
+ }
+ }
+
+ public void setJndiEnvironment(Hashtable<String, String> jndiEnvironment)
+ {
+ this.jndiEnvironment = jndiEnvironment;
+ }
+
+// --- binding logic ---
+
+ public void bind(BootstrapBean bootstrapBean, String deploymentUnitName) throws NamingException
+ {
+ Map<String, BeanManager> beanManagers = new DeploymentVisitor(bootstrapBean).visit().getBeanManagers();
+ Context context = beanManagerContext.createSubcontext(deploymentUnitName);
+
+ for (Map.Entry<String, BeanManager> entry : beanManagers.entrySet())
+ {
+ NonSerializableFactory.rebind(context, entry.getKey(), entry.getValue());
+ }
+ }
+
+ public void unbind(BootstrapBean bootstrapBean, String deploymentUnitName) throws NamingException
+ {
+ Map<String, BeanManager> beanManagers = new DeploymentVisitor(bootstrapBean).visit().getBeanManagers();
+ Context context = (Context)beanManagerContext.lookup(deploymentUnitName); // or how do I get existing sub-context
+
+ for (Map.Entry<String, BeanManager> entry : beanManagers.entrySet())
+ {
+ context.unbind(entry.getKey());
+ NonSerializableFactory.unbind(entry.getKey());
+ }
+
+ beanManagerContext.destroySubcontext(deploymentUnitName);
+ }
+
+ protected static class DeploymentVisitor
+ {
+ private final Map<String, BeanManager> beanManagers;
+ private final BootstrapBean bootstrapBean;
+
+ public DeploymentVisitor(BootstrapBean bootstrapBean)
+ {
+ this.beanManagers = new HashMap<String, BeanManager>();
+ this.bootstrapBean = bootstrapBean;
+ }
+
+ public Map<String, BeanManager> getBeanManagers()
+ {
+ return Collections.unmodifiableMap(beanManagers);
+ }
+
+ public DeploymentVisitor visit()
+ {
+ for (BeanDeploymentArchive bda : bootstrapBean.getDeployment().getBeanDeploymentArchives())
+ {
+ visit(bda, new HashSet<BeanDeploymentArchive>());
+ }
+ return this;
+ }
+
+ private void visit(BeanDeploymentArchive bda, Set<BeanDeploymentArchive> seenBdas)
+ {
+ WeldManager beanManager = bootstrapBean.getBootstrap().getManager(bda);
+ // BeanManager id is simply the BDA ID
+ String key = beanManager.getId();
+ beanManagers.put(key, beanManager);
+ for (BeanDeploymentArchive child : bda.getBeanDeploymentArchives())
+ {
+ if (!seenBdas.contains(child))
+ {
+ visit(child, seenBdas);
+ }
+ }
+ }
+
+ }
+
+ protected static void addNonSerializableFactory(Hashtable<String, String> environment)
+ {
+ String nonSerializableFactory = NonSerializableFactory.class.getName();
+ String objFactory = environment.get(Context.OBJECT_FACTORIES);
+ if (objFactory != null)
+ {
+ objFactory = nonSerializableFactory + ":" + objFactory;
+ }
+ else
+ {
+ objFactory = nonSerializableFactory;
+ }
+
+ environment.put(Context.OBJECT_FACTORIES, objFactory);
+ }
+}
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list