View Javadoc

1   /*
2    * Copyright 2008 fastConnect.
3    *    
4    * This file is part of Composite BulkDataPersister.  
5    *
6    * This is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 3 of
9    * the License, or (at your option) any later version.
10   *
11   * This software is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this software. If not, see <http://www.gnu.org/licenses/>.
18   */
19  package fr.fastconnect.gigaspaces.datasource.strategy;
20  
21  import java.util.List;
22  import java.util.Properties;
23  import java.util.logging.Level;
24  import java.util.logging.Logger;
25  
26  import net.jini.core.lease.Lease;
27  
28  import com.gigaspaces.datasource.BulkDataPersister;
29  import com.gigaspaces.datasource.BulkItem;
30  import com.gigaspaces.datasource.DataSourceException;
31  import com.j_spaces.core.IJSpace;
32  import com.j_spaces.core.client.FinderException;
33  import com.j_spaces.core.client.SpaceFinder;
34  
35  import fr.fastconnect.gigaspaces.datasource.ExecutionFailureStrategy;
36  
37  /**
38   * Use a space to store data.
39   * <br />
40   * Space is defined by a URL configured through {@value SpaceStorageExecutionFailureStrategy#SPACE_STORAGE_EXECUTION_EXCEPTION_STRATEGY_URL_PROPERTY}SPACE_STORAGE_EXECUTION_EXCEPTION_STRATEGY_URL_PROPERTY.
41   * If undefined then {@link SpaceStorageExecutionFailureStrategy#init(Properties)} will throw a {@link DataSourceException}.
42   * <br />
43   * If underlying {@link SpaceFinder#find(String)} throws an exception then {@link SpaceStorageExecutionFailureStrategy#init(Properties)} will throw a {@link DataSourceException}.
44   */
45  public class SpaceStorageExecutionFailureStrategy implements ExecutionFailureStrategy {
46  	
47  	private final static Logger LOGGER = Logger.getLogger(SpaceStorageExecutionFailureStrategy.class.getName());
48  	
49  	public static final String SPACE_STORAGE_EXECUTION_EXCEPTION_STRATEGY_URL_PROPERTY = "spacestorage-executionexceptionstrategy-url";
50  	
51  	private IJSpace proxy;
52  	
53  	public SpaceStorageExecutionFailureStrategy() {
54  	}
55  	
56  	public SpaceStorageExecutionFailureStrategy(final IJSpace proxy) {
57  		this.proxy = proxy;
58  	}
59  	
60  	/** 
61  	 * {@inheritDoc}
62  	 */
63  	public void init(final Properties properties) throws DataSourceException {
64  		if (this.proxy != null) {
65  			final String url = properties.getProperty(SPACE_STORAGE_EXECUTION_EXCEPTION_STRATEGY_URL_PROPERTY);
66  			if (url != null) {
67  				if (SpaceStorageExecutionFailureStrategy.LOGGER.isLoggable(Level.FINE)) {
68  					SpaceStorageExecutionFailureStrategy.LOGGER.fine("Using <"+url+"> as "+SPACE_STORAGE_EXECUTION_EXCEPTION_STRATEGY_URL_PROPERTY);
69  				}
70  				
71  				try {
72  					this.proxy = IJSpace.class.cast(SpaceFinder.find(url));
73  				} catch(FinderException e) {
74  					final String message = "Cannot find Space using URL <"+url+">";
75  					if (SpaceStorageExecutionFailureStrategy.LOGGER.isLoggable(Level.SEVERE)) {
76  						SpaceStorageExecutionFailureStrategy.LOGGER.log(Level.SEVERE, message, e);
77  					}
78  					throw new DataSourceException(message, e);
79  				}
80  			} else {
81  				final String message = "No "+SPACE_STORAGE_EXECUTION_EXCEPTION_STRATEGY_URL_PROPERTY+" defined";
82  				if (SpaceStorageExecutionFailureStrategy.LOGGER.isLoggable(Level.SEVERE)) {
83  					SpaceStorageExecutionFailureStrategy.LOGGER.log(Level.SEVERE, message);
84  				}
85  				throw new DataSourceException(message);
86  			}
87  		} else {
88  			if (SpaceStorageExecutionFailureStrategy.LOGGER.isLoggable(Level.CONFIG)) {
89  				SpaceStorageExecutionFailureStrategy.LOGGER.log(Level.CONFIG, SpaceStorageExecutionFailureStrategy.class.getSimpleName()+" configured manually; ignoring properties");
90  			}
91  		}
92  		
93  		if (SpaceStorageExecutionFailureStrategy.LOGGER.isLoggable(Level.CONFIG)) {
94  			SpaceStorageExecutionFailureStrategy.LOGGER.log(Level.CONFIG, "Using proxy <"+this.proxy+">");
95  		}
96  	}
97  	
98  	public void handleExecutionFailure(final BulkDataPersister bulkDataPersister, final List<BulkItem> bulkItems, final DataSourceException exception) throws Exception {
99  		this.proxy.write(new SpaceStorageExecutionFailure(bulkDataPersister.getClass().getName(), bulkItems, exception), null, Lease.FOREVER);
100 	}
101 	
102 	public void setSpace(final IJSpace space) {
103 		this.proxy = space;
104 	}
105 
106 }