View Javadoc

1   /*
2    * Copyright 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.admin.domain.service;
17  
18  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.ARCHIVE_TYPE;
19  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.ARTIFACT_ID;
20  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.CLASSIFIER;
21  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.GROUP_ID;
22  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.VERSION;
23  
24  import java.io.Serializable;
25  
26  import javax.naming.Context;
27  import javax.naming.InitialContext;
28  import javax.naming.NamingException;
29  
30  import org.apache.commons.lang.StringUtils;
31  
32  import de.smartics.properties.api.core.app.JndiContext;
33  import de.smartics.properties.resource.domain.ArtifactId;
34  
35  /**
36   * Responsible to read application identifier properties from the JNDI.
37   */
38  public class ApplicationIdConfigurationJndiLoader implements Serializable
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    /**
45     * The class version identifier.
46     * <p>
47     * The value of this constant is {@value}.
48     * </p>
49     */
50    private static final long serialVersionUID = 1L;
51  
52    // --- members --------------------------------------------------------------
53  
54    // ****************************** Initializer *******************************
55  
56    // ****************************** Constructors ******************************
57  
58    /**
59     * Default constructor.
60     */
61    public ApplicationIdConfigurationJndiLoader()
62    {
63    }
64  
65    // ****************************** Inner Classes *****************************
66  
67    // ********************************* Methods ********************************
68  
69    // --- init -----------------------------------------------------------------
70  
71    // --- get&set --------------------------------------------------------------
72  
73    // --- business -------------------------------------------------------------
74  
75    /**
76     * Loads the application identifier configuration.
77     *
78     * @return the application identifier configuration.
79     */
80    public ArtifactId load()
81    {
82      try
83      {
84        final Context context = new InitialContext();
85  
86        final JndiContext lookup = JndiContext.boot(context);
87        final String sourceId = lookup.getSourceId();
88        final String groupId = lookup.lookup(GROUP_ID);
89        final String artifactId = lookup.lookup(ARTIFACT_ID);
90        final String version = lookup.lookup(VERSION);
91        final String archiveType = lookup.lookup(ARCHIVE_TYPE);
92        final String classifier = readClassifier(lookup);
93        try
94        {
95          final ArtifactId applicationId =
96              new ArtifactId.Builder().withGroupId(groupId).withName(artifactId)
97                  .withVersion(version).withArchiveType(archiveType)
98                  .withClassifier(classifier).build();
99  
100         return applicationId;
101       }
102       catch (final Exception e)
103       {
104         throw new IllegalStateException(String.format(
105             "Cannot define the application identifier from JNDI %s: %s",
106             sourceId, e.getMessage(), e));
107       }
108     }
109     catch (final Exception e)
110     {
111       throw new IllegalStateException(String.format(
112           "Cannot define the application identifier: %s", e.getMessage(), e));
113     }
114   }
115 
116   private static String readClassifier(final JndiContext lookup)
117     throws NamingException
118   {
119 
120     final String classifier = lookup.lookup(CLASSIFIER);
121     if (StringUtils.isBlank(classifier))
122     {
123       return null;
124     }
125     return classifier;
126   }
127 
128   // --- object basics --------------------------------------------------------
129 
130 }