|
| 1 | +/* |
| 2 | + * Copyright [2019] [恒宇少年 - 于起宇] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.minbox.framework.api.boot.autoconfigure.push; |
| 19 | + |
| 20 | +import cn.jpush.api.JPushClient; |
| 21 | +import org.minbox.framework.api.boot.plugin.message.push.ApiBootMessagePushService; |
| 22 | +import org.minbox.framework.api.boot.plugin.message.push.aop.advistor.ApiBootMessagePushClientSwitchAdvisor; |
| 23 | +import org.minbox.framework.api.boot.plugin.message.push.aop.interceptor.ApiBootMessagePushSwitchAnnotationInterceptor; |
| 24 | +import org.minbox.framework.api.boot.plugin.message.push.model.PushClientConfig; |
| 25 | +import org.minbox.framework.api.boot.plugin.message.push.support.ApiBootMessagePushJiGuangServiceImpl; |
| 26 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 27 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 28 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | +import org.springframework.util.ObjectUtils; |
| 32 | + |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +/** |
| 37 | + * ApiBoot Message Push Auto Configuration |
| 38 | + * |
| 39 | + * @author:恒宇少年 - 于起宇 |
| 40 | + * <p> |
| 41 | + * DateTime:2019-04-20 14:27 |
| 42 | + * Blog:http://blog.yuqiyu.com |
| 43 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 44 | + * Gitee:https://gitee.com/hengboy |
| 45 | + * GitHub:https://github.com/hengboy |
| 46 | + */ |
| 47 | +@Configuration |
| 48 | +@ConditionalOnClass({ApiBootMessagePushService.class, JPushClient.class}) |
| 49 | +@EnableConfigurationProperties(ApiBootMessagePushProperties.class) |
| 50 | +public class ApiBootMessagePushAutoConfiguration { |
| 51 | + /** |
| 52 | + * ApiBoot Message Push Properties |
| 53 | + */ |
| 54 | + private ApiBootMessagePushProperties apiBootMessagePushProperties; |
| 55 | + /** |
| 56 | + * default client name |
| 57 | + */ |
| 58 | + private static final String DEFAULT_CLIENT_NAME = "default"; |
| 59 | + |
| 60 | + public ApiBootMessagePushAutoConfiguration(ApiBootMessagePushProperties apiBootMessagePushProperties) { |
| 61 | + this.apiBootMessagePushProperties = apiBootMessagePushProperties; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * instantiation message push service |
| 66 | + * |
| 67 | + * @return ApiBootMessagePushService |
| 68 | + */ |
| 69 | + @Bean |
| 70 | + @ConditionalOnMissingBean |
| 71 | + ApiBootMessagePushService apiBootMessagePushService() { |
| 72 | + Map<String, PushClientConfig> configs = getClientConfig(); |
| 73 | + return new ApiBootMessagePushJiGuangServiceImpl(configs, apiBootMessagePushProperties.isProduction()); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Message Push Aop Interceptor |
| 78 | + * |
| 79 | + * @return ApiBootMessagePushSwitchAnnotationInterceptor |
| 80 | + */ |
| 81 | + @Bean |
| 82 | + @ConditionalOnMissingBean |
| 83 | + ApiBootMessagePushSwitchAnnotationInterceptor apiBootMessagePushSwitchAnnotationInterceptor() { |
| 84 | + return new ApiBootMessagePushSwitchAnnotationInterceptor(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Message Push Aop Advisor |
| 89 | + * |
| 90 | + * @return ApiBootMessagePushClientSwitchAdvisor |
| 91 | + */ |
| 92 | + @Bean |
| 93 | + @ConditionalOnMissingBean |
| 94 | + ApiBootMessagePushClientSwitchAdvisor apiBootMessagePushClientSwitchAdvisor() { |
| 95 | + return new ApiBootMessagePushClientSwitchAdvisor(apiBootMessagePushSwitchAnnotationInterceptor()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * get client config |
| 100 | + * |
| 101 | + * @return |
| 102 | + */ |
| 103 | + private Map<String, PushClientConfig> getClientConfig() { |
| 104 | + |
| 105 | + Map<String, PushClientConfig> configs = new HashMap(1); |
| 106 | + |
| 107 | + // default client config |
| 108 | + configs.put(DEFAULT_CLIENT_NAME, apiBootMessagePushProperties.getClient()); |
| 109 | + |
| 110 | + // multiple |
| 111 | + if (!ObjectUtils.isEmpty(apiBootMessagePushProperties.getMultiple())) { |
| 112 | + configs.putAll(apiBootMessagePushProperties.getMultiple()); |
| 113 | + } |
| 114 | + return configs; |
| 115 | + } |
| 116 | +} |
0 commit comments