Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

JavaSpaces Troubleshooting

Thread view: 
Vivek! - 26 Apr 2006 16:09 GMT
 Dear all,

     M new to Javaspaces, and having a tough time running my first
JavaSpace program.
Everything runs fine, but the program is not able to locate the
javaspace.

  Suggestions welcomed!! :-)
Patrick May - 27 Apr 2006 17:14 GMT
>       M new to Javaspaces, and having a tough time running my first
> JavaSpace program.
> Everything runs fine, but the program is not able to locate the
> javaspace.
>
>    Suggestions welcomed!! :-)

    Here's a class I use for finding JavaSpaces:

/**
* JavaSpaceCache is a class that caches references to JavaSpace
* instances available from all reachable lookup service.
* <br>
* This source code is copyright 2006 by S P Engineering, Inc.
* Permission is granted for non-commercial use.
*
* @author Patrick May (pjm@spe.com)
* @author &copy; 2006 S P Engineering, Inc.  All rights reserved.
* @version 1
*/

package com.spe.jini.util;

import java.util.logging.Logger;

import net.jini.space.JavaSpace;

public class JavaSpaceCache
{
 private static Logger logger_
   = Logger.getLogger("com.spe.jini.util.JavaSpaceCache");

 private ServiceCache serviceCache_ = null;

 /**
  * The full constructor for the JavaSpaceCache class.
  *
  * @param groups The names of the Jini groups to which JavaSpace
  *               instances must belong.
  */
 public JavaSpaceCache(String[] groups)
   {
   serviceCache_ = new ServiceCache(groups,JavaSpace.class);
   }

 /**
  * The default constructor for the JavaSpaceCache class.
  */
 public JavaSpaceCache()
   {
   this(new String[] { "" });  // default to public group
   }

 /**
  * Return a JavaSpace from the cache.
  */
 public JavaSpace javaSpace()
   {
   JavaSpace javaSpace = (JavaSpace)serviceCache_.service();

   if (javaSpace == null)
     logger_.info("No JavaSpace found.");
   else
     logger_.info("JavaSpace found.");

   return javaSpace;
   }

 /**
  * A test harness for the JavaSpaceCache class.
  *
  * @param args The command line arguments passed in.
  */
 public static void main(String args[])
   {
   System.setSecurityManager(new java.rmi.RMISecurityManager());

   JavaSpaceCache cache = new JavaSpaceCache();
   JavaSpace space = null;
   while (space == null)
     {
     space = cache.javaSpace();
     if (space == null)
       {
       try { Thread.currentThread().sleep(5000); }
       catch (Exception e) { }
       }
     }
   }
}  // end JavaSpaceCache

JavaSpaceCache depends on ServiceCache for most of its functionality:

/**
* ServiceCache is a class that maintains a cache of Jini services.  Both
* lookup services discovered dynamically via multicast and those
* statically specified lookup services accessed via unicast can be used
* as sources of services.
* <br>
* A security manager such as RMISecurityManager must be in effect for
* this class to work.
* <br>
* This class is intended to be immutable, hence the lack of methods to
* change groups, locators, and service templates.
* <br>
* This source code is copyright 2006 by S P Engineering, Inc.
* Permission is granted for non-commercial use.
*
* @author Patrick May (pjm@spe.com)
* @author &copy; 2006 S P Engineering, Inc.  All rights reserved.
* @version 1
*/

package com.spe.jini.util;

import java.util.logging.Logger;

import net.jini.discovery.LookupDiscoveryManager;
import net.jini.discovery.DiscoveryGroupManagement;
import net.jini.discovery.DiscoveryListener;
import net.jini.discovery.DiscoveryEvent;
import net.jini.lookup.ServiceDiscoveryListener;
import net.jini.lookup.ServiceDiscoveryManager;
import net.jini.lookup.LookupCache;
import net.jini.lookup.ServiceItemFilter;
import net.jini.lookup.ServiceDiscoveryEvent;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.core.lookup.ServiceItem;
import net.jini.lease.LeaseRenewalManager;

