Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2017 Stormpath, Inc.
*
* 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 com.stormpath.sdk.lang;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.Provider;
import java.security.Security;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* @since 1.3.0
*/
public final class DefaultRuntimeEnvironment implements RuntimeEnvironment {

private static final Logger log = LoggerFactory.getLogger(DefaultRuntimeEnvironment.class);

public static final DefaultRuntimeEnvironment INSTANCE = new DefaultRuntimeEnvironment();

private DefaultRuntimeEnvironment() {
}

private static final String BC_PROVIDER_CLASS_NAME = "org.bouncycastle.jce.provider.BouncyCastleProvider";

private static final AtomicBoolean bcLoaded = new AtomicBoolean(false);

private static void enableBouncyCastleIfPossible() {

if (bcLoaded.get()) {
return;
}

try {
Class clazz = Classes.forName(BC_PROVIDER_CLASS_NAME);

//check to see if the user has already registered the BC provider:

Provider[] providers = Security.getProviders();

for (Provider provider : providers) {
if (clazz.isInstance(provider)) {
bcLoaded.set(true);
return;
}
}

//bc provider not enabled - add it:
Security.addProvider((Provider) Classes.newInstance(clazz));
bcLoaded.set(true);

} catch (UnknownClassException e) {
log.debug("Unable to load BouncyCastle. This is an acceptable outcome and this exception " +
"does not necessarily reflect a problem and can be ignored.", e);
//not available
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a warn or debug log message? Nature abhors an empty catch block! ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a perfectly fine/expected condition, but I suppose a debug statement couldn't hurt, except for waiting for CI for another hour before merge. :/

}
}

static {
enableBouncyCastleIfPossible();
}

@Override
public boolean isClassAvailable(String fqcn) {
return Classes.isAvailable(fqcn);
}
}
25 changes: 25 additions & 0 deletions api/src/main/java/com/stormpath/sdk/lang/RuntimeEnvironment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2017 Stormpath, Inc.
*
* 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 com.stormpath.sdk.lang;

/**
* @since 1.3.0
*/
public interface RuntimeEnvironment {

boolean isClassAvailable(String fqcn);

}
Loading