001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.graph.commands.executor;
023    
024    import org.jboss.dna.common.util.Logger;
025    import org.jboss.dna.graph.GraphI18n;
026    import org.jboss.dna.graph.commands.CompositeCommand;
027    import org.jboss.dna.graph.commands.CopyBranchCommand;
028    import org.jboss.dna.graph.commands.CopyNodeCommand;
029    import org.jboss.dna.graph.commands.CreateNodeCommand;
030    import org.jboss.dna.graph.commands.DeleteBranchCommand;
031    import org.jboss.dna.graph.commands.GetChildrenCommand;
032    import org.jboss.dna.graph.commands.GetNodeCommand;
033    import org.jboss.dna.graph.commands.GetPropertiesCommand;
034    import org.jboss.dna.graph.commands.GraphCommand;
035    import org.jboss.dna.graph.commands.MoveBranchCommand;
036    import org.jboss.dna.graph.commands.RecordBranchCommand;
037    import org.jboss.dna.graph.commands.SetPropertiesCommand;
038    import org.jboss.dna.graph.connectors.RepositorySourceException;
039    
040    /**
041     * @author Randall Hauch
042     */
043    public class LoggingCommandExecutor extends DelegatingCommandExecutor {
044    
045        private final Logger logger;
046        private final Logger.Level level;
047    
048        /**
049         * Create a command executor that logs before and after each method call, logging messages at the {@link Logger.Level#TRACE
050         * trace} level.
051         * 
052         * @param delegate the delegate executor
053         * @param logger the logger
054         */
055        public LoggingCommandExecutor( CommandExecutor delegate,
056                                       Logger logger ) {
057            this(delegate, logger, Logger.Level.TRACE);
058        }
059    
060        /**
061         * Create a command executor that logs before and after each method call, logging messages at the supplied
062         * {@link Logger.Level level}.
063         * 
064         * @param delegate the delegate executor
065         * @param logger the logger
066         * @param level the logging level, or null if {@link Logger.Level#TRACE trace-level} logging should be used.
067         */
068        public LoggingCommandExecutor( CommandExecutor delegate,
069                                       Logger logger,
070                                       Logger.Level level ) {
071            super(delegate);
072            assert logger != null;
073            this.logger = logger;
074            this.level = level != null ? level : Logger.Level.TRACE;
075        }
076    
077        /**
078         * {@inheritDoc}
079         * 
080         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#close()
081         */
082        @Override
083        public void close() {
084            this.logger.log(level, GraphI18n.closingCommandExecutor);
085            super.close();
086            this.logger.log(level, GraphI18n.closedCommandExecutor);
087        }
088    
089        /**
090         * {@inheritDoc}
091         * 
092         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.CompositeCommand)
093         */
094        @Override
095        public void execute( CompositeCommand command ) throws RepositorySourceException {
096            this.logger.log(level, GraphI18n.executingGraphCommand, command);
097            super.execute(command);
098            this.logger.log(level, GraphI18n.executedGraphCommand, command);
099        }
100    
101        /**
102         * {@inheritDoc}
103         * 
104         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.CopyBranchCommand)
105         */
106        @Override
107        public void execute( CopyBranchCommand command ) throws RepositorySourceException {
108            this.logger.log(level, GraphI18n.executingGraphCommand, command);
109            super.execute(command);
110            this.logger.log(level, GraphI18n.executedGraphCommand, command);
111        }
112    
113        /**
114         * {@inheritDoc}
115         * 
116         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.CopyNodeCommand)
117         */
118        @Override
119        public void execute( CopyNodeCommand command ) throws RepositorySourceException {
120            this.logger.log(level, GraphI18n.executingGraphCommand, command);
121            super.execute(command);
122            this.logger.log(level, GraphI18n.executedGraphCommand, command);
123        }
124    
125        /**
126         * {@inheritDoc}
127         * 
128         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.CreateNodeCommand)
129         */
130        @Override
131        public void execute( CreateNodeCommand command ) throws RepositorySourceException {
132            this.logger.log(level, GraphI18n.executingGraphCommand, command);
133            super.execute(command);
134            this.logger.log(level, GraphI18n.executedGraphCommand, command);
135        }
136    
137        /**
138         * {@inheritDoc}
139         * 
140         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.DeleteBranchCommand)
141         */
142        @Override
143        public void execute( DeleteBranchCommand command ) throws RepositorySourceException {
144            this.logger.log(level, GraphI18n.executingGraphCommand, command);
145            super.execute(command);
146            this.logger.log(level, GraphI18n.executedGraphCommand, command);
147        }
148    
149        /**
150         * {@inheritDoc}
151         * 
152         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.GetChildrenCommand)
153         */
154        @Override
155        public void execute( GetChildrenCommand command ) throws RepositorySourceException {
156            this.logger.log(level, GraphI18n.executingGraphCommand, command);
157            super.execute(command);
158            this.logger.log(level, GraphI18n.executedGraphCommand, command);
159        }
160    
161        /**
162         * {@inheritDoc}
163         * 
164         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.GetNodeCommand)
165         */
166        @Override
167        public void execute( GetNodeCommand command ) throws RepositorySourceException {
168            this.logger.log(level, GraphI18n.executingGraphCommand, command);
169            super.execute(command);
170            this.logger.log(level, GraphI18n.executedGraphCommand, command);
171        }
172    
173        /**
174         * {@inheritDoc}
175         * 
176         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.GetPropertiesCommand)
177         */
178        @Override
179        public void execute( GetPropertiesCommand command ) throws RepositorySourceException {
180            this.logger.log(level, GraphI18n.executingGraphCommand, command);
181            super.execute(command);
182            this.logger.log(level, GraphI18n.executedGraphCommand, command);
183        }
184    
185        /**
186         * {@inheritDoc}
187         * 
188         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.GraphCommand)
189         */
190        @Override
191        public void execute( GraphCommand command ) throws RepositorySourceException {
192            this.logger.log(level, GraphI18n.executingGraphCommand, command);
193            super.execute(command);
194            this.logger.log(level, GraphI18n.executedGraphCommand, command);
195        }
196    
197        /**
198         * {@inheritDoc}
199         * 
200         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.MoveBranchCommand)
201         */
202        @Override
203        public void execute( MoveBranchCommand command ) throws RepositorySourceException {
204            this.logger.log(level, GraphI18n.executingGraphCommand, command);
205            super.execute(command);
206            this.logger.log(level, GraphI18n.executedGraphCommand, command);
207        }
208    
209        /**
210         * {@inheritDoc}
211         * 
212         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.RecordBranchCommand)
213         */
214        @Override
215        public void execute( RecordBranchCommand command ) throws RepositorySourceException {
216            this.logger.log(level, GraphI18n.executingGraphCommand, command);
217            super.execute(command);
218            this.logger.log(level, GraphI18n.executedGraphCommand, command);
219        }
220    
221        /**
222         * {@inheritDoc}
223         * 
224         * @see org.jboss.dna.graph.commands.executor.DelegatingCommandExecutor#execute(org.jboss.dna.graph.commands.SetPropertiesCommand)
225         */
226        @Override
227        public void execute( SetPropertiesCommand command ) throws RepositorySourceException {
228            this.logger.log(level, GraphI18n.executingGraphCommand, command);
229            super.execute(command);
230            this.logger.log(level, GraphI18n.executedGraphCommand, command);
231        }
232    
233    }