1 package org.apache.maven.model.profile.activation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.model.Activation;
23 import org.apache.maven.model.ActivationOS;
24 import org.apache.maven.model.Profile;
25 import org.apache.maven.model.building.ModelProblemCollector;
26 import org.apache.maven.model.profile.ProfileActivationContext;
27 import org.codehaus.plexus.component.annotations.Component;
28 import org.codehaus.plexus.util.Os;
29
30
31
32
33
34
35
36 @Component( role = ProfileActivator.class, hint = "os" )
37 public class OperatingSystemProfileActivator
38 implements ProfileActivator
39 {
40
41 public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
42 {
43 Activation activation = profile.getActivation();
44
45 if ( activation == null )
46 {
47 return false;
48 }
49
50 ActivationOS os = activation.getOs();
51
52 if ( os == null )
53 {
54 return false;
55 }
56
57 boolean active = ensureAtLeastOneNonNull( os );
58
59 if ( active && os.getFamily() != null )
60 {
61 active = determineFamilyMatch( os.getFamily() );
62 }
63 if ( active && os.getName() != null )
64 {
65 active = determineNameMatch( os.getName() );
66 }
67 if ( active && os.getArch() != null )
68 {
69 active = determineArchMatch( os.getArch() );
70 }
71 if ( active && os.getVersion() != null )
72 {
73 active = determineVersionMatch( os.getVersion() );
74 }
75
76 return active;
77 }
78
79 private boolean ensureAtLeastOneNonNull( ActivationOS os )
80 {
81 return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
82 }
83
84 private boolean determineVersionMatch( String version )
85 {
86 String test = version;
87 boolean reverse = false;
88
89 if ( test.startsWith( "!" ) )
90 {
91 reverse = true;
92 test = test.substring( 1 );
93 }
94
95 boolean result = Os.isVersion( test );
96
97 return reverse ? !result : result;
98 }
99
100 private boolean determineArchMatch( String arch )
101 {
102 String test = arch;
103 boolean reverse = false;
104
105 if ( test.startsWith( "!" ) )
106 {
107 reverse = true;
108 test = test.substring( 1 );
109 }
110
111 boolean result = Os.isArch( test );
112
113 return reverse ? !result : result;
114 }
115
116 private boolean determineNameMatch( String name )
117 {
118 String test = name;
119 boolean reverse = false;
120
121 if ( test.startsWith( "!" ) )
122 {
123 reverse = true;
124 test = test.substring( 1 );
125 }
126
127 boolean result = Os.isName( test );
128
129 return reverse ? !result : result;
130 }
131
132 private boolean determineFamilyMatch( String family )
133 {
134 String test = family;
135 boolean reverse = false;
136
137 if ( test.startsWith( "!" ) )
138 {
139 reverse = true;
140 test = test.substring( 1 );
141 }
142
143 boolean result = Os.isFamily( test );
144
145 return reverse ? !result : result;
146 }
147
148 }