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.proxy.backend.context;
19  
20  import lombok.AccessLevel;
21  import lombok.Getter;
22  import lombok.NoArgsConstructor;
23  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
24  import org.apache.shardingsphere.infra.state.instance.InstanceStateContext;
25  import org.apache.shardingsphere.mode.manager.ContextManager;
26  import org.apache.shardingsphere.proxy.backend.connector.jdbc.datasource.JDBCBackendDataSource;
27  
28  import java.util.Collection;
29  import java.util.Optional;
30  import java.util.stream.Collectors;
31  
32  /**
33   * Proxy context.
34   */
35  @NoArgsConstructor(access = AccessLevel.PRIVATE)
36  @Getter
37  public final class ProxyContext {
38      
39      private static final ProxyContext INSTANCE = new ProxyContext();
40      
41      private final JDBCBackendDataSource backendDataSource = new JDBCBackendDataSource();
42      
43      private ContextManager contextManager;
44      
45      /**
46       * Initialize proxy context.
47       *
48       * @param contextManager context manager
49       */
50      public static void init(final ContextManager contextManager) {
51          INSTANCE.contextManager = contextManager;
52      }
53      
54      /**
55       * Get instance of proxy context.
56       *
57       * @return got instance
58       */
59      public static ProxyContext getInstance() {
60          return INSTANCE;
61      }
62      
63      /**
64       * Check database exists.
65       *
66       * @param name database name
67       * @return database exists or not
68       */
69      public boolean databaseExists(final String name) {
70          return contextManager.getMetaDataContexts().getMetaData().containsDatabase(name);
71      }
72      
73      /**
74       * Get all database names.
75       *
76       * @return all database names
77       */
78      public Collection<String> getAllDatabaseNames() {
79          return contextManager.getMetaDataContexts().getMetaData().getDatabases().values().stream().map(ShardingSphereDatabase::getName).collect(Collectors.toList());
80      }
81      
82      /**
83       * Get instance state context.
84       * 
85       * @return instance state context
86       */
87      public Optional<InstanceStateContext> getInstanceStateContext() {
88          return null == contextManager.getInstanceContext() ? Optional.empty() : Optional.ofNullable(contextManager.getInstanceContext().getInstance().getState());
89      }
90  }