[jboss-osgi-commits] JBoss-OSGI SVN: r88709 - in projects/jboss-osgi/trunk: testsuite and 5 other directories.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Tue May 12 04:57:29 EDT 2009


Author: thomas.diesler at jboss.com
Date: 2009-05-12 04:57:28 -0400 (Tue, 12 May 2009)
New Revision: 88709

Added:
   projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/NamingServiceFactory.java
Removed:
   projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/example/
Modified:
   projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/JNPServer.java
   projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/NamingServiceImpl.java
   projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/ServiceActivator.java
   projects/jboss-osgi/trunk/testsuite/pom.xml
   projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/frontend/HttpRenderServlet.java
   projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/frontend/RequestHandler.java
   projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/shop/sports/ClimbingStore.java
   projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/types/Product.java
   projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/resources/trailblazer/shop-sports.bnd
Log:
Trailblazer sports store uses NamingService

Modified: projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/JNPServer.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/JNPServer.java	2009-05-12 08:49:18 UTC (rev 88708)
+++ projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/JNPServer.java	2009-05-12 08:57:28 UTC (rev 88709)
@@ -36,18 +36,18 @@
 import org.osgi.service.log.LogService;
 
 /**
- * A Service Activator that activates the JNP Server
+ * Start/Stop the {@link NamingServer}
  * 
  * @author thomas.diesler at jboss.com
  * @since 24-Apr-2009
  */
-public class JNPServer
+public class JNPServer 
 {
    private LogService log;
    private Main namingMain;
    private Naming namingServer;
 
-   public JNPServer(BundleContext context, String jndiHost, int jndiPort, int jndiRmiPort)
+   public JNPServer(BundleContext context, String jndiHost, int jndiPort, int jndiRmiPort) 
    {
       this.log = new LogServiceTracker(context);
 

Added: projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/NamingServiceFactory.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/NamingServiceFactory.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/NamingServiceFactory.java	2009-05-12 08:57:28 UTC (rev 88709)
@@ -0,0 +1,83 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.osgi.jndi.internal;
+
+//$Id$
+
+import java.util.Hashtable;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.naming.NamingContextFactory;
+import org.jboss.osgi.jndi.NamingService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * A {@link ServiceFactory} for the {@link NamingService}
+ * 
+ * @author thomas.diesler at jboss.com
+ * @since 24-Apr-2009
+ */
+public class NamingServiceFactory implements ServiceFactory
+{
+   private String jndiHost, jndiPort;
+
+   public NamingServiceFactory(String jndiHost, String jndiPort)
+   {
+      this.jndiHost = jndiHost;
+      this.jndiPort = jndiPort;
+   }
+
+   @SuppressWarnings("unchecked")
+   public Object getService(Bundle bundle, ServiceRegistration registration)
+   {
+      ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
+      try
+      {
+         Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+         
+         Hashtable env = new Hashtable();
+         env.put("java.naming.factory.initial", NamingContextFactory.class.getName());
+         env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
+         env.put("java.naming.provider.url", "jnp://" + jndiHost + ":" + jndiPort);
+
+         InitialContext iniCtx = new InitialContext(env);
+         return new NamingServiceImpl(iniCtx);
+      }
+      catch (NamingException ex)
+      {
+         throw new IllegalStateException("Cannot get the InitialContext", ex);
+      }
+      finally
+      {
+         Thread.currentThread().setContextClassLoader(ctxLoader);
+      }
+   }
+
+   public void ungetService(Bundle bundle, ServiceRegistration registration, Object service)
+   {
+      // nothing to do
+   }
+}
\ No newline at end of file


Property changes on: projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/NamingServiceFactory.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/NamingServiceImpl.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/NamingServiceImpl.java	2009-05-12 08:49:18 UTC (rev 88708)
+++ projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/NamingServiceImpl.java	2009-05-12 08:57:28 UTC (rev 88709)
@@ -23,52 +23,28 @@
 
 //$Id$
 
-import java.util.Hashtable;
-
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 
 import org.jboss.osgi.jndi.NamingService;
 
 /**
- * A Service Activator that activates the JNP Server
+ * An implementation of the {@link NamingService}
  * 
  * @author thomas.diesler at jboss.com
  * @since 24-Apr-2009
  */
 public class NamingServiceImpl implements NamingService
 {
-   private String jndiHost, jndiPort;
    private InitialContext initialContext;
-   
-   public NamingServiceImpl(String jndiHost, String jndiPort)
+
+   public NamingServiceImpl(InitialContext initialContext)
    {
-      this.jndiHost = jndiHost;
-      this.jndiPort = jndiPort;
+      this.initialContext = initialContext;
    }
 
-   @SuppressWarnings("unchecked")
    public InitialContext getInitialContext() throws NamingException
    {
-      if (initialContext == null)
-      {
-         ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
-         try
-         {
-            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
-            
-            Hashtable env = new Hashtable();
-            env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
-            env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
-            env.put("java.naming.provider.url", "jnp://" + jndiHost + ":" + jndiPort);
-            
-            initialContext = new InitialContext(env);
-         }
-         finally
-         {
-            Thread.currentThread().setContextClassLoader(ctxLoader);
-         }
-      }
       return initialContext;
    }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/ServiceActivator.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/ServiceActivator.java	2009-05-12 08:49:18 UTC (rev 88708)
+++ projects/jboss-osgi/trunk/bundle/jndi/src/main/java/org/jboss/osgi/jndi/internal/ServiceActivator.java	2009-05-12 08:57:28 UTC (rev 88709)
@@ -63,7 +63,7 @@
       jnpServer = new JNPServer(context, jndiHost, Integer.parseInt(jndiPort), Integer.parseInt(jndiRmiPort));
       jnpServer.start();
       
-      context.registerService(NamingService.class.getName(), new NamingServiceImpl(jndiHost, jndiPort), null);
+      context.registerService(NamingService.class.getName(), new NamingServiceFactory(jndiHost, jndiPort), null);
       log.log(LogService.LOG_DEBUG, "NamingService registered");
    }
 

Modified: projects/jboss-osgi/trunk/testsuite/pom.xml
===================================================================
--- projects/jboss-osgi/trunk/testsuite/pom.xml	2009-05-12 08:49:18 UTC (rev 88708)
+++ projects/jboss-osgi/trunk/testsuite/pom.xml	2009-05-12 08:57:28 UTC (rev 88709)
@@ -16,6 +16,7 @@
 
   <modules>
     <module>example</module>
+    <module>trailblazer</module>
     <module>functional</module>
   </modules>
   

Modified: projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/frontend/HttpRenderServlet.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/frontend/HttpRenderServlet.java	2009-05-12 08:49:18 UTC (rev 88708)
+++ projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/frontend/HttpRenderServlet.java	2009-05-12 08:57:28 UTC (rev 88709)
@@ -88,8 +88,6 @@
       if (path == null)
          path = "/";
 
-      log.log(LogService.LOG_INFO, "doGet "+ path);
-      
       renderHeader(req, res);
       
       if (mallService != null)

Modified: projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/frontend/RequestHandler.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/frontend/RequestHandler.java	2009-05-12 08:49:18 UTC (rev 88708)
+++ projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/frontend/RequestHandler.java	2009-05-12 08:57:28 UTC (rev 88709)
@@ -68,7 +68,7 @@
       }
       else
       {
-         out.println("No active shops available, please try later.");
+         out.println("No active shops, please try later.");
       }
    }
 
