Author: dan.j.allen
Date: 2009-12-23 16:12:45 -0500 (Wed, 23 Dec 2009)
New Revision: 11877
Added:
modules/envconfig/
modules/envconfig/branches/
modules/envconfig/tags/
modules/envconfig/trunk/
modules/envconfig/trunk/pom.xml
modules/envconfig/trunk/readme.txt
modules/envconfig/trunk/src/
modules/envconfig/trunk/src/main/
modules/envconfig/trunk/src/main/java/
modules/envconfig/trunk/src/main/java/org/
modules/envconfig/trunk/src/main/java/org/jboss/
modules/envconfig/trunk/src/main/java/org/jboss/seam/
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/Bind.java
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/EnvironmentBinding.java
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/EnvironmentBindingExtension.java
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/JndiBinder.java
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/Placeholder.java
modules/envconfig/trunk/src/main/resources/
modules/envconfig/trunk/src/main/resources/META-INF/
modules/envconfig/trunk/src/main/resources/META-INF/beans.xml
modules/envconfig/trunk/src/main/resources/META-INF/services/
modules/envconfig/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Log:
add new module: envconfig
module driver: Matt Corey
Property changes on: modules/envconfig/trunk
___________________________________________________________________
Name: svn:ignore
+ target
.project
.classpath
.settings
Added: modules/envconfig/trunk/pom.xml
===================================================================
--- modules/envconfig/trunk/pom.xml (rev 0)
+++ modules/envconfig/trunk/pom.xml 2009-12-23 21:12:45 UTC (rev 11877)
@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.jboss.seam</groupId>
+ <artifactId>seam-parent</artifactId>
+ <version>3.0.0-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.jboss.seam</groupId>
+ <artifactId>seam-envconfig</artifactId>
+ <version>3.0.0-SNAPSHOT</version>
+ <packaging>ejb</packaging>
+
+ <name>Seam Environment Configuration</name>
+ <description>A set of CDI extensions that deal with configuration of the Java EE
environment (e.g., binding to JNDI)</description>
+ <inceptionYear>2009</inceptionYear>
+
+ <developers>
+ <developer>
+ <name>Matt Corey</name>
+ <url>http://smokeandice.blogspot.com</url>
+ <timezone>GMT-05:00</timezone>
+ <roles>
+ <role>Module driver</role>
+ </roles>
+ </developer>
+ <developer>
+ <id>dan.j.allen</id>
+ <name>Dan Allen</name>
+ <email>dan.j.allen(a)gmail.com</email>
+ <url>http://in.relation.to/Bloggers/Dan</url>
+ <organization>JBoss, by Red Hat</organization>
+ <timezone>GMT-05:00</timezone>
+ <roles>
+ <role>Developer</role>
+ </roles>
+ </developer>
+ </developers>
+
+ <properties>
+ <seam.version>3.0.0-SNAPSHOT</seam.version>
+ </properties>
+
+ <!-- Snapshots repo to get parent -->
+ <repositories>
+ <repository>
+ <
id>oss.sonatype.org/jboss-snapshots</id>
+ <name>JBoss (Nexus) Snapshots Repository</name>
+
<
url>http://oss.sonatype.org/content/repositories/jboss-snapshots</u...
+ <releases>
+ <enabled>false</enabled>
+ </releases>
+ <snapshots>
+ <enabled>true</enabled>
+ <updatePolicy>never</updatePolicy>
+ </snapshots>
+ </repository>
+ </repositories>
+
+ <dependencies>
+ <dependency>
+ <groupId>javax.enterprise</groupId>
+ <artifactId>cdi-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.ejb3</groupId>
+ <artifactId>jboss-ejb3-api</artifactId>
+ <scope>provided</scope>
+ <version>3.1.0</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-ejb-plugin</artifactId>
+ <configuration>
+ <ejbVersion>3.0</ejbVersion>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <scm>
+
<
connection>scm:svn:http://anonsvn.jboss.org/repos/seam/modules/envconf...
+
<
developerConnection>scm:svn:https://svn.jboss.org/repos/seam/modules/e...
+
<
url>http://fisheye.jboss.org/browse/Seam/modules/envconfig/trunk</u...
+ </scm>
+
+</project>
Added: modules/envconfig/trunk/readme.txt
===================================================================
--- modules/envconfig/trunk/readme.txt (rev 0)
+++ modules/envconfig/trunk/readme.txt 2009-12-23 21:12:45 UTC (rev 11877)
@@ -0,0 +1,21 @@
+Seam Environment Configuration
+==============================
+
+Example:
+
+The following bean will cause the string "Hello World!" to
+be bound to the JNDI name java:global/msg when the archive
+that contains this bean is deployed.
+
+import org.jboss.seam.envconfig.Bind;
+import org.jboss.seam.envconfig.EnvironmentBinding;
+
+public @EnvironmentBinding class EnvironmentVars
+{
+ @Bind(jndiAddress="msg") private final String msg = "Hello
World!";
+}
+
+You can then inject the value into a managed bean as follows:
+
+@Resource(lookup = "java:global/msg") String msg;
+
Added: modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/Bind.java
===================================================================
--- modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/Bind.java
(rev 0)
+++ modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/Bind.java 2009-12-23
21:12:45 UTC (rev 11877)
@@ -0,0 +1,33 @@
+/*
+ * Seam 3 Modules -
http://seamframework.org
+ * CDI portable extensions for Java EE and non-Java EE technologies
+ *
+ * JBoss, by Red Hat
+ *
+ * This library 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;
+ * version 2.1 of the License.
+ *
+ * This library 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.
+ */
+package org.jboss.seam.envconfig;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author Matt Corey
+ */
+@Target( { ElementType.METHOD, ElementType.FIELD })
+(a)Retention(RetentionPolicy.RUNTIME)
+public @interface Bind
+{
+ String jndiAddress();
+ boolean overwrite() default true;
+}
Added:
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/EnvironmentBinding.java
===================================================================
---
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/EnvironmentBinding.java
(rev 0)
+++
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/EnvironmentBinding.java 2009-12-23
21:12:45 UTC (rev 11877)
@@ -0,0 +1,36 @@
+/*
+ * Seam 3 Modules -
http://seamframework.org
+ * CDI portable extensions for Java EE and non-Java EE technologies
+ *
+ * JBoss, by Red Hat
+ *
+ * This library 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;
+ * version 2.1 of the License.
+ *
+ * This library 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.
+ */
+package org.jboss.seam.envconfig;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Stereotype;
+
+/**
+ * @author Matt Corey
+ */
+@Stereotype
+@ApplicationScoped
+(a)Target(ElementType.TYPE)
+(a)Retention(RetentionPolicy.RUNTIME)
+public @interface EnvironmentBinding
+{
+}
Added:
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/EnvironmentBindingExtension.java
===================================================================
---
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/EnvironmentBindingExtension.java
(rev 0)
+++
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/EnvironmentBindingExtension.java 2009-12-23
21:12:45 UTC (rev 11877)
@@ -0,0 +1,70 @@
+/*
+ * Seam 3 Modules -
http://seamframework.org
+ * CDI portable extensions for Java EE and non-Java EE technologies
+ *
+ * JBoss, by Red Hat
+ *
+ * This library 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;
+ * version 2.1 of the License.
+ *
+ * This library 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.
+ */
+package org.jboss.seam.envconfig.extension;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessBean;
+import org.jboss.seam.envconfig.EnvironmentBinding;
+
+/**
+ * @author Matt Corey
+ */
+public class EnvironmentBindingExtension implements Extension
+{
+ private Logger log = Logger.getLogger(getClass().getName());
+
+ private Set<Bean> envBeans = new HashSet<Bean>();
+ private BeanManager beanManager;
+
+ public void discoverEnvironmentBindingClasses(@Observes ProcessBean pb, BeanManager
bm) throws Exception
+ {
+ this.beanManager = bm;
+
+ Bean bean = pb.getBean();
+ Class beanClass = bean.getBeanClass();
+
+ Set<Class> sts = bean.getStereotypes();
+
+ for (Class st : sts)
+ {
+ if (st.equals(EnvironmentBinding.class))
+ {
+ log.info("Found class annotated with EnvironmentBinding: " +
beanClass.getName());
+ envBeans.add(bean);
+ break;
+ }
+ }
+ }
+
+ public Set<Bean> getEnvBeans()
+ {
+ return Collections.unmodifiableSet(envBeans);
+ }
+
+ public BeanManager getBeanManager()
+ {
+ return beanManager;
+ }
+}
Added:
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/JndiBinder.java
===================================================================
---
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/JndiBinder.java
(rev 0)
+++
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/JndiBinder.java 2009-12-23
21:12:45 UTC (rev 11877)
@@ -0,0 +1,93 @@
+/*
+ * Seam 3 Modules -
http://seamframework.org
+ * CDI portable extensions for Java EE and non-Java EE technologies
+ *
+ * JBoss, by Red Hat
+ *
+ * This library 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;
+ * version 2.1 of the License.
+ *
+ * This library 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.
+ */
+package org.jboss.seam.envconfig.extension;
+
+import java.lang.reflect.Field;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Startup;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.spi.Context;
+import javax.enterprise.inject.spi.Bean;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.jboss.seam.envconfig.Bind;
+
+/**
+ * @author Matt Corey
+ */
+public @Singleton @Startup class JndiBinder
+{
+ private Logger log = Logger.getLogger(getClass().getName());
+
+ @Inject
+ private EnvironmentBindingExtension bindingExtension;
+
+ @PostConstruct
+ public void processBindings() throws Exception
+ {
+ Set<Bean> envBeans = bindingExtension.getEnvBeans();
+
+ log.info("Processing EnvironmentBinding Classes: " + envBeans);
+
+ Context appContext =
bindingExtension.getBeanManager().getContext(ApplicationScoped.class);
+ for (Bean bean : envBeans)
+ {
+ Class beanClass = bean.getBeanClass();
+
+ Object beanInstance = appContext.get(bean,
bindingExtension.getBeanManager().createCreationalContext(bean));
+
+ Field[] fields = beanClass.getDeclaredFields();
+ for (Field field : fields)
+ {
+ if (field.isAnnotationPresent(Bind.class))
+ {
+ log.info("Processing Binding field " + beanClass.getName() +
"." + field.getName());
+
+ field.setAccessible(true);
+
+ String jndi = field.getAnnotation(Bind.class).jndiAddress();
+ Object val = field.get(beanInstance);
+
+ bindValue(jndi, val);
+ }
+ }
+ }
+ }
+
+ private void bindValue(String nameSuffix, Object value) throws NamingException
+ {
+ InitialContext ic = new InitialContext();
+
+ StringBuilder nameToBind = new StringBuilder();
+ if (!nameSuffix.matches("java:/?global"))
+ {
+ nameToBind.append("java:global");
+ if (!nameSuffix.endsWith("/"))
+ {
+ nameToBind.append('/');
+ }
+ }
+ nameToBind.append(nameSuffix);
+ log.info("Bound " + nameToBind + " to " + value);
+ ic.rebind(nameToBind.toString(), value);
+ }
+}
Added:
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/Placeholder.java
===================================================================
---
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/Placeholder.java
(rev 0)
+++
modules/envconfig/trunk/src/main/java/org/jboss/seam/envconfig/extension/Placeholder.java 2009-12-23
21:12:45 UTC (rev 11877)
@@ -0,0 +1,28 @@
+/*
+ * Seam 3 Modules -
http://seamframework.org
+ * CDI portable extensions for Java EE and non-Java EE technologies
+ *
+ * JBoss, by Red Hat
+ *
+ * This library 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;
+ * version 2.1 of the License.
+ *
+ * This library 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.
+ */
+package org.jboss.seam.envconfig.extension;
+
+import javax.ejb.Stateless;
+
+/**
+ * This is a temporary class that is present because GlassFish V3 doesn't seem
+ * to recognize an EJB-JAR archive as valid if it only has a @Singleton bean.
+ *
+ * @author Dan Alen
+ */
+public @Stateless class Placeholder {
+}
Added: modules/envconfig/trunk/src/main/resources/META-INF/beans.xml
===================================================================
Added:
modules/envconfig/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
===================================================================
---
modules/envconfig/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
(rev 0)
+++
modules/envconfig/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension 2009-12-23
21:12:45 UTC (rev 11877)
@@ -0,0 +1 @@
+org.jboss.seam.envconfig.extension.EnvironmentBindingExtension
\ No newline at end of file