[jboss-cvs] JBossAS SVN: r63595 - in projects/microcontainer/trunk/docs/examples/aspects/src: resources/META-INF and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jun 21 09:40:51 EDT 2007


Author: alesj
Date: 2007-06-21 09:40:51 -0400 (Thu, 21 Jun 2007)
New Revision: 63595

Added:
   projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/AnnotatedSimpleBean.java
   projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/EnvInitialContextFactory.java
   projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/MockJndiProvider.java
   projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/MockNameParser.java
   projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/SimpleBean.java
   projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/SimpleBeanLookup.java
Modified:
   projects/microcontainer/trunk/docs/examples/aspects/src/resources/META-INF/jboss-beans.xml
Log:
Aspect example with JNDI binding.

Added: projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/AnnotatedSimpleBean.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/AnnotatedSimpleBean.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/AnnotatedSimpleBean.java	2007-06-21 13:40:51 UTC (rev 63595)
@@ -0,0 +1,38 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.example.microcontainer.aspects;
+
+import java.io.Serializable;
+
+import org.jboss.aop.microcontainer.aspects.jndi.JndiBinding;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+ at JndiBinding(name = "beans/AnnotatedSimpleBean")
+public class AnnotatedSimpleBean implements Serializable
+{
+   public String toString()
+   {
+      return "I'm annotated SimpleBean.";
+   }
+}

Added: projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/EnvInitialContextFactory.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/EnvInitialContextFactory.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/EnvInitialContextFactory.java	2007-06-21 13:40:51 UTC (rev 63595)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt 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.example.microcontainer.aspects;
+
+import java.util.Hashtable;
+import java.util.List;
+import javax.management.MBeanServerFactory;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.naming.spi.InitialContextFactory;
+
+/**
+ * We check if we are in AS or standalone.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class EnvInitialContextFactory implements InitialContextFactory
+{
+   public Context getInitialContext(Hashtable<?, ?> env) throws NamingException
+   {
+      return isApplicationServer() ? new InitialContext(env) : new MockJndiProvider(env);
+   }
+
+   protected boolean isApplicationServer()
+   {
+      List servers = MBeanServerFactory.findMBeanServer("jboss");
+      return servers != null && servers.isEmpty() == false;
+   }
+}

Added: projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/MockJndiProvider.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/MockJndiProvider.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/MockJndiProvider.java	2007-06-21 13:40:51 UTC (rev 63595)
@@ -0,0 +1,217 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt 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.example.microcontainer.aspects;
+
+import java.io.IOException;
+import java.rmi.MarshalledObject;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import javax.naming.Binding;
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+/**
+ * @author Scott.Stark at jboss.org
+ * @version $Revision:$
+ */
+public class MockJndiProvider implements Context
+{
+   private static Map<Object, Object> bindings = new ConcurrentHashMap<Object, Object>();
+   private Hashtable<?, ?> env;
+
+   MockJndiProvider(Hashtable<?, ?> env)
+   {
+      this.env = env;
+   }
+
+   public Object addToEnvironment(String propName, Object propVal) throws NamingException
+   {
+      return null;
+   }
+
+   public void bind(Name name, Object obj) throws NamingException
+   {
+      String sname = name.toString();
+      bind(sname, obj);
+   }
+
+   public void bind(String name, Object obj) throws NamingException
+   {
+      try
+      {
+         MarshalledObject mo = new MarshalledObject(obj);
+         bindings.put(name, mo);
+      }
+      catch(IOException e)
+      {
+         NamingException ex = new NamingException("Failed to bind name: "+name);
+         ex.setRootCause(e);
+         throw ex;
+      }
+   }
+
+   public void close() throws NamingException
+   {
+   }
+
+   public Name composeName(Name name, Name prefix) throws NamingException
+   {
+      return null;
+   }
+
+   public String composeName(String name, String prefix) throws NamingException
+   {
+      return null;
+   }
+
+   public Context createSubcontext(Name name) throws NamingException
+   {
+      return null;
+   }
+
+   public Context createSubcontext(String name) throws NamingException
+   {
+      return null;
+   }
+
+   public void destroySubcontext(Name name) throws NamingException
+   {
+   }
+
+   public void destroySubcontext(String name) throws NamingException
+   {
+
+   }
+
+   public Hashtable<?, ?> getEnvironment() throws NamingException
+   {
+      return env;
+   }
+
+   public String getNameInNamespace() throws NamingException
+   {
+      return null;
+   }
+
+   public NameParser getNameParser(Name name) throws NamingException
+   {
+      return new MockNameParser();
+   }
+
+   public NameParser getNameParser(String name) throws NamingException
+   {
+      return new MockNameParser();
+   }
+
+   public NamingEnumeration<NameClassPair> list(Name name) throws NamingException
+   {
+      return null;
+   }
+
+   public NamingEnumeration<NameClassPair> list(String name) throws NamingException
+   {
+      return null;
+   }
+
+   public NamingEnumeration<Binding> listBindings(Name name) throws NamingException
+   {
+      return null;
+   }
+
+   public NamingEnumeration<Binding> listBindings(String name) throws NamingException
+   {
+      return null;
+   }
+
+   public Object lookup(Name name) throws NamingException
+   {
+      String sname = name.toString();
+      return lookup(sname);
+   }
+
+   public Object lookup(String name) throws NamingException
+   {
+      MarshalledObject mo = (MarshalledObject) bindings.get(name);
+      Object value = null;
+      if( mo != null )
+      {
+         try
+         {
+            value = mo.get();
+         }
+         catch(Exception e)
+         {
+            NamingException ex = new NamingException();
+            ex.setRootCause(e);
+            throw ex;
+         }
+      }
+      return value;
+   }
+
+   public Object lookupLink(Name name) throws NamingException
+   {
+      return null;
+   }
+
+   public Object lookupLink(String name) throws NamingException
+   {
+      return null;
+   }
+
+   public void rebind(Name name, Object obj) throws NamingException
+   {
+   }
+
+   public void rebind(String name, Object obj) throws NamingException
+   {
+      bindings.put(name, obj);
+   }
+
+   public Object removeFromEnvironment(String propName) throws NamingException
+   {
+      return null;
+   }
+
+   public void rename(Name oldName, Name newName) throws NamingException
+   {
+   }
+
+   public void rename(String oldName, String newName) throws NamingException
+   {
+   }
+
+   public void unbind(Name name) throws NamingException
+   {
+      unbind(name.toString());
+   }
+
+   public void unbind(String name) throws NamingException
+   {
+      bindings.remove(name);
+   }
+}

