Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EnforcerPortRule |
|
| 3.2;3.2 |
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 | 0 | public class EnforcerPortRule implements EnforcerRule |
19 | { | |
20 | /** | |
21 | * Simple param. This rule will fail if the value is true. | |
22 | */ | |
23 | 0 | private boolean shouldIfail = false; |
24 | 0 | 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 | 0 | ServerSocket ss = null; |
34 | 0 | DatagramSocket ds = null; |
35 | try | |
36 | { | |
37 | 0 | ss = new ServerSocket(port); |
38 | 0 | ss.setReuseAddress(true); |
39 | 0 | ds = new DatagramSocket(port); |
40 | 0 | ds.setReuseAddress(true); |
41 | 0 | return true; |
42 | } | |
43 | 0 | catch (IOException e) |
44 | { | |
45 | } | |
46 | finally | |
47 | { | |
48 | 0 | if (ds != null) |
49 | { | |
50 | 0 | ds.close(); |
51 | } | |
52 | ||
53 | 0 | if (ss != null) |
54 | { | |
55 | try | |
56 | { | |
57 | 0 | ss.close(); |
58 | } | |
59 | 0 | catch (IOException e) |
60 | { | |
61 | /* should not be thrown */ | |
62 | 0 | } |
63 | } | |
64 | } | |
65 | 0 | return false; |
66 | } | |
67 | ||
68 | public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException | |
69 | { | |
70 | 0 | Log log = helper.getLog(); |
71 | 0 | log.info("checking availability of port : " + this.port); |
72 | ||
73 | // make sure it's > 0 | |
74 | 0 | if (!(this.port > 0)) |
75 | { | |
76 | 0 | throw new EnforcerRuleException("Port is not valid " + this.port); |
77 | } | |
78 | ||
79 | // check availability | |
80 | 0 | if (!available(this.port)) |
81 | { | |
82 | 0 | throw new EnforcerRuleException("Port is not available " + this.port); |
83 | } | |
84 | ||
85 | 0 | if (this.shouldIfail) |
86 | { | |
87 | 0 | throw new EnforcerRuleException("Failing because my param said so."); |
88 | } | |
89 | 0 | } |
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 | 0 | 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 | 0 | 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 | 0 | return false; |
125 | } | |
126 | } |