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