View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.metadata;
19  
20  import lombok.Getter;
21  import lombok.SneakyThrows;
22  import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
23  import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationProperties;
24  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
25  import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
26  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
27  import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
28  import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
29  import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
30  import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute;
31  import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
32  import org.apache.shardingsphere.infra.rule.scope.GlobalRule.GlobalRuleChangedType;
33  
34  import java.util.Collections;
35  import java.util.HashMap;
36  import java.util.Map;
37  import java.util.Optional;
38  import java.util.Properties;
39  import java.util.concurrent.ConcurrentHashMap;
40  
41  /**
42   * ShardingSphere meta data.
43   */
44  @Getter
45  public final class ShardingSphereMetaData {
46      
47      private final Map<String, ShardingSphereDatabase> databases;
48      
49      private final ResourceMetaData globalResourceMetaData;
50      
51      private final RuleMetaData globalRuleMetaData;
52      
53      private final ConfigurationProperties props;
54      
55      private final TemporaryConfigurationProperties temporaryProps;
56      
57      public ShardingSphereMetaData() {
58          this(new HashMap<>(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties()));
59      }
60      
61      public ShardingSphereMetaData(final Map<String, ShardingSphereDatabase> databases, final ResourceMetaData globalResourceMetaData,
62                                    final RuleMetaData globalRuleMetaData, final ConfigurationProperties props) {
63          this.databases = new ConcurrentHashMap<>(databases.size(), 1F);
64          databases.forEach((key, value) -> this.databases.put(key.toLowerCase(), value));
65          this.globalResourceMetaData = globalResourceMetaData;
66          this.globalRuleMetaData = globalRuleMetaData;
67          this.props = props;
68          temporaryProps = new TemporaryConfigurationProperties(props.getProps());
69      }
70      
71      /**
72       * Judge contains database from meta data or not.
73       *
74       * @param databaseName database name
75       * @return contains database from meta data or not
76       */
77      public boolean containsDatabase(final String databaseName) {
78          return databases.containsKey(databaseName.toLowerCase());
79      }
80      
81      /**
82       * Get database.
83       *
84       * @param databaseName database name
85       * @return meta data database
86       */
87      public ShardingSphereDatabase getDatabase(final String databaseName) {
88          return databases.get(databaseName.toLowerCase());
89      }
90      
91      /**
92       * Add database.
93       *
94       * @param databaseName database name
95       * @param protocolType protocol database type
96       * @param props configuration properties
97       */
98      public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) {
99          ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props);
100         databases.put(database.getName().toLowerCase(), database);
101         globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases, GlobalRuleChangedType.DATABASE_CHANGED));
102     }
103     
104     /**
105      * Drop database.
106      *
107      * @param databaseName database name
108      */
109     public void dropDatabase(final String databaseName) {
110         cleanResources(databases.remove(databaseName.toLowerCase()));
111     }
112     
113     @SneakyThrows(Exception.class)
114     private void cleanResources(final ShardingSphereDatabase database) {
115         globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases, GlobalRuleChangedType.DATABASE_CHANGED));
116         for (ShardingSphereRule each : database.getRuleMetaData().getRules()) {
117             if (each instanceof AutoCloseable) {
118                 ((AutoCloseable) each).close();
119             }
120         }
121         database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class).forEach(StaticDataSourceRuleAttribute::cleanStorageNodeDataSources);
122         Optional.ofNullable(database.getResourceMetaData())
123                 .ifPresent(optional -> optional.getStorageUnits().values().forEach(each -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy()));
124     }
125 }