@@ -98,7 +98,8 @@
    public void renderConfirmPage(HttpServletRequest req, HttpServletResponse res) throws IOException
    {
       PrintWriter out = res.getWriter();
-      out.println("Thanks for shopping with us");
+      Shop shop = getShopFromRequestParam(req);
+      out.println("Thanks for shopping with " + shop.getName());
    }
 
    public void renderCartPage(HttpServletRequest req, HttpServletResponse res) throws IOException
@@ -154,8 +155,6 @@
             out.println("Payment service not available, please try later.");
          }
       }
-      out.println("<p/>");
-
    }
 
    public void actionAddToCart(HttpServletRequest req, HttpServletResponse res)

Modified: projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/shop/sports/ClimbingStore.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/shop/sports/ClimbingStore.java	2009-05-12 08:49:18 UTC (rev 88708)
+++ projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/shop/sports/ClimbingStore.java	2009-05-12 08:57:28 UTC (rev 88709)
@@ -23,21 +23,24 @@
 
 //$Id$
 
+import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
 
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+import org.jboss.osgi.jndi.NamingService;
 import org.jboss.test.osgi.trailblazer.PaymentService;
 import org.jboss.test.osgi.trailblazer.Shop;
 import org.jboss.test.osgi.trailblazer.types.Product;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 