Added: projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/MockNameParser.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/MockNameParser.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/MockNameParser.java	2007-06-21 13:40:51 UTC (rev 63595)
@@ -0,0 +1,22 @@
+package org.jboss.example.microcontainer.aspects;
+
+import java.util.Properties;
+import javax.naming.CompoundName;
+import javax.naming.Name;
+import javax.naming.NameParser;
+import javax.naming.NamingException;
+
+public class MockNameParser implements NameParser
+{
+   private static final Properties syntax = new Properties();
+
+   static
+   {
+      syntax.setProperty("jndi.syntax.direction", "flat");
+   }
+
+   public Name parse(String name) throws NamingException
+   {
+      return new CompoundName(name, syntax);
+   }
+}

Added: projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/SimpleBean.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/SimpleBean.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/SimpleBean.java	2007-06-21 13:40:51 UTC (rev 63595)
@@ -0,0 +1,35 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.example.microcontainer.aspects;
+
+import java.io.Serializable;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class SimpleBean implements Serializable
+{
+   public String toString()
+   {
+      return "I'm plain SimpleBean.";
+   }
+}

Added: projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/SimpleBeanLookup.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/SimpleBeanLookup.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/aspects/src/main/org/jboss/example/microcontainer/aspects/SimpleBeanLookup.java	2007-06-21 13:40:51 UTC (rev 63595)
@@ -0,0 +1,52 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.example.microcontainer.aspects;
+
+import java.util.Properties;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class SimpleBeanLookup
+{
+   private Properties env;
+
+   public String getName()
+   {
+      return getClass().getName();
+   }
+
+   public void setEnv(Properties env)
+   {
+      this.env = env;
+   }
+
+   public void start() throws Exception
+   {
+      Context context = new InitialContext(env);
+      System.out.println(context.lookup("beans/SimpleBean"));
+      System.out.println(context.lookup("beans/AnnotatedSimpleBean"));
+      System.out.println(context.lookup("beans/XmlAnnotatedSimpleBean"));
+   }
+}

