View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.properties.api.core.security;
17  
18  import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_ALGORITHM;
19  import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_ID;
20  import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_KEY;
21  import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_PROVIDER;
22  import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_TRANSFORMATION;
23  
24  import java.io.Serializable;
25  import java.util.Properties;
26  
27  import javax.naming.Context;
28  import javax.naming.InitialContext;
29  
30  import de.smartics.properties.api.core.app.JndiContext;
31  
32  /**
33   * Responsible to read configuration properties from a JNDI to secure property
34   * values.
35   */
36  class SecurityConfigurationJndiLoader implements Serializable
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    /**
43     * The class version identifier.
44     * <p>
45     * The value of this constant is {@value}.
46     * </p>
47     */
48    private static final long serialVersionUID = 1L;
49  
50    // --- members --------------------------------------------------------------
51  
52    // ****************************** Initializer *******************************
53  
54    // ****************************** Constructors ******************************
55  
56    /**
57     * Default constructor.
58     */
59    SecurityConfigurationJndiLoader()
60    {
61    }
62  
63    // ****************************** Inner Classes *****************************
64  
65    // ********************************* Methods ********************************
66  
67    // --- init -----------------------------------------------------------------
68  
69    // --- get&set --------------------------------------------------------------
70  
71    // --- business -------------------------------------------------------------
72  
73    /**
74     * Loads the security configuration.
75     *
76     * @return the security configuration.
77     * @throws IllegalStateException on any problem reading the security
78     *           properties.
79     */
80    Properties load() throws IllegalStateException
81    {
82      try
83      {
84  
85        final Context context = new InitialContext();
86        final JndiContext lookup = JndiContext.boot(context);
87        final String sourceId = lookup.getSourceId();
88        final String key = lookup.lookup(SECURITY_KEY);
89        final String provider = lookup.lookup(SECURITY_PROVIDER);
90        final String algorithm = lookup.lookup(SECURITY_ALGORITHM);
91        final String transformation = lookup.lookup(SECURITY_TRANSFORMATION);
92        try
93        {
94          final Properties properties = new Properties();
95          properties.setProperty(SECURITY_ID, sourceId);
96          properties.setProperty(SECURITY_KEY, key);
97          properties.setProperty(SECURITY_PROVIDER, provider);
98          properties.setProperty(SECURITY_ALGORITHM, algorithm);
99          properties.setProperty(SECURITY_TRANSFORMATION, transformation);
100 
101         return properties;
102       }
103       catch (final Exception e)
104       {
105         throw new IllegalStateException(String.format(
106             "Cannot configure security module from JNDI %s: %s", sourceId,
107             e.getMessage(), e));
108       }
109     }
110     catch (final Exception e)
111     {
112       throw new IllegalStateException(String.format(
113           "Cannot configure security module: %s", e.getMessage(), e));
114     }
115   }
116 
117   // --- object basics --------------------------------------------------------
118 
119 }