-
-
 /**
- * [TODO]
+ * this climbing store uses JNDI to bind product information.
  * 
  * @author thomas.diesler at jboss.com
  * @since 10-May-2009
@@ -45,17 +48,29 @@
 public class ClimbingStore implements Shop
 {
    private BundleContext context;
-   private Map<String, Product> products = new LinkedHashMap<String, Product>();
    
    public ClimbingStore(BundleContext context)
    {
       this.context = context;
-      add(new Product(this, "Shoes", 9500));
-      add(new Product(this, "Chalk", 150));
-      add(new Product(this, "Harness", 7500));
-      add(new Product(this, "Rope", 13500));
-      add(new Product(this, "Pants", 7500));
-      add(new Product(this, "Shirt", 3500));
+      
+      NamingService namingService = getNamingService(context);
+      try
+      {
+         Context ctx = namingService.getInitialContext();
+         ctx = ctx.createSubcontext("shop");
+         ctx = ctx.createSubcontext("climbing");
+         ctx = ctx.createSubcontext("products");
+         add(ctx, new Product(null, "Shoes", 9500));
+         add(ctx, new Product(null, "Chalk", 150));
+         add(ctx, new Product(null, "Harness", 7500));
+         add(ctx, new Product(null, "Rope", 13500));
+         add(ctx, new Product(null, "Pants", 7500));
+         add(ctx, new Product(null, "Shirt", 3500));
+      }
+      catch (NamingException ex)
+      {
+         throw new IllegalStateException("Cannot bind products", ex);
+      }
    }
 
    public String getName()
@@ -63,25 +78,72 @@
       return "ClimbingStore";
    }
    
+   public PaymentService getPaymentService()
+   {
+      ServiceReference sref = context.getServiceReference(PaymentService.class.getName());
+      return sref != null ? (PaymentService)context.getService(sref) : null;
+   }
+   
    public Collection<Product> getProductList()
    {
-      return Collections.unmodifiableCollection(products.values());
+      Collection<Product> products = new ArrayList<Product>();
+      try
+      {
+         NamingService namingService = getNamingService(context);
+         InitialContext iniCtx = namingService.getInitialContext();
+         
+         NamingEnumeration<NameClassPair> list = iniCtx.list("shop/climbing/products");
+         while (list.hasMoreElements())
+         {
+            NameClassPair pair = list.nextElement();
+            String name = pair.getName();
+            products.add(getProductByName(name));
+         }
+      }
+      catch (NamingException ex)
+      {
+         throw new IllegalStateException("Cannot lookup products", ex);
+      }
+      return products;
    }
 
    public Product getProductByName(String name)
    {
-      return products.get(name);
+      Product product;
+      
+      ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
+      try
+      {
+         Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+         
+         NamingService namingService = getNamingService(context);
+         InitialContext iniCtx = namingService.getInitialContext();
+         product = (Product)iniCtx.lookup("shop/climbing/products/" + name);
+         product.setShop(this);
+      }
+      catch (NamingException ex)
+      {
+         throw new IllegalStateException("Cannot lookup product: " + name, ex);
+      }
+      finally
+      {
+         Thread.currentThread().setContextClassLoader(ctxLoader);
+      }
+      return product;
    }
 
-   private void add(Product p)
+   private void add(Context ctx, Product p) throws NamingException
    {
-      products.put(p.getName(), p);
+      ctx.bind(p.getName(), p);
    }
-   
-   public PaymentService getPaymentService()
+
+   private NamingService getNamingService(BundleContext context)
    {
-      ServiceReference sref = context.getServiceReference(PaymentService.class.getName());
-      return sref != null ? (PaymentService)context.getService(sref) : null;
+      ServiceReference sref = context.getServiceReference(NamingService.class.getName());
+      if (sref == null)
+         throw new IllegalStateException("Cannot access the NamingService");
+      
+      NamingService namingService = (NamingService)context.getService(sref);
+      return namingService;
    }
-
 }

Modified: projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/types/Product.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/types/Product.java	2009-05-12 08:49:18 UTC (rev 88708)
+++ projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/java/org/jboss/test/osgi/trailblazer/types/Product.java	2009-05-12 08:57:28 UTC (rev 88709)
@@ -21,6 +21,8 @@
  */
 package org.jboss.test.osgi.trailblazer.types;
 
+import java.io.Serializable;
+
 import org.jboss.test.osgi.trailblazer.Shop;
 
 //$Id$
@@ -32,8 +34,10 @@
  * @author thomas.diesler at jboss.com
  * @since 10-May-2009
  */
-public class Product
+public class Product implements Serializable
 {
+   private static final long serialVersionUID = 1L;
+   
    private String name;
    private Integer price;
    private Shop shop;
@@ -45,11 +49,23 @@
       this.price = price;
    }
 
+   public Product(String name, Integer price)
+   {
+      this.name = name;
+      this.price = price;
+   }
+
    public Shop getShop()
    {
       return shop;
    }
 
+   
+   public void setShop(Shop shop)
+   {
+      this.shop = shop;
+   }
+
    public String getName()
    {
       return name;

Modified: projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/resources/trailblazer/shop-sports.bnd
===================================================================
--- projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/resources/trailblazer/shop-sports.bnd	2009-05-12 08:49:18 UTC (rev 88708)
+++ projects/jboss-osgi/trunk/testsuite/trailblazer/src/test/resources/trailblazer/shop-sports.bnd	2009-05-12 08:57:28 UTC (rev 88709)
@@ -4,6 +4,8 @@
 Bundle-Activator: org.jboss.test.osgi.trailblazer.shop.sports.ServiceActivator
 Private-Package: org.jboss.test.osgi.trailblazer.shop.sports
 Import-Package: \
+	javax.naming, \
+	org.jboss.osgi.jndi, \
 	org.osgi.framework, \
 	org.osgi.service.log, \
 	org.jboss.osgi.common.log, \




More information about the jboss-osgi-commits mailing list