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