View Javadoc

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