View Javadoc

1   package br.com.ibnetwork.guara.pull.impl;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.List;
7   import java.util.Map;
8   
9   import org.apache.avalon.framework.configuration.Configurable;
10  import org.apache.avalon.framework.configuration.Configuration;
11  import org.apache.avalon.framework.configuration.ConfigurationException;
12  import org.apache.avalon.framework.service.ServiceException;
13  import org.apache.avalon.framework.service.ServiceManager;
14  import org.apache.avalon.framework.service.Serviceable;
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  import br.com.ibnetwork.xingu.factory.Factory;
19  import br.com.ibnetwork.guara.pull.ApplicationTool;
20  import br.com.ibnetwork.guara.pull.ApplicationToolHandler;
21  import br.com.ibnetwork.guara.pull.PullManager;
22  
23  /***
24   * @author leandro
25   */
26  public class PullManagerImpl 
27  	implements PullManager, Serviceable, Configurable
28  {
29      private Log logger;
30      
31      private Factory factory;
32      
33      private Map registry;
34      
35      public void service(ServiceManager manager) 
36      	throws ServiceException
37      {
38          logger = LogFactory.getLog(PullManagerImpl.class);
39          registry = new HashMap(5);
40          factory = (Factory) manager.lookup(Factory.ROLE);
41      }
42  
43      public void configure(Configuration conf) 
44      	throws ConfigurationException
45      {
46          Configuration[] toolsConf = conf.getChild("tools").getChildren("tool");
47          for (int i = 0; i < toolsConf.length; i++)
48          {
49              Configuration toolConfig = toolsConf[i];
50              String name = toolConfig.getAttribute("name");
51              String scope = toolConfig.getAttribute("scope");
52              logger.info("Adding tool name["+name+"] scope ["+scope+"]");
53              ApplicationToolHandler handler = createToolHandler(toolConfig);
54              addToRegistry(handler);
55          }
56      }
57  
58      private ApplicationToolHandler createToolHandler(Configuration conf) 
59      	throws ConfigurationException
60      {
61          return new ApplicationToolHandlerImpl(conf,factory);
62      }
63  
64      private void addToRegistry(ApplicationToolHandler handler)
65      {
66          String scope = handler.getScope();
67          List handlersOnScope = (List) registry.get(scope);
68          if(handlersOnScope == null)
69          {
70              handlersOnScope = new ArrayList();
71              registry.put(scope,handlersOnScope);
72          }
73          handlersOnScope.add(handler);
74      }
75  
76      public List getAllToolHandlers(String scope)
77      {
78          return (List) registry.get(scope);
79      }
80  
81  //    public Object getTool(String name) 
82  //    	throws Exception
83  //    {
84  //        for (Iterator iter = registry.keySet().iterator(); iter.hasNext();)
85  //        {
86  //            String scope = (String) iter.next();
87  //            List tools = (List) registry.get(scope);
88  //            for (Iterator iterator = tools.iterator(); iterator.hasNext();)
89  //            {
90  //                ApplicationToolHandler handler = (ApplicationToolHandler) iterator.next();
91  //                if(handler.getName().equals(name))
92  //                {
93  //                    return handler.newToolInstance();
94  //                }
95  //            }
96  //        }
97  //        return null;
98  //    }
99  }