public class ServiceCache
 implements DiscoveryListener, ServiceDiscoveryListener
{
 private static Logger logger_
   = Logger.getLogger("com.spe.jini.util.ServiceCache");

 private LookupDiscoveryManager lookupDiscoveryManager_ = null;
 private LeaseRenewalManager leaseRenewalManager_ = new LeaseRenewalManager();
 private ServiceDiscoveryManager serviceDiscoveryManager_ = null;
 private LookupCache lookupCache_ = null;

 /**
  * The full constructor for the ServiceCache class.
  *
  * @param groups The names of the Jini groups to which Lookup Services
  *               must belong.
  * @param locators Jini unicast Lookup Service locators.
  * @param template The ServiceTemplate that specifies the services of
  *                 interest.
  * @param filter The ServiceItemFilter that further specifies the
  *               services of interest.
  */
 public ServiceCache(String[] groups,
                     LookupLocator[] locators,
                     ServiceTemplate template,
                     ServiceItemFilter filter)
   {
   try
     {
     lookupDiscoveryManager_ = new LookupDiscoveryManager(groups,
                                                          locators,
                                                          this);
     serviceDiscoveryManager_
       = new ServiceDiscoveryManager(lookupDiscoveryManager_,
                                     leaseRenewalManager_);
     lookupCache_ = serviceDiscoveryManager_.createLookupCache(template,
                                                               filter,
                                                               this);

     if (groups != null)
       for (String group : groups)
         logger_.info("Group = " + group);
     else
       logger_.info("No groups found.");
     }
   catch (Exception e)
     {
     logger_.severe(e.getMessage());
     throw new JiniUtilityException(e);
     }
   }

 /**
  * A convenience constructor for the ServiceCache class.
  *
  * @param template The ServiceTemplate that specifies the services of
  *                 interest.
  */
 public ServiceCache(ServiceTemplate template)
   {
   this(DiscoveryGroupManagement.ALL_GROUPS,  // all groups, via multicast
        null,                                 // no unicast lookup servers
        template,
        null);                                // no service filter
   }

 /**
  * A convenience constructor for the ServiceCache class.
  *
  * @param groups The names of the Jini groups to which Lookup Services
  *               must belong.
  * @param serviceClass The class of the service of interest.
  */
 public ServiceCache(String[] groups,Class serviceClass)
   {
   this(groups,
        null,                                 // no unicast lookup servers
        new ServiceTemplate(null,new Class[] { serviceClass },null),
        null);                                // no service filter
   }

 /**
  * A convenience constructor for the ServiceCache class.
  *
  * @param serviceClass The class of the service of interest.
  */
 public ServiceCache(Class serviceClass)
   {
   this(DiscoveryGroupManagement.ALL_GROUPS,  // all groups, via multicast
        null,                                 // no unicast lookup servers
        new ServiceTemplate(null,new Class[] { serviceClass },null),
        null);                                // no service filter
   }

 /**
  * The default constructor for the ServiceCache class.
  */
 public ServiceCache()
   {
   this(DiscoveryGroupManagement.ALL_GROUPS,  // all groups, via multicast
        null,                                 // no unicast lookup servers
        new ServiceTemplate(null,null,null),  // all services
        null);                                // no service filter
   }

 /**
  * Called when a new lookup service is found.  Inherited from
  * DiscoveryListener.
  */
 public void discovered(DiscoveryEvent event)
   {
   logger_.info("Discovery event received.");

   ServiceTemplate template = new ServiceTemplate(null,null,null);
   ServiceRegistrar[] registrars = event.getRegistrars();

   for (ServiceRegistrar registrar : registrars)
     {
     try
       {
       logger_.info("Registrar discovered on "
                    + registrar.getLocator().getHost()
                    + ":"
                    + registrar.getLocator().getPort()
                    + ".");
       }
     catch (Exception e)
       { logger_.warning(e.getMessage()); }
     }
   }

 /**
  * Called when a lookup service is discarded.  Inherited from
  * DiscoveryListener.
  */
 public void discarded(DiscoveryEvent event) { }

 /**
  * Called when a service of interest is registered with one of the
  * cached lookup services.  Inherited from ServiceDiscoveryListener.
  */
 public void serviceAdded(ServiceDiscoveryEvent event)
   {
   ServiceItem item = event.getPostEventServiceItem();

   logger_.info("Service discovery event received:  "
                + item.service.getClass().getName()
                + "("
                + item.serviceID.toString()
                + ")");
   }

 /**
  * Called when a service of interest is changed within one of the
  * cached lookup services.  Inherited from ServiceDiscoveryListener.
  */
 public void serviceChanged(ServiceDiscoveryEvent event)
   {
   ServiceItem preEventItem = event.getPreEventServiceItem();
   ServiceItem postEventItem = event.getPostEventServiceItem();

   logger_.info("Service change event received:  "
                + preEventItem.service.getClass().getName()
                + "("
                + preEventItem.serviceID.toString()
                + ") to "
                + postEventItem.service.getClass().getName()
                + "("
                + postEventItem.serviceID.toString()
                + ")");
   }

 /**
  * Called when a service of interest is removed from one of the
  * cached lookup services.  Inherited from ServiceDiscoveryListener.
  */
 public void serviceRemoved(ServiceDiscoveryEvent event)
   {
   ServiceItem item = event.getPreEventServiceItem();

   logger_.info("Service removed event received:  "
                + item.service.getClass().getName()
                + "("
                + item.serviceID.toString()
                + ")");
   }

 /**
  * Find a service that passes the specified filter.
  *
  * @param filter The filter to apply to the services in the cache.
  */
 public Object service(ServiceItemFilter filter)
   {
   ServiceItem item = lookupCache_.lookup(filter);

   return (item == null) ? null : item.service;
   }

 /**
  * Find a service with the specified class.
  *
  * @param serviceClass The class of the service to find.
  */
 public Object service(final Class serviceClass)
   {
   return service(new ServiceItemFilter()
                    {
                    public boolean check(ServiceItem item)
                      {
                      return (item.service.getClass() == serviceClass);
                      }
                    });
   }

 /**
  * Find any service.  This is only useful if the ServiceCache instance
  * has been configured to only cache services of a single type.
  */
 public Object service()
   {
   return service(new ServiceItemFilter()
                    {
                    public boolean check(ServiceItem item)
                      {
                      return true;
                      }
                    });
   }
}  // end ServiceCache

Let me know if you have any problems with these.

Regards,

Patrick

------------------------------------------------------------------------
S P Engineering, Inc.    | The experts in large scale distributed OO
                        | systems design and implementation.
         pjm@spe.com    | (C++, Java, Common Lisp, Jini, CORBA, UML)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.