View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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   * @author gjoseph
50   * @version $Revision: $ ($Author: $)
51   */
52  class DependencyLevelComparator implements Comparator<ModuleDefinition> {
53      private final Map<String, ModuleDefinition> allKnownModulesDefinitions;
54  
55      DependencyLevelComparator(Map<String, ModuleDefinition> allKnownModulesDefinitions) {
56          this.allKnownModulesDefinitions = allKnownModulesDefinitions;
57      }
58  
59      @Override
60      public int compare(ModuleDefinition def1, ModuleDefinition def2) {
61  
62          // the core module must always be installed/updated/started first
63          if ("core".equals(def1.getName())) {
64              return -1;
65          } else if ("core".equals(def2.getName())) {
66              return 1;
67          }
68  
69          // the webapp module must always be installed/updated/started last
70          if ("webapp".equals(def1.getName())) {
71              return 1;
72          } else if ("webapp".equals(def2.getName())) {
73              return -1;
74          }
75  
76          int level1 = calcDependencyDepth(def1);
77          int level2 = calcDependencyDepth(def2);
78  
79          // lower level first
80          int dif = level1 - level2;
81          if (dif != 0) {
82              return dif;
83          }
84  
85          // rest is ordered alphabetically
86          return def1.getName().compareTo(def2.getName());
87  
88      }
89  
90      /**
91       * Calculates the depth of dependency. 0 means no dependency. If none of the dependencies
92       * has itself dependencies, the level will be 1. If one or more of the dependencies has
93       * dependencies that has a dependency it would return 2. And so on...
94       *
95       * @param def module definition
96       * @return the level
97       */
98      protected int calcDependencyDepth(ModuleDefinition def) {
99          if (def.getDependencies() == null || def.getDependencies().size() == 0) {
100             return 0;
101         }
102         final List<Integer> dependencyLevels = new ArrayList<Integer>();
103         for (final DependencyDefinition dep : def.getDependencies()) {
104             final ModuleDefinition depDef = allKnownModulesDefinitions.get(dep.getName());
105             if (depDef == null && !dep.isOptional()) {
106                 throw new RuntimeException("Missing definition for module:" + dep.getName());
107             } else if (depDef != null) {
108                 dependencyLevels.add(Integer.valueOf(calcDependencyDepth(depDef)));
109             } else {
110                 // optional dependency not present (will return 0 if no other dependencies add higher level)
111                 dependencyLevels.add(-1);
112             }
113         }
114         return (Collections.max(dependencyLevels)).intValue() + 1;
115     }
116 
117     /**
118      * @deprecated since Magnolia 4.1, renamed to calcDependencyDepth()
119      */
120     protected int calcDependencyLevel(ModuleDefinition def) {
121         return calcDependencyDepth(def);
122     }
123 
124 }