View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.model.reader;
35  
36  import info.magnolia.module.model.DependencyDefinition;
37  import info.magnolia.module.model.ModuleDefinition;
38  
39  import java.util.ArrayList;
40  import java.util.Collections;
41  import java.util.Comparator;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * A comparator used to sort modules according to their dependencies.
47   * It hardcodes "core" to be first and "webapp" to be last.
48   */
49  class DependencyLevelComparator implements Comparator<ModuleDefinition> {
50      private final Map<String, ModuleDefinition> allKnownModulesDefinitions;
51  
52      DependencyLevelComparator(Map<String, ModuleDefinition> allKnownModulesDefinitions) {
53          this.allKnownModulesDefinitions = allKnownModulesDefinitions;
54      }
55  
56      @Override
57      public int compare(ModuleDefinition../../../info/magnolia/module/model/ModuleDefinition.html#ModuleDefinition">ModuleDefinition def1, ModuleDefinition def2) {
58  
59          // the core module must always be installed/updated/started first
60          if ("core".equals(def1.getName())) {
61              return -1;
62          } else if ("core".equals(def2.getName())) {
63              return 1;
64          }
65  
66          // the webapp module must always be installed/updated/started last
67          if ("webapp".equals(def1.getName())) {
68              return 1;
69          } else if ("webapp".equals(def2.getName())) {
70              return -1;
71          }
72  
73          int level1 = calcDependencyDepth(def1);
74          int level2 = calcDependencyDepth(def2);
75  
76          // lower level first
77          int dif = level1 - level2;
78          if (dif != 0) {
79              return dif;
80          }
81  
82          // rest is ordered alphabetically
83          return def1.getName().compareTo(def2.getName());
84  
85      }
86  
87      /**
88       * Calculates the depth of dependency. 0 means no dependency. If none of the dependencies
89       * has itself dependencies, the level will be 1. If one or more of the dependencies has
90       * dependencies that has a dependency it would return 2. And so on...
91       *
92       * @param def module definition
93       * @return the level
94       */
95      protected int calcDependencyDepth(ModuleDefinition def) {
96          if (def.getDependencies() == null || def.getDependencies().size() == 0) {
97              return 0;
98          }
99          final List<Integer> dependencyLevels = new ArrayList<Integer>();
100         for (final DependencyDefinition dep : def.getDependencies()) {
101             final ModuleDefinition depDef = allKnownModulesDefinitions.get(dep.getName());
102             if (depDef == null && !dep.isOptional()) {
103                 throw new RuntimeException("Missing definition for module:" + dep.getName());
104             } else if (depDef != null) {
105                 dependencyLevels.add(Integer.valueOf(calcDependencyDepth(depDef)));
106             } else {
107                 // optional dependency not present (will return 0 if no other dependencies add higher level)
108                 dependencyLevels.add(-1);
109             }
110         }
111         return (Collections.max(dependencyLevels)).intValue() + 1;
112     }
113 
114     /**
115      * @deprecated since Magnolia 4.1, renamed to calcDependencyDepth()
116      */
117     @Deprecated
118     protected int calcDependencyLevel(ModuleDefinition def) {
119         return calcDependencyDepth(def);
120     }
121 
122 }