buildframework/helium/sf/java/sysdef/src/com/nokia/helium/sysdef/ant/taskdefs/CreateRootSysdefTask.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    32 import org.apache.tools.ant.Project;
    32 import org.apache.tools.ant.Project;
    33 import org.apache.tools.ant.Task;
    33 import org.apache.tools.ant.Task;
    34 import org.apache.tools.ant.types.Resource;
    34 import org.apache.tools.ant.types.Resource;
    35 import org.apache.tools.ant.types.ResourceCollection;
    35 import org.apache.tools.ant.types.ResourceCollection;
    36 
    36 
       
    37 import com.nokia.helium.sysdef.PackageDefinition;
       
    38 import com.nokia.helium.sysdef.PackageDefinitionParsingException;
    37 import com.nokia.helium.sysdef.PackageMap;
    39 import com.nokia.helium.sysdef.PackageMap;
    38 import com.nokia.helium.sysdef.PackageMapParsingException;
    40 import com.nokia.helium.sysdef.PackageMapParsingException;
    39 
    41 
    40 import freemarker.cache.ClassTemplateLoader;
    42 import freemarker.cache.ClassTemplateLoader;
    41 import freemarker.cache.FileTemplateLoader;
    43 import freemarker.cache.FileTemplateLoader;
    48  * file. All package must also have a package_data.xml file in order to retrieve
    50  * file. All package must also have a package_data.xml file in order to retrieve
    49  * the root and the layer location of the package.
    51  * the root and the layer location of the package.
    50  *
    52  *
    51  * Example:
    53  * Example:
    52  * <pre>
    54  * <pre>
    53  * &lt;hlm:createPackageMapping epocroot="E:\sdk" destFile="E:\sdk\sysdef_root.xml" &gt;
    55  * &lt;hlm:createRootSysdef epocroot="E:\sdk" destFile="E:\sdk\sysdef_root.xml" &gt;
    54  *     &lt;fileset dir="E:\sdk" includes="root/&#42;&#42;/package_definition.xml"&gt;
    56  *     &lt;fileset dir="E:\sdk" includes="root/&#42;&#42;/package_definition.xml"&gt;
    55  * &lt;/hlm:createPackageMapping&gt;
    57  * &lt;/hlm:createRootSysdef&gt;
    56  * </pre>
    58  * </pre>
    57  *
    59  *
    58  * @ant.task name="createRootSysdef" category="Sysdef"
    60  * @ant.task name="createRootSysdef" category="Sysdef"
    59  */
    61  */
    60 public class CreateRootSysdefTask extends Task {
    62 public class CreateRootSysdefTask extends Task {
    61 
    63 
    62     private File destFile;
    64     private File destFile;
    63     private List<ResourceCollection> resourceCollections = new ArrayList<ResourceCollection>(); 
    65     private List<ResourceCollection> resourceCollections = new ArrayList<ResourceCollection>(); 
    64     private Map<String, Map<String, List<String>>> roots = new HashMap<String, Map<String, List<String>>>();
    66     private Map<String, Map<String, List<Map<String, Object>>>> roots = new HashMap<String, Map<String, List<Map<String, Object>>>>();
    65     private Map<String, List<String>> layers = new HashMap<String, List<String>>();
    67     private Map<String, List<String>> layers = new HashMap<String, List<String>>();
    66     private File epocroot;
    68     private File epocroot;
    67     private boolean failOnError = true;
    69     private boolean failOnError = true;
    68     private boolean checkPackageExists;
    70     private boolean checkPackageExists;
    69     private File template;
    71     private File template;
       
    72     private String idNamespace;
    70 
    73 
    71     /**
    74     /**
    72      * {@inheritDoc}
    75      * {@inheritDoc}
    73      */
    76      */
    74     @SuppressWarnings("unchecked")
    77     @SuppressWarnings("unchecked")
    99                 File pkgMapFile  = new File(pkgDir, CreatePackageMappingTask.PACKAGE_MAP_FILENAME);
   102                 File pkgMapFile  = new File(pkgDir, CreatePackageMappingTask.PACKAGE_MAP_FILENAME);
   100                 try {
   103                 try {
   101                     if (pkgMapFile.exists()) {
   104                     if (pkgMapFile.exists()) {
   102                         log("Package map file: " + pkgMapFile);
   105                         log("Package map file: " + pkgMapFile);
   103                         if (!checkPackageExists) {
   106                         if (!checkPackageExists) {
   104                             addPackage(pkgMapFile, pkgDir.getName());
   107                             addPackage(pkgDefFile, pkgMapFile, pkgDir.getName());
   105                         } else {
   108                         } else {
   106                             PackageMap pkgMap = new PackageMap(pkgMapFile);
   109                             PackageMap pkgMap = new PackageMap(pkgMapFile);
   107                             File destPkg = new File(epocroot, pkgMap.getRoot() + File.separator +
   110                             File destPkg = new File(epocroot, pkgMap.getRoot() + File.separator +
   108                                     pkgMap.getLayer() + File.separator + pkgDir.getName() + File.separator +
   111                                     pkgMap.getLayer() + File.separator + pkgDir.getName() + File.separator +
   109                                     CreatePackageMappingTask.PACKAGE_DEFINITION_FILENAME);
   112                                     CreatePackageMappingTask.PACKAGE_DEFINITION_FILENAME);
   110                             if (destPkg.exists()) {
   113                             if (destPkg.exists()) {
   111                                 addPackage(pkgMapFile, pkgDir.getName());
   114                                 addPackage(pkgDefFile, pkgMapFile, pkgDir.getName());
   112                             } else {
   115                             } else {
   113                                 log("Could not find " + destPkg.getAbsolutePath() +
   116                                 log("Could not find " + destPkg.getAbsolutePath() +
   114                                         " so entry is not added to the root system definition.", Project.MSG_ERR);
   117                                         " so entry is not added to the root system definition.", Project.MSG_ERR);
   115                             }
   118                             }
   116                         }
   119                         }
   117                     } else {
   120                     } else {
   118                         pkgMapFile = new File(pkgDir.getParentFile().getParentFile(), CreatePackageMappingTask.PACKAGE_MAP_FILENAME);
   121                         pkgMapFile = new File(pkgDir.getParentFile().getParentFile(), CreatePackageMappingTask.PACKAGE_MAP_FILENAME);
   119                         log("Package map file: " + pkgMapFile);
   122                         log("Package map file: " + pkgMapFile);
   120                         if (pkgMapFile.exists()) {
   123                         if (pkgMapFile.exists()) {
   121                             if (!checkPackageExists) {
   124                             if (!checkPackageExists) {
   122                                 addPackage(pkgMapFile, pkgDir.getName());
   125                                 // slash must be use to generate correct path.
       
   126                                 addPackage(pkgDefFile, pkgMapFile, pkgMapFile.getParentFile().getName() + "/" +
       
   127                                         pkgDir.getParentFile().getName() + "/" + pkgDir.getName());
   123                             } else {
   128                             } else {
   124                                 PackageMap pkgMap = new PackageMap(pkgMapFile);
   129                                 PackageMap pkgMap = new PackageMap(pkgMapFile);
   125                                 File destPkg = new File(epocroot, pkgMap.getRoot() + File.separator +
   130                                 File destPkg = new File(epocroot, pkgMap.getRoot() + File.separator +
   126                                         pkgMap.getLayer() + File.separator +
   131                                         pkgMap.getLayer() + File.separator +
   127                                         pkgMapFile.getParentFile().getName() + File.separator +
   132                                         pkgMapFile.getParentFile().getName() + File.separator +
   128                                         pkgDir.getParentFile().getName() + File.separator + pkgDir.getName() +
   133                                         pkgDir.getParentFile().getName() + File.separator + pkgDir.getName() +
   129                                         File.separator +
   134                                         File.separator +
   130                                         CreatePackageMappingTask.PACKAGE_DEFINITION_FILENAME);
   135                                         CreatePackageMappingTask.PACKAGE_DEFINITION_FILENAME);
   131                                 if (destPkg.exists()) {
   136                                 if (destPkg.exists()) {
   132                                     addPackage(pkgMapFile, pkgDir.getParentFile().getName() +
   137                                     addPackage(destPkg, pkgMapFile, pkgMapFile.getParentFile().getName() + "/" +
   133                                             File.separator + pkgDir.getName());
   138                                             pkgDir.getParentFile().getName() + "/" + pkgDir.getName());
   134                                 } else {
   139                                 } else {
   135                                     log("Could not find " + destPkg.getAbsolutePath() +
   140                                     log("Could not find " + destPkg.getAbsolutePath() +
   136                                             " so entry is not added to the root system definition.", Project.MSG_ERR);
   141                                             " so entry is not added to the root system definition.", Project.MSG_ERR);
   137                                 }
   142                                 }
   138                             }
   143                             }
   139                         } else {
   144                         } else {
   140                             log("Could not find: " + pkgMapFile.toString(), Project.MSG_ERR);
   145                             log("Could not find package_map.xml file for " + pkgDefFile.toString(), Project.MSG_ERR);
   141                             if (shouldFailOnError()) {
   146                             if (shouldFailOnError()) {
   142                                 throw new BuildException("Could not find:" + pkgMapFile.toString());
   147                                 throw new BuildException("Could not find package_map.xml file for " + pkgDefFile.toString());
   143                             }
   148                             }
   144                         }
   149                         }
   145                     }
   150                     }
   146                 } catch (PackageMapParsingException e) {
   151                 } catch (PackageMapParsingException e) {
   147                     log("Invalid " + CreatePackageMappingTask.PACKAGE_MAP_FILENAME 
   152                     log("Invalid " + CreatePackageMappingTask.PACKAGE_MAP_FILENAME 
   148                             + " file: " + pkgMapFile.toString() 
   153                             + " file: " + pkgMapFile.toString() 
   149                             + "(" + e.getMessage() + ")", Project.MSG_ERR);
   154                             + "(" + e.getMessage() + ")", Project.MSG_ERR);
   150                     if (shouldFailOnError()) {
   155                     if (shouldFailOnError()) {
   151                         throw new BuildException(e.getMessage(), e);
   156                         throw new BuildException(e.getMessage(), e);
   152                     }
   157                     }
       
   158                 } catch (PackageDefinitionParsingException e) {
       
   159                     log("Invalid " + CreatePackageMappingTask.PACKAGE_DEFINITION_FILENAME 
       
   160                             + "(" + e.getMessage() + ")", Project.MSG_ERR);
       
   161                     if (shouldFailOnError()) {
       
   162                         throw new BuildException(e.getMessage(), e);
       
   163                     }
   153                 }
   164                 }
   154                     
   165                     
   155             }
   166             }
   156         }
   167         }
   157         generateRootSysdef();
   168         generateRootSysdef();
   158     }
   169     }
   159 
   170 
   160     private void addPackage(File pkgMapFile, String pkgPath) throws 
   171     private void addPackage(File pkgDefinition, File pkgMapFile, String pkgPath) throws
   161         PackageMapParsingException {
   172         PackageMapParsingException, PackageDefinitionParsingException {
       
   173         // Some quick validity checking. 
       
   174         PackageDefinition pkg = new PackageDefinition(pkgDefinition);
       
   175         if (idNamespace == null && (pkg.getIdNamespace() != null && pkg.getIdNamespace().length() > 0)) {
       
   176             idNamespace = pkg.getIdNamespace();
       
   177         } else if (idNamespace != null && !idNamespace.equals(pkg.getIdNamespace())) {
       
   178             log("Warning: " + pkgDefinition.toString() +
       
   179                     " namespace doesn't match the default one. (" 
       
   180                     + idNamespace + " != " + pkg.getIdNamespace(), Project.MSG_WARN);
       
   181         }
       
   182             
       
   183         // Adding the package in the structure.
   162         log("Adding: " + pkgMapFile, Project.MSG_DEBUG);
   184         log("Adding: " + pkgMapFile, Project.MSG_DEBUG);
   163         // Adding the package in the structure.
       
   164         PackageMap pkgMap = new PackageMap(pkgMapFile);
   185         PackageMap pkgMap = new PackageMap(pkgMapFile);
   165         if (!roots.containsKey(pkgMap.getRoot())) {
   186         if (!roots.containsKey(pkgMap.getRoot())) {
   166             roots.put(pkgMap.getRoot(), new HashMap<String, List<String>>());
   187             roots.put(pkgMap.getRoot(), new HashMap<String, List<Map<String, Object>>>());
   167         }
   188         }
   168         if (!roots.get(pkgMap.getRoot()).containsKey(pkgMap.getLayer())) {
   189         if (!roots.get(pkgMap.getRoot()).containsKey(pkgMap.getLayer())) {
   169             roots.get(pkgMap.getRoot()).put(pkgMap.getLayer(), new ArrayList<String>());
   190             roots.get(pkgMap.getRoot()).put(pkgMap.getLayer(), new ArrayList<Map<String, Object>>());
   170             layers.put(pkgMap.getLayer(), new ArrayList<String>());
   191             layers.put(pkgMap.getLayer(), new ArrayList<String>());
   171         }
   192         }
   172         roots.get(pkgMap.getRoot()).get(pkgMap.getLayer()).add(pkgPath);
   193         Map<String, Object> data = new HashMap<String, Object>();
       
   194         data.put("path", pkgPath);
       
   195         data.put("id", pkg.getId());
       
   196         data.put("namespaces", pkg.getNamespaces());
       
   197         roots.get(pkgMap.getRoot()).get(pkgMap.getLayer()).add(data);
   173         layers.get(pkgMap.getLayer()).add(pkgPath);
   198         layers.get(pkgMap.getLayer()).add(pkgPath);
   174     }
   199     }
   175     
   200     
   176     /**
   201     /**
   177      * Defines the location of the generated INI file.
   202      * Defines the location of the generated INI file.
   280             Map<String, Object> data = new Hashtable<String, Object>();
   305             Map<String, Object> data = new Hashtable<String, Object>();
   281             // Content by root
   306             // Content by root
   282             data.put("roots", roots);
   307             data.put("roots", roots);
   283             // Content by layer
   308             // Content by layer
   284             data.put("layers", layers);
   309             data.put("layers", layers);
       
   310             // id-namespace
       
   311             if (idNamespace != null) {
       
   312                 data.put("idnamespace", idNamespace);
       
   313             }
   285             // Environment location
   314             // Environment location
   286             data.put("epocroot", getEpocroot().getAbsolutePath());
   315             data.put("epocroot", getEpocroot().getAbsolutePath());
   287             // Relative path from destFile to epocroot.
   316             // Relative path from destFile to epocroot.
   288             data.put("dest_dir_to_epocroot", getRelativeDiff());
   317             data.put("dest_dir_to_epocroot", getRelativeDiff());
   289             StringWriter out = new StringWriter();
   318             StringWriter out = new StringWriter();
   311      * Get the relative path to go to epocroot from destdir.
   340      * Get the relative path to go to epocroot from destdir.
   312      * @return the path relative path to go to root.
   341      * @return the path relative path to go to root.
   313      */
   342      */
   314     protected String getRelativeDiff() {
   343     protected String getRelativeDiff() {
   315         String rel = getEpocroot().toURI().relativize(getDestFile().getParentFile().toURI()).getPath();
   344         String rel = getEpocroot().toURI().relativize(getDestFile().getParentFile().toURI()).getPath();
   316         String[] relArray = rel.split("/"); // This is an URI not a File.
   345         if (rel.length() > 0) {
   317         rel = "";
   346             String[] relArray = rel.split("/"); // This is an URI not a File.
   318         for (String string : relArray) {
   347             rel = ""; //"." + File.separatorChar;
   319             rel += ".." + File.separatorChar; 
   348             for (@SuppressWarnings("unused") String string : relArray) {
       
   349                 rel += ".." + File.separatorChar; 
       
   350             }
       
   351         } else {
       
   352             rel = "." + File.separatorChar;
   320         }
   353         }
   321         return rel;
   354         return rel;
   322     }
   355     }
   323 }
   356 }