View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.apache.maven.enforcer.rule;
8   
9   import java.io.IOException;
10  import java.net.DatagramSocket;
11  import java.net.ServerSocket;
12  
13  import org.apache.maven.enforcer.rule.api.EnforcerRule;
14  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
15  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
16  import org.apache.maven.plugin.logging.Log;
17  
18  public class EnforcerPortRule implements EnforcerRule
19  {
20      /**
21       * Simple param. This rule will fail if the value is true.
22       */
23      private boolean shouldIfail = false;
24      private int port = 0;
25  
26      /**
27       * Check port availability. Taken from:
28       * http://svn.apache.org/viewvc/mina/trunk/core
29       * /src/main/java/org/apache/mina/util/AvailablePortFinder.java?view=markup
30       */
31      public static boolean available(int port)
32      {
33          ServerSocket ss = null;
34          DatagramSocket ds = null;
35          try
36          {
37              ss = new ServerSocket(port);
38              ss.setReuseAddress(true);
39              ds = new DatagramSocket(port);
40              ds.setReuseAddress(true);
41              return true;
42          }
43          catch (IOException e)
44          {
45          }
46          finally
47          {
48              if (ds != null)
49              {
50                  ds.close();
51              }
52  
53              if (ss != null)
54              {
55                  try
56                  {
57                      ss.close();
58                  }
59                  catch (IOException e)
60                  {
61                      /* should not be thrown */
62                  }
63              }
64          }
65          return false;
66      }
67  
68      public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException
69      {
70          Log log = helper.getLog();
71          log.info("checking availability of port : " + this.port);
72  
73          // make sure it's > 0
74          if (!(this.port > 0))
75          {
76              throw new EnforcerRuleException("Port is not valid " + this.port);
77          }
78  
79          // check availability
80          if (!available(this.port))
81          {
82              throw new EnforcerRuleException("Port is not available " + this.port);
83          }
84  
85          if (this.shouldIfail)
86          {
87              throw new EnforcerRuleException("Failing because my param said so.");
88          }
89      }
90  
91      /**
92       * If your rule is cacheable, you must return a unique id when parameters or
93       * conditions change that would cause the result to be different. Multiple cached
94       * results are stored based on their id. The easiest way to do this is to return
95       * a hash computed from the values of your parameters. If your rule is not
96       * cacheable, then the result here is not important, you may return anything.
97       */
98      public String getCacheId()
99      {
100         // no hash on boolean...only parameter so no hash is needed.
101         return "" + this.shouldIfail;
102     }
103 
104     /**
105      * This tells the system if the results are cacheable at all. Keep in mind that
106      * during forked builds and other things, a given rule may be executed more than
107      * once for the same project. This means that even things that change from
108      * project to project may still be cacheable in certain instances.
109      */
110     public boolean isCacheable()
111     {
112         return false;
113     }
114 
115     /**
116      * If the rule is cacheable and the same id is found in the cache, the stored
117      * results are passed to this method to allow double checking of the results.
118      * Most of the time this can be done by generating unique ids, but sometimes the
119      * results of objects returned by the helper need to be queried. You may for
120      * example, store certain objects in your rule and then query them later.
121      */
122     public boolean isResultValid(EnforcerRule arg0)
123     {
124         return false;
125     }
126 }