KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > toolagent > ToolAgentFactoryImpl


1 package org.enhydra.shark.toolagent;
2
3
4 import org.enhydra.shark.api.internal.toolagent.*;
5 import org.enhydra.shark.api.SharkTransaction;
6 import org.enhydra.shark.api.RootException;
7 import org.enhydra.shark.api.internal.working.CallbackUtilities;
8
9 import java.util.*;
10
11 /**
12  * Factory for creating tool agents.
13  *
14  * @author Sasa Bojanic
15  * @version 1.0
16  */

17 public class ToolAgentFactoryImpl implements ToolAgentFactory {
18    private final static String JavaDoc TOOL_AGENT_PREFIX="ToolAgent.";
19    private List toolAgents=new ArrayList();
20
21    private CallbackUtilities cus;
22    public void configure (CallbackUtilities cus) throws RootException {
23       this.cus=cus;
24       createToolAgentList();
25    }
26
27    /**
28     * Returns all tool agents specified in shark's properties.
29     */

30    public List getDefinedToolAgents (SharkTransaction t) throws RootException {
31       return Collections.unmodifiableList(toolAgents);
32    }
33
34
35    public ToolAgent createToolAgent(SharkTransaction t,String JavaDoc className) throws RootException {
36       try {
37          Class JavaDoc cls=Class.forName(className);
38          ToolAgent ta=(ToolAgent)cls.newInstance();
39          ta.configure(cus);
40          return ta;
41       } catch (Exception JavaDoc ex) {
42          throw new RootException("Failed to create tool agent for class "+className,ex);
43       }
44    }
45
46    private void createToolAgentList () {
47       String JavaDoc taName=null;
48       String JavaDoc className=null;
49       Properties props= cus.getProperties();
50
51       Iterator it=props.entrySet().iterator();
52       while (it.hasNext()) {
53          try {
54             Map.Entry me=(Map.Entry)it.next();
55             taName=me.getKey().toString();
56             if (taName.startsWith(TOOL_AGENT_PREFIX)) {
57                taName=taName.substring(TOOL_AGENT_PREFIX.length());
58                className=me.getValue().toString();
59                toolAgents.add(className);
60             }
61          } catch (Throwable JavaDoc ex) {
62             //ex.printStackTrace();
63
cus.error("ToolAgentFactoryImpl -> Error when retrieving list of tool agents from properties!!!");
64          }
65       }
66    }
67
68 }
69
Popular Tags