Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * JBoss, Home of Professional Open Source
   * Copyright 2014, JBoss Inc., and individual contributors as indicated
   * by the @authors tag.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
TCP syslog server implementation for syslog4j.

Josef Cacek
 public class TLSSyslogServer extends TCPSyslogServer {
     private static final Logger LOGGER = Logger.getLogger(TLSSyslogServer.class);
     private SSLContext sslContext;
     private boolean addBouncyCastleOnShutdown = false;

Creates custom sslContext from keystore and truststore configured in

     public void initialize() throws SyslogRuntimeException {
         if(isBouncyCastleInstalled()) {
              = true;
         try {
             final char[] keystorePwd = config.getKeyStorePassword().toCharArray();
             final KeyStore keystore = loadKeyStore(config.getKeyStore(), keystorePwd);
             final char[] truststorePassword = config.getTrustStorePassword().toCharArray();
             final KeyStore truststore = loadKeyStore(config.getTrustStore(), truststorePassword);
             final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
             final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory
              = SSLContext.getInstance("TLS");
             .init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
         } catch (Exception e) {
             .error("Exception occurred during SSLContext for TLS syslog server initialization"e);
             throw new SyslogRuntimeException(e);

     protected ServerSocketFactory getServerSocketFactory() throws IOException {
         return .getServerSocketFactory();

Loads a JKS keystore with given path and password.

keystoreFile path to keystore file
keystorePwd keystore password
    private static KeyStore loadKeyStore(final String keystoreFilefinal char[] keystorePwdthrows KeyStoreException,
        final KeyStore keystore = KeyStore.getInstance("JKS");
        InputStream is = null;
        try {
            is = new FileInputStream(keystoreFile);
        } finally {
        return keystore;

Performs shutdown and adds Bouncy Castle if it was added before starting the server.

    public synchronized void shutdown() {
        if() {
             = false;

Removes Bouncy Castle from Security Manager.
    public void removeBouncyCastle() {
        try {
        } catch (SecurityException ex) {
            .warn("Cannot deregister BouncyCastleProvider"ex);

Adds Bouncy Castle to Security Manager.
    private void addBouncyCastle() {
        try {
            if (Security.getProvider(.) == null) {
                Security.addProvider(new BouncyCastleProvider());
        } catch (SecurityException ex) {
            .warn("Cannot register BouncyCastleProvider"ex);

Returns true if Bouncy Castle has been added to the Security Manager.
    private boolean isBouncyCastleInstalled() {
        return Security.getProvider(.) != null;
New to GrepCode? Check out our FAQ X