View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package org.apache.jdo.impl.enhancer;
18  
19  import java.io.PrintWriter;
20  import java.io.IOException;
21  import java.io.FileNotFoundException;
22  import java.io.InputStream;
23  import java.io.FileInputStream;
24  import java.io.BufferedInputStream;
25  import java.io.File;
26  
27  import org.apache.jdo.impl.enhancer.util.PathResourceLocator;
28  
29  
30  
31  /***
32   * Base class for JDO command line enhancer and tests.
33   *
34   * @author Martin Zaun
35   */
36  public class ClassArgMain
37      extends GenericMain
38  {
39      /***
40       *  The options and arguments.
41       */
42      protected ClassArgOptions options;
43  
44      /***
45       *  The locator for classes.
46       */
47      protected PathResourceLocator classes;
48  
49      /***
50       * Creates an instance.
51       */
52      public ClassArgMain(PrintWriter out,
53                          PrintWriter err)
54      {
55          this(out, err, new ClassArgOptions(out, err));
56      }
57  
58      /***
59       * Creates an instance.
60       */
61      public ClassArgMain(PrintWriter out,
62                          PrintWriter err,
63                          ClassArgOptions options)
64      {
65          super(out, err, options);
66          this.options = options;
67      }
68  
69      // ----------------------------------------------------------------------
70  
71      /***
72       * Initializes the class locator.
73       */
74      protected void initClassLocator()
75          throws IOException
76      {
77          // create resource locator for specified source path
78          final String path = options.sourcePath.value;
79          if (path != null) {
80              affirm(path.length() > 0);
81              final boolean verbose = options.verbose.value;
82              classes = new PathResourceLocator(out, verbose, path);
83          }
84      }
85  
86      /***
87       * Initializes all components.
88       */
89      protected void init()
90          throws EnhancerFatalError, EnhancerUserException
91      {
92          try {
93              initClassLocator();
94          } catch (Exception ex) {
95              throw new EnhancerFatalError(ex);
96          }
97      }
98      
99      // ----------------------------------------------------------------------
100 
101     /***
102      *  Returns the file name for a class name.
103      *  This is done by replacing <code>'.'</code> by <code>'/'</code>.
104      *
105      *  @param  className  the classname
106      *  @return  the filename
107      */
108     static protected String getClassFileName(String className)
109     {
110         return className.replace('.', '/') + ".class";
111     }
112 
113     /***
114      *  Opens an input stream for the given filename
115      *
116      *  @param  fileName  the name of the file
117      *  @return  the input stream
118      *  @exception  FileNotFoundException  if the file could not be found
119      */
120     protected InputStream openFileInputStream(String fileName)
121         throws FileNotFoundException
122     {
123         affirm(fileName != null);
124         //^olsen: support for timing
125         //if (options.doTiming.value) {...}
126      	return new BufferedInputStream(new FileInputStream(fileName));
127     }
128 
129     /***
130      * Opens an input stream for the given classname. The input stream is
131      * created via an URL that is obtained by the value of the sourcepath
132      * option and zip/jar file arguments.
133      * 
134      * @param  className  the name of the class (dot-notation)
135      * @return  the input stream
136      * @exception IOException if an I/O error occured
137      */
138     protected InputStream openClassInputStream(String className)
139         throws IOException
140     {
141         affirm(className != null);
142         final String resName = className.replace('.', '/') + ".class";
143         //^olsen: support for timing
144         //if (options.doTiming.value) {...}
145         final InputStream s = classes.getInputStreamForResource(resName);
146         affirm(s != null);
147         return new BufferedInputStream(s);
148     }
149 
150     /***
151      *  Closes an input stream.
152      *
153      *  @param  in  the input stream
154      */
155     protected void closeInputStream(InputStream in)
156     {
157         if (in != null) {
158             try {
159                 in.close();
160             } catch (IOException ex) {
161                 printlnErr("", ex);
162             }
163         }
164     }
165 
166     // ----------------------------------------------------------------------
167 
168     /***
169      * Runs this class
170      */
171     static public void main(String[] args)
172     {
173         final PrintWriter out = new PrintWriter(System.out, true);
174         out.println("--> ClassArgMain.main()");
175         final ClassArgMain main = new ClassArgMain(out, out);
176         int res = main.run(args);
177         out.println("<-- ClassArgMain.main(): exit = " + res);
178         System.exit(res);
179     }
180 }