KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > profiles > activation > OperatingSystemProfileActivator


1 package org.apache.maven.profiles.activation;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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