View Javadoc

1   /*$Id: FileNameGen.java,v 1.2 2005/04/25 12:13:30 clq2 Exp $
2    * Created on 24-Feb-2005
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.common.namegen;
12  
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.apache.commons.transaction.file.FileSequence;
17  import org.apache.commons.transaction.file.ResourceManagerException;
18  
19  import java.io.File;
20  
21  /*** Implementation of name generator using the FileSequence from commons-transactions - 
22   * provides a stream of unique identifiers, in a thread-safe, persistent manner.
23   * @author Noel Winstanley nw@jb.man.ac.uk 24-Feb-2005
24   */
25  public class FileNameGen implements NameGen {
26      /***
27       * Commons Logger for this class
28       */
29      private static final Log logger = LogFactory.getLog(FileNameGen.class);
30  
31      protected final FileSequence seq;
32      protected final String sequenceName;
33      /*** Construct a new FileNameGen
34       * @throws ResourceManagerException
35       * 
36       */
37      public FileNameGen(File workingDir, String sequenceName) throws ResourceManagerException, IllegalArgumentException {
38          super();
39          this.sequenceName = sequenceName;
40          verifyWorkingDir(workingDir);
41          seq = new FileSequence(workingDir.toString(),new CommonsLoggingFacade(logger));
42          //create sequence, if not already there, starting from 100.
43          if (!seq.exists(sequenceName)) {
44              seq.create(sequenceName,100L);
45          }
46      }
47      /*** Verify that working directory exists, is readable, etc.
48       * will create working directory if it is missing, and is able to.
49       * @param workingDir
50       * @throws IllegalArgumentException
51       */
52      private final void verifyWorkingDir(File workingDir) throws IllegalArgumentException {
53          if (! workingDir.exists()) {
54              logger.info("creating storage for id generator");
55              workingDir.mkdirs();            
56          }
57          if (!workingDir.exists()) {
58              String msg = "Base directory for id generator does not exist, and cannot be created";
59              logger.fatal(msg);
60              throw new IllegalArgumentException(msg);            
61          }
62          if (!workingDir.isDirectory()) {
63              String msg = "Base for id generator must be a directory";
64              logger.fatal(msg);
65              throw new IllegalArgumentException(msg);
66          }
67          if (! (workingDir.canRead() && workingDir.canWrite())) {
68              String msg = "Must be able to read and write to base directory for id generator";
69              logger.fatal(msg);
70              throw new IllegalArgumentException(msg);
71          }
72          // now set up node and account dir;
73          
74          logger.info("storage locations seem ok");
75      }
76  
77      
78  
79      /***
80       * @throws FileManagerFault
81       * @throws ResourceManagerException
82       * @throws ResourceManagerException
83       * @see org.astrogrid.common.namegen.NameGen#next()
84       */
85      public String next() throws  ResourceManagerException {
86              return Long.toString(seq.nextSequenceValueBottom(sequenceName,1L));
87  
88      }
89      
90      public String toString() {
91          StringBuffer buffer = new StringBuffer();
92          buffer.append("[FileNameGen:");
93          buffer.append(" seq: ");
94          buffer.append(seq);
95          buffer.append("]");
96          return buffer.toString();
97      }
98  }
99  
100 
101 /* 
102 $Log: FileNameGen.java,v $
103 Revision 1.2  2005/04/25 12:13:30  clq2
104 common-nww-1035
105 
106 Revision 1.1.2.1  2005/04/11 11:03:21  nw
107 moved name generator into common from filemanager - can then
108 reuse in jes too.
109 
110 Revision 1.2  2005/03/11 13:37:05  clq2
111 new filemanager merged with filemanager-nww-jdt-903-943
112 
113 Revision 1.1.2.1  2005/03/01 23:43:38  nw
114 split code inito client and server projoects again.
115 
116 Revision 1.1.2.2  2005/03/01 15:07:27  nw
117 close to finished now.
118 
119 Revision 1.1.2.1  2005/02/27 23:03:12  nw
120 first cut of talking to filestore
121 
122 Revision 1.1.2.1  2005/02/25 12:33:27  nw
123 finished transactional store
124  
125 */