diff --git a/docs/spring/cs858fd53a-3f01-11ee-a772-acde48001122.java b/docs/spring/cs858fd53a-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..6b5509de --- /dev/null +++ b/docs/spring/cs858fd53a-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,135 @@ +package com.huifer.bigfile.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * 限流下载service + */ +@Service +public class LimiterFileService { + /** + * 下载计数器:记录有多少个下载请求 + */ + public static final AtomicInteger DOWNLOAD_NUM = new AtomicInteger(); + protected static final Logger log = LoggerFactory.getLogger(LimiterFileService.class); + /** + * 最大下载速度,单位:kb + */ + @Value("${max.download.speed}") + public int maxDownloadSpeed; + + + public void downloadBreak(String name, HttpServletRequest request, HttpServletResponse response) { + + + } + + + /** + * 限流下载 + * // TODO: 2019/9/2 下载暂停会出现异常 + * + * @param exportUrl 下载文件url + * @param speed 速度 + * @param response response + */ + public void downloadLimit(String exportUrl, int speed, HttpServletResponse response) { + OutputStream out = null; + String fileName = exportUrl.substring(exportUrl.lastIndexOf('/') + 1); + response.setContentType("application/octet-stream"); + response.setHeader("Content-Disposition", "attachment; filename=" + fileName); + int downloadSpeed = speed; + + try ( + InputStream fileInputStream = new FileInputStream(exportUrl); + ) { + BandwidthLimiter bandwidthLimiter = new BandwidthLimiter(downloadSpeed); + DownloadLimiter downloadLimiter = new DownloadLimiter(fileInputStream, bandwidthLimiter); + + out = response.getOutputStream(); + int len = 0; + while ((len = downloadLimiter.read()) != -1) { + out.write(len); + bandwidthLimiter.setMaxRate(speed); + } + out.flush(); + out.close(); + downloadLimiter.close(); + } catch (IOException e) { + log.error("下载失败={}", e); + } finally { + closeStream(out, null); + } + } + + /** + * 限流下载 + * + * @param exportUrl 下载文件url + * @param response response + */ + public void downloadLimit(String exportUrl, HttpServletResponse response) { + DOWNLOAD_NUM.incrementAndGet(); + OutputStream out = null; + DownloadLimiter downloadLimiter = null; + String fileName = exportUrl.substring(exportUrl.lastIndexOf('/') + 1); + response.setContentType("application/octet-stream"); + response.setHeader("Content-Disposition", "attachment; filename=" + fileName); + + try (InputStream fileInputStream = new FileInputStream(exportUrl);) { + int downloadSpeed = maxDownloadSpeed / DOWNLOAD_NUM.get(); + BandwidthLimiter bandwidthLimiter = new BandwidthLimiter(downloadSpeed); + downloadLimiter = new DownloadLimiter(fileInputStream, bandwidthLimiter); + out = response.getOutputStream(); + int len = 0; + while ((len = downloadLimiter.read()) != -1) { + out.write(len); + bandwidthLimiter.setMaxRate(maxDownloadSpeed / DOWNLOAD_NUM.get()); + } + out.flush(); + out.close(); + downloadLimiter.close(); + } catch (IOException e) { + log.error("下载失败={}", e); + } finally { + closeStream(out, downloadLimiter); + } + DOWNLOAD_NUM.decrementAndGet(); + } + + /** + * 关闭流 + * + * @param out OutputStream + * @param downloadLimiter DownloadLimiter + */ + protected void closeStream(OutputStream out, DownloadLimiter downloadLimiter) { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + log.error("输出流关闭失败{}", e); + } + } + if (downloadLimiter != null) { + try { + downloadLimiter.close(); + } catch (IOException e) { + log.error("下载限流关闭失败={}", e); + } + } + } + + +} diff --git a/docs/spring/cs85d051b4-3f01-11ee-a772-acde48001122.java b/docs/spring/cs85d051b4-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..248c97b8 --- /dev/null +++ b/docs/spring/cs85d051b4-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,93 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * 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 org.apache.ibatis.mapping; + +import org.apache.ibatis.builder.InitializingObject; +import org.apache.ibatis.cache.Cache; +import org.apache.ibatis.cache.CacheException; +import org.apache.ibatis.cache.impl.PerpetualCache; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; + +import static com.googlecode.catchexception.apis.BDDCatchException.caughtException; +import static com.googlecode.catchexception.apis.BDDCatchException.when; +import static org.assertj.core.api.BDDAssertions.then; + +class CacheBuilderTest { + + @Test + void testInitializing() { + InitializingCache cache = unwrap(new CacheBuilder("test").implementation(InitializingCache.class).build()); + + Assertions.assertThat(cache.initialized).isTrue(); + } + + @Test + void testInitializingFailure() { + when(() -> new CacheBuilder("test").implementation(InitializingFailureCache.class).build()); + then(caughtException()).isInstanceOf(CacheException.class) + .hasMessage("Failed cache initialization for 'test' on 'org.apache.ibatis.mapping.CacheBuilderTest$InitializingFailureCache'"); + } + + @SuppressWarnings("unchecked") + private T unwrap(Cache cache) { + Field field; + try { + field = cache.getClass().getDeclaredField("delegate"); + } catch (NoSuchFieldException e) { + throw new IllegalStateException(e); + } + try { + field.setAccessible(true); + return (T) field.get(cache); + } catch (IllegalAccessException e) { + throw new IllegalStateException(e); + } finally { + field.setAccessible(false); + } + } + + private static class InitializingCache extends PerpetualCache implements InitializingObject { + + private boolean initialized; + + public InitializingCache(String id) { + super(id); + } + + @Override + public void initialize() { + this.initialized = true; + } + + } + + private static class InitializingFailureCache extends PerpetualCache implements InitializingObject { + + public InitializingFailureCache(String id) { + super(id); + } + + @Override + public void initialize() { + throw new IllegalStateException("error"); + } + + } + +} diff --git a/docs/spring/cs860d1cd4-3f01-11ee-a772-acde48001122.java b/docs/spring/cs860d1cd4-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..45980329 --- /dev/null +++ b/docs/spring/cs860d1cd4-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,94 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * 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 org.apache.ibatis.type; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class EnumOrdinalTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new EnumOrdinalTypeHandler(MyEnum.class); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, MyEnum.ONE, null); + verify(ps).setInt(1, 0); + } + + @Test + public void shouldSetNullParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, null, JdbcType.VARCHAR); + verify(ps).setNull(1, JdbcType.VARCHAR.TYPE_CODE); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getInt("column")).thenReturn(0); + when(rs.wasNull()).thenReturn(false); + assertEquals(MyEnum.ONE, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getInt("column")).thenReturn(0); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getInt(1)).thenReturn(0); + when(rs.wasNull()).thenReturn(false); + assertEquals(MyEnum.ONE, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getInt(1)).thenReturn(0); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getInt(1)).thenReturn(0); + when(cs.wasNull()).thenReturn(false); + assertEquals(MyEnum.ONE, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getInt(1)).thenReturn(0); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } + + enum MyEnum { + ONE, TWO + } + +} diff --git a/docs/spring/cs864a0da6-3f01-11ee-a772-acde48001122.java b/docs/spring/cs864a0da6-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..87bc3a31 --- /dev/null +++ b/docs/spring/cs864a0da6-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,89 @@ +package com.huifer.utils.entity; + +import lombok.EqualsAndHashCode; +import lombok.ToString; + +import java.util.List; + +/** + * @author: wang + * @description: 表 + */ +@ToString +@EqualsAndHashCode +public class TableEntity { + + /** + * 表的名称 + */ + private String tableName; + /** + * 表的备注 + */ + private String comments; + /** + * 表的主键 + */ + private ColumnEntity pk; + /** + * 表的列名(不包含主键) + */ + private List columns; + + /** + * 类名(第一个字母大写),如:sys_user => SysUser + */ + private String className; + /** + * 类名(第一个字母小写),如:sys_user => sysUser + */ + private String classname; + + public String getTableName() { + return tableName; + } + + public void setTableName(String tableName) { + this.tableName = tableName; + } + + public String getComments() { + return comments; + } + + public void setComments(String comments) { + this.comments = comments; + } + + public ColumnEntity getPk() { + return pk; + } + + public void setPk(ColumnEntity pk) { + this.pk = pk; + } + + public List getColumns() { + return columns; + } + + public void setColumns(List columns) { + this.columns = columns; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getClassname() { + return classname; + } + + public void setClassname(String classname) { + this.classname = classname; + } +} \ No newline at end of file diff --git a/docs/spring/cs86874b12-3f01-11ee-a772-acde48001122.java b/docs/spring/cs86874b12-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..e1de7b99 --- /dev/null +++ b/docs/spring/cs86874b12-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,22 @@ +/** + * Copyright 2009-2018 the original author or authors. + *

+ * 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 org.apache.ibatis.binding; + +public interface WrongNamespaceMapper { + + void get(); + +} diff --git a/docs/spring/cs86c36e3a-3f01-11ee-a772-acde48001122.java b/docs/spring/cs86c36e3a-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..7825daef --- /dev/null +++ b/docs/spring/cs86c36e3a-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,125 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * 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 org.apache.ibatis.binding; + +import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder; +import org.apache.ibatis.io.ResolverUtil; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSession; + +import java.util.*; + +/** + * @author Clinton Begin + * @author Eduardo Macarron + * @author Lasse Voss + */ +public class MapperRegistry { + + private final Configuration config; + private final Map, MapperProxyFactory> knownMappers = new HashMap<>(); + + public MapperRegistry(Configuration config) { + this.config = config; + } + + /** + * 获取 mapper + * + * @param type + * @param sqlSession + * @param + * @return + */ + @SuppressWarnings("unchecked") + public T getMapper(Class type, SqlSession sqlSession) { + final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) knownMappers.get(type); + if (mapperProxyFactory == null) { + throw new BindingException("Type " + type + " is not known to the MapperRegistry."); + } + try { + return mapperProxyFactory.newInstance(sqlSession); + } catch (Exception e) { + throw new BindingException("Error getting mapper instance. Cause: " + e, e); + } + } + + /** + * 判断是否包含当前 mapper + * + * @param type mapper.class + * @param + * @return + */ + public boolean hasMapper(Class type) { + return knownMappers.containsKey(type); + } + + /** + * 注册mapper + * + * @param type + * @param + */ + public void addMapper(Class type) { + if (type.isInterface()) { + if (hasMapper(type)) { + throw new BindingException("Type " + type + " is already known to the MapperRegistry."); + } + boolean loadCompleted = false; + try { + knownMappers.put(type, new MapperProxyFactory<>(type)); + // It's important that the type is added before the parser is run + // otherwise the binding may automatically be attempted by the + // mapper parser. If the type is already known, it won't try. + MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type); + parser.parse(); + loadCompleted = true; + } finally { + if (!loadCompleted) { + knownMappers.remove(type); + } + } + } + } + + /** + * @since 3.2.2 + */ + public Collection> getMappers() { + return Collections.unmodifiableCollection(knownMappers.keySet()); + } + + /** + * @since 3.2.2 + */ + public void addMappers(String packageName, Class superType) { + ResolverUtil> resolverUtil = new ResolverUtil<>(); + resolverUtil.find(new ResolverUtil.IsA(superType), packageName); + Set>> mapperSet = resolverUtil.getClasses(); + for (Class mapperClass : mapperSet) { + addMapper(mapperClass); + } + } + + /** + * @since 3.2.2 + */ + public void addMappers(String packageName) { + addMappers(packageName, Object.class); + } + +} diff --git a/docs/spring/cs86fead56-3f01-11ee-a772-acde48001122.java b/docs/spring/cs86fead56-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..5b3d2469 --- /dev/null +++ b/docs/spring/cs86fead56-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,42 @@ +/** + * Copyright 2009-2018 the original author or authors. + *

+ * 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 org.apache.ibatis.submitted.call_setters_on_nulls; + +public class User { + + public boolean nullReceived; + private Integer id; + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + if (name == null) { + nullReceived = true; + } + this.name = name; + } +} diff --git a/docs/spring/cs873debce-3f01-11ee-a772-acde48001122.java b/docs/spring/cs873debce-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..8d13734e --- /dev/null +++ b/docs/spring/cs873debce-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,16 @@ +package com.huifer.design.factory; + +/** + *

Title : MengNiu

+ *

Description : 蒙牛

+ * + * @author huifer + * @date 2019-05-15 + */ +public class MengNiu implements Milk { + + @Override + public String getName() { + return "蒙牛"; + } +} diff --git a/docs/spring/cs877885d6-3f01-11ee-a772-acde48001122.java b/docs/spring/cs877885d6-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..d06731ab --- /dev/null +++ b/docs/spring/cs877885d6-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,19 @@ +/** + * Copyright 2009-2015 the original author or authors. + *

+ * 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 org.apache.ibatis.domain.blog; + +public class DraftPost extends Post { +} diff --git a/docs/spring/cs87b3a8c8-3f01-11ee-a772-acde48001122.java b/docs/spring/cs87b3a8c8-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..07f69430 --- /dev/null +++ b/docs/spring/cs87b3a8c8-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,16 @@ +package com.huifer.security; + +import com.huifer.security.properties.SecurityProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * 描述: + * + * @author: huifer + * @date: 2019-11-17 + */ +@Configuration +@EnableConfigurationProperties(SecurityProperties.class) +public class SecurityCoreConfig { +} diff --git a/docs/spring/cs87ed6a18-3f01-11ee-a772-acde48001122.java b/docs/spring/cs87ed6a18-3f01-11ee-a772-acde48001122.java new file mode 100644 index 00000000..cfd8c4f8 --- /dev/null +++ b/docs/spring/cs87ed6a18-3f01-11ee-a772-acde48001122.java @@ -0,0 +1,20 @@ +package com.huifer.security.properties; + +/** + * 描述: + * + * @author: huifer + * @date: 2019-11-17 + */ +public enum LoginType { + + /** + * json + */ + JSON, + /** + * 跳转 + */ + REDIRECT, + ; +}