View Javadoc

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