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
   *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * 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.
  */
 package org.jboss.as.test.syslogserver;
 
 
TCP syslog server implementation for syslog4j.

Author(s):
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

 
     @Override
     public void initialize() throws SyslogRuntimeException {
         super.initialize();
 
         if(isBouncyCastleInstalled()) {
             removeBouncyCastle();
              = 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());
             keyManagerFactory.init(keystorekeystorePwd);
 
             final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory
                     .getDefaultAlgorithm());
             trustManagerFactory.init(truststore);
 
              = 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);
         }
     }

    
 
     @Override
     protected ServerSocketFactory getServerSocketFactory() throws IOException {
         return .getServerSocketFactory();
     }

    
Loads a JKS keystore with given path and password.

Parameters:
keystoreFile path to keystore file
keystorePwd keystore password
Returns:
Throws:
java.security.KeyStoreException
java.security.NoSuchAlgorithmException
java.security.cert.CertificateException
java.io.IOException
    private static KeyStore loadKeyStore(final String keystoreFilefinal char[] keystorePwdthrows KeyStoreException,
        final KeyStore keystore = KeyStore.getInstance("JKS");
        InputStream is = null;
        try {
            is = new FileInputStream(keystoreFile);
            keystore.load(iskeystorePwd);
        } finally {
            IOUtils.closeQuietly(is);
        }
        return keystore;
    }

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

    @Override
    public synchronized void shutdown() {
        super.shutdown();
        if() {
            addBouncyCastle();
             = false;
        }
    }

    
Removes Bouncy Castle from Security Manager.
    public void removeBouncyCastle() {
        try {
            Security.removeProvider(.);
        } 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