View Javadoc
1   /*
2    * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and contributors.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the GNU Lesser General Public License
6    * (LGPL) version 2.1 which accompanies this distribution, and is available at
7    * http://www.gnu.org/licenses/lgpl-2.1.html
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * Contributors:
15   *     bstefanescu
16   */
17  package org.nuxeo.build.ant.profile;
18  
19  import org.apache.tools.ant.BuildException;
20  
21  /**
22   * TODO NXBT-258
23   */
24  public class ProfileGroup {
25  
26      protected Profile defaultProfile;
27  
28      protected Profile[] profiles;
29  
30      public ProfileGroup(AntProfileManager mgr, String[] profiles,
31              String defaultProfile) {
32          if (defaultProfile != null) {
33              this.defaultProfile = mgr.getOrCreateProfile(defaultProfile);
34          }
35          this.profiles = new Profile[profiles.length];
36          Profile activeProfile = null;
37          for (int i = 0; i < profiles.length; i++) {
38              String profileName = profiles[i];
39              Profile profile = mgr.getOrCreateProfile(profileName);
40              if (profile.group != null) {
41                  throw new BuildException(
42                          "A profile is part of 2 distinct groups: "
43                                  + profileName);
44              }
45              profile.group = this;
46              if (profile.isActive()) {
47                  if (activeProfile != null) {
48                      throw new BuildException(
49                              "Profile Group has 2 active profiles: "
50                                      + activeProfile.getName() + ", "
51                                      + profileName);
52                  }
53                  activeProfile = profile;
54              }
55              this.profiles[i] = profile;
56          }
57          if (activeProfile == null && this.defaultProfile != null) {
58              this.defaultProfile._setActive(true);
59          }
60      }
61  
62      void activateProfile(Profile profile, boolean isActive) {
63          profile._setActive(isActive);
64          for (Profile p : profiles) {
65              if (p != profile) {
66                  p._setActive(false);
67              }
68          }
69          if (!isActive) {
70              defaultProfile._setActive(true);
71          }
72      }
73  
74      public Profile getActiveProfile() {
75          for (Profile p : profiles) {
76              if (p.isActive()) {
77                  return p;
78              }
79          }
80          return null;
81      }
82  
83  }