Search in sources :

Example 1 with InitializationException

use of com.adeptj.runtime.exception.InitializationException in project adeptj-runtime by AdeptJ.

the class KeyStores method getKeyStore.

public static KeyStore getKeyStore(String keyStoreLoc, char[] keyStorePwd) {
    try (InputStream is = Environment.useProvidedKeyStore() ? Files.newInputStream(Paths.get(keyStoreLoc)) : KeyStores.class.getResourceAsStream(keyStoreLoc)) {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(is, keyStorePwd);
        return keyStore;
    } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException ex) {
        LoggerFactory.getLogger(SslContextFactory.class).error("Exception while loading KeyStore!!", ex);
        throw new InitializationException(ex.getMessage(), ex);
    }
}
Also used : InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InitializationException(com.adeptj.runtime.exception.InitializationException) KeyStore(java.security.KeyStore)

Example 2 with InitializationException

use of com.adeptj.runtime.exception.InitializationException in project adeptj-runtime by AdeptJ.

the class ContainerInitializer method onStartup.

/**
 * {@inheritDoc}
 */
@Override
public void onStartup(Set<Class<?>> startupAwareClasses, ServletContext context) {
    Logger logger = LoggerFactory.getLogger(ContainerInitializer.class);
    if (startupAwareClasses == null || startupAwareClasses.isEmpty()) {
        // We can't go ahead if StartupAware implementations are not passed by container.
        logger.error("No @HandlesTypes(StartupAware) on classpath!!");
        throw new IllegalStateException("No @HandlesTypes(StartupAware) on classpath!!");
    } else {
        ServletContextHolder.INSTANCE.setServletContext(context);
        startupAwareClasses.stream().sorted(new StartupAwareComparator()).peek(startupAwareClass -> logger.info("@HandlesTypes: [{}]", startupAwareClass)).forEach(startupAwareClass -> {
            try {
                startupAwareClass.asSubclass(StartupAware.class).getDeclaredConstructor().newInstance().onStartup(context);
            } catch (Exception ex) {
                // NOSONAR
                logger.error("Exception while executing StartupAware#onStartup!!", ex);
                throw new InitializationException("Exception while executing StartupAware#onStartup!!", ex);
            }
        });
        // If we are here means startup went well above, register FrameworkShutdownHandler now.
        context.addListener(FrameworkShutdownHandler.class);
    }
}
Also used : ServletContainerInitializer(javax.servlet.ServletContainerInitializer) Logger(org.slf4j.Logger) ServletContextHolder(com.adeptj.runtime.common.ServletContextHolder) LoggerFactory(org.slf4j.LoggerFactory) HandlesTypes(javax.servlet.annotation.HandlesTypes) FrameworkShutdownHandler(com.adeptj.runtime.osgi.FrameworkShutdownHandler) Set(java.util.Set) InitializationException(com.adeptj.runtime.exception.InitializationException) ServletContext(javax.servlet.ServletContext) Logger(org.slf4j.Logger) InitializationException(com.adeptj.runtime.exception.InitializationException) InitializationException(com.adeptj.runtime.exception.InitializationException)

Example 3 with InitializationException

use of com.adeptj.runtime.exception.InitializationException in project adeptj-runtime by AdeptJ.

the class SslContextFactory method createSslContext.

public static SSLContext createSslContext() {
    try {
        String keyStoreLoc = System.getProperty("javax.net.ssl.keyStore");
        String keyStorePwd = System.getProperty("javax.net.ssl.keyStorePassword");
        String keyPwd = System.getProperty("javax.net.ssl.keyPassword");
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(KeyStores.getKeyStore(keyStoreLoc, keyStorePwd.toCharArray()), keyPwd.toCharArray());
        SSLContext sslContext = SSLContext.getInstance(PROTOCOL_TLS);
        sslContext.init(kmf.getKeyManagers(), null, null);
        return sslContext;
    } catch (NoSuchAlgorithmException | KeyManagementException | UnrecoverableKeyException | KeyStoreException ex) {
        LoggerFactory.getLogger(SslContextFactory.class).error("Exception while initializing SSLContext!!", ex);
        throw new InitializationException("Exception while initializing SSLContext!!", ex);
    }
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) InitializationException(com.adeptj.runtime.exception.InitializationException) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Aggregations

InitializationException (com.adeptj.runtime.exception.InitializationException)3 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ServletContextHolder (com.adeptj.runtime.common.ServletContextHolder)1 FrameworkShutdownHandler (com.adeptj.runtime.osgi.FrameworkShutdownHandler)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 KeyManagementException (java.security.KeyManagementException)1 KeyStore (java.security.KeyStore)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 CertificateException (java.security.cert.CertificateException)1 Set (java.util.Set)1 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)1 SSLContext (javax.net.ssl.SSLContext)1 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)1 ServletContext (javax.servlet.ServletContext)1 HandlesTypes (javax.servlet.annotation.HandlesTypes)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1