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.session.connection;
19  
20  import lombok.AccessLevel;
21  import lombok.Getter;
22  import lombok.RequiredArgsConstructor;
23  import lombok.Setter;
24  import org.apache.shardingsphere.infra.metadata.user.Grantee;
25  import org.apache.shardingsphere.infra.session.connection.cursor.CursorConnectionContext;
26  import org.apache.shardingsphere.infra.session.connection.datasource.UsedDataSourceProvider;
27  import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext;
28  
29  import java.util.Collection;
30  import java.util.HashSet;
31  import java.util.Optional;
32  
33  /**
34   * Connection context.
35   */
36  @RequiredArgsConstructor
37  @Getter
38  @Setter
39  public final class ConnectionContext implements AutoCloseable {
40      
41      @Getter(AccessLevel.NONE)
42      private final UsedDataSourceProvider usedDataSourceProvider;
43      
44      private final Grantee grantee;
45      
46      private final CursorConnectionContext cursorContext = new CursorConnectionContext();
47      
48      private final TransactionConnectionContext transactionContext = new TransactionConnectionContext();
49      
50      @Setter(AccessLevel.NONE)
51      private String currentDatabaseName;
52      
53      public ConnectionContext(final UsedDataSourceProvider usedDataSourceProvider) {
54          this(usedDataSourceProvider, null);
55      }
56      
57      /**
58       * Get used data source names.
59       *
60       * @return used data source names
61       */
62      public Collection<String> getUsedDataSourceNames() {
63          Collection<String> result = new HashSet<>(usedDataSourceProvider.getNames().size(), 1F);
64          for (String each : usedDataSourceProvider.getNames()) {
65              result.add(each.contains(".") ? each.split("\\.")[1] : each);
66          }
67          return result;
68      }
69      
70      /**
71       * Clear cursor connection context.
72       */
73      public void clearCursorContext() {
74          cursorContext.close();
75      }
76      
77      /**
78       * Clear transaction connection context.
79       */
80      public void clearTransactionContext() {
81          transactionContext.close();
82      }
83      
84      /**
85       * Set current database name.
86       *
87       * @param currentDatabaseName current database name
88       */
89      public void setCurrentDatabaseName(final String currentDatabaseName) {
90          if (null != currentDatabaseName && !currentDatabaseName.equals(this.currentDatabaseName)) {
91              this.currentDatabaseName = currentDatabaseName;
92          }
93      }
94      
95      /**
96       * Get current database name.
97       *
98       * @return current database name
99       */
100     public Optional<String> getCurrentDatabaseName() {
101         return Optional.ofNullable(currentDatabaseName);
102     }
103     
104     @Override
105     public void close() {
106         clearCursorContext();
107         clearTransactionContext();
108     }
109 }