View Javadoc
1   /*
2    * 
3    * $Revision: 1.9 $
4    * $Date: 2005/01/11 08:21:44 $
5    *
6    * ====================================================================
7    * struts-it
8    * Copyright (C) 2004 - strutsit community
9    * 
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   * 
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   * 
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   * created: 2004-06-05  Manfred Wolff
23   */
24  package org.strutsit.chain;
25  
26  import org.apache.commons.chain.Catalog;
27  import org.apache.commons.chain.Command;
28  import org.apache.commons.chain.Context;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.strutsit.chain.configuration.ChainConfigurationFactory;
32  import org.strutsit.chain.interfaces.ChainConfiguration;
33  import org.strutsit.chain.interfaces.ChainFacade;
34  
35  /***
36   * Implementation of the {@link ChainFacade}-Interface. The Facade is a
37   * singleton. It holds the configuration of the chain-Framework.
38   * 
39   * @author <a href="mailto:wolff@struts-it.de">Manfred Wolff </a>
40   * @since JDK 1.4
41   * @version $Revision: 1.9 $
42   */
43  public final class ChainFacadeImpl implements ChainFacade {
44  
45      /***
46       * Hiding the default constructor of this class.
47       */
48      private ChainFacadeImpl() {
49          super();
50      }
51  
52      /***
53       * The logger for this class (commons-logging).
54       */
55      private static Log log = LogFactory.getLog(ChainFacadeImpl.class);
56  
57      /***
58       * The {@link ChainConfiguration}- Instance of this facade.
59       */
60      private ChainConfiguration config;
61  
62      /***
63       * The {@link ChainFacade}instance (singleton semantics).
64       */
65      private static ChainFacade instance;
66  
67      /***
68       * Returns the singleton of the facade.
69       * 
70       * @return The one and only instance of the facade.
71       */
72      public static ChainFacade getInstance() {
73  
74          if (log.isTraceEnabled()) {
75              log.trace("getInstance -> START");
76          }
77          if (instance == null) {
78              instance = new ChainFacadeImpl();
79              try {
80                 instance.initFacade();
81              } catch (ChainException e) {
82              	 throw new IllegalArgumentException();
83              }	 
84          }
85          if (log.isTraceEnabled()) {
86              log.trace("getInstance -> END");
87          }
88          return instance;
89      }
90  
91      /***
92       * Inits the framework. The method has the following responsibilities:
93       * <ul>
94       * <li>Reading the configuration and preparing the ChainConfiguration.
95       * </li>
96       * <li>Checking the dependencies of the chains and commands.</li>
97       * </ul>
98       * 
99       * @throws ChainException if something fails
100      * @see org.strutsit.chain.interfaces.ChainFacade#initFacade()
101      */
102     public void initFacade() throws ChainException {
103         if (log.isTraceEnabled()) {
104             log.trace("initFacade -> START");
105         }
106         
107         // Preventing the multiple call of initFacade. Usually, initFacade
108         // might be called once.
109         if (config == null) {
110             ChainConfigurationFactory factory = ChainConfigurationFactory
111                 .getInstance();
112 
113             config = factory.createConfiguration();
114             if (config == null) { throw new ChainException(
115                 "Error creating configuration."); }
116 
117             // init already called in config.checkConfiguration()
118             // config.init();
119             config.checkConfiguration();
120 
121         }
122         if (log.isTraceEnabled()) {
123             log.trace("initFacade -> END");
124         }
125     }
126 
127     /***
128      * Frees all Resources.
129      * 
130      * @see org.strutsit.chain.interfaces.ChainFacade#destroy()
131      */
132     public void destroy() {
133         if (log.isTraceEnabled()) {
134             log.trace("destroy -> START");
135         }
136         // nothing to do
137         if (log.isTraceEnabled()) {
138             log.trace("getConfiguration -> END");
139         }
140     }
141 
142     /***
143      * Executes a chain or a single command by name.
144      * 
145      * @param commandString The name of the command or chain.
146      * @param context The Context for executing
147      * @return True if the command can fulfill the operation, otherwise false
148      * @throws ChainException if something goes wrong
149      */
150     public boolean execute(String commandString, Context context)
151             throws ChainException {
152 
153 
154         if (log.isTraceEnabled()) {
155             log.trace("execute -> START");
156         }
157         Catalog catalog = config.getCatalog();
158         Command command = null;
159         
160         // ! because getCommand is not synchronized
161         synchronized (catalog) {
162             command = catalog.getCommand(commandString);
163         }
164         if (command == null) { throw new ChainException(
165             "unable to get command for " + commandString); }
166         boolean returnValue = false;
167         try {
168             returnValue = command.execute(context);
169         } catch (Exception e) {
170             throw new ChainException(e);
171         }
172         if (log.isTraceEnabled()) {
173             log.trace("execute -> END");
174         }
175         return returnValue;
176     }
177 }