All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclouds.compute.predicates.OperatingSystemPredicates Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jclouds.compute.predicates;

import java.util.Set;

import org.jclouds.compute.domain.OperatingSystem;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Sets;

/**
 * Container for operating system filters (predicates).
 * 
 * This class has static methods that create customized predicates to use with
 * {@link org.jclouds.compute.ComputeService}.
 * 
 * @author Adrian Cole
 */
public class OperatingSystemPredicates {
   /**
    * evaluates true if the OperatingSystem is unix like
    * 
    */
   public static Predicate isUnix() {
      return new Predicate() {
         @Override
         public boolean apply(OperatingSystem os) {
            if (os.getFamily() != null) {
               switch (os.getFamily()) {
                  case WINDOWS:
                     return false;
               }
            }
            for (String toMatch : searchStrings(os))
               if (toMatch != null && toMatch.toLowerCase().indexOf("windows") != -1)
                  return false;
            return true;
         }

         @Override
         public String toString() {
            return "isUnix()";
         }
      };
   }

   /**
    * evaluates true if the OperatingSystem supports the apt installer
    * 
    */
   public static Predicate supportsApt() {
      return new Predicate() {
         @Override
         public boolean apply(OperatingSystem os) {
            if (os.getFamily() != null) {
               switch (os.getFamily()) {
                  case DEBIAN:
                  case UBUNTU:
                     return true;
               }
            }
            for (String toMatch : searchStrings(os))
               if (toMatch != null && (toMatch.toLowerCase().indexOf("ubuntu") != -1
                        || toMatch.toLowerCase().indexOf("debian") != -1))
                  return true;
            return false;
         }

         @Override
         public String toString() {
            return "supportsApt()";
         }
      };
   }

   /**
    * evaluates true if the OperatingSystem supports the yum installer
    * 
    */
   public static Predicate supportsYum() {
      return new Predicate() {
         @Override
         public boolean apply(OperatingSystem os) {
            if (os.getFamily() != null) {
               switch (os.getFamily()) {
                  case CENTOS:
                  case AMZN_LINUX:
                  case FEDORA:
                  case RHEL:
                     return true;
               }
            }

            for (String toMatch : searchStrings(os))
               if (toMatch.toLowerCase().indexOf("centos") != -1 || toMatch.toLowerCase().indexOf("rhel") != -1
                        || toMatch.toLowerCase().replace(" ", "").indexOf("redhate") != -1
                        || toMatch.toLowerCase().indexOf("fedora") != -1)
                  return true;
            return false;
         }

         @Override
         public String toString() {
            return "supportsYum()";
         }
      };
   }

   /**
    * evaluates true if the OperatingSystem supports the zypper installer
    * 
    */
   public static Predicate supportsZypper() {
      return new Predicate() {
         @Override
         public boolean apply(OperatingSystem os) {
            if (os.getFamily() != null) {
               switch (os.getFamily()) {
                  case SUSE:
                     return true;
               }
            }
            for (String toMatch : searchStrings(os))
               if (toMatch != null && toMatch.toLowerCase().indexOf("suse") != -1)
                  return true;
            return false;
         }

         @Override
         public String toString() {
            return "supportsZypper()";
         }
      };
   }

   /**
    * return everything.
    */
   public static Predicate any() {
      return Predicates. alwaysTrue();
   }

   /**
    * return true if this is a 64bit os.
    */
   public static Predicate is64Bit() {
      return new Predicate() {
         @Override
         public boolean apply(OperatingSystem os) {
            return os.is64Bit();
         }

         @Override
         public String toString() {
            return "is64Bit()";
         }
      };
   }

   static Iterable searchStrings(OperatingSystem os) {
      Set search = Sets.newLinkedHashSet();
      if (os.getName() != null)
         search.add(os.getName());
      if (os.getDescription() != null)
         search.add(os.getDescription());
      return search;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy