View Javadoc

1   /*
2    * @(#)TemplateManager.java   1.0
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;
12  
13  import java.util.Hashtable ;
14  import java.io.IOException ;
15  import java.io.BufferedInputStream ;
16  import org.astrogrid.log.Log;
17  import org.astrogrid.i18n.AstroGridMessage ;
18  
19  import org.jconfig.utils.*;
20  import java.io.* ;
21  
22  
23  /***
24   * The <code>TemplateManager</code> class represents
25   *
26   * @author  Jeff Lusted
27   * @version 1.0 08-Jul-2003
28   * @see
29   * @see
30   * @since   AstroGrid 1.2
31   */
32  public class TemplateManager {
33  
34      private static TemplateManager
35          singletonTemplateManager = new TemplateManager() ;
36  
37     private static String
38         ASTROGRIDERROR_UNABLE_TO_LOCATE_TEMPLATE = "AG{0}Z00007:{1}: Unable to locate template file [{2}]" ;
39  
40      private Hashtable
41          templates = new Hashtable() ;
42  
43  
44     public static TemplateManager getInstance(){ return singletonTemplateManager; }
45  
46  
47      private TemplateManager() {}
48  
49  
50      public String getTemplate( String subsystemAcronym, String name ) {
51        Log.trace( "getTemplate(): entry") ;
52  
53        String
54           targetTemplate = null ;
55        BufferedInputStream
56           bistream = null ;
57  
58        try {
59  
60           targetTemplate = (String)templates.get( name ) ;
61  
62           if( targetTemplate == null ) {
63  
64                  ResourceLocator
65                      resourceLocator = new ResourceLocator( name ) ;
66  
67                  BufferedReader
68                      bufferedReader = new BufferedReader( new FileReader( resourceLocator.getFile() ) ) ;
69  
70              StringBuffer
71                  sBuffer = new StringBuffer( 1024 ) ;
72                  String
73                      line = null ;
74  
75              line = bufferedReader.readLine() ;
76               while( line != null ){
77                 sBuffer.append( line ) ;
78                      line = bufferedReader.readLine() ;
79               }
80  
81              targetTemplate = sBuffer.toString() ;
82              templates.put( name, targetTemplate ) ;
83  
84           } // endif
85  
86        }
87        catch ( IOException ex ) {
88  
89           AstroGridMessage
90              message = new AstroGridMessage( ASTROGRIDERROR_UNABLE_TO_LOCATE_TEMPLATE
91                                                , subsystemAcronym
92                                                , Configurator.getClassName( TemplateManager.class )
93                                                , name ) ;
94           Log.logError( message.toString(), ex ) ;
95  
96        }
97        finally {
98           if( bistream != null ) try{ bistream.close(); } catch( IOException ioex ){ ; }
99           Log.trace( "getTemplate(): exit") ;
100       }
101 
102       return targetTemplate ;
103 
104     } // end of getTemplate()
105 
106 
107 } // end of class TemplateManager