Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * IronJacamar, a Java EE Connector Architecture implementation
   * Copyright 2012, Red Hat Inc, 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.jca.core.workmanager;
 
 
 import java.util.List;
 import java.util.Map;
 
Coordinator for WorkManager instances

Author(s):
Jesper Pedersen
 
 public class WorkManagerCoordinator
 {
   
The logger
 
    private static CoreLogger log = Logger.getMessageLogger(CoreLogger.class,
                                                            WorkManagerCoordinator.class.getName());

   
Whether trace is enabled
 
    private static boolean trace = .isTraceEnabled();

   
The instance
 
    private static final WorkManagerCoordinator INSTANCE = new WorkManagerCoordinator();

   
The work managers
 
    private ConcurrentMap<StringWorkManagerworkmanagers;

   
The default work manager
 
    private WorkManager defaultWorkManager;

   
The activate work managers
 
    private Map<StringWorkManageractiveWorkmanagers;

   
The ref count for activate work managers
 
    private Map<StringIntegerrefCountWorkmanagers;

   
Constructor
 
    private WorkManagerCoordinator()
    {
       this. = new ConcurrentHashMap<StringWorkManager>();
       this. = null;
       this. = new HashMap<StringWorkManager>();
       this. = new HashMap<StringInteger>();
    }

   
Get the instance

Returns:
The instance
 
    public static WorkManagerCoordinator getInstance()
    {
       return ;
    }

   
Register work manager

Parameters:
wm The work manager
 
    public void registerWorkManager(WorkManager wm)
    {
       if (wm != null)
       {
          if (wm.getName() == null || wm.getName().trim().equals(""))
             throw new IllegalArgumentException("The name of WorkManager is invalid: " + wm);
 
         if ()
            .tracef("Registering WorkManager: %s"wm);
         if (!.keySet().contains(wm.getName()))
         {
            .put(wm.getName(), wm);
            // Replay events for distributed work managers
            if (wm instanceof DistributedWorkManager)
            {
               WorkManagerEventQueue wmeq = WorkManagerEventQueue.getInstance();
               List<WorkManagerEventevents = wmeq.getEvents(wm.getName());
               if (events.size() > 0)
               {
                  if ()
                     .tracef("%s: Events=%s"wm.getName(), events);
                  for (WorkManagerEvent event : events)
                  {
                     if (event.getType() == .)
                     {
                        DistributedWorkManager dwm = resolveDistributedWorkManager(event.getAddress());
                        if (dwm != null)
                        {
                           Collection<NotificationListenercopy =
                              new ArrayList<NotificationListener>(dwm.getNotificationListeners());
                           for (NotificationListener nl : copy)
                           {
                              nl.join(event.getAddress());
                           }
                        }
                     }
                     else if (event.getType() == .)
                     {
                        DistributedWorkManager dwm = 
                           (DistributedWorkManager).get(event.getAddress().getWorkManagerId());
                        if (dwm != null)
                        {
                           Collection<NotificationListenercopy =
                              new ArrayList<NotificationListener>(dwm.getNotificationListeners());
                           for (NotificationListener nl : copy)
                           {
                              nl.leave(event.getAddress());
                           }
                        }
                        removeWorkManager(event.getAddress().getWorkManagerId());
                     }
                     else if (event.getType() == .)
                     {
                        DistributedWorkManager dwm = 
                           (DistributedWorkManager).get(event.getAddress().getWorkManagerId());
                        if (dwm != null)
                        {
                           Collection<NotificationListenercopy =
                              new ArrayList<NotificationListener>(dwm.getNotificationListeners());
                           for (NotificationListener nl : copy)
                           {
                              nl.updateShortRunningFree(event.getAddress(), event.getValue());
                           }
                        }
                     }
                     else if (event.getType() == .)
                     {
                        DistributedWorkManager dwm = 
                           (DistributedWorkManager).get(event.getAddress().getWorkManagerId());
                        if (dwm != null)
                        {
                           Collection<NotificationListenercopy =
                              new ArrayList<NotificationListener>(dwm.getNotificationListeners());
                           for (NotificationListener nl : copy)
                           {
                              nl.updateLongRunningFree(event.getAddress(), event.getValue());
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }

   
Unregister work manager

Parameters:
wm The work manager
   public void unregisterWorkManager(WorkManager wm)
   {
      if (wm != null)
      {
         if (wm.getName() == null || wm.getName().trim().equals(""))
            throw new IllegalArgumentException("The name of WorkManager is invalid: " + wm);
         if ()
            .tracef("Unregistering WorkManager: %s"wm);
         if (.keySet().contains(wm.getName()))
         {
            .remove(wm.getName());
            // Clear any events
            if (wm instanceof DistributedWorkManager)
            {
               WorkManagerEventQueue wmeq = WorkManagerEventQueue.getInstance();
               List<WorkManagerEventevents = wmeq.getEvents(wm.getName());
               events.clear();
            }
         }
      }
   }

   
Get the default work manager

Returns:
The work manager
   {
      return ;
   }

   
Set the default work manager

Parameters:
wm The work manager
   public void setDefaultWorkManager(WorkManager wm)
   {
      if ()
         .tracef("Default WorkManager: %s"wm);
      String currentName = null;
      if ( != null)
         currentName = .getName();
       = wm;
      if (wm != null)
      {
         .put(wm.getName(), wm);
      }
      else if (currentName != null)
      {
         .remove(currentName);
      }
   }

   
Resolve a work manager

Parameters:
address The work manager address
Returns:
The value
   public WorkManager resolveWorkManager(Address address)
   {
      if ()
      {
         .tracef("resolveWorkManager(%s)"address);
         .tracef("  ActiveWorkManagers: %s");
      }
      WorkManager wm = .get(address.getWorkManagerId());
      if (wm != null)
      {
         if ()
            .tracef(" WorkManager: %s"wm);
         return wm;
      }
      try
      {
         // Create a new instance
         WorkManager template = .get(address.getWorkManagerName());
         if (template != null)
         {
            wm = template.clone();
            wm.setId(address.getWorkManagerId());
            if (wm instanceof DistributedWorkManager)
            {
               DistributedWorkManager dwm = (DistributedWorkManager)wm;
               dwm.initialize();
            }
            .put(address.getWorkManagerId(), wm);
            .put(address.getWorkManagerId(), Integer.valueOf(0));
            if ()
               .tracef("Created WorkManager: %s"wm);
            return wm;
         }
      }
      catch (Throwable t)
      {
         //throw new IllegalStateException("The WorkManager couldn't be created: " + name);
      }
      return null;
   }

   
Resolve a distributed work manager

Parameters:
address The work manager address
Returns:
The value
   {
      if ()
      {
         .tracef("resolveDistributedWorkManager(%s)"address);
         .tracef("  ActiveWorkManagers: %s");
      }
      WorkManager wm = .get(address.getWorkManagerId());
      if (wm != null)
      {
         if (wm instanceof DistributedWorkManager)
         {
            if ()
               .tracef(" WorkManager: %s"wm);
            return (DistributedWorkManager)wm;
         }
         else
         {
            if ()
               .tracef(" WorkManager not distributable: %s"wm);
            return null;
         }
      }
      try
      {
         // Create a new instance
         WorkManager template = .get(address.getWorkManagerName());
         if (template != null)
         {
            wm = template.clone();
            wm.setId(address.getWorkManagerId());
            if (wm instanceof DistributedWorkManager)
            {
               DistributedWorkManager dwm = (DistributedWorkManager)wm;
               dwm.initialize();
               .put(address.getWorkManagerId(), dwm);
               .put(address.getWorkManagerId(), Integer.valueOf(0));
               
               if ()
                  .tracef("Created WorkManager: %s"dwm);
               
               return dwm;
            }
         }
      }
      catch (Throwable t)
      {
         //throw new IllegalStateException("The WorkManager couldn't be created: " + name);
      }
      return null;
   }

   
Create a work manager

Parameters:
id The id of the work manager
name The name of the work manager; if null default value is used
Returns:
The work manager
   public synchronized WorkManager createWorkManager(String idString name)
   {
      if (id == null || id.trim().equals(""))
         throw new IllegalArgumentException("The id of WorkManager is invalid: " + id);
      // Check for an active work manager
      if (.keySet().contains(id))
      {
         if ()
            .tracef("RefCounting WorkManager: %s"id);
         Integer i = .get(id);
         .put(id, Integer.valueOf(i.intValue() + 1));
         WorkManager wm = .get(id);
         if (wm instanceof DistributedWorkManager)
         {
            DistributedWorkManager dwm = (DistributedWorkManager)wm;
            if (dwm.getTransport() != null)
               dwm.getTransport().register(new Address(wm.getId(), wm.getName(), dwm.getTransport().getId()));
         }
         return wm;
      }
      try
      {
         // Create a new instance
         WorkManager template = null;
         if (name != null)
         {
            template = .get(name);
         }
         else
         {
            template = ;
         }
         if (template == null)
            throw new IllegalArgumentException("The WorkManager wasn't found: " + name);
         WorkManager wm = template.clone();
         wm.setId(id);
         if (wm instanceof DistributedWorkManager)
         {
            DistributedWorkManager dwm = (DistributedWorkManager)wm;
            dwm.initialize();
            if (dwm.getTransport() != null)
            {
               dwm.getTransport().register(new Address(wm.getId(), wm.getName(), dwm.getTransport().getId()));
            }
            else
            {
               .debugf("DistributedWorkManager '%s' doesn't have a transport associated"dwm.getName());
            }
         }
         .put(idwm);
         .put(id, Integer.valueOf(1));
         if ()
            .tracef("Created WorkManager: %s"wm);
         return wm;
      }
      catch (Throwable t)
      {
         throw new IllegalStateException("The WorkManager couldn't be created: " + namet);
      }
   }

   
Remove a work manager

Parameters:
id The id of the work manager
   public synchronized void removeWorkManager(String id)
   {
      if (id == null || id.trim().equals(""))
         throw new IllegalArgumentException("The id of WorkManager is invalid: " + id);
      Integer i = .get(id);
      if (i != null)
      {
         int newValue = i.intValue() - 1;
         if (newValue == 0)
         {
            if ()
               .tracef("Removed WorkManager: %s"id);
            WorkManager wm = .get(id);
            if (wm instanceof DistributedWorkManager)
            {
               DistributedWorkManager dwm = (DistributedWorkManager)wm;
               if (dwm.getTransport() != null)
                  dwm.getTransport().unregister(new Address(wm.getId(), wm.getName(), dwm.getTransport().getId()));
            }
            .remove(id);
            .remove(id);
         }
         else
         {
            if ()
               .tracef("DerefCount WorkManager: %s"id);
            .put(id, Integer.valueOf(newValue));
         }
      }
   }
New to GrepCode? Check out our FAQ X