View Javadoc
1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.logging.log4j.catalog.api.annotation;
17  
18  import java.util.Map;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.springframework.context.annotation.Condition;
23  import org.springframework.context.annotation.ConditionContext;
24  import org.springframework.core.env.Environment;
25  import org.springframework.core.type.AnnotatedTypeMetadata;
26  
27  /**
28   *
29   */
30  public class JdbcUrlCondition implements Condition {
31  
32      private static final Logger LOGGER = LogManager.getLogger(JdbcUrlCondition.class);
33      @Override
34      public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
35          Environment env = context.getEnvironment();
36          Map<String, Object> map = metadata.getAnnotationAttributes(JdbcUrl.class.getName());
37          if (map != null && map.containsKey("value")) {
38              String value = map.get("value").toString();
39              String jdbcUrl = env.getProperty("jdbcUrl");
40              Boolean isEmbedded = Boolean.parseBoolean(env.getProperty("isEmbedded"));
41              boolean result;
42              if (value.equals("hsqldb")) {
43                  result = jdbcUrl == null || isEmbedded;
44              } else if (jdbcUrl == null || isEmbedded) {
45                  result = false;
46              } else if (!jdbcUrl.startsWith("jdbc:")) {
47                  result = false;
48              } else {
49                  result = jdbcUrl.substring(5).toLowerCase().startsWith(value.toLowerCase());
50              }
51              LOGGER.debug("Returning {} for {}", result, value);
52              return result;
53          }
54          LOGGER.debug("No data provided");
55          return false;
56      }
57  }