1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 package it.jnrpe;
17
18 import it.jnrpe.commands.CommandRepository;
19 import it.jnrpe.events.IJNRPEEventListener;
20 import it.jnrpe.plugins.IPluginRepository;
21
22 import java.nio.charset.Charset;
23 import java.util.ArrayList;
24 import java.util.Collection;
25
26 /**
27 * Builder for the {@link JNRPE} object.
28 *
29 * @author Massimiliano Ziccardi
30 */
31 public final class JNRPEBuilder {
32
33 /**
34 * Default read timeout is 10 seconds.
35 */
36 private static final int DEFAULT_READ_TIMEOUT = 10;
37
38 /**
39 * Default write timeout is 60 seconds.
40 */
41 private static final int DEFAULT_WRITE_TIMEOUT = 60;
42
43 /**
44 * The plugin repository.
45 */
46 private final IPluginRepository pluginRepository;
47
48 /**
49 * The command repository.
50 */
51 private final CommandRepository commandRepository;
52
53 /**
54 * The list of accepted hosts.
55 */
56 private final Collection<String> acceptedHosts = new ArrayList<String>();
57
58 /**
59 * All the event listeners.
60 */
61 private final Collection<IJNRPEEventListener> eventListeners = new ArrayList<IJNRPEEventListener>();
62
63 /**
64 * Sets if macros ($ARGxx$) should be expanded or not.
65 */
66 private boolean acceptParams = false;
67
68 /**
69 * Maximum number of concurrent connections.
70 */
71 private int maxAcceptedConnections = JNRPE.DEFAULT_MAX_ACCEPTED_CONNECTIONS;
72
73 /**
74 * The JNRPE charset.
75 */
76 private Charset charset = Charset.defaultCharset();
77
78 /**
79 * Read timeout in seconds.
80 */
81 private int readTimeout = DEFAULT_READ_TIMEOUT;
82
83 /**
84 * Write timeout in seconds.
85 */
86 private int writeTimeout = DEFAULT_WRITE_TIMEOUT;
87
88 /**
89 * Constructor.
90 *
91 * @param jnrpePluginRepository
92 * The plugin repository
93 * @param jnrpeCommandRepository
94 * The command repository
95 */
96 private JNRPEBuilder(final IPluginRepository jnrpePluginRepository,
97 final CommandRepository jnrpeCommandRepository) {
98 this.pluginRepository = jnrpePluginRepository;
99 this.commandRepository = jnrpeCommandRepository;
100 }
101
102 /**
103 * Entry point for the builder.
104 *
105 * @param pluginRepository
106 * The plugin repository
107 * @param commandRepository
108 * The command repository
109 * @return this
110 */
111 public static JNRPEBuilder forRepositories(
112 final IPluginRepository pluginRepository,
113 final CommandRepository commandRepository) {
114
115 return new JNRPEBuilder(pluginRepository, commandRepository);
116 }
117
118 /**
119 * Pass <code>true</code> if $ARGxx$ macros should be expanded.
120 *
121 * @param accept
122 * <code>true</code> if $ARGxx$ macros should be expanded.
123 * @return this
124 */
125 public JNRPEBuilder acceptParams(final boolean accept) {
126 this.acceptParams = accept;
127 return this;
128 }
129
130 /**
131 * Adds a client host to the list of accepted hosts.
132 *
133 * @param hostName
134 * the hostname or ip address
135 * @return this
136 */
137 public JNRPEBuilder acceptHost(final String hostName) {
138 this.acceptedHosts.add(hostName);
139 return this;
140 }
141
142 /**
143 * Adds a listener to the list of event listener.
144 *
145 * @param listener
146 * the listener
147 * @return this
148 */
149 public JNRPEBuilder withListener(final IJNRPEEventListener listener) {
150 this.eventListeners.add(listener);
151 return this;
152 }
153
154 /**
155 * Sets the charset to be used.
156 *
157 * @param newCharset
158 * the charset to be used
159 * @return this
160 */
161 public JNRPEBuilder withCharset(final Charset newCharset) {
162 this.charset = newCharset;
163 return this;
164 }
165
166 /**
167 * Sets the maximum number of accepted connections.
168 *
169 * @param maxConnections
170 * the maximum number of accepted connections.
171 * @return this
172 */
173 public JNRPEBuilder withMaxAcceptedConnections(
174 final int maxConnections) {
175 this.maxAcceptedConnections = maxConnections;
176 return this;
177 }
178
179 /**
180 * Sets the read timeout in seconds. Default is
181 * {@link #DEFAULT_READ_TIMEOUT} seconds.
182 *
183 * @param readTimeoutSecs
184 * the new read timeout in seconds
185 * @return this
186 */
187 public JNRPEBuilder withReadTimeout(final int readTimeoutSecs) {
188 this.readTimeout = readTimeoutSecs;
189 return this;
190 }
191
192 /**
193 * Sets the write timeout in seconds. Default is
194 * {@link #DEFAULT_WRITE_TIMEOUT} seconds.
195 *
196 * @param writeTimeoutSecs
197 * the new write timeout in seconds
198 * @return this
199 */
200 public JNRPEBuilder withWriteTimeout(final int writeTimeoutSecs) {
201 this.writeTimeout = writeTimeoutSecs;
202 return this;
203 }
204
205 /**
206 * Builds the configured JNRPE instance.
207 *
208 * @return the configured JNRPE instance
209 */
210 public JNRPE build() {
211 JNRPE jnrpe = new JNRPE(pluginRepository, commandRepository, charset,
212 acceptParams, acceptedHosts, maxAcceptedConnections,
213 readTimeout, writeTimeout, eventListeners);
214
215 return jnrpe;
216 }
217 }