View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.river.outrigger.snaplogstore;
19  
20  import org.apache.river.config.Config;
21  import org.apache.river.outrigger.LogOps;
22  import org.apache.river.outrigger.Recover;
23  import org.apache.river.outrigger.OutriggerServerImpl;
24  import org.apache.river.outrigger.Store;
25  import org.apache.river.system.FileSystem;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.logging.Level;
30  import java.util.logging.Logger;
31  
32  import net.jini.config.Configuration;
33  import net.jini.config.ConfigurationException;
34  import net.jini.space.InternalSpaceException;
35  
36  /**
37   * @author Sun Microsystems, Inc.
38   *
39   * @see org.apache.river.outrigger.OutriggerServerImpl
40   */
41  public class LogStore implements Store {
42      private volatile LogOutputFile	log;
43      private final String	path;
44      private final BackEnd		be;
45      private final int			maxOps;
46  
47      /** Logger for logging persistent store related information */
48      private static final Logger logger = 
49  	Logger.getLogger(OutriggerServerImpl.storeLoggerName);
50  
51      /**
52       * Create a new <code>LogStore</code>.
53       * @param config the directory to use for persistence.
54       * @throws ConfigurationException if there's a problem with the configuration.
55       */
56      public LogStore(Configuration config) throws ConfigurationException {
57  	path = (String)Config.getNonNullEntry(config,
58  				OutriggerServerImpl.COMPONENT_NAME,
59  				OutriggerServerImpl.PERSISTENCE_DIR_CONFIG_ENTRY,
60  				String.class);
61  
62  	logger.log(Level.CONFIG, "using directory {0}", path);
63  
64  	FileSystem.ensureDir(path);
65  
66  	be = new BackEnd(path);
67  
68  	maxOps = Config.getIntEntry(config, 
69  				OutriggerServerImpl.COMPONENT_NAME,
70  				"maxOps",
71  				1000, 1, Integer.MAX_VALUE);
72      }
73  
74      /**
75       * Setup store, recover previous state if any.
76       *
77       * @param space object used for recovery of previous state
78       *
79       * @return object used to persist state
80       */
81      public LogOps setupStore(Recover space) {
82  	try {
83  	    be.setupStore(space);
84  
85  	    // Use the log type as the file prefix
86  	    //
87  	    log = new LogOutputFile(
88  		new File(path, LogFile.LOG_TYPE).getAbsolutePath(),
89  		maxOps);
90  
91  	    log.observable().addObserver(be);
92  	} catch (IOException e) {
93  	    final String msg = "LogStore: log creation failed";
94  	    final InternalSpaceException ise = 
95  		new InternalSpaceException(msg, e);
96  	    logger.log(Level.SEVERE, msg, ise);
97  	    throw ise;
98  	}
99  	return log;
100     }
101 
102     /**
103      * Destroy everything.
104      */
105     public void destroy() throws IOException {
106 	be.destroy();
107 	log.destroy();
108 	new File(path).delete();
109     }
110 
111     // Inherit from super
112     public void close() throws IOException {
113 	be.close();
114 	log.close();
115     }
116 }