Modified: projects/microcontainer/trunk/docs/examples/aspects/src/resources/META-INF/jboss-beans.xml
===================================================================
--- projects/microcontainer/trunk/docs/examples/aspects/src/resources/META-INF/jboss-beans.xml	2007-06-21 13:19:48 UTC (rev 63594)
+++ projects/microcontainer/trunk/docs/examples/aspects/src/resources/META-INF/jboss-beans.xml	2007-06-21 13:40:51 UTC (rev 63595)
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+   <bean name="Repository" class="org.jboss.metadata.plugins.repository.basic.BasicMetaDataRepository"/>
+
+   <bean name="AspectManager" class="org.jboss.aop.AspectManager">
+      <constructor factoryClass="org.jboss.aop.AspectManager" factoryMethod="instance"/>
+   </bean>
+
+	<!-- Define the jndi binding advice -->
+   <beanfactory name="JndiAdvice" class="org.jboss.aop.microcontainer.aspects.jndi.JndiIntroduction">
+   	<property name="env">
+           <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+              <entry>
+                 <key>java.naming.factory.initial</key>
+                 <value>org.jboss.example.microcontainer.aspects.EnvInitialContextFactory</value>
+              </entry>
+           </map>
+   	</property>
+   </beanfactory>
+
+   <bean name="JndiAspect" class="org.jboss.aop.microcontainer.beans.Aspect">
+      <property name="advice"><inject bean="JndiAdvice"/></property>
+      <property name="manager"><inject bean="AspectManager"/></property>
+   </bean>
+
+   <!-- Introduce the KernelControllerContextAware interface to the JndiBinding annotation -->
+   <bean name="JndiIntroduction" class="org.jboss.aop.microcontainer.beans.IntroductionBinding">
+      <property name="classes">@org.jboss.aop.microcontainer.aspects.jndi.JndiBinding</property>
+      <property name="interfaces">
+         <list elementClass="java.lang.String">
+            <value>org.jboss.kernel.spi.dependency.KernelControllerContextAware</value>
+         </list>
+      </property>
+      <property name="manager"><inject bean="AspectManager"/></property>
+   </bean>
+
+   <!-- Apply the jndi binding to all KernelControllerContextAware interface callbacks -->
+   <bean name="JndiBinding" class="org.jboss.aop.microcontainer.beans.AspectBinding">
+      <property name="pointcut">execution(* $instanceof{org.jboss.kernel.spi.dependency.KernelControllerContextAware}->$implements{org.jboss.kernel.spi.dependency.KernelControllerContextAware}(..))</property>
+      <property name="aspect"><inject bean="JndiAspect" property="definition"/></property>
+      <property name="manager"><inject bean="AspectManager"/></property>
+   </bean>
+
+   <bean name="Bean" class="org.jboss.example.microcontainer.aspects.SimpleBean">
+      <annotation>@org.jboss.aop.microcontainer.aspects.jndi.JndiBinding(name="beans/SimpleBean", aliases={"beans/XmlAnnotatedSimpleBean"})</annotation>
+   </bean>
+
+   <bean name="AnnotatedBean" class="org.jboss.example.microcontainer.aspects.AnnotatedSimpleBean" />
+
+   <bean name="lookup" class="org.jboss.example.microcontainer.aspects.SimpleBeanLookup">
+      <property name="env">
+           <map class="java.util.Properties" keyClass="java.lang.String" valueClass="java.lang.String">
+              <entry>
+                 <key>java.naming.factory.initial</key>
+                 <value>org.jboss.example.microcontainer.aspects.EnvInitialContextFactory</value>
+              </entry>
+           </map>
+      </property>
+      <demand>JndiBinding</demand>
+   </bean>
+
+</deployment>




More information about the jboss-cvs-commits mailing list