下面列出了org.springframework.util.ClassUtils#isPresent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void contextPrepared(ConfigurableApplicationContext applicationContext) {
if (ClassUtils.isPresent("com.bstek.dorado.web.loader.DoradoLoader", this.getClass().getClassLoader())){
System.setProperty("doradoHome", "classpath:dorado-home/");
DoradoLoader doradoLoader = DoradoLoader.getInstance();
try {
Context context = CommonContext.init(applicationContext);
DoradoLoader.getInstance().setFailSafeContext(context);
doradoLoader.preload(true);;
} catch (Exception e) {
e.printStackTrace();
}
Set<String> sources = new LinkedHashSet<String>();
sources.addAll(doradoLoader
.getContextLocations(false));
application.setSources(sources);
}
}
ProtobufHttpMessageConverter(@Nullable ProtobufFormatSupport formatSupport,
@Nullable ExtensionRegistry extensionRegistry) {
if (formatSupport != null) {
this.protobufFormatSupport = formatSupport;
}
else if (ClassUtils.isPresent("com.googlecode.protobuf.format.FormatFactory", getClass().getClassLoader())) {
this.protobufFormatSupport = new ProtobufJavaFormatSupport();
}
else if (ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", getClass().getClassLoader())) {
this.protobufFormatSupport = new ProtobufJavaUtilSupport(null, null);
}
else {
this.protobufFormatSupport = null;
}
setSupportedMediaTypes(Arrays.asList(this.protobufFormatSupport != null ?
this.protobufFormatSupport.supportedMediaTypes() : new MediaType[] {PROTOBUF, TEXT_PLAIN}));
this.extensionRegistry = (extensionRegistry == null ? ExtensionRegistry.newInstance() : extensionRegistry);
}
@PostConstruct
public void init() {
appEnv = env;
if (env.containsProperty(RABBIT_TRACE_LOG_TYPE_KEY)) {
String type = env.getProperty(RABBIT_TRACE_LOG_TYPE_KEY);
if (type.equals(RABBIT_TRACE_LOG_TYPE_FILE)) {
AbstractTraceLog.setTraceLogger(Slf4jTraceLogger.instance);
} else if (type.equals(RABBIT_TRACE_LOG_TYPE_MYSQL)) {
AbstractTraceLog.setTraceLogger(new DatabaseMySQLTraceLogger(dataSource, capacity));
} else if (type.equals(RABBIT_TRACE_LOG_TYPE_NONE)) {
AbstractTraceLog.setTraceLogger(NoopTraceLogger.instance);
}
} else {
if (dataSource != null
&& ClassUtils.isPresent("com.mysql.jdbc.Driver", RabbitAutoConfiguration.class.getClassLoader())) {
AbstractTraceLog.setTraceLogger(new DatabaseMySQLTraceLogger(dataSource, capacity));
} else {
AbstractTraceLog.setTraceLogger(Slf4jTraceLogger.instance);
}
}
}
@Nullable
public static SpecificPlatform get() {
ClassLoader classLoader = MBeanExportConfiguration.class.getClassLoader();
for (SpecificPlatform environment : values()) {
if (ClassUtils.isPresent(environment.identifyingClass, classLoader)) {
return environment;
}
}
return null;
}
/**
* Convert a message from the handler into one that is safe to consume in the caller's
* class loader. If the handler is a wrapper for a function in an isolated class
* loader, then the message will be created with the target class loader (therefore
* the {@link Message} class must be on the classpath of the target class loader).
* @param handler the function that generated the message
* @param message the message to convert
* @return a message with the correct class loader
*/
public static Message<?> unpack(Object handler, Object message) {
if (handler instanceof FluxWrapper) {
handler = ((FluxWrapper<?>) handler).getTarget();
}
if (!(handler instanceof Isolated)) {
if (message instanceof Message) {
return (Message<?>) message;
}
return MessageBuilder.withPayload(message).build();
}
ClassLoader classLoader = ((Isolated) handler).getClassLoader();
Class<?> type = ClassUtils.isPresent(Message.class.getName(), classLoader)
? ClassUtils.resolveClassName(Message.class.getName(), classLoader)
: null;
Object payload;
Map<String, Object> headers;
if (type != null && type.isAssignableFrom(message.getClass())) {
Method getPayload = ClassUtils.getMethod(type, "getPayload");
Method getHeaders = ClassUtils.getMethod(type, "getHeaders");
payload = ReflectionUtils.invokeMethod(getPayload, message);
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) ReflectionUtils
.invokeMethod(getHeaders, message);
headers = map;
}
else {
payload = message;
headers = Collections.emptyMap();
}
return MessageBuilder.withPayload(payload).copyHeaders(headers).build();
}
public DeferredResultMethodReturnValueHandler() {
this.adapterMap = new HashMap<Class<?>, DeferredResultAdapter>(5);
this.adapterMap.put(DeferredResult.class, new SimpleDeferredResultAdapter());
this.adapterMap.put(ListenableFuture.class, new ListenableFutureAdapter());
if (ClassUtils.isPresent("java.util.concurrent.CompletionStage", getClass().getClassLoader())) {
this.adapterMap.put(CompletionStage.class, new CompletionStageAdapter());
}
}
public boolean isTemplateAvailable(final String view, final Environment environment, final ClassLoader classLoader,
final ResourceLoader resourceLoader) {
if (ClassUtils.isPresent("org.trimou.Mustache", classLoader)) {
final PropertyResolver resolver =
new RelaxedPropertyResolver(environment, TrimouProperties.PROPERTY_PREFIX + '.');
final String prefix = resolver.getProperty("prefix", SpringResourceTemplateLocator.DEFAULT_PREFIX);
final String suffix = resolver.getProperty("suffix", SpringResourceTemplateLocator.DEFAULT_SUFFIX);
final String resourceLocation = prefix + view + suffix;
return resourceLoader.getResource(resourceLocation).exists();
}
return false;
}
public static boolean isJsr303Present(ApplicationContext applicationContext) {
ClassLoader classLoader = applicationContext.getClassLoader();
for (String validatorClass : VALIDATOR_CLASSES) {
if (!ClassUtils.isPresent(validatorClass, classLoader)) {
return false;
}
}
return true;
}
@Override
protected void registerRequestContextFactory(String className,
List<TilesRequestContextFactory> factories, TilesRequestContextFactory parent) {
// Avoid Tiles 2.2 warn logging when default RequestContextFactory impl class not found
if (ClassUtils.isPresent(className, TilesConfigurer.class.getClassLoader())) {
super.registerRequestContextFactory(className, factories, parent);
}
}
@Override
public void afterPropertiesSet() throws Exception {
if (ClassUtils.isPresent(CLASS_HYSTRIX_FEIGN, ClassUtils.getDefaultClassLoader())) {
cloudTargeter = new HystrixTargeter();
} else {
cloudTargeter = new DefaultTargeter();
}
}
/**
* Create a registry and auto-register default adapters.
* @see #getSharedInstance()
*/
public ReactiveAdapterRegistry() {
ClassLoader classLoader = ReactiveAdapterRegistry.class.getClassLoader();
// Reactor
boolean reactorRegistered = false;
if (ClassUtils.isPresent("reactor.core.publisher.Flux", classLoader)) {
new ReactorRegistrar().registerAdapters(this);
reactorRegistered = true;
}
this.reactorPresent = reactorRegistered;
// RxJava1
if (ClassUtils.isPresent("rx.Observable", classLoader) &&
ClassUtils.isPresent("rx.RxReactiveStreams", classLoader)) {
new RxJava1Registrar().registerAdapters(this);
}
// RxJava2
if (ClassUtils.isPresent("io.reactivex.Flowable", classLoader)) {
new RxJava2Registrar().registerAdapters(this);
}
// Java 9+ Flow.Publisher
if (ClassUtils.isPresent("java.util.concurrent.Flow.Publisher", classLoader)) {
new ReactorJdkFlowAdapterRegistrar().registerAdapter(this);
}
// If not present, do nothing for the time being...
// We can fall back on "reactive-streams-flow-bridge" (once released)
}
private boolean isClassPresent(String className) {
return ClassUtils.isPresent(className, classLoader);
}
private String determineTransactionAspectClass() {
return (ClassUtils.isPresent("javax.transaction.Transactional", getClass().getClassLoader()) ?
TransactionManagementConfigUtils.JTA_TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME :
TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME);
}
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
boolean tomcatPresent = ClassUtils.isPresent("org.apache.catalina.core.StandardContext", getClass().getClassLoader());
return tomcatPresent ? new String[] {"com.github.yizzuide.milkomeda.hydrogen.filter.TomcatFilterConfig"}
: new String[] {};
}
public static boolean isRequestMappingPresent(){
return ClassUtils.isPresent(CLASS_REQUEST_MAPPING, null);
}
public static boolean isPresent() {
return ClassUtils.isPresent(ENTITY_ANNOTATION, JDDDTypes.class.getClassLoader());
}
/**
* Set the Spring {@link JtaTransactionManager} or the JTA {@link TransactionManager}
* to be used with Hibernate, if any. Allows for using a Spring-managed transaction
* manager for Hibernate 5's session and cache synchronization, with the
* "hibernate.transaction.jta.platform" automatically set to it.
* <p>A passed-in Spring {@link JtaTransactionManager} needs to contain a JTA
* {@link TransactionManager} reference to be usable here, except for the WebSphere
* case where we'll automatically set {@code WebSphereExtendedJtaPlatform} accordingly.
* <p>Note: If this is set, the Hibernate settings should not contain a JTA platform
* setting to avoid meaningless double configuration.
*/
public LocalSessionFactoryBuilder setJtaTransactionManager(Object jtaTransactionManager) {
Assert.notNull(jtaTransactionManager, "Transaction manager reference must not be null");
if (jtaTransactionManager instanceof JtaTransactionManager) {
boolean webspherePresent = ClassUtils.isPresent("com.ibm.wsspi.uow.UOWManager", getClass().getClassLoader());
if (webspherePresent) {
getProperties().put(AvailableSettings.JTA_PLATFORM,
"org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform");
}
else {
JtaTransactionManager jtaTm = (JtaTransactionManager) jtaTransactionManager;
if (jtaTm.getTransactionManager() == null) {
throw new IllegalArgumentException(
"Can only apply JtaTransactionManager which has a TransactionManager reference set");
}
getProperties().put(AvailableSettings.JTA_PLATFORM,
new ConfigurableJtaPlatform(jtaTm.getTransactionManager(), jtaTm.getUserTransaction(),
jtaTm.getTransactionSynchronizationRegistry()));
}
}
else if (jtaTransactionManager instanceof TransactionManager) {
getProperties().put(AvailableSettings.JTA_PLATFORM,
new ConfigurableJtaPlatform((TransactionManager) jtaTransactionManager, null, null));
}
else {
throw new IllegalArgumentException(
"Unknown transaction manager type: " + jtaTransactionManager.getClass().getName());
}
// Hibernate 5.1/5.2: manually enforce connection release mode AFTER_STATEMENT (the JTA default)
try {
// Try Hibernate 5.2
AvailableSettings.class.getField("CONNECTION_HANDLING");
getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT");
}
catch (NoSuchFieldException ex) {
// Try Hibernate 5.1
try {
AvailableSettings.class.getField("ACQUIRE_CONNECTIONS");
getProperties().put("hibernate.connection.release_mode", "AFTER_STATEMENT");
}
catch (NoSuchFieldException ex2) {
// on Hibernate 5.0.x or lower - no need to change the default there
}
}
return this;
}
private void configureInterface(Reference reference, ReferenceBean referenceBean) {
Class<?> interfaceClass = reference.interfaceClass();
if (void.class.equals(interfaceClass)) {
interfaceClass = null;
String interfaceClassName = reference.interfaceName();
if (StringUtils.hasText(interfaceClassName)) {
if (ClassUtils.isPresent(interfaceClassName, classLoader)) {
interfaceClass = ClassUtils.resolveClassName(interfaceClassName, classLoader);
}
}
}
if (interfaceClass == null) {
interfaceClass = this.interfaceClass;
}
Assert.isTrue(interfaceClass.isInterface(),
"The class of field or method that was annotated @Reference is not an interface!");
referenceBean.setInterface(interfaceClass);
}
private static boolean fallBackToVersionSpecificClasses() {
ClassLoader usedClassLoader = ActorRepositoryIntegrationTest.class.getClassLoader();
String fqnBoot210Class = "org.springframework.boot.autoconfigure.insight.InsightsProperties";
String fqnBoot205Class = "org.springframework.boot.autoconfigure.security.servlet.RequestMatcherProvider";
return ClassUtils.isPresent(fqnBoot210Class, usedClassLoader) //
|| ClassUtils.isPresent(fqnBoot205Class, usedClassLoader);
}
private Class<?> resolveServiceInterfaceClass(Class<?> annotatedServiceBeanClass, Service service) {
Class<?> interfaceClass = service.interfaceClass();
if (void.class.equals(interfaceClass)) {
interfaceClass = null;
String interfaceClassName = service.interfaceName();
if (StringUtils.hasText(interfaceClassName)) {
if (ClassUtils.isPresent(interfaceClassName, classLoader)) {
interfaceClass = resolveClassName(interfaceClassName, classLoader);
}
}
}
if (interfaceClass == null) {
Class<?>[] allInterfaces = annotatedServiceBeanClass.getInterfaces();
if (allInterfaces.length > 0) {
interfaceClass = allInterfaces[0];
}
}
Assert.notNull(interfaceClass,
"@Service interfaceClass() or interfaceName() or interface class must be present!");
Assert.isTrue(interfaceClass.isInterface(),
"The type that was annotated @Service is not an interface!");
return interfaceClass;
}