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.graph.ExecutionContext;
025    import org.jboss.dna.graph.commands.CopyBranchCommand;
026    import org.jboss.dna.graph.commands.CopyNodeCommand;
027    import org.jboss.dna.graph.commands.CreateNodeCommand;
028    import org.jboss.dna.graph.commands.DeleteBranchCommand;
029    import org.jboss.dna.graph.commands.GetChildrenCommand;
030    import org.jboss.dna.graph.commands.GetPropertiesCommand;
031    import org.jboss.dna.graph.commands.MoveBranchCommand;
032    import org.jboss.dna.graph.commands.RecordBranchCommand;
033    import org.jboss.dna.graph.commands.SetPropertiesCommand;
034    import org.jboss.dna.graph.connectors.RepositoryConnection;
035    import org.jboss.dna.graph.connectors.RepositoryConnectionFactory;
036    import org.jboss.dna.graph.connectors.RepositorySource;
037    import org.jboss.dna.graph.connectors.RepositorySourceException;
038    import org.jboss.dna.graph.properties.DateTime;
039    
040    /**
041     * @author Randall Hauch
042     */
043    public class SingleSourceCommandExecutor extends AbstractCommandExecutor {
044    
045        private RepositoryConnection connection;
046        private final RepositoryConnectionFactory connectionFactory;
047    
048        /**
049         * Create a command executor that does nothing.
050         * 
051         * @param context the execution context in which the executor will be run; may not be null
052         * @param sourceName the name of the {@link RepositorySource} that is making use of this executor; may not be null or empty
053         * @param connectionFactory the factory for {@link RepositoryConnection} instances
054         */
055        public SingleSourceCommandExecutor( ExecutionContext context,
056    
057                                            String sourceName,
058                                            RepositoryConnectionFactory connectionFactory ) {
059            this(context, sourceName, null, connectionFactory);
060        }
061    
062        /**
063         * Create a command executor that does nothing.
064         * 
065         * @param context the execution context in which the executor will be run; may not be null
066         * @param sourceName the name of the {@link RepositorySource} that is making use of this executor; may not be null or empty
067         * @param now the current time; may be null if the system time is to be used
068         * @param connectionFactory the factory for {@link RepositoryConnection} instances
069         */
070        public SingleSourceCommandExecutor( ExecutionContext context,
071                                            String sourceName,
072                                            DateTime now,
073                                            RepositoryConnectionFactory connectionFactory ) {
074            super(context, sourceName, now);
075            assert connectionFactory != null;
076            this.connectionFactory = connectionFactory;
077        }
078    
079        protected RepositoryConnection getConnection() throws RepositorySourceException {
080            if (connection == null) {
081                // Create a connection ...
082                connection = this.connectionFactory.createConnection(getSourceName());
083            }
084            return connection;
085        }
086    
087        /**
088         * {@inheritDoc}
089         * 
090         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#close()
091         */
092        @Override
093        public void close() {
094            if (this.connection != null) {
095                try {
096                    this.connection.close();
097                } finally {
098                    this.connection = null;
099                }
100            }
101            super.close();
102        }
103    
104        /**
105         * {@inheritDoc}
106         * 
107         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#execute(org.jboss.dna.graph.commands.CopyBranchCommand)
108         */
109        @Override
110        public void execute( CopyBranchCommand command ) throws RepositorySourceException {
111            getConnection().execute(this.getExecutionContext(), command);
112        }
113    
114        /**
115         * {@inheritDoc}
116         * 
117         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#execute(org.jboss.dna.graph.commands.CopyNodeCommand)
118         */
119        @Override
120        public void execute( CopyNodeCommand command ) throws RepositorySourceException {
121            getConnection().execute(this.getExecutionContext(), command);
122        }
123    
124        /**
125         * {@inheritDoc}
126         * 
127         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#execute(org.jboss.dna.graph.commands.CreateNodeCommand)
128         */
129        @Override
130        public void execute( CreateNodeCommand command ) throws RepositorySourceException {
131            getConnection().execute(this.getExecutionContext(), command);
132        }
133    
134        /**
135         * {@inheritDoc}
136         * 
137         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#execute(org.jboss.dna.graph.commands.DeleteBranchCommand)
138         */
139        @Override
140        public void execute( DeleteBranchCommand command ) throws RepositorySourceException {
141            getConnection().execute(this.getExecutionContext(), command);
142        }
143    
144        /**
145         * {@inheritDoc}
146         * 
147         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#execute(org.jboss.dna.graph.commands.GetChildrenCommand)
148         */
149        @Override
150        public void execute( GetChildrenCommand command ) throws RepositorySourceException {
151            getConnection().execute(this.getExecutionContext(), command);
152        }
153    
154        /**
155         * {@inheritDoc}
156         * 
157         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#execute(org.jboss.dna.graph.commands.GetPropertiesCommand)
158         */
159        @Override
160        public void execute( GetPropertiesCommand command ) throws RepositorySourceException {
161            getConnection().execute(this.getExecutionContext(), command);
162        }
163    
164        /**
165         * {@inheritDoc}
166         * 
167         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#execute(org.jboss.dna.graph.commands.MoveBranchCommand)
168         */
169        @Override
170        public void execute( MoveBranchCommand command ) throws RepositorySourceException {
171            getConnection().execute(this.getExecutionContext(), command);
172        }
173    
174        /**
175         * {@inheritDoc}
176         * 
177         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#execute(org.jboss.dna.graph.commands.RecordBranchCommand)
178         */
179        @Override
180        public void execute( RecordBranchCommand command ) throws RepositorySourceException {
181            getConnection().execute(this.getExecutionContext(), command);
182        }
183    
184        /**
185         * {@inheritDoc}
186         * 
187         * @see org.jboss.dna.graph.commands.executor.AbstractCommandExecutor#execute(org.jboss.dna.graph.commands.SetPropertiesCommand)
188         */
189        @Override
190        public void execute( SetPropertiesCommand command ) throws RepositorySourceException {
191            getConnection().execute(this.getExecutionContext(), command);
192        }
193    }