View Javadoc

1   /*
2    * $Id: CommandFactoryService.java 269 2005-08-10 17:49:22Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.command.factory;
24  
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.Map;
28  
29  import org.talika.tarsis.command.Command;
30  import org.talika.tarsis.command.action.ActionException;
31  import org.talika.tarsis.service.AbstractService;
32  
33  /**
34   * Base implementation of <code>CommandFactory</code> interface.<br>
35   * <br>
36   * Command factories plug in system like a service extending <code>AbstractService</code>.
37   *
38   * @author  Jose M. Palomar
39   * @version $Revision: 269 $
40   */
41  public abstract class CommandFactoryService extends AbstractService implements CommandFactory {
42  
43      // Fields
44      /**
45       * Commands map.
46       */
47      private Map commands = new HashMap();
48  
49      // Costructors
50      /**
51       * Creates a new <code>CommandFactoryService</code> object.
52       */
53      public CommandFactoryService() {
54      }
55  
56      // Methods
57      /**
58       * Returns matching name command instance.
59       *
60       * @param name String name of command.
61       * @return Command command instance for given name or <code>null</code> if none
62       * found.
63       * @see org.talika.tarsis.command.factory.CommandFactory#findCommand(String)
64       */
65      public final Command findCommand(String name) {
66          Command cmd = (Command) commands.get(name);
67          if (cmd == null) {
68              loadCommand(name);
69              cmd = (Command) commands.get(name);
70          }
71  
72          return cmd;
73      }
74  
75      /**
76       * Tries to load command definition from repository for a given name.
77       *
78       * @param commandName String name of command to load.
79       */
80      protected abstract void loadCommand(String commandName);
81  
82      /**
83       * Tries to load all command definitions from repository for a given package.
84       *
85       * @param packageName String name for package to load.
86       */
87      protected abstract void loadPackage(String packageName);
88  
89      /**
90       * Adds a command definition to factory.
91       *
92       * @param name String name of command.
93       * @param cmd Command instance of command.
94       */
95      protected final void addCommand(String name, Command cmd) {
96  
97          try {
98  
99              if (getLogger().isDebugEnabled()) {
100                 getLogger().logDebug("Command " + cmd.getFullName() + ": init");
101             }
102 
103             cmd.getAction().init(getContext());
104             commands.put(name, cmd);
105 
106             if (getLogger().isDebugEnabled()) {
107                 getLogger().logDebug("Command " + cmd.getFullName() +
108                                               ": enabled");
109             }
110 
111         }
112         catch (ActionException ae) {
113             if (getLogger().isErrorEnabled()) {
114                 getLogger().logError("Error initializing command " +
115                                               cmd.getFullName(), ae);
116             }
117             if (getLogger().isDebugEnabled()) {
118                 getLogger().logDebug("Command " + cmd.getFullName() +
119                                               ": disabled");
120             }
121         }
122 
123     }
124 
125     /**
126      * Adds a map of command definitions to factory. Map keys are command's names and
127      * values are command's instances.
128      *
129      * @param cmds Map map of command instances.
130      */
131     protected final void addCommands(Map cmds) {
132         Iterator cmdsIterator = cmds.values().iterator();
133         while (cmdsIterator.hasNext()) {
134             Command cmd = (Command) cmdsIterator.next();
135             addCommand(cmd.getFullName(), cmd);
136         }
137     }
138 
139 }