Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EnforcerPortRule |
|
| 3.2;3.2 |
1 | /* | |
2 | * $Id: EnforcerPortRule.java 19191 2010-08-25 21:05:23Z tcarlson $ | |
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 | 0 | public class EnforcerPortRule implements EnforcerRule |
23 | { | |
24 | /** | |
25 | * Simple param. This rule will fail if the value is true. | |
26 | */ | |
27 | 0 | private boolean shouldIfail = false; |
28 | 0 | 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 | * @param port | |
36 | * @return | |
37 | */ | |
38 | public static boolean available(int port) | |
39 | { | |
40 | 0 | ServerSocket ss = null; |
41 | 0 | DatagramSocket ds = null; |
42 | try | |
43 | { | |
44 | 0 | ss = new ServerSocket(port); |
45 | 0 | ss.setReuseAddress(true); |
46 | 0 | ds = new DatagramSocket(port); |
47 | 0 | ds.setReuseAddress(true); |
48 | 0 | return true; |
49 | } | |
50 | 0 | catch (IOException e) |
51 | { | |
52 | } | |
53 | finally | |
54 | { | |
55 | 0 | if (ds != null) |
56 | { | |
57 | 0 | ds.close(); |
58 | } | |
59 | ||
60 | 0 | if (ss != null) |
61 | { | |
62 | try | |
63 | { | |
64 | 0 | ss.close(); |
65 | } | |
66 | 0 | catch (IOException e) |
67 | { | |
68 | /* should not be thrown */ | |
69 | 0 | } |
70 | } | |
71 | } | |
72 | 0 | return false; |
73 | } | |
74 | ||
75 | public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException | |
76 | { | |
77 | 0 | Log log = helper.getLog(); |
78 | 0 | log.info("checking availability of port : " + this.port); |
79 | ||
80 | // make sure it's > 0 | |
81 | 0 | if (!(this.port > 0)) |
82 | { | |
83 | 0 | throw new EnforcerRuleException("Port is not valid " + this.port); |
84 | } | |
85 | ||
86 | // check availability | |
87 | 0 | if (!available(this.port)) |
88 | { | |
89 | 0 | throw new EnforcerRuleException("Port is not available " + this.port); |
90 | } | |
91 | ||
92 | 0 | if (this.shouldIfail) |
93 | { | |
94 | 0 | throw new EnforcerRuleException("Failing because my param said so."); |
95 | } | |
96 | 0 | } |
97 | ||
98 | /** | |
99 | * If your rule is cacheable, you must return a unique id when parameters or | |
100 | * conditions change that would cause the result to be different. Multiple cached | |
101 | * results are stored based on their id. The easiest way to do this is to return | |
102 | * a hash computed from the values of your parameters. If your rule is not | |
103 | * cacheable, then the result here is not important, you may return anything. | |
104 | */ | |
105 | public String getCacheId() | |
106 | { | |
107 | // no hash on boolean...only parameter so no hash is needed. | |
108 | 0 | return "" + this.shouldIfail; |
109 | } | |
110 | ||
111 | /** | |
112 | * This tells the system if the results are cacheable at all. Keep in mind that | |
113 | * during forked builds and other things, a given rule may be executed more than | |
114 | * once for the same project. This means that even things that change from | |
115 | * project to project may still be cacheable in certain instances. | |
116 | */ | |
117 | public boolean isCacheable() | |
118 | { | |
119 | 0 | return false; |
120 | } | |
121 | ||
122 | /** | |
123 | * If the rule is cacheable and the same id is found in the cache, the stored | |
124 | * results are passed to this method to allow double checking of the results. | |
125 | * Most of the time this can be done by generating unique ids, but sometimes the | |
126 | * results of objects returned by the helper need to be queried. You may for | |
127 | * example, store certain objects in your rule and then query them later. | |
128 | */ | |
129 | public boolean isResultValid(EnforcerRule arg0) | |
130 | { | |
131 | 0 | return false; |
132 | } | |
133 | } |