Search in sources :

Example 1 with InitializationException

use of com.helger.commons.exception.InitializationException in project ph-schematron by phax.

the class SchematronTransformerFactory method createTransformerFactorySaxonFirst.

/**
 * Create a new {@link TransformerFactory} trying to invoke the Saxon
 * implementation first using the class
 * {@value #SAXON_TRANSFORMER_FACTORY_CLASS}.
 *
 * @param aClassLoader
 *        The optional class loader to be used. May be <code>null</code>.
 * @param aErrorListener
 *        An optional XSLT error listener to be used. May be
 *        <code>null</code>.
 * @param aURIResolver
 *        An optional XSLT URI resolver to be used. May be <code>null</code>.
 * @return A new {@link TransformerFactory} and not <code>null</code>.
 * @throws InitializationException
 *         In case initialization fails.
 */
@Nonnull
public static TransformerFactory createTransformerFactorySaxonFirst(@Nullable final ClassLoader aClassLoader, @Nullable final ErrorListener aErrorListener, @Nullable final URIResolver aURIResolver) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Calling createTransformerFactorySaxonFirst");
    final ClassLoader aEffectiveClassLoader = aClassLoader != null ? aClassLoader : ClassLoaderHelper.getContextClassLoader();
    TransformerFactory aFactory;
    try {
        // Try Saxon first
        aFactory = TransformerFactory.newInstance(SAXON_TRANSFORMER_FACTORY_CLASS, aEffectiveClassLoader);
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Created TransformerFactory with Saxon using '" + SAXON_TRANSFORMER_FACTORY_CLASS + "'");
        // Maintain position #52
        aFactory.setFeature(FeatureKeys.LINE_NUMBERING, true);
        // Allow XInclude #86
        aFactory.setFeature(FeatureKeys.XINCLUDE, true);
        // Debug/testing only
        if (false)
            aFactory.setFeature(FeatureKeys.TRACE_OPTIMIZER_DECISIONS, true);
        if (false)
            aFactory.setFeature(FeatureKeys.COMPILE_WITH_TRACING, true);
    } catch (final TransformerFactoryConfigurationError | TransformerConfigurationException ex) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Failed to create TransformerFactory with Saxon.", ex);
            try {
                LOGGER.debug("Done checking implementations using classloader " + aEffectiveClassLoader);
                final Enumeration<URL> x = aEffectiveClassLoader.getResources("META-INF/services/javax.xml.transform.TransformerFactory");
                while (x.hasMoreElements()) {
                    LOGGER.debug("  " + x.nextElement().toExternalForm());
                }
                LOGGER.debug("Done checking implementations");
            } catch (final Exception ex2) {
                LOGGER.error("Error determining implementations", ex2);
            }
        }
        try {
            // Try default afterwards
            aFactory = TransformerFactory.newInstance();
        } catch (final TransformerFactoryConfigurationError ex2) {
            throw new InitializationException("Failed to create XML TransformerFactory", ex2);
        }
    }
    if (aErrorListener != null)
        aFactory.setErrorListener(aErrorListener);
    if (aURIResolver != null)
        aFactory.setURIResolver(aURIResolver);
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Created TransformerFactory is " + aFactory);
    return aFactory;
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) TransformerFactory(javax.xml.transform.TransformerFactory) Enumeration(java.util.Enumeration) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InitializationException(com.helger.commons.exception.InitializationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InitializationException(com.helger.commons.exception.InitializationException) Nonnull(javax.annotation.Nonnull)

Example 2 with InitializationException

use of com.helger.commons.exception.InitializationException in project phoss-directory by phax.

the class PDMetaManager method onAfterInstantiation.

@Override
protected void onAfterInstantiation(@Nonnull final IScope aScope) {
    try {
        m_aLucene = new PDLucene();
        m_aStorageMgr = new PDStorageManager(m_aLucene);
        m_aIndexerMgr = new PDIndexerManager(m_aStorageMgr);
        LOGGER.info(ClassHelper.getClassLocalName(this) + " was initialized");
    } catch (final Exception ex) {
        if (GlobalDebug.isProductionMode()) {
            new InternalErrorBuilder().setThrowable(ex).addErrorMessage(ClassHelper.getClassLocalName(this) + " init failed").handle();
        }
        throw new InitializationException("Failed to init " + ClassHelper.getClassLocalName(this), ex);
    }
}
Also used : PDLucene(com.helger.pd.indexer.lucene.PDLucene) InternalErrorBuilder(com.helger.photon.core.interror.InternalErrorBuilder) InitializationException(com.helger.commons.exception.InitializationException) PDStorageManager(com.helger.pd.indexer.storage.PDStorageManager) InitializationException(com.helger.commons.exception.InitializationException)

Example 3 with InitializationException

use of com.helger.commons.exception.InitializationException in project phoss-directory by phax.

the class PDPMetaManager method onAfterInstantiation.

@Override
protected void onAfterInstantiation(@Nonnull final IScope aScope) {
    try {
        m_aSMLInfoMgr = new SMLInfoManager(SML_INFO_XML);
        final URI aFixedSMPURI = PDServerConfiguration.getFixedSMPURI();
        if (aFixedSMPURI != null) {
            // Use only the configured SMP
            PDMetaManager.setBusinessCardProvider(SMPBusinessCardProvider.createForFixedSMP(PDServerConfiguration.getSMPMode(), aFixedSMPURI));
        } else {
            // Auto detect SMLs
            PDMetaManager.setBusinessCardProvider(SMPBusinessCardProvider.createWithSMLAutoDetect(PDServerConfiguration.getSMPMode(), PDServerConfiguration.getURLProvider(), m_aSMLInfoMgr::getAllSorted));
        }
        LOGGER.info(ClassHelper.getClassLocalName(this) + " was initialized");
    } catch (final Exception ex) {
        if (GlobalDebug.isProductionMode()) {
            new InternalErrorBuilder().setThrowable(ex).addErrorMessage(ClassHelper.getClassLocalName(this) + " init failed").handle();
        }
        throw new InitializationException("Failed to init " + ClassHelper.getClassLocalName(this), ex);
    }
}
Also used : InternalErrorBuilder(com.helger.photon.core.interror.InternalErrorBuilder) InitializationException(com.helger.commons.exception.InitializationException) URI(java.net.URI) InitializationException(com.helger.commons.exception.InitializationException)

Example 4 with InitializationException

use of com.helger.commons.exception.InitializationException in project as2-lib by phax.

the class AbstractCertificateFactory method initEmptyKeyStore.

/**
 * This method is responsible to create a new empty keystore based on the
 * configured type.
 *
 * @throws AS2Exception
 *         In case of error
 * @see #getKeyStoreType()
 * @see #createNewKeyStore(EKeyStoreType)
 * @see #setKeyStore(KeyStore)
 */
protected void initEmptyKeyStore() throws AS2Exception {
    try {
        final String sKeyStoreType = getKeyStoreType();
        final EKeyStoreType eKeyStoreType = EKeyStoreType.getFromIDCaseInsensitiveOrDefault(sKeyStoreType, DEFAULT_KEY_STORE_TYPE);
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Using internal keystore of type " + eKeyStoreType);
        final KeyStore aKeyStore = createNewKeyStore(eKeyStoreType);
        if (aKeyStore == null) {
            debugLog(() -> "initDynamicComponent -> no keystore");
            throw new InitializationException("Failed to create new keystore with type " + eKeyStoreType);
        }
        setKeyStore(aKeyStore);
    } catch (final GeneralSecurityException ex) {
        debugLog(() -> "initDynamicComponent -> " + _debug(ex));
        throw WrappedAS2Exception.wrap(ex);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) EKeyStoreType(com.helger.security.keystore.EKeyStoreType) InitializationException(com.helger.commons.exception.InitializationException) KeyStore(java.security.KeyStore)

Example 5 with InitializationException

use of com.helger.commons.exception.InitializationException in project phoss-smp by phax.

the class RegistrationHookWriteToSML method _createSMLCaller.

@Nonnull
private static ManageParticipantIdentifierServiceCaller _createSMLCaller() {
    // SML endpoint (incl. the service name)
    final ISMLInfo aSMLInfo = SMPMetaManager.getSettings().getSMLInfo();
    if (aSMLInfo == null)
        throw new IllegalStateException("Failed to get SML manage participant endpoint URL");
    final URL aSMLEndpointURL = aSMLInfo.getManageParticipantIdentifierEndpointAddress();
    final String sEndpointURL = aSMLEndpointURL.toExternalForm();
    final String sLowerURL = sEndpointURL.toLowerCase(Locale.US);
    if (LOGGER.isInfoEnabled())
        LOGGER.info("Performing SML query to '" + sEndpointURL + "'");
    // SSL socket factory
    final SSLSocketFactory aSocketFactory;
    if (sLowerURL.startsWith("https://")) {
        // https connection
        if (!SMPKeyManager.isKeyStoreValid())
            throw new InitializationException("Cannot init registration hook to SML, because private key/certificate setup has errors: " + SMPKeyManager.getInitializationError());
        try {
            aSocketFactory = SMPKeyManager.getInstance().createSSLContext().getSocketFactory();
        } catch (final Exception ex) {
            throw new IllegalStateException("Failed to init SSLContext for SML access", ex);
        }
    } else {
        // Local, http only access - no socket factory
        aSocketFactory = null;
    }
    // Hostname verifier
    final HostnameVerifier aHostnameVerifier;
    if (sLowerURL.contains("//localhost") || sLowerURL.contains("//127.0.0.1")) {
        // Accept all hostnames
        aHostnameVerifier = new HostnameVerifierVerifyAll(false);
    } else
        aHostnameVerifier = null;
    // Build WS client
    final ManageParticipantIdentifierServiceCaller ret = new ManageParticipantIdentifierServiceCaller(aSMLEndpointURL);
    ret.setSSLSocketFactory(aSocketFactory);
    ret.setHostnameVerifier(aHostnameVerifier);
    final Integer aConnectionTimeoutMS = SMPServerConfiguration.getSMLConnectionTimeoutMS();
    if (aConnectionTimeoutMS != null)
        ret.setConnectionTimeoutMS(aConnectionTimeoutMS.intValue());
    final int nRequestTimeoutMS = SMPServerConfiguration.getSMLRequestTimeoutMS();
    if (nRequestTimeoutMS >= 0)
        ret.setRequestTimeoutMS(nRequestTimeoutMS);
    return ret;
}
Also used : HostnameVerifierVerifyAll(com.helger.commons.ws.HostnameVerifierVerifyAll) ISMLInfo(com.helger.peppol.sml.ISMLInfo) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) InitializationException(com.helger.commons.exception.InitializationException) URL(java.net.URL) InitializationException(com.helger.commons.exception.InitializationException) ManageParticipantIdentifierServiceCaller(com.helger.peppol.smlclient.ManageParticipantIdentifierServiceCaller) HostnameVerifier(javax.net.ssl.HostnameVerifier) Nonnull(javax.annotation.Nonnull)

Aggregations

InitializationException (com.helger.commons.exception.InitializationException)17 KeyStore (java.security.KeyStore)4 X509Certificate (java.security.cert.X509Certificate)3 EPeppolCertificateCheckResult (com.helger.peppol.utils.EPeppolCertificateCheckResult)2 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)2 InternalErrorBuilder (com.helger.photon.core.interror.InternalErrorBuilder)2 LoadedKeyStore (com.helger.security.keystore.LoadedKeyStore)2 SMPClientReadOnly (com.helger.smpclient.peppol.SMPClientReadOnly)2 Nonnull (javax.annotation.Nonnull)2 Test (org.junit.Test)2 PDTFromString (com.helger.commons.datetime.PDTFromString)1 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 HostnameVerifierVerifyAll (com.helger.commons.ws.HostnameVerifierVerifyAll)1 PDLucene (com.helger.pd.indexer.lucene.PDLucene)1 PDConfiguredTrustStore (com.helger.pd.indexer.settings.PDConfiguredTrustStore)1 PDStorageManager (com.helger.pd.indexer.storage.PDStorageManager)1 ISMLInfo (com.helger.peppol.sml.ISMLInfo)1 ManageParticipantIdentifierServiceCaller (com.helger.peppol.smlclient.ManageParticipantIdentifierServiceCaller)1 ESMPIdentifierType (com.helger.peppolid.factory.ESMPIdentifierType)1 SMPBackendRegistry (com.helger.phoss.smp.backend.SMPBackendRegistry)1