BrightSide Workbench Full Report + Source Code
Secrets.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.action;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.collections.parser.ParserException;
24 import org.turro.string.Strings;
25 import org.turro.annotation.Secret;
26 import org.turro.collections.KeyValueMap;
27 import org.turro.reflection.Instances;
28 
33 public class Secrets {
34 
35  /* Secrets */
36 
37  public static String getSecret(String keys) {
38  try {
39  return Secrets.<String>getObject(new KeyValueMap(keys));
40  } catch (ParserException ex) {
41  Logger.getLogger(Secrets.class.getName()).log(Level.SEVERE, null, ex);
42  }
43  return null;
44  }
45 
46  public static <T> T getObject(String keys) {
47  try {
48  return Secrets.<T>getObject(new KeyValueMap(keys));
49  } catch (ParserException ex) {
50  Logger.getLogger(Secrets.class.getName()).log(Level.SEVERE, null, ex);
51  }
52  return null;
53  }
54 
55  public static <T> T getObject(KeyValueMap kvm) {
56  for(ISecret iSecret : Instances.cached().byAnnotation(Secret.class, ISecret.class)) {
57  T value = iSecret.<T>get(kvm);
58  if(!isEmpty(value)) {
59  return value;
60  }
61  }
62  return null;
63  }
64 
65  public static boolean isSecret(String key, Object value) {
66  for(ISecret iSecret : Instances.cached().byAnnotation(Secret.class, ISecret.class)) {
67  if(iSecret.is(key, value)) {
68  return true;
69  }
70  }
71  return false;
72  }
73 
74  private static boolean isEmpty(Object value) {
75  if(value instanceof String) {
76  return Strings.isBlank((String) value);
77  } else {
78  return value == null;
79  }
80  }
81 
82  private Secrets() {
83  }
84 
85 }
static String getSecret(String keys)
Definition: Secrets.java:37
static< T > T getObject(String keys)
Definition: Secrets.java:46
static< T > T getObject(KeyValueMap kvm)
Definition: Secrets.java:55
static boolean isSecret(String key, Object value)
Definition: Secrets.java:65