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.spi.config.support;
17  
18  import java.net.URL;
19  
20  import de.smartics.properties.api.config.domain.PropertyLocation;
21  import de.smartics.properties.api.config.domain.key.ApplicationId;
22  import de.smartics.properties.api.config.domain.key.ApplicationIdLoader;
23  
24  /**
25   * Helper to create property location information from the manifest meta data.
26   */
27  class PropertyLocationHelper
28  {
29    // ********************************* Fields *********************************
30  
31    // --- constants ------------------------------------------------------------
32  
33    // --- members --------------------------------------------------------------
34  
35    // ****************************** Initializer *******************************
36  
37    // ****************************** Constructors ******************************
38  
39    // ****************************** Inner Classes *****************************
40  
41    // ********************************* Methods ********************************
42  
43    // --- init -----------------------------------------------------------------
44  
45    // --- get&set --------------------------------------------------------------
46  
47    // --- business -------------------------------------------------------------
48  
49    PropertyLocation createPropertyLocation(final ClassLoader classLoader,
50        final String propertiesFile)
51    {
52      final URL url = classLoader.getResource(propertiesFile);
53      final String fullUrl = url.toExternalForm();
54      final ApplicationId appId = determineAppId(propertiesFile, fullUrl);
55      final PropertyLocation location = new PropertyLocation(appId, fullUrl);
56      return location;
57    }
58  
59    private ApplicationId determineAppId(final String propertiesFile,
60        final String fullUrl)
61    {
62      try
63      {
64        final ApplicationIdLoader appLoader = new ApplicationIdLoader(false);
65        final int endIndex = fullUrl.length() - propertiesFile.length();
66        final String rootUrlString = fullUrl.substring(0, endIndex);
67        final URL rootUrl = new URL(rootUrlString);
68        final ApplicationId appId = appLoader.load(rootUrl);
69        return appId;
70      }
71      catch (final Exception e)
72      {
73        return null;
74      }
75    }
76    // --- object basics --------------------------------------------------------
77  
78  }