Codebase list jboss-logmanager / b92ca88
Update upstream source from tag 'upstream/2.1.5' Update to upstream version '2.1.5' with Debian dir 5fcad89009ba84b812245a11d7979548b75ff347 Markus Koschany 5 years ago
14 changed file(s) with 143 addition(s) and 53 deletion(s). Raw diff Collapse all Expand all
2727 <groupId>org.jboss.logmanager</groupId>
2828 <artifactId>jboss-logmanager</artifactId>
2929 <packaging>jar</packaging>
30 <version>2.1.4.Final</version>
30 <version>2.1.5.Final</version>
3131
3232 <parent>
3333 <groupId>org.jboss</groupId>
4646 <properties>
4747 <!-- Dependency versions -->
4848 <version.javax.json>1.0</version.javax.json>
49 <version.org.byteman>4.0.3</version.org.byteman>
49 <version.org.byteman>4.0.4</version.org.byteman>
5050 <version.org.glassfish.javax.json>1.0.4</version.org.glassfish.javax.json>
5151 <version.org.jboss.modules.jboss-modules>1.7.0.Final</version.org.jboss.modules.jboss-modules>
5252 <version.org.wildfly.common.wildfly-common>1.2.0.Final</version.org.wildfly.common.wildfly-common>
5353 <version.junit.junit>4.12</version.junit.junit>
54
55 <!-- Plugin versions -->
56 <version.org.jboss.apiviz.plugin>1.3.2.GA</version.org.jboss.apiviz.plugin>
5754
5855 <!-- Test properties -->
5956 <org.jboss.test.address>127.0.0.1</org.jboss.test.address>
296293 </plugin>
297294 </plugins>
298295 </build>
299 <reporting>
300 <plugins>
301 <plugin>
302 <artifactId>maven-javadoc-plugin</artifactId>
303 <configuration>
304 <doclet>net.gleamynode.apiviz.APIviz</doclet>
305 <docletArtifact>
306 <groupId>org.jboss.apiviz</groupId>
307 <artifactId>apiviz</artifactId>
308 <version>${version.org.jboss.apiviz.plugin}</version>
309 </docletArtifact>
310 <doctitle>JBoss LogManager ${project.version}</doctitle>
311 <header>JBoss LogManager ${project.version}</header>
312 <footer>JBoss LogManager ${project.version}</footer>
313 <bottom><![CDATA[<i>Copyright &#169; 2017 JBoss, a division of Red Hat, Inc.</i>]]></bottom>
314 <links>
315 <link>http://docs.oracle.com/javase/8/docs/api/</link>
316 </links>
317 </configuration>
318 </plugin>
319 </plugins>
320 </reporting>
321296 </project>
109109 setSourceMethodName(original.getSourceMethodName());
110110 sourceFileName = original.sourceFileName;
111111 sourceLineNumber = original.sourceLineNumber;
112 sourceModuleName = original.sourceModuleName;
113 sourceModuleVersion = original.sourceModuleVersion;
112114 }
113115 formatStyle = original.formatStyle;
114116 mdcCopy = original.mdcCopy;
110110 break;
111111 }
112112 case 'h': {
113 stepList.add(Formatters.hostnameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, argument == null ? "1" : argument));
113 stepList.add(Formatters.hostnameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, false));
114114 break;
115115 }
116116 case 'H': {
4444 /**
4545 * Formatter utility methods.
4646 */
47 @SuppressWarnings({"WeakerAccess", "unused"})
4748 public final class Formatters {
4849
4950 public static final String THREAD_ID = "id";
5051
5152 private static final boolean DEFAULT_TRUNCATE_BEGINNING = false;
52 private static final String NEW_LINE = String.format("%n");
5353 private static final Pattern PRECISION_INT_PATTERN = Pattern.compile("\\d+");
5454
5555
546546 * @return the format step
547547 */
548548 public static FormatStep hostnameFormatStep(final boolean leftJustify, final int minimumWidth, final boolean truncateBeginning, final int maximumWidth, final boolean qualified) {
549 return hostnameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, qualified ? null : "1");
549 return qualified ? hostnameFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, null) : new SegmentedFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, null) {
550 public String getSegmentedSubject(final ExtLogRecord record) {
551 final String hostName = record.getHostName();
552 final int idx = hostName.indexOf('.');
553 return idx == -1 ? hostName :hostName.substring(0, idx);
554 }
555 };
550556 }
551557
552558 /**
560566 * @return the format step
561567 */
562568 public static FormatStep hostnameFormatStep(final boolean leftJustify, final int minimumWidth, final boolean truncateBeginning, final int maximumWidth, final String precision) {
563 return new SegmentedFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, precision) {
569 return new SegmentedFormatStep(leftJustify, minimumWidth, truncateBeginning, maximumWidth, null) {
564570 public String getSegmentedSubject(final ExtLogRecord record) {
565 return record.getHostName();
571 final String hostName = record.getHostName();
572 // Check for a specified precision. This is not passed to the constructor because we want truncate
573 // segments from the right intsead of the left.
574 if (precision != null && PRECISION_INT_PATTERN.matcher(precision).matches()) {
575 int count = Integer.parseInt(precision);
576 int end = 0;
577 for (int i = 0; i < hostName.length(); i++) {
578 // If we've got a dot we're at a new segment
579 if (hostName.charAt(i) == '.') {
580 count--;
581 end = i;
582 }
583 // We've reached the precision we want
584 if (count == 0) {
585 break;
586 }
587 }
588 if (end != 0 && count == 0) {
589 return hostName.substring(0, end);
590 }
591 }
592 return hostName;
566593 }
567594 };
568595 }
2424 import java.io.UnsupportedEncodingException;
2525 import java.io.Writer;
2626
27 import java.nio.charset.Charset;
2728 import java.util.logging.ErrorManager;
2829 import java.util.logging.Formatter;
2930
3435 public class OutputStreamHandler extends WriterHandler {
3536
3637 private OutputStream outputStream;
38 private Charset charset;
3739
3840 /**
3941 * Construct a new instance with no formatter.
8385 public void setEncoding(final String encoding) throws SecurityException, UnsupportedEncodingException {
8486 // superclass checks access
8587 synchronized (outputLock) {
88 charset = encoding == null ? null : Charset.forName(encoding);
8689 super.setEncoding(encoding);
87 if (this.outputStream != null) {
88 final OutputStream outputStream = this.outputStream;
89 updateWriter(outputStream, encoding);
90 }
90 // we only want to change the writer, not the output stream
91 final OutputStream outputStream = this.outputStream;
92 if (outputStream != null) {
93 super.setWriter(getNewWriter(outputStream));
94 }
9195 }
9296 }
9397
9599 public void setWriter(final Writer writer) {
96100 synchronized (outputLock) {
97101 super.setWriter(writer);
102 final OutputStream oldStream = this.outputStream;
98103 outputStream = null;
104 safeFlush(oldStream);
105 safeClose(oldStream);
99106 }
100107 }
101108
102109 /**
103 * Set the output stream to write to.
110 * Set the output stream to write to. The output stream will then belong to this handler; when the handler is
111 * closed or a new writer or output stream is set, this output stream will be closed.
104112 *
105113 * @param outputStream the new output stream or {@code null} for none
106114 */
107115 public void setOutputStream(final OutputStream outputStream) {
116 if (outputStream == null) {
117 // call ours, not the superclass one
118 this.setWriter(null);
119 return;
120 }
108121 checkAccess(this);
122 // Close the writer, then close the old stream, then establish the new stream with a new writer.
109123 try {
110124 synchronized (outputLock) {
111 this.outputStream = outputStream;
112 updateWriter(outputStream, getEncoding());
125 final OutputStream oldStream = this.outputStream;
126 // do not close the old stream if creating the writer fails
127 final Writer writer = getNewWriter(outputStream);
128 try {
129 this.outputStream = outputStream;
130 super.setWriter(writer);
131 } finally {
132 safeFlush(oldStream);
133 safeClose(oldStream);
134 }
113135 }
114 } catch (UnsupportedEncodingException e) {
115 throw new IllegalArgumentException("The specified encoding is invalid");
116136 } catch (Exception e) {
117137 reportError("Error opening output stream", e, ErrorManager.OPEN_FAILURE);
118138 return;
119139 }
120140 }
121141
122 private void updateWriter(final OutputStream newOutputStream, final String encoding) throws UnsupportedEncodingException {
123 final UninterruptibleOutputStream outputStream = new UninterruptibleOutputStream(newOutputStream);
124 super.setWriter(newOutputStream == null ? null : encoding == null ? new OutputStreamWriter(outputStream) : new OutputStreamWriter(outputStream, encoding));
142 private Writer getNewWriter(OutputStream newOutputStream) {
143 if (newOutputStream == null) return null;
144 final UninterruptibleOutputStream outputStream = new UninterruptibleOutputStream(new UncloseableOutputStream(newOutputStream));
145 final Charset charset = this.charset;
146 return charset == null ? new OutputStreamWriter(outputStream) : new OutputStreamWriter(outputStream, charset);
125147 }
126148 }
497497
498498 @Override
499499 public final void doPublish(final ExtLogRecord record) {
500 // Don't log empty messages
501 if (record.getMessage() == null || record.getMessage().isEmpty()) {
502 return;
503 }
504500 synchronized (outputLock) {
505501 init();
506502 if (out == null) {
164164 } catch (Throwable ignored) {}
165165 }
166166
167 private void safeFlush(Flushable f) {
167 void safeFlush(Flushable f) {
168168 try {
169169 if (f != null) f.flush();
170170 } catch (Exception e) {
247247 Assert.assertTrue(formatted.contains("CIRCULAR REFERENCE:java.lang.IllegalStateException: suppressedLevel1"));
248248 }
249249
250 @Test
251 public void unqualifiedHost() {
252 final String hostName = "logmanager.jboss.org";
253 final ExtLogRecord record = createLogRecord("test");
254 record.setHostName(hostName);
255 PatternFormatter formatter = new PatternFormatter("%h");
256 Assert.assertEquals("logmanager", formatter.format(record));
257
258 // This should still return just the first portion
259 formatter = new PatternFormatter("%h{2}");
260 Assert.assertEquals("logmanager", formatter.format(record));
261
262 // Should truncate from the beginning
263 formatter = new PatternFormatter("%.3h");
264 Assert.assertEquals("log", formatter.format(record));
265
266 // Should truncate from the end
267 formatter = new PatternFormatter("%.-7h");
268 Assert.assertEquals("manager", formatter.format(record));
269 }
270
271 @Test
272 public void qualifiedHost() {
273 final String hostName = "logmanager.jboss.org";
274 final ExtLogRecord record = createLogRecord("test");
275 record.setHostName(hostName);
276 PatternFormatter formatter = new PatternFormatter("%H");
277 Assert.assertEquals(hostName, formatter.format(record));
278
279 formatter = new PatternFormatter("%H{1}");
280 Assert.assertEquals("logmanager", formatter.format(record));
281
282 formatter = new PatternFormatter("%H{2}");
283 Assert.assertEquals("logmanager.jboss", formatter.format(record));
284
285 formatter = new PatternFormatter("%H{3}");
286 Assert.assertEquals(hostName, formatter.format(record));
287
288 formatter = new PatternFormatter("%H{4}");
289 Assert.assertEquals(hostName, formatter.format(record));
290
291 // Truncate from the beginning
292 formatter = new PatternFormatter("%.10H");
293 Assert.assertEquals("logmanager", formatter.format(record));
294
295 // Truncate from the end
296 formatter = new PatternFormatter("%.-3H");
297 Assert.assertEquals("org", formatter.format(record));
298 formatter = new PatternFormatter("%.-5H{2}");
299 Assert.assertEquals("jboss", formatter.format(record));
300 }
301
250302
251303 private void systemProperties(final String propertyPrefix) throws Exception {
252304 final ExtLogRecord record = createLogRecord("test");
179179 part2 = "second message 𥹖";
180180 testMultibyteTruncation(part1, part2, 2);
181181
182 }
183
184 @Test
185 public void testNullMessage() throws Exception {
186 // Setup the handler
187 handler.setSyslogType(SyslogType.RFC5424);
188 final ByteArrayOutputStream out = new ByteArrayOutputStream();
189 handler.setOutputStream(out);
190
191 final Calendar cal = getCalendar();
192 // Create the record
193 handler.setHostname("test");
194 ExtLogRecord record = createRecord(cal, null);
195 final String expectedMessage = "<14>1 2012-01-09T04:39:22.000" + calculateTimeZone(cal) + " test java " + handler.getPid() + " - - " + BOM + "null";
196 handler.publish(record);
197 Assert.assertEquals(expectedMessage, createString(out));
182198 }
183199
184200 private void testMultibyteTruncation(final String part1, final String part2, final int charsToTruncate) throws Exception {
1717
1818 dname="CN=localhost, OU=Server Unit, O=Red Hat, L=Raleigh, S=NC, C=US"
1919 # Create server keystore - file server-keystore.jks
20 keytool -genkey -v -alias server -keystore server-keystore.jks -validity 3650 -keypass testpassword -storepass testpassword -dname "${dname}"
20 keytool -genkey -v -alias server -keystore server-keystore.jks -keyalg RSA -validity 3650 -keypass testpassword -storepass testpassword -dname "${dname}"
2121
2222 # Export Server's Public Key - file server.cer
2323 keytool -export -keystore server-keystore.jks -alias server -file server.cer -keypass testpassword -storepass testpassword
2424
2525 # Export Client Key Store - file client-keystore.jsk
26 keytool -genkey -v -alias client -keystore client-keystore.jks -validity 3650 -keypass testpassword -storepass testpassword -dname "${dname}"
26 keytool -genkey -v -alias client -keystore client-keystore.jks -keyalg RSA -validity 3650 -keypass testpassword -storepass testpassword -dname "${dname}"
2727
2828 # Exporting Client's Public Key - file client.cer
2929 keytool -export -keystore client-keystore.jks -alias client -file client.cer -keypass testpassword -storepass testpassword
3030
3131 # Importing Client's Public key into server's truststore
32 keytool -import -v -trustcacerts -alias client -file client.cer -keystore server-keystore.jks -keypass testpassword -storepass testpassword
32 keytool -import -v -trustcacerts -alias client -file client.cer -keystore server-keystore.jks -keypass testpassword -storepass testpassword -noprompt
3333
3434 # Importing Server's Public key into client's truststore
35 keytool -import -v -trustcacerts -alias server -file server.cer -keystore client-keystore.jks -keypass testpassword -storepass testpassword
35 keytool -import -v -trustcacerts -alias server -file server.cer -keystore client-keystore.jks -keypass testpassword -storepass testpassword -noprompt
3636
3737 popd