View Javadoc

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