View Javadoc

1   /*
2    * UML2MMBase module.
3    *
4    * The contents of this file are subject to the Mozilla Public License
5    * Version 1.0 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11   * License for the specific language governing rights and limitations
12   * under the License.
13   */
14  
15  package com.finalist.mmbase.modules;
16  
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.util.*;
20  import java.net.URL;
21  import org.andromda.core.mdr.MDRepositoryFacade;
22  import com.finalist.mmbase.uml.MMBasePackageHelper;
23  
24  /***
25   * The MMBasePackage handler is used in the postprocessing part of UML2MMBase.
26   * It will check which UMLPackages in the model haven been marked with the MMBasePackage stereotype.
27   * For these packages the package name will be used to do some package specific post processing.
28   *
29   * @author Rudie Ekkelenkamp
30   * @version $Revision: 1.5 $, $Date: 2005/07/12 10:31:59 $
31   */
32  public class MMBasePackageHandler {
33  
34      private static Properties properties;
35      /*
36        Static code to load property file
37      */
38      static {
39  
40         try {
41            properties = new Properties();
42            File file = new File("conf" + File.separator + "/uml2mmbase-package-generator.properties");
43            System.out.println("Trying to read file: " + file.getAbsolutePath());
44            properties.load(new FileInputStream(file));
45            System.out.println("The uml2mmbase-package-generator.properties was read.");
46         }
47         catch (Exception e) {
48            System.out.println("Error while reading : /conf/uml2mmbase-package-generator.properties");
49            e.printStackTrace();
50         }
51      }
52  
53  
54     /***
55      * Creates a new package handler.
56      */
57     private MMBasePackageHandler() {
58     }
59  
60  
61     /***
62      * This method will be called from the ant task after all code generation has been done.
63      *
64      * @param args the commandline arguments;
65      *               The first argument should be the name of the model. For example: model.xmi
66      *               The second argument should be the generated dir where all code generation has been done.
67      *                       For example: generated
68      *               The third argument should be the mmbaseVersion number. For example: 1.8
69      *
70      * @throws Exception an exception.
71      */
72     public static void main(String[] args) throws Exception {
73        if (args.length != 3) {
74           System.out.println("Three parameters expected for the MMbase Package Handler!");
75           return;
76        }
77  
78        String modelName = args[0];
79        String generatedDir = args[1];
80        String mmbaseVersion = args[2];
81        MMBasePackageHelper mmbasePackageHelper = null;
82        MDRepositoryFacade mdr = new MDRepositoryFacade();
83  
84        String [] modules = {"modules","profiles"};
85        loadModel(mdr, modelName, modules);
86        mmbasePackageHelper = new MMBasePackageHelper(mdr.getModel());
87     
88        Collection mmbasePackages = mmbasePackageHelper.getMMBasePackages();
89         for (Iterator iterator = mmbasePackages.iterator(); iterator.hasNext();) {
90             String packageName =  (String) iterator.next();
91  
92             // Set the configuration with the passed arguments.
93             MMBasePackageConfiguration config = new MMBasePackageConfiguration();
94             config.setGeneratedDir(generatedDir);
95             config.setMmbaseVersion(mmbaseVersion);
96             config.setModelName(modelName);
97             config.setPackageName(packageName);
98  
99             // Not check if a mapping to a java class was speficied for this package.
100            String className = null;
101            if (properties != null) {
102              className = (String) properties.getProperty(packageName);
103            }
104            if (className == null) {
105                // If no mapping was specified, we use the default generator.
106                className = MMBasePackageGenerator.DEFAULT_MMBASE_PACKAGE_GENERATOR;
107            }
108            try {
109                // Now use the classloader to get the implementation for this package.
110                MMBasePackageGenerator generator = (MMBasePackageGenerator) Class.forName(className).newInstance();
111                 // Now do the actual generation: init, process, destroy.
112                generator.init(config);
113                generator.process(config);
114                generator.destroy(config);
115            } catch (ClassNotFoundException e) {
116                System.out.println("Couldn't find the custom classname " + className + " for package: " + packageName);
117                System.out.println("Skipping the MMBasePackage generation for this package.");
118            }
119        }
120       closeModel(mdr);
121    }
122 
123     /***
124      *
125      * Load the model into the MDR.
126      * @param modelName The reference to the model on which the tests will be applied.
127      */
128     private static void loadModel(MDRepositoryFacade mdr, String modelName, String[] modules) {
129        System.out.println("Reading a new XMI file: " + modelName + ".");
130        mdr.open();
131 
132        try {
133           // Read the model file.
134           URL xmiURL = (new File(modelName)).toURL();
135           mdr.readModel(xmiURL, modules);
136        }
137        catch (Exception e) {
138           e.printStackTrace();
139        }
140     }
141 
142     /***
143      * Close the MDR.
144      * @param mdr Meta Data Repository
145      */
146     private static void closeModel(MDRepositoryFacade mdr) {
147        System.out.println("Closing de MDR.");
148        try {
149        if (mdr != null) {
150            mdr.close();
151        }
152        }
153        catch (Exception e) {
154           e.printStackTrace();
155        }
156     }
157 }