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  
22  import java.util.Properties;
23  
24  import org.apache.jdo.impl.enhancer.meta.EnhancerMetaData;
25  import org.apache.jdo.impl.enhancer.meta.EnhancerMetaDataFatalError;
26  import org.apache.jdo.impl.enhancer.meta.model.EnhancerMetaDataJDOModelImpl;
27  import org.apache.jdo.impl.enhancer.meta.prop.EnhancerMetaDataPropertyImpl;
28  import org.apache.jdo.impl.enhancer.meta.util.EnhancerMetaDataTimer;
29  
30  /***
31   * Base class for JDO command line enhancer and tests.
32   *
33   * @author Martin Zaun
34   */
35  public class JdoMetaMain
36      extends ClassArgMain
37  {
38      /***
39       *  The options and arguments.
40       */
41      protected JdoMetaOptions options;
42  
43      /***
44       *  The metadata.
45       */
46      protected EnhancerMetaData jdoMeta;
47  
48      /***
49       * Creates an instance.
50       */
51      public JdoMetaMain(PrintWriter out,
52                         PrintWriter err)
53      {
54          this(out, err, new JdoMetaOptions(out, err));
55      }
56  
57      /***
58       * Creates an instance.
59       */
60      public JdoMetaMain(PrintWriter out,
61                         PrintWriter err,
62                         JdoMetaOptions options)
63      {
64          super(out, err, options);
65          this.options = options;
66      }
67  
68      // ----------------------------------------------------------------------
69  
70      /***
71       * Initializes the jdo metadata component.
72       */
73      protected void initJdoMetaData()
74          throws EnhancerMetaDataFatalError
75      {
76          final boolean verbose = options.verbose.value;
77          final String path = options.jdoPath.value;
78          final String jdoPropsFile = options.jdoPropertiesFile.value;
79  
80          if (jdoPropsFile != null && jdoPropsFile.length() > 0) {
81              // read JDO metadata from properties file
82              if (path != null && path.length() > 0) {
83                  // load the properties file using the path specified with
84                  // -j (if available)
85                  try {
86                      final Properties props = new Properties();
87                      props.load(classes.getInputStreamForResource(jdoPropsFile));
88                      jdoMeta = new EnhancerMetaDataPropertyImpl(out, 
89                                                                 verbose, 
90                                                                 props);
91                  } catch (IOException ex) {
92                      throw new EnhancerMetaDataFatalError(ex);
93                  }  
94              } else {
95                  // no -j option => take the properties file name as it is
96                  jdoMeta = new EnhancerMetaDataPropertyImpl(out, 
97                                                             verbose, 
98                                                             jdoPropsFile);
99              }
100         } else {
101             //^olsen: simplify interface; just append archives to jdo-path
102             jdoMeta = new EnhancerMetaDataJDOModelImpl(
103                 out, verbose,
104                 null,
105                 options.archiveFileNames,
106                 path);
107         }
108 
109 //^olsen: add archives to path locator...
110 /*
111             // create resource locator for specified zip files
112             if (archiveFileNames != null && !archiveFileNames.isEmpty()) {
113                 final StringBuffer s = new StringBuffer();
114                 final Iterator i = archiveFileNames.iterator();
115                 s.append(i.next());
116                 while (i.hasNext()) {
117                     s.append(File.pathSeparator + i.next());
118                 }
119                 final ResourceLocator zips
120                     = new PathResourceLocator(out, verbose, s.toString());
121                 printMessage(getI18N("enhancer.using_zip_files",
122                                      s.toString()));
123                 locators.add(zips);
124             }
125 */
126 
127         // wrap with timing meta data object
128         if (options.doTiming.value) {
129             jdoMeta = new EnhancerMetaDataTimer(jdoMeta);
130         }
131     }
132 
133     /***
134      * Initializes all components.
135      */
136     protected void init()
137         throws EnhancerFatalError, EnhancerUserException
138     {
139         super.init();
140         try {
141             initJdoMetaData();
142         } catch (Exception ex) {
143             throw new EnhancerFatalError(ex);
144         }
145     }
146 
147     // ----------------------------------------------------------------------
148 
149     /***
150      * Runs this class
151      */
152     static public void main(String[] args)
153     {
154         final PrintWriter out = new PrintWriter(System.out, true);
155         out.println("--> JdoMetaMain.main()");
156         final JdoMetaMain main = new JdoMetaMain(out, out);
157         int res = main.run(args);
158         out.println("<-- JdoMetaMain.main(): exit = " + res);
159         System.exit(res);
160     }
161 }