001package org.apache.maven.model.profile.activation; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.model.Activation; 023import org.apache.maven.model.ActivationOS; 024import org.apache.maven.model.Profile; 025import org.apache.maven.model.building.ModelProblemCollector; 026import org.apache.maven.model.profile.ProfileActivationContext; 027import org.codehaus.plexus.component.annotations.Component; 028import org.codehaus.plexus.util.Os; 029 030/** 031 * Determines profile activation based on the operating system of the current runtime platform. 032 * 033 * @author Benjamin Bentmann 034 * @see ActivationOS 035 */ 036@Component( role = ProfileActivator.class, hint = "os" ) 037public class OperatingSystemProfileActivator 038 implements ProfileActivator 039{ 040 041 public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems ) 042 { 043 Activation activation = profile.getActivation(); 044 045 if ( activation == null ) 046 { 047 return false; 048 } 049 050 ActivationOS os = activation.getOs(); 051 052 if ( os == null ) 053 { 054 return false; 055 } 056 057 boolean active = ensureAtLeastOneNonNull( os ); 058 059 if ( active && os.getFamily() != null ) 060 { 061 active = determineFamilyMatch( os.getFamily() ); 062 } 063 if ( active && os.getName() != null ) 064 { 065 active = determineNameMatch( os.getName() ); 066 } 067 if ( active && os.getArch() != null ) 068 { 069 active = determineArchMatch( os.getArch() ); 070 } 071 if ( active && os.getVersion() != null ) 072 { 073 active = determineVersionMatch( os.getVersion() ); 074 } 075 076 return active; 077 } 078 079 @Override 080 public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems ) 081 { 082 Activation activation = profile.getActivation(); 083 084 if ( activation == null ) 085 { 086 return false; 087 } 088 089 ActivationOS os = activation.getOs(); 090 091 if ( os == null ) 092 { 093 return false; 094 } 095 return true; 096 } 097 098 private boolean ensureAtLeastOneNonNull( ActivationOS os ) 099 { 100 return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null; 101 } 102 103 private boolean determineVersionMatch( String version ) 104 { 105 String test = version; 106 boolean reverse = false; 107 108 if ( test.startsWith( "!" ) ) 109 { 110 reverse = true; 111 test = test.substring( 1 ); 112 } 113 114 boolean result = Os.isVersion( test ); 115 116 return reverse ? !result : result; 117 } 118 119 private boolean determineArchMatch( String arch ) 120 { 121 String test = arch; 122 boolean reverse = false; 123 124 if ( test.startsWith( "!" ) ) 125 { 126 reverse = true; 127 test = test.substring( 1 ); 128 } 129 130 boolean result = Os.isArch( test ); 131 132 return reverse ? !result : result; 133 } 134 135 private boolean determineNameMatch( String name ) 136 { 137 String test = name; 138 boolean reverse = false; 139 140 if ( test.startsWith( "!" ) ) 141 { 142 reverse = true; 143 test = test.substring( 1 ); 144 } 145 146 boolean result = Os.isName( test ); 147 148 return reverse ? !result : result; 149 } 150 151 private boolean determineFamilyMatch( String family ) 152 { 153 String test = family; 154 boolean reverse = false; 155 156 if ( test.startsWith( "!" ) ) 157 { 158 reverse = true; 159 test = test.substring( 1 ); 160 } 161 162 boolean result = Os.isFamily( test ); 163 164 return reverse ? !result : result; 165 } 166 167}