mirror of https://github.com/longtai-cn/hippo4j
commit
f3aa0b878b
@ -0,0 +1,40 @@
|
||||
#
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You 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.
|
||||
#
|
||||
|
||||
# This workflow will build a Java project with Maven
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- develop
|
||||
|
||||
jobs:
|
||||
contrib-readme-job:
|
||||
runs-on: ubuntu-latest
|
||||
name: A job to automate contrib in readme
|
||||
steps:
|
||||
- name: Contribute List
|
||||
uses: akhilmhdh/contributors-readme-action@v2.3.6
|
||||
with:
|
||||
image_size: 50
|
||||
columns_per_row: 9
|
||||
committer_email: m7798432@163.com
|
||||
committer_username: pirme
|
||||
commit_message: 'Update the list of contributors'
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.ACTION_TOKEN }}
|
@ -0,0 +1 @@
|
||||
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.adapter.web;
|
||||
|
||||
import cn.hippo4j.common.config.ApplicationContextHolder;
|
||||
import cn.hippo4j.common.model.WebIpAndPortInfo;
|
||||
import cn.hippo4j.common.toolkit.Assert;
|
||||
import cn.hippo4j.common.toolkit.StringUtil;
|
||||
import cn.hippo4j.core.toolkit.inet.InetUtils;
|
||||
import org.springframework.boot.web.server.WebServer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* Ip and port Holder
|
||||
*/
|
||||
public class WebIpAndPortHolder {
|
||||
|
||||
/**
|
||||
* Application ip and application post
|
||||
*/
|
||||
protected static AtomicReference<WebIpAndPortInfo> webIpAndPort = new AtomicReference<>();
|
||||
|
||||
public static final String ALL = "*";
|
||||
|
||||
protected static final String SEPARATOR = ",";
|
||||
|
||||
private WebIpAndPortHolder() {
|
||||
|
||||
}
|
||||
|
||||
protected static void initIpAndPort() {
|
||||
webIpAndPort.compareAndSet(null, getWebIpAndPortInfo());
|
||||
}
|
||||
|
||||
private static WebIpAndPortInfo getWebIpAndPortInfo() {
|
||||
InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class);
|
||||
InetUtils.HostInfo loopBackHostInfo = inetUtils.findFirstNonLoopBackHostInfo();
|
||||
Assert.notNull(loopBackHostInfo, "Unable to get the application IP address");
|
||||
String ip = loopBackHostInfo.getIpAddress();
|
||||
WebThreadPoolHandlerChoose webThreadPoolHandlerChoose = ApplicationContextHolder.getBean(WebThreadPoolHandlerChoose.class);
|
||||
WebThreadPoolService webThreadPoolService = webThreadPoolHandlerChoose.choose();
|
||||
// When get the port at startup, can get the message: "port xxx was already in use" or use two ports
|
||||
WebServer webServer = webThreadPoolService.getWebServer();
|
||||
String port = String.valueOf(webServer.getPort());
|
||||
return new WebIpAndPortInfo(ip, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* get WebIpAndPortInfo, If it is null, initialize it
|
||||
*
|
||||
* @return WebIpAndPortInfo
|
||||
*/
|
||||
public static WebIpAndPortInfo getWebIpAndPort() {
|
||||
if (webIpAndPort.get() == null) {
|
||||
initIpAndPort();
|
||||
}
|
||||
return WebIpAndPortHolder.webIpAndPort.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the new properties and instance IP and port
|
||||
*
|
||||
* @param nodes nodes in properties
|
||||
* @return Whether it meets the conditions
|
||||
*/
|
||||
public static boolean check(String nodes) {
|
||||
WebIpAndPortInfo webIpAndPort = WebIpAndPortHolder.getWebIpAndPort();
|
||||
if (StringUtil.isEmpty(nodes) || ALL.equals(nodes)) {
|
||||
return true;
|
||||
}
|
||||
String[] splitNodes = nodes.split(SEPARATOR);
|
||||
return Arrays.stream(splitNodes)
|
||||
.distinct()
|
||||
.map(WebIpAndPortInfo::build)
|
||||
.filter(Objects::nonNull)
|
||||
.anyMatch(each -> each.check(webIpAndPort.getIpSegment(), webIpAndPort.getPort()));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.auth.secuity;
|
||||
|
||||
public final class JwtTokenManagerTest {
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.auth.toolkit;
|
||||
|
||||
public final class JwtTokenUtilTest {
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.common.api;
|
||||
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
|
||||
/**
|
||||
* when init thread-pool dynamic refresh.
|
||||
*/
|
||||
public interface ThreadPoolInitRefresh extends ApplicationRunner {
|
||||
|
||||
/**
|
||||
* Initializes the thread pool after system startup
|
||||
*
|
||||
* @param context new properties
|
||||
*/
|
||||
void initRefresh(String context);
|
||||
|
||||
/**
|
||||
* get from the Configuration center
|
||||
*
|
||||
* @return new properties
|
||||
* @throws Exception exception
|
||||
*/
|
||||
String getProperties() throws Exception;
|
||||
|
||||
@Override
|
||||
default void run(ApplicationArguments args) throws Exception {
|
||||
String properties = getProperties();
|
||||
if (properties == null) {
|
||||
return;
|
||||
}
|
||||
initRefresh(properties);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.common.executor;
|
||||
|
||||
public final class ExecutorFactoryTest {
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.common.executor;
|
||||
|
||||
public final class ThreadPoolManagerTest {
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.common.function;
|
||||
|
||||
import cn.hippo4j.common.toolkit.Assert;
|
||||
import java.math.BigDecimal;
|
||||
import org.junit.Test;
|
||||
|
||||
public final class MatcherFunctionTest {
|
||||
|
||||
public static <T> boolean matchTest(Matcher<T> matcher, T value) {
|
||||
return matcher.match(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertMatch() {
|
||||
Assert.isTrue(matchTest(Boolean.TRUE::equals, true));
|
||||
Assert.isTrue(matchTest(BigDecimal.ZERO::equals, BigDecimal.ZERO));
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.common.function;
|
||||
|
||||
public final class NoArgsConsumerTest {
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.common.spi;
|
||||
|
||||
public final class DynamicThreadPoolServiceLoaderTest {
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.common.toolkit;
|
||||
|
||||
public final class AssertTest {
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.common.toolkit;
|
||||
|
||||
import org.checkerframework.checker.units.qual.A;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class GroupKeyTest {
|
||||
|
||||
@Test
|
||||
public void getKey() {
|
||||
String dataId = "dataId";
|
||||
String group = "group";
|
||||
String datumStr = "datumStr";
|
||||
String expected = "dataId+group+datumStr";
|
||||
String key = GroupKey.getKey(dataId, group, datumStr);
|
||||
Assert.isTrue(key.equals(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetKey() {
|
||||
String dataId = "dataId";
|
||||
String group = "group";
|
||||
String expected = "dataId+group";
|
||||
String key = GroupKey.getKey(dataId, group);
|
||||
Assert.isTrue(key.equals(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetKey1() {
|
||||
String[] strings = {"dataId", "group", "datumStr"};
|
||||
String expected = "dataId+group+datumStr";
|
||||
String key = GroupKey.getKey(strings);
|
||||
Assert.isTrue(key.equals(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getKeyTenant() {
|
||||
String dataId = "dataId";
|
||||
String group = "group";
|
||||
String datumStr = "datumStr";
|
||||
String expected = "dataId+group+datumStr";
|
||||
|
||||
String keyTenant = GroupKey.getKeyTenant(dataId, group, datumStr);
|
||||
Assert.isTrue(keyTenant.equals(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseKey() {
|
||||
String groupKey = "prescription+dynamic-threadpool-example+message-consume+12";
|
||||
String[] strings = GroupKey.parseKey(groupKey);
|
||||
Assert.isTrue(strings.length == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void urlEncode() {
|
||||
String str = "hello+World%";
|
||||
String expected = "hello%2BWorld%25";
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
GroupKey.urlEncode(str, stringBuilder);
|
||||
Assert.isTrue(stringBuilder.toString().contains(expected));
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.common.web;
|
||||
|
||||
public final class ResultsTest {
|
||||
}
|
File diff suppressed because one or more lines are too long
@ -1 +1 @@
|
||||
.waves-ripple{position:absolute;border-radius:100%;background-color:rgba(0,0,0,.15);background-clip:padding-box;pointer-events:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-webkit-transform:scale(0);transform:scale(0);opacity:1}.waves-ripple.z-active{opacity:0;-webkit-transform:scale(2);transform:scale(2);-webkit-transition:opacity 1.2s ease-out,-webkit-transform .6s ease-out;transition:opacity 1.2s ease-out,-webkit-transform .6s ease-out;transition:opacity 1.2s ease-out,transform .6s ease-out;transition:opacity 1.2s ease-out,transform .6s ease-out,-webkit-transform .6s ease-out}.dashboard-editor-container[data-v-490e92e5]{padding:32px;background-color:#f0f2f5;position:relative;min-height:100vh}
|
||||
.waves-ripple{position:absolute;border-radius:100%;background-color:rgba(0,0,0,.15);background-clip:padding-box;pointer-events:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-webkit-transform:scale(0);transform:scale(0);opacity:1}.waves-ripple.z-active{opacity:0;-webkit-transform:scale(2);transform:scale(2);-webkit-transition:opacity 1.2s ease-out,-webkit-transform .6s ease-out;transition:opacity 1.2s ease-out,-webkit-transform .6s ease-out;transition:opacity 1.2s ease-out,transform .6s ease-out;transition:opacity 1.2s ease-out,transform .6s ease-out,-webkit-transform .6s ease-out}.dashboard-editor-container[data-v-a25e0782]{padding:32px;background-color:#f0f2f5;position:relative;min-height:100vh}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.server.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Database configuration.
|
||||
*
|
||||
* <p> Quoted from org.apache.shenyu.admin.config.DataBaseConfiguration
|
||||
*/
|
||||
@Configuration
|
||||
public class DataBaseConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(value = DataBaseProperties.class)
|
||||
public DataBaseProperties dataBaseProperties(@Value("${hippo4j.database.dialect:h2}") String dialect,
|
||||
@Value("${hippo4j.database.init_script:sql-script/h2/schema.sql}") String initScript,
|
||||
@Value("${hippo4j.database.init_enable:false}") Boolean initEnable) {
|
||||
DataBaseProperties dataSourceProperties = new DataBaseProperties();
|
||||
dataSourceProperties.setDialect(dialect);
|
||||
dataSourceProperties.setInitScript(initScript);
|
||||
dataSourceProperties.setInitEnable(initEnable);
|
||||
return dataSourceProperties;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.server.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Database properties.
|
||||
*
|
||||
* <p> Quoted from org.apache.shenyu.admin.config.properties.DataBaseProperties
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "hippo4j.database")
|
||||
public class DataBaseProperties {
|
||||
|
||||
/**
|
||||
* Dialect
|
||||
*/
|
||||
private String dialect;
|
||||
|
||||
/**
|
||||
* Init script
|
||||
*/
|
||||
private String initScript;
|
||||
|
||||
/**
|
||||
* Init enable
|
||||
*/
|
||||
private Boolean initEnable;
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.server.init;
|
||||
|
||||
import cn.hippo4j.server.config.DataBaseProperties;
|
||||
import com.google.common.base.Splitter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.ibatis.io.Resources;
|
||||
import org.apache.ibatis.jdbc.ScriptRunner;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.*;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Local datasource loader.
|
||||
*
|
||||
* <p> Quoted from org.apache.shenyu.admin.spring.LocalDataSourceLoader
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@ConditionalOnExpression("'${hippo4j.database.dialect}' == 'mysql' or '${hippo4j.database.dialect}' == 'h2'")
|
||||
public class LocalDataSourceLoader implements InstantiationAwareBeanPostProcessor {
|
||||
|
||||
private static final String PRE_FIX = "file:";
|
||||
|
||||
@Resource
|
||||
private DataBaseProperties dataBaseProperties;
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(@NonNull final Object bean, final String beanName) throws BeansException {
|
||||
if ((bean instanceof DataSourceProperties) && dataBaseProperties.getInitEnable()) {
|
||||
this.init((DataSourceProperties) bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
private void init(final DataSourceProperties properties) {
|
||||
try {
|
||||
String jdbcUrl = properties.getUrl();
|
||||
// If jdbcUrl in the configuration file specifies the hippo4j database, it is removed,
|
||||
// because the hippo4j database does not need to be specified when executing the SQL file,
|
||||
// otherwise the hippo4j database will be disconnected when the hippo4j database does not exist
|
||||
if (Objects.equals(dataBaseProperties.getDialect(), "mysql")) {
|
||||
jdbcUrl = StringUtils.replace(properties.getUrl(), "/hippo4j_manager?", "?");
|
||||
}
|
||||
Connection connection = DriverManager.getConnection(jdbcUrl, properties.getUsername(), properties.getPassword());
|
||||
// TODO Compatible with h2 to execute `INSERT IGNORE INTO` statement error
|
||||
if (Objects.equals(dataBaseProperties.getDialect(), "h2") && ifNonExecute(connection)) {
|
||||
return;
|
||||
}
|
||||
execute(connection, dataBaseProperties.getInitScript());
|
||||
} catch (Exception ex) {
|
||||
log.error("Datasource init error.", ex);
|
||||
throw new RuntimeException(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean ifNonExecute(final Connection conn) throws SQLException {
|
||||
try (
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM `user`")) {
|
||||
if (resultSet.next()) {
|
||||
int countUser = resultSet.getInt(1);
|
||||
return countUser > 0 ? true : false;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
log.error("Query data for errors.", ignored);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void execute(final Connection conn, final String script) throws Exception {
|
||||
ScriptRunner runner = new ScriptRunner(conn);
|
||||
try {
|
||||
// Doesn't print logger
|
||||
runner.setLogWriter(null);
|
||||
runner.setAutoCommit(true);
|
||||
Resources.setCharset(StandardCharsets.UTF_8);
|
||||
List<String> initScripts = Splitter.on(";").splitToList(script);
|
||||
for (String sqlScript : initScripts) {
|
||||
if (sqlScript.startsWith(PRE_FIX)) {
|
||||
String sqlFile = sqlScript.substring(PRE_FIX.length());
|
||||
try (Reader fileReader = getResourceAsReader(sqlFile)) {
|
||||
log.info("Execute hippo4j schema sql: {}", sqlFile);
|
||||
runner.runScript(fileReader);
|
||||
}
|
||||
} else {
|
||||
try (Reader fileReader = Resources.getResourceAsReader(sqlScript)) {
|
||||
log.info("Execute hippo4j schema sql: {}", sqlScript);
|
||||
runner.runScript(fileReader);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static Reader getResourceAsReader(final String resource) throws IOException {
|
||||
return new InputStreamReader(new FileInputStream(resource), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
### Data source customization section
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/hippo4j_manager?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=root
|
@ -1,15 +1,7 @@
|
||||
|
||||
,--, ,--, ,---._
|
||||
,--.'| ,--.'| .-- -.' \ Hippo4J ${application.version}
|
||||
,--, | : ,--, ,-.----. ,-.----. ,--, | : | | : Port: ${server.port}
|
||||
,---.'| : ',--.'| \ / \ \ / \ ,---. ,---.'| : ' : ; | PID: ${pid}
|
||||
| | : _' || |, | : || : | ' ,'\ ; : | | ; : | Console: http://127.0.0.1:${server.port}/index.html
|
||||
: : |.' |`--'_ | | .\ :| | .\ : / / || | : _' | | : :
|
||||
| ' ' ; :,' ,'| . : |: |. : |: |. ; ,. :: : |.' | : https://hippo4j.cn
|
||||
' | .'. |' | | | | \ :| | \ :' | |: :| ' ' ; : | ; |
|
||||
| | : | '| | : | : . || : . |' | .; :\ \ .'. | ___ l
|
||||
' : | : ;' : |__ : |`-': |`-'| : | `---`: | ' / /\ J :
|
||||
| | ' ,/ | | '.'|: : : : : : \ \ / ' ; |/ ../ `..- ,
|
||||
; : ;--' ; : ;| | : | | : `----' | : ;\ \ ;
|
||||
| ,/ | , / `---'.| `---'.| ' ,/ \ \ ,'
|
||||
'---' ---`-' `---` `---` '--' "---....--'
|
||||
__ __ ___ ___ __
|
||||
| |--.|__|.-----..-----..-----.| | | |__| Hippo4J ${application.version}
|
||||
| || || _ || _ || _ || | | | | Port: ${server.port} PID: ${pid}
|
||||
|__|__||__|| __|| __||_____||____ | | |
|
||||
|__| |__| |: ||___| Site: https://hippo4j.cn
|
||||
`---'
|
||||
|
Loading…
Reference